-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils_io.hpp
145 lines (101 loc) · 4.34 KB
/
utils_io.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/** \file utils_io.hpp
*
* `utils_io' gathers functions useful for any program.
* Copyright (C) 2011-2013 Timothee Flutre
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UTILS_UTILS_IO_HPP
#define UTILS_UTILS_IO_HPP
#include <cstdlib>
#include <ctime>
#include <vector>
#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "zlib.h"
#ifdef __APPLE__
#define __MAC_10_8 1080
#include <Availability.h>
#if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_8
#define CONST_DIRENT_T struct dirent
#else
#define CONST_DIRENT_T const struct dirent
#endif
#else
#define CONST_DIRENT_T const struct dirent
#endif
namespace utils {
std::vector<std::string> & split (const std::string & s, char delim, std::vector<std::string> & tokens);
std::vector<std::string> split (const std::string & s, char delim);
std::vector<std::string> & split (const std::string & s, const char * delim,
std::vector<std::string> & tokens);
std::vector<std::string> split (const std::string & s, const char * delim);
std::string split (const std::string & s, const char * delim, const size_t & idx);
double getElapsedTime (const clock_t & startTime);
std::string getElapsedTime (const time_t & startRawTime, const time_t & endRawTime);
std::string getDateTime (const time_t & inTime);
void openFile (const std::string & pathToFile, std::ifstream & fileStream);
void openFile (const std::string & pathToFile, std::ofstream & fileStream);
void openFile (const std::string & pathToFile, gzFile & fileStream,
const char * mode);
void closeFile (const std::string & pathToFile, std::ifstream & fileStream);
void closeFile (const std::string & pathToFile, std::ofstream & fileStream);
void closeFile (const std::string & pathToFile, gzFile & fileStream);
int getline (gzFile & fileStream, std::string & line);
int readFile (const std::string & pathToFile, std::vector<std::string> & lines);
void gzwriteLine (gzFile & fileStream, const std::string & line,
const std::string & pathToFile, const size_t & lineId);
std::vector<size_t> getCounters (const size_t & nbIterations,
const size_t & nbSteps);
void printCounter (const size_t & currentIter,
const std::vector<size_t> & vCounters);
void progressBar (std::string msg, double currentIter, double nbIterations);
/** \brief Convert int, float, etc into a string.
* \note http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/
*/
template <class T> inline std::string toString (const T & t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
std::string copyString (const std::string input);
void replaceAll (std::string & str, const std::string & from, const std::string & to);
bool doesFileExist (const std::string & filename);
std::vector<std::string> scanInputDirectory (const std::string & inDir, const int & verbose);
bool isDirectory (const char path[]);
void createDirectory (const std::string & dirName);
void changeDirectory (const std::string & dirName);
std::string getCurrentDirectory (void);
int removeDir (std::string path);
void removeFiles (const std::vector<std::string> & vFileNames);
std::vector<std::string> glob (const std::string & pattern);
double getMaxMemUsedByProcess (void);
std::string getMaxMemUsedByProcess2Str (void);
std::string getCmdLine (int argc, char ** argv);
/** \brief Fill a vector with the keys of a map
* \note http://stackoverflow.com/a/771463/597069
* \note http://stackoverflow.com/a/10632266/597069
*/
template <typename M, typename V>
void keys2vec(const M & m, V & v)
{
for(typename M::const_iterator it = m.begin(); it != m.end(); ++it)
v.push_back(it->first);
}
} // namespace utils
#endif // UTILS_UTILS_IO_HPP