0
|
1 |
/*
|
|
2 |
* Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description: Misc utility functions
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
#include "appdep.hpp"
|
|
20 |
|
|
21 |
// ----------------------------------------------------------------------------------------------------------
|
|
22 |
|
|
23 |
void PrintOutputLn(const string& s)
|
|
24 |
{
|
|
25 |
// check if printing to a file or directory to STDOUT
|
|
26 |
if (!_cl_outputfile.empty())
|
|
27 |
{
|
|
28 |
_outputf << s << endl;
|
|
29 |
}
|
|
30 |
else
|
|
31 |
{
|
|
32 |
cout << s << endl;
|
|
33 |
}
|
|
34 |
}
|
|
35 |
|
|
36 |
// ----------------------------------------------------------------------------------------------------------
|
|
37 |
|
|
38 |
void MakeSureTrailingDirectoryMarkerExists(string& path)
|
|
39 |
{
|
|
40 |
if (!path.empty() && path.at(path.length()-1) != DIR_SEPARATOR2)
|
|
41 |
{
|
|
42 |
path.insert(path.length(), DIR_SEPARATOR);
|
|
43 |
}
|
|
44 |
}
|
|
45 |
|
|
46 |
// ----------------------------------------------------------------------------------------------------------
|
|
47 |
|
|
48 |
bool FileExists(const string& path)
|
|
49 |
{
|
|
50 |
if (!path.empty())
|
|
51 |
{
|
|
52 |
struct stat stat_p;
|
|
53 |
if (stat(path.c_str(), &stat_p) == 0)
|
|
54 |
return !S_ISDIR(stat_p.st_mode); // return true if not a directory
|
|
55 |
else
|
|
56 |
return false; // cannot find entry
|
|
57 |
}
|
|
58 |
else
|
|
59 |
return false;
|
|
60 |
}
|
|
61 |
|
|
62 |
// ----------------------------------------------------------------------------------------------------------
|
|
63 |
|
|
64 |
bool DirectoryExists(const string& path)
|
|
65 |
{
|
|
66 |
if (!path.empty())
|
|
67 |
{
|
|
68 |
string temp_path = path;
|
|
69 |
|
|
70 |
// remove trailing directory marker if exists
|
|
71 |
if (path.at(path.length()-1) == DIR_SEPARATOR2)
|
|
72 |
temp_path = path.substr(0, path.length()-1);
|
|
73 |
|
|
74 |
struct stat stat_p;
|
|
75 |
if (stat(temp_path.c_str(), &stat_p) == 0)
|
|
76 |
return S_ISDIR(stat_p.st_mode); // return true if a directory
|
|
77 |
else
|
|
78 |
return false; // cannot find entry
|
|
79 |
}
|
|
80 |
else
|
|
81 |
return false;
|
|
82 |
}
|
|
83 |
|
|
84 |
// ----------------------------------------------------------------------------------------------------------
|
|
85 |
|
|
86 |
bool RemoveFile(const string& path)
|
|
87 |
{
|
|
88 |
return _unlink(path.c_str()) == 0;
|
|
89 |
}
|
|
90 |
|
|
91 |
// ----------------------------------------------------------------------------------------------------------
|
|
92 |
|
|
93 |
bool RemoveDirectoryWithAllFiles(const string& path)
|
|
94 |
{
|
|
95 |
string temp_path = path;
|
|
96 |
MakeSureTrailingDirectoryMarkerExists(temp_path);
|
|
97 |
|
|
98 |
// remove all files in the directory via OS call
|
|
99 |
string del_command = DEL_ALL_COMMAND;
|
|
100 |
string cmd = del_command + " " + temp_path + "* " + CERR_TO_NULL;
|
|
101 |
|
|
102 |
vector<string> tempVector;
|
|
103 |
ExecuteCommand(cmd, tempVector);
|
|
104 |
|
|
105 |
// finally tries to remove the directory, fails if not empty
|
|
106 |
return _rmdir(path.c_str()) == 0;
|
|
107 |
}
|
|
108 |
|
|
109 |
// ----------------------------------------------------------------------------------------------------------
|
|
110 |
|
|
111 |
string LowerCase(const string& s)
|
|
112 |
{
|
|
113 |
char* buf = new char[s.length()];
|
|
114 |
s.copy(buf, s.length());
|
|
115 |
|
|
116 |
for(unsigned int i = 0; i < s.length(); i++)
|
|
117 |
buf[i] = tolower(buf[i]);
|
|
118 |
|
|
119 |
string r(buf, s.length());
|
|
120 |
delete buf;
|
|
121 |
return r;
|
|
122 |
}
|
|
123 |
|
|
124 |
// ----------------------------------------------------------------------------------------------------------
|
|
125 |
|
|
126 |
void MkDirAll(const string& path)
|
|
127 |
{
|
|
128 |
if (!path.empty() && !DirectoryExists(path))
|
|
129 |
{
|
|
130 |
string target_path = path;
|
|
131 |
|
|
132 |
// make sure that the directory has a trailing directory marker
|
|
133 |
MakeSureTrailingDirectoryMarkerExists(target_path);
|
|
134 |
|
|
135 |
// loop through each character in the string and try to find directory delimeters
|
|
136 |
for (unsigned int i=0; i<target_path.length(); i++)
|
|
137 |
{
|
|
138 |
string::size_type pos = target_path.find(DIR_SEPARATOR, i);
|
|
139 |
|
|
140 |
if (pos != string::npos)
|
|
141 |
{
|
|
142 |
// construct the base directory name
|
|
143 |
string base_directory = target_path.substr(0, pos+1);
|
|
144 |
|
|
145 |
if (!DirectoryExists(base_directory))
|
|
146 |
{
|
|
147 |
_mkdir(base_directory.c_str());
|
|
148 |
}
|
|
149 |
|
|
150 |
i=pos;
|
|
151 |
}
|
|
152 |
}
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
|
156 |
// ----------------------------------------------------------------------------------------------------------
|
|
157 |
|
|
158 |
bool ExecuteCommand(const string& command, vector<string>& resultset)
|
|
159 |
{
|
|
160 |
// note, cannot use compiler parameters "-std=c++98" because of popen/pclose
|
|
161 |
// also cannot compile this code is MSVC because usage of popen/pclose
|
|
162 |
|
|
163 |
FILE* fp;
|
|
164 |
char buffer[1024];
|
|
165 |
string tempstr;
|
|
166 |
|
|
167 |
resultset.clear();
|
|
168 |
|
|
169 |
if ((fp = _popen(command.c_str(), "r")) == NULL)
|
|
170 |
{
|
|
171 |
return false;
|
|
172 |
}
|
|
173 |
|
|
174 |
while (fgets(buffer, sizeof(buffer), fp))
|
|
175 |
{
|
|
176 |
tempstr = buffer;
|
|
177 |
resultset.push_back(tempstr.substr(0, tempstr.size()-1));
|
|
178 |
}
|
|
179 |
|
|
180 |
_pclose(fp);
|
|
181 |
|
|
182 |
return true;
|
|
183 |
}
|
|
184 |
|
|
185 |
// ----------------------------------------------------------------------------------------------------------
|
|
186 |
|
|
187 |
string& TrimRight(string& s)
|
|
188 |
{
|
|
189 |
int pos(s.size());
|
|
190 |
for (; pos && (s[pos-1]==' ' || s[pos-1]=='\t'); --pos);
|
|
191 |
s.erase(pos, s.size()-pos);
|
|
192 |
return s;
|
|
193 |
}
|
|
194 |
|
|
195 |
// ----------------------------------------------------------------------------------------------------------
|
|
196 |
|
|
197 |
string& TrimLeft(string& s)
|
|
198 |
{
|
|
199 |
int pos(0);
|
|
200 |
for (; s[pos]==' ' || s[pos]=='\t'; ++pos);
|
|
201 |
s.erase(0, pos);
|
|
202 |
return s;
|
|
203 |
}
|
|
204 |
|
|
205 |
// ----------------------------------------------------------------------------------------------------------
|
|
206 |
|
|
207 |
string& TrimAll(string& s)
|
|
208 |
{
|
|
209 |
return TrimLeft(TrimRight(s));
|
|
210 |
}
|
|
211 |
|
|
212 |
// ----------------------------------------------------------------------------------------------------------
|
|
213 |
|
|
214 |
int StringICmp(const string& s1, const string& s2)
|
|
215 |
{
|
|
216 |
string ss1 = LowerCase(s1);
|
|
217 |
string ss2 = LowerCase(s2);
|
|
218 |
|
|
219 |
return ss1.compare(ss2);
|
|
220 |
}
|
|
221 |
|
|
222 |
// ----------------------------------------------------------------------------------------------------------
|
|
223 |
|
|
224 |
int StringICmpFileNamesWithoutExtension(const string& s1, const string& s2)
|
|
225 |
{
|
|
226 |
// remove extension and then compare
|
|
227 |
string ss1;
|
|
228 |
string ss2;
|
|
229 |
|
|
230 |
string::size_type dot_pos1 = s1.find_last_of('.');
|
|
231 |
if (dot_pos1 == string::npos)
|
|
232 |
ss1 = s1;
|
|
233 |
else
|
|
234 |
ss1 = s1.substr(0, dot_pos1);
|
|
235 |
|
|
236 |
string::size_type dot_pos2 = s2.find_last_of('.');
|
|
237 |
if (dot_pos2 == string::npos)
|
|
238 |
ss2 = s2;
|
|
239 |
else
|
|
240 |
ss2 = s2.substr(0, dot_pos2);
|
|
241 |
|
|
242 |
return StringICmp(ss1, ss2);
|
|
243 |
}
|
|
244 |
|
|
245 |
// ----------------------------------------------------------------------------------------------------------
|
|
246 |
|
|
247 |
bool TimestampsMatches(const time_t& orginal_time, const time_t& new_time)
|
|
248 |
{
|
|
249 |
// allow two second difference to both directions
|
|
250 |
if (new_time-2 <= orginal_time && orginal_time <= new_time+2)
|
|
251 |
return true;
|
|
252 |
else
|
|
253 |
return false;
|
|
254 |
}
|
|
255 |
|
|
256 |
// ----------------------------------------------------------------------------------------------------------
|
|
257 |
|
|
258 |
string Int2Str(int value)
|
|
259 |
{
|
|
260 |
ostringstream os;
|
|
261 |
if (os << value)
|
|
262 |
return os.str();
|
|
263 |
else
|
|
264 |
return "";
|
|
265 |
}
|
|
266 |
|
|
267 |
// ----------------------------------------------------------------------------------------------------------
|
|
268 |
|
|
269 |
int Str2Int(const string& s)
|
|
270 |
{
|
|
271 |
int res(0);
|
|
272 |
|
|
273 |
// return 0 for empty string
|
|
274 |
if (s.empty())
|
|
275 |
{
|
|
276 |
}
|
|
277 |
|
|
278 |
// hex conversion if the string begings with 0x...
|
|
279 |
else if (s.length() >= 3 && s.at(0) == '0' && s.at(1) == 'x')
|
|
280 |
{
|
|
281 |
istringstream is(s);
|
|
282 |
is >> hex >> res;
|
|
283 |
if(!is || !is.eof())
|
|
284 |
res = 0;
|
|
285 |
}
|
|
286 |
|
|
287 |
// normal integer
|
|
288 |
else
|
|
289 |
{
|
|
290 |
istringstream is(s);
|
|
291 |
is >> res;
|
|
292 |
if(!is || !is.eof())
|
|
293 |
res = 0;
|
|
294 |
}
|
|
295 |
|
|
296 |
return res;
|
|
297 |
}
|
|
298 |
|
|
299 |
// ----------------------------------------------------------------------------------------------------------
|
|
300 |
|
|
301 |
void InsertQuotesToFilePath(string& s)
|
|
302 |
{
|
|
303 |
// example C:\Program Files\do something.exe -> C:\"Program Files"\"do something.exe"
|
|
304 |
|
|
305 |
bool firstBacklashFound = false;
|
|
306 |
bool anyQuoteInserted = false;
|
|
307 |
|
|
308 |
if (!s.empty())
|
|
309 |
{
|
|
310 |
int s_length = s.length();
|
|
311 |
|
|
312 |
for (int i=0; i<s_length; i++)
|
|
313 |
{
|
|
314 |
string::size_type pos = s.find(DIR_SEPARATOR, i);
|
|
315 |
|
|
316 |
if (pos != string::npos)
|
|
317 |
{
|
|
318 |
if (!firstBacklashFound)
|
|
319 |
{
|
|
320 |
// replace \ -> \"
|
|
321 |
s.insert(pos+1, "\"");
|
|
322 |
|
|
323 |
anyQuoteInserted = true;
|
|
324 |
firstBacklashFound = true;
|
|
325 |
s_length++;
|
|
326 |
i = pos+1;
|
|
327 |
}
|
|
328 |
else
|
|
329 |
{
|
|
330 |
// replace \ -> "\"
|
|
331 |
s.insert(pos, "\"");
|
|
332 |
s.insert(pos+2, "\"");
|
|
333 |
|
|
334 |
anyQuoteInserted = true;
|
|
335 |
s_length += 2;
|
|
336 |
i = pos+2;
|
|
337 |
}
|
|
338 |
}
|
|
339 |
|
|
340 |
if (i>255)
|
|
341 |
return; // something went wrong..
|
|
342 |
}
|
|
343 |
|
|
344 |
// append extra quote to the end if needed
|
|
345 |
if (anyQuoteInserted)
|
|
346 |
s.insert(s.length(), "\"");
|
|
347 |
}
|
|
348 |
}
|
|
349 |
|
|
350 |
// ----------------------------------------------------------------------------------------------------------
|
|
351 |
|
|
352 |
void ShowProgressInfo(unsigned int& current_progress_percentage, unsigned int& current_progress, unsigned int& max_progress, bool print_initial_value)
|
|
353 |
{
|
|
354 |
if (print_initial_value)
|
|
355 |
{
|
|
356 |
cerr << "( 0% complete)";
|
|
357 |
}
|
|
358 |
else
|
|
359 |
{
|
|
360 |
current_progress++;
|
|
361 |
|
|
362 |
unsigned int temp_percentage = int( (double) current_progress / (double) max_progress * 100 );
|
|
363 |
|
|
364 |
if (temp_percentage > current_progress_percentage)
|
|
365 |
{
|
|
366 |
current_progress_percentage = temp_percentage;
|
|
367 |
cerr << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
|
|
368 |
|
|
369 |
if (current_progress_percentage < 10)
|
|
370 |
cerr << " ";
|
|
371 |
else if (current_progress_percentage < 100)
|
|
372 |
cerr << " ";
|
|
373 |
cerr << current_progress_percentage << "% complete)";
|
|
374 |
}
|
|
375 |
}
|
|
376 |
}
|
|
377 |
|
|
378 |
|
|
379 |
// ----------------------------------------------------------------------------------------------------------
|