-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHelper.cpp
216 lines (172 loc) · 4.19 KB
/
Helper.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "Helper.h"
#include <fstream>
#include <string>
#include <sstream>
#ifdef DEBUG
#include <iostream>
#endif
using namespace std;
string validateFilename(string s)
{
if (s.empty()) return s;
for (int i = 0; i < (int) invalidFilenameCharacters.length(); i++)
{
replace(s.begin(),s.end(),invalidFilenameCharacters.c_str()[i],
INVALID_CHAR_REPLACEMENT_CHAR);
}
if (s.length() > MAX_LENGTH_SAVE_FILENAME)
s.erase(MAX_LENGTH_SAVE_FILENAME); //truncate the name if too long
// cout << s << endl;
return s;
}
// from http://www.techbytes.ca/techbyte103.html
bool fileExists(string strFilename)
{
struct stat stFileInfo;
bool blnReturn;
int intStat;
// Attempt to get the file attributes
intStat = stat(strFilename.c_str(),&stFileInfo);
if(intStat == 0)
{
// We were able to get the file attributes
// so the file obviously exists.
blnReturn = true;
} else
{
// We were not able to get the file attributes.
// This may mean that we don't have permission to
// access the folder which contains this file. If you
// need to do that level of checking, lookup the
// return values of stat which will give you
// more details on why stat failed.
blnReturn = false;
}
return(blnReturn);
}
int getFileList (string dir, vector<string> &files, string extension)
{
DIR *dp;
struct dirent *dirp;
files.clear();
if (dir.length() == 0)
dir = getCurrentDir();
if((dp = opendir(dir.c_str())) == NULL)
{
cerr << "Error opening " << dir << endl;
return -1;
}
while ((dirp = readdir(dp)) != NULL)
{
string s(dirp->d_name);
if (s.length() > extension.length() &&
(s.compare(s.length() - extension.length(),
extension.length(),extension) == 0))
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}
string getCurrentDir()
{
char * s = new char[250];
string *str;
if (getcwd(s,250))
str = new string(s);
else
str = new string;
delete s;
return *str;
}
string joinStrings(vector<string> strings, string separator)
{
string s;
for (int i = 0; i < (int) strings.size(); ++i)
{
if (s.length() != 0)
s += separator;
s += strings[i];
}
return s;
}
// from http://www.codeproject.com/KB/stl/stdstringtrim.aspx
string trimString(string& str)
{
string::size_type pos = str.find_last_not_of(' ');
if(pos != string::npos) {
str.erase(pos + 1);
pos = str.find_first_not_of(' ');
if(pos != string::npos) str.erase(0, pos);
}
else str.erase(str.begin(), str.end());
return str;
}
bool parseNameValuePair(const std::string& s,
std::string& name, std::string& value,
std::string separator)
{
int pos = s.find_first_of(separator);
if (pos == (int) string::npos) return false; // parse failed
name = s.substr(0,pos - 1);
value = ((pos == (int) s.size() - 1)?
"" : s.substr(pos + 1, string::npos));
trimString(name);
trimString(value);
return true;
}
bool atob(const std::string& s)
{
int i = atoi(s.c_str());
return (i == 0)? false : true;
}
int atoi(const std::string& s)
{
return atoi(s.c_str());
}
float atof(const std::string& s)
{
return atof(s.c_str());
}
string itos(const int i)
{
string s;
stringstream out;
out << i;
return out.str();
}
string ftos(const float i)
{
string s;
stringstream out;
out << i;
return out.str();
}
void getLines(const string& filename, vector<string>& lines)
{
ifstream file(filename.c_str());
string line;
lines.clear();
if (file.is_open())
{
while (!file.eof())
{
getline(file,line);
if (!line.empty())
lines.push_back(line);
}
file.close();
}
}
void appendLine(const string& filename, const string& line)
{
ofstream file;
file.open(filename.c_str(),ios::app);
file << line << endl;
file.close();
}
#ifdef USE_QT
QString s(const string& str)
{
return QString::fromStdString(str);
}
#endif //USE_QT