|
0
|
1 |
/*
|
|
|
2 |
* Copyright (c) 2006-2009 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:
|
|
|
15 |
*
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
#include "CmdGlobals.h"
|
|
|
19 |
#ifdef __WIN__
|
|
|
20 |
#pragma warning(disable:4786)
|
|
|
21 |
#endif
|
|
|
22 |
|
|
|
23 |
#include <fstream>
|
|
|
24 |
#include <stdlib.h>
|
|
|
25 |
#include <string>
|
|
|
26 |
#include <map>
|
|
|
27 |
#include <iostream>
|
|
|
28 |
#include <sys/types.h>
|
|
|
29 |
#include <sys/stat.h>
|
|
|
30 |
#ifdef __WIN__
|
|
|
31 |
#include <io.h>
|
|
|
32 |
#include <windows.h>
|
|
|
33 |
#else
|
|
|
34 |
#include <unistd.h>
|
|
|
35 |
#include <glob.h>
|
|
|
36 |
#include <fcntl.h>
|
|
|
37 |
|
|
|
38 |
#include <sys/types.h>
|
|
|
39 |
#include <sys/stat.h>
|
|
|
40 |
#include <sys/param.h>
|
|
|
41 |
#include <unistd.h>
|
|
|
42 |
#define _MAX_PATH MAXPATHLEN
|
|
|
43 |
|
|
|
44 |
#endif
|
|
|
45 |
#include "BBCFileUtils.h"
|
|
|
46 |
#include "HAException.h"
|
|
|
47 |
#include "Utils.h"
|
|
|
48 |
#include "XMLUtils.h"
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
// ----------------------------------------------------------------------------
|
|
|
52 |
// BBCFileUtils::BBCFileUtils
|
|
|
53 |
// ----------------------------------------------------------------------------
|
|
|
54 |
//
|
|
|
55 |
BBCFileUtils::BBCFileUtils(string basedir)
|
|
|
56 |
{
|
|
|
57 |
iBaseDir = basedir;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
// ----------------------------------------------------------------------------
|
|
|
61 |
// BBCFileUtils::~BBCFileUtils
|
|
|
62 |
// ----------------------------------------------------------------------------
|
|
|
63 |
//
|
|
|
64 |
BBCFileUtils::~BBCFileUtils()
|
|
|
65 |
{
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
typedef int intptr_t;
|
|
|
69 |
|
|
|
70 |
// ----------------------------------------------------------------------------
|
|
|
71 |
// BBCFileUtils::getFilesInDir
|
|
|
72 |
// ----------------------------------------------------------------------------
|
|
|
73 |
//
|
|
|
74 |
list<pair<string, string> > BBCFileUtils::getFilesInDir(string wildcard, string aRelPath)
|
|
|
75 |
{
|
|
|
76 |
// START -- Support for multiple header directories
|
|
|
77 |
list<pair<string, string> > ret;
|
|
|
78 |
list<pair<string, string> > dirs = BBCFileUtils::extractFilenames(iBaseDir);
|
|
|
79 |
list<pair<string, string> >::iterator dirbegin = dirs.begin();
|
|
|
80 |
for(; dirbegin != dirs.end(); dirbegin++)
|
|
|
81 |
{
|
|
|
82 |
// END -- Support for multiple header directories
|
|
|
83 |
string basedir = BBCFileUtils::getFullPath(dirbegin->first);
|
|
|
84 |
if (aRelPath != "")
|
|
|
85 |
{
|
|
|
86 |
iWildcard = basedir + DIR_SEPARATOR + aRelPath + DIR_SEPARATOR + wildcard;
|
|
|
87 |
} else
|
|
|
88 |
{
|
|
|
89 |
iWildcard = basedir + DIR_SEPARATOR + wildcard;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// search for all files which were specified in wildcard string
|
|
|
93 |
#ifdef __WIN__
|
|
|
94 |
_finddata_t finddata;
|
|
|
95 |
intptr_t searchsession =_findfirst(iWildcard.c_str(),&finddata);
|
|
|
96 |
int found = -1;
|
|
|
97 |
if (searchsession > 0)
|
|
|
98 |
{
|
|
|
99 |
found = 0;
|
|
|
100 |
}
|
|
|
101 |
while (found == 0)
|
|
|
102 |
{
|
|
|
103 |
string path;
|
|
|
104 |
if (aRelPath != "")
|
|
|
105 |
{
|
|
|
106 |
path = DIR_SEPARATOR + aRelPath + DIR_SEPARATOR + string(finddata.name);
|
|
|
107 |
} else
|
|
|
108 |
{
|
|
|
109 |
path = DIR_SEPARATOR + string(finddata.name);
|
|
|
110 |
}
|
|
|
111 |
string fullpath = basedir + path;
|
|
|
112 |
if (isValidFilename(fullpath))
|
|
|
113 |
{
|
|
|
114 |
pair<string, string> tempval(path, toLowerCaseWin(path));
|
|
|
115 |
ret.push_back(tempval);
|
|
|
116 |
}
|
|
|
117 |
found = _findnext(searchsession, &finddata);
|
|
|
118 |
}
|
|
|
119 |
_findclose(searchsession);
|
|
|
120 |
#else
|
|
|
121 |
glob_t p_glob;
|
|
|
122 |
|
|
|
123 |
glob(iWildcard.c_str() , 0, NULL, &p_glob);
|
|
|
124 |
for (unsigned int i = 0; i < p_glob.gl_pathc; i++ )
|
|
|
125 |
{
|
|
|
126 |
string path = p_glob.gl_pathv[i];
|
|
|
127 |
if (isValidFilename(path))
|
|
|
128 |
{
|
|
|
129 |
path = StripPath(path);
|
|
|
130 |
if (aRelPath != "")
|
|
|
131 |
{
|
|
|
132 |
path = aRelPath + DIR_SEPARATOR + path;
|
|
|
133 |
}
|
|
|
134 |
path = DIR_SEPARATOR + path;
|
|
|
135 |
pair<string, string> tempval(path, toLowerCaseWin(path));
|
|
|
136 |
ret.push_back(tempval);
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
globfree(&p_glob);
|
|
|
140 |
#endif
|
|
|
141 |
}
|
|
|
142 |
return ret;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
// ----------------------------------------------------------------------------
|
|
|
146 |
// BBCFileUtils::getFilesInDir
|
|
|
147 |
// ----------------------------------------------------------------------------
|
|
|
148 |
//
|
|
|
149 |
list<pair<string,string> > BBCFileUtils::getFilesInDir(list<pair<string, string> >& fileset, string aRelPath, list<pair<string, string> >& aFoundfiles)
|
|
|
150 |
{
|
|
|
151 |
// START -- Support for multiple header directories
|
|
|
152 |
list<pair<string, string> > ret;
|
|
|
153 |
list<pair<string, string> > notfound;
|
|
|
154 |
list<pair<string, string> > dirs = BBCFileUtils::extractFilenames(iBaseDir);
|
|
|
155 |
list<pair<string, string> >::iterator dirbegin = dirs.begin();
|
|
|
156 |
for(; dirbegin != dirs.end(); dirbegin++)
|
|
|
157 |
{
|
|
|
158 |
// END -- Support for multiple header directories
|
|
|
159 |
string basedir = BBCFileUtils::getFullPath(dirbegin->first);
|
|
|
160 |
if (aRelPath != "")
|
|
|
161 |
{
|
|
|
162 |
iWildcard = basedir + DIR_SEPARATOR + aRelPath + DIR_SEPARATOR + WILDCARD_ALLFILES;
|
|
|
163 |
} else
|
|
|
164 |
{
|
|
|
165 |
iWildcard = basedir + DIR_SEPARATOR + WILDCARD_ALLFILES;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
|
|
|
169 |
// Search for all files which were specified in wildcard string
|
|
|
170 |
#ifdef __WIN__
|
|
|
171 |
_finddata_t finddata;
|
|
|
172 |
intptr_t searchsession =_findfirst(iWildcard.c_str(),&finddata);
|
|
|
173 |
int found = -1;
|
|
|
174 |
if (searchsession > 0)
|
|
|
175 |
{
|
|
|
176 |
found = 0;
|
|
|
177 |
}
|
|
|
178 |
while (found == 0)
|
|
|
179 |
{
|
|
|
180 |
string filename = DIR_SEPARATOR + string(finddata.name);
|
|
|
181 |
if (aRelPath != "")
|
|
|
182 |
{
|
|
|
183 |
filename = DIR_SEPARATOR + aRelPath + filename;
|
|
|
184 |
}
|
|
|
185 |
string fullpath = basedir + filename;
|
|
|
186 |
// Check if path is pointing to a file
|
|
|
187 |
if (isValidFilename(fullpath))
|
|
|
188 |
{
|
|
|
189 |
list<pair<string, string> >::iterator file = FindFromList(toLowerCaseWin(filename), fileset, ERightValue);
|
|
|
190 |
if (file == fileset.end() && aRelPath != "")
|
|
|
191 |
{
|
|
|
192 |
file = FindFromList(toLowerCaseWin(finddata.name), fileset, ERightValue);
|
|
|
193 |
}
|
|
|
194 |
if (file != fileset.end())
|
|
|
195 |
{
|
|
|
196 |
pair<string, string> tempval;
|
|
|
197 |
tempval.first = filename;
|
|
|
198 |
tempval.second = toLowerCaseWin(filename);
|
|
|
199 |
ret.push_back(tempval);
|
|
|
200 |
aFoundfiles.push_back(*file);
|
|
|
201 |
fileset.erase(file);
|
|
|
202 |
} else if (aRelPath == "")
|
|
|
203 |
{
|
|
|
204 |
pair<string, string> tempvar(filename, toLowerCaseWin(filename));
|
|
|
205 |
notfound.push_back(tempvar);
|
|
|
206 |
} else
|
|
|
207 |
{
|
|
|
208 |
list<pair<string, string> >::iterator alreadyfound = FindFromList(toLowerCaseWin(finddata.name), aFoundfiles, ERightValue);
|
|
|
209 |
if (alreadyfound != aFoundfiles.end())
|
|
|
210 |
{
|
|
|
211 |
cout << "Warning: More than one files matches for the given filename '" << finddata.name;
|
|
|
212 |
cout << "' in the file set, only the first file is used." << endl;
|
|
|
213 |
aFoundfiles.erase(alreadyfound);
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
found = _findnext(searchsession, &finddata);
|
|
|
218 |
}
|
|
|
219 |
_findclose(searchsession);
|
|
|
220 |
#else
|
|
|
221 |
glob_t p_glob;
|
|
|
222 |
|
|
|
223 |
int result = glob(iWildcard.c_str() , 0, NULL, &p_glob);
|
|
|
224 |
if (result == 0)
|
|
|
225 |
{
|
|
|
226 |
for ( unsigned int i = 0; i < p_glob.gl_pathc; i++ )
|
|
|
227 |
{
|
|
|
228 |
string path = p_glob.gl_pathv[i];
|
|
|
229 |
// Check if path is pointing to a file
|
|
|
230 |
if (isValidFilename(path))
|
|
|
231 |
{
|
|
|
232 |
string filename = DIR_SEPARATOR + StripPath(path);
|
|
|
233 |
string pathfile = "";
|
|
|
234 |
|
|
|
235 |
if (aRelPath != "")
|
|
|
236 |
{
|
|
|
237 |
pathfile = DIR_SEPARATOR + aRelPath + filename;
|
|
|
238 |
} else
|
|
|
239 |
{
|
|
|
240 |
pathfile = filename;
|
|
|
241 |
}
|
|
|
242 |
list<pair<string, string> >::iterator file = FindFromList(toLowerCaseWin(pathfile), fileset,ERightValue);
|
|
|
243 |
if (file == fileset.end() && aRelPath != "")
|
|
|
244 |
{
|
|
|
245 |
file = FindFromList(toLowerCaseWin(filename.substr(1)), fileset,ERightValue);
|
|
|
246 |
}
|
|
|
247 |
if (file != fileset.end())
|
|
|
248 |
{
|
|
|
249 |
pair<string, string> tempval;
|
|
|
250 |
tempval.first = pathfile;
|
|
|
251 |
tempval.second = toLowerCaseWin(pathfile);
|
|
|
252 |
ret.push_back(tempval);
|
|
|
253 |
aFoundfiles.push_back(*file);
|
|
|
254 |
fileset.erase(file);
|
|
|
255 |
} else if (aRelPath == "")
|
|
|
256 |
{
|
|
|
257 |
pair<string, string> tempvar(pathfile, toLowerCaseWin(pathfile));
|
|
|
258 |
notfound.push_back(tempvar);
|
|
|
259 |
} else
|
|
|
260 |
{
|
|
|
261 |
list<pair<string, string> >::iterator alreadyfound = FindFromList(toLowerCaseWin(filename.substr(1)), aFoundfiles,ERightValue);
|
|
|
262 |
if (alreadyfound != aFoundfiles.end())
|
|
|
263 |
{
|
|
|
264 |
cout << "Warning: More than one files matches for the given filename '" << filename.substr(1);
|
|
|
265 |
cout << "' in the file set, only the first file is used.";
|
|
|
266 |
aFoundfiles.erase(alreadyfound);
|
|
|
267 |
}
|
|
|
268 |
}
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
globfree(&p_glob);
|
|
|
272 |
}
|
|
|
273 |
#endif
|
|
|
274 |
}
|
|
|
275 |
if (notfound.size() != 0)
|
|
|
276 |
{
|
|
|
277 |
list<pair<string, string> >::iterator file = notfound.begin();
|
|
|
278 |
list<pair<string, string> >::iterator fileend = notfound.end();
|
|
|
279 |
for(; file != fileend; file++)
|
|
|
280 |
{
|
|
|
281 |
string filename = file->second.substr(1);
|
|
|
282 |
list<pair<string, string> >::iterator file = FindFromList(filename, fileset, ERightValue);
|
|
|
283 |
if (file != fileset.end())
|
|
|
284 |
{
|
|
|
285 |
pair<string, string> tempval;
|
|
|
286 |
tempval.first = DIR_SEPARATOR + file->first;
|
|
|
287 |
tempval.second = DIR_SEPARATOR + file->second;
|
|
|
288 |
ret.push_back(tempval);
|
|
|
289 |
aFoundfiles.push_back(*file);
|
|
|
290 |
fileset.erase(file);
|
|
|
291 |
} else
|
|
|
292 |
{
|
|
|
293 |
list<pair<string, string> >::iterator alreadyfound = FindFromList(filename, aFoundfiles, ERightValue);
|
|
|
294 |
if (alreadyfound != aFoundfiles.end())
|
|
|
295 |
{
|
|
|
296 |
cout << "Warning: More than one files matches for the given filename '" << filename;
|
|
|
297 |
cout << "' in the file set, only the first file is used." << endl;
|
|
|
298 |
aFoundfiles.erase(alreadyfound);
|
|
|
299 |
}
|
|
|
300 |
}
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
ret.sort();
|
|
|
304 |
return ret;
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
// ----------------------------------------------------------------------------
|
|
|
308 |
// BBCFileUtils::setTemp
|
|
|
309 |
// Set temp dir
|
|
|
310 |
// ----------------------------------------------------------------------------
|
|
|
311 |
//
|
|
|
312 |
void BBCFileUtils::setTemp(const string& aTempDir)
|
|
|
313 |
{
|
|
|
314 |
iTempDir = aTempDir;
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
|
|
|
318 |
// ----------------------------------------------------------------------------
|
|
|
319 |
// BBCFileUtils::getFilesInDir
|
|
|
320 |
// Calculates quite unique fingerprint for a string.
|
|
|
321 |
// ----------------------------------------------------------------------------
|
|
|
322 |
//
|
|
|
323 |
unsigned int BBCFileUtils::quickHash(string str)
|
|
|
324 |
{
|
|
|
325 |
int ret = 0;
|
|
|
326 |
unsigned int len = (unsigned int)str.length();
|
|
|
327 |
for (unsigned int i = 0; i < len; i++)
|
|
|
328 |
{
|
|
|
329 |
ret += (i+1) * str.at(i);
|
|
|
330 |
}
|
|
|
331 |
return ret;
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
|
|
|
335 |
// ----------------------------------------------------------------------------
|
|
|
336 |
// BBCFileUtils::trimFilenames
|
|
|
337 |
//
|
|
|
338 |
// ----------------------------------------------------------------------------
|
|
|
339 |
//
|
|
|
340 |
/*
|
|
|
341 |
string BBCFileUtils::trimFilenames(string aFilenameList)
|
|
|
342 |
{
|
|
|
343 |
string ret("");
|
|
|
344 |
unsigned int i = 0;
|
|
|
345 |
for (; i < aFilenameList.length(); i++)
|
|
|
346 |
{
|
|
|
347 |
char ch = aFilenameList.at(i);
|
|
|
348 |
if (ch != ';')
|
|
|
349 |
{
|
|
|
350 |
ret += ch;
|
|
|
351 |
} else
|
|
|
352 |
{
|
|
|
353 |
ret += ' ';
|
|
|
354 |
}
|
|
|
355 |
}
|
|
|
356 |
return ret;
|
|
|
357 |
}
|
|
|
358 |
*/
|
|
|
359 |
// ----------------------------------------------------------------------------
|
|
|
360 |
// BBCFileUtils::extractFilenames
|
|
|
361 |
// Extract filenames to string list
|
|
|
362 |
// ----------------------------------------------------------------------------
|
|
|
363 |
//
|
|
|
364 |
list<pair<string, string> > BBCFileUtils::extractFilenames(string aFilenameList)
|
|
|
365 |
{
|
|
|
366 |
size_t i = 0;
|
|
|
367 |
list<pair<string, string> > ret;
|
|
|
368 |
string current("");
|
|
|
369 |
for (; i < aFilenameList.length(); i++)
|
|
|
370 |
{
|
|
|
371 |
char ch = aFilenameList.at(i);
|
|
|
372 |
if (ch != ';')
|
|
|
373 |
{
|
|
|
374 |
current += ch;
|
|
|
375 |
} else
|
|
|
376 |
{
|
|
|
377 |
pair<string, string> tempvar(current, toLowerCaseWin(current));
|
|
|
378 |
ret.push_back(tempvar);
|
|
|
379 |
current = "";
|
|
|
380 |
}
|
|
|
381 |
|
|
|
382 |
}
|
|
|
383 |
if (current.length() > 0)
|
|
|
384 |
{
|
|
|
385 |
pair<string, string> tempvar(current, toLowerCaseWin(current));
|
|
|
386 |
ret.push_back(tempvar);
|
|
|
387 |
}
|
|
|
388 |
return ret;
|
|
|
389 |
}
|
|
|
390 |
|
|
|
391 |
// ----------------------------------------------------------------------------
|
|
|
392 |
// BBCFileUtils::isValidFilename
|
|
|
393 |
// Check if the given filename is valid
|
|
|
394 |
// ----------------------------------------------------------------------------
|
|
|
395 |
//
|
|
|
396 |
bool BBCFileUtils::isValidFilename(const string& aFilename)
|
|
|
397 |
{
|
|
|
398 |
bool valid = true;
|
|
|
399 |
if (aFilename.length() < 1)
|
|
|
400 |
{
|
|
|
401 |
valid = false;
|
|
|
402 |
}
|
|
|
403 |
struct stat stats;
|
|
|
404 |
int fSuccess = stat(aFilename.c_str(), &stats);
|
|
|
405 |
// Check if path is pointing to a directory
|
|
|
406 |
if (fSuccess == -1)
|
|
|
407 |
{
|
|
|
408 |
valid = false;
|
|
|
409 |
} else if ((stats.st_mode & S_IFDIR) != 0)
|
|
|
410 |
{
|
|
|
411 |
valid = false;
|
|
|
412 |
}
|
|
|
413 |
|
|
|
414 |
return valid;
|
|
|
415 |
}
|
|
|
416 |
|
|
|
417 |
// ----------------------------------------------------------------------------
|
|
|
418 |
// BBCFileUtils::isValidDirectory
|
|
|
419 |
// Check if the given path is valid directory
|
|
|
420 |
// ----------------------------------------------------------------------------
|
|
|
421 |
//
|
|
|
422 |
bool BBCFileUtils::isValidDirectory(const string& aPath)
|
|
|
423 |
{
|
|
|
424 |
bool valid = true;
|
|
|
425 |
if (aPath.length() < 1)
|
|
|
426 |
{
|
|
|
427 |
valid = false;
|
|
|
428 |
}
|
|
|
429 |
struct stat stats;
|
|
|
430 |
int fSuccess = stat(aPath.c_str(), &stats);
|
|
|
431 |
// Check if path is pointing to a directory
|
|
|
432 |
if (fSuccess == -1)
|
|
|
433 |
{
|
|
|
434 |
valid = false;
|
|
|
435 |
} else if ((stats.st_mode & S_IFDIR) == 0)
|
|
|
436 |
{
|
|
|
437 |
valid = false;
|
|
|
438 |
}
|
|
|
439 |
|
|
|
440 |
return valid;
|
|
|
441 |
}
|
|
|
442 |
|
|
|
443 |
// ----------------------------------------------------------------------------
|
|
|
444 |
// BBCFileUtils::getFullPath
|
|
|
445 |
// Check if the given path is valid directory
|
|
|
446 |
// ----------------------------------------------------------------------------
|
|
|
447 |
//
|
|
|
448 |
string BBCFileUtils::getFullPath(const string& aPath)
|
|
|
449 |
{
|
|
|
450 |
string ret;
|
|
|
451 |
|
|
|
452 |
#ifdef __WIN__
|
|
|
453 |
char temppath[_MAX_PATH+2];
|
|
|
454 |
char * filepart = NULL;
|
|
|
455 |
GetFullPathName(aPath.c_str(), _MAX_PATH, temppath, &filepart);
|
|
|
456 |
ret = temppath;
|
|
|
457 |
#else
|
|
|
458 |
string path = aPath;
|
|
|
459 |
string filename;
|
|
|
460 |
struct stat statinfo;
|
|
|
461 |
|
|
|
462 |
if ( 0 != stat(aPath.c_str(),&statinfo) )
|
|
|
463 |
{
|
|
|
464 |
filename = StripPath(aPath.c_str());
|
|
|
465 |
path = aPath.substr(0,aPath.size()-filename.size());
|
|
|
466 |
}
|
|
|
467 |
|
|
|
468 |
if (path.size() == 0)
|
|
|
469 |
{
|
|
|
470 |
path = ".";
|
|
|
471 |
}
|
|
|
472 |
|
|
|
473 |
if ( !absolutePath(path,ret) )
|
|
|
474 |
{
|
|
|
475 |
throw HAException("Internal error: Error parsing path.");
|
|
|
476 |
}
|
|
|
477 |
|
|
|
478 |
if (filename.size())
|
|
|
479 |
{
|
|
|
480 |
ret += DIR_SEPARATOR;
|
|
|
481 |
ret += filename;
|
|
|
482 |
}
|
|
|
483 |
#endif
|
|
|
484 |
|
|
|
485 |
return ret;
|
|
|
486 |
}
|
|
|
487 |
|
|
|
488 |
|
|
|
489 |
#ifdef __UNIX__
|
|
|
490 |
// ----------------------------------------------------------------------------
|
|
|
491 |
// BBCFileUtils::absolutePath
|
|
|
492 |
// Get full path
|
|
|
493 |
// ----------------------------------------------------------------------------
|
|
|
494 |
//
|
|
|
495 |
bool BBCFileUtils::absolutePath(const string& aRelpath, string& aAbspath)
|
|
|
496 |
{
|
|
|
497 |
char* base = new char[_MAX_PATH+1];
|
|
|
498 |
char* home;
|
|
|
499 |
getcwd(base, _MAX_PATH);
|
|
|
500 |
home = getenv("HOME");
|
|
|
501 |
aAbspath = base;
|
|
|
502 |
if (aRelpath.length() == 0)
|
|
|
503 |
{
|
|
|
504 |
aAbspath = "";
|
|
|
505 |
return false;
|
|
|
506 |
}
|
|
|
507 |
vector<string> splittedabspath = splitString(aAbspath, '/');
|
|
|
508 |
vector<string> splittedrelpath = splitString(aRelpath, '/');
|
|
|
509 |
vector<string> splittedhomepath = splitString(home, '/');
|
|
|
510 |
int longest;
|
|
|
511 |
if (splittedabspath.size() > splittedhomepath.size())
|
|
|
512 |
{
|
|
|
513 |
longest = splittedabspath.size();
|
|
|
514 |
} else
|
|
|
515 |
{
|
|
|
516 |
longest = splittedhomepath.size();
|
|
|
517 |
}
|
|
|
518 |
splittedabspath.reserve(longest + splittedrelpath.size());
|
|
|
519 |
if (splittedrelpath.at(0) == "")
|
|
|
520 |
{
|
|
|
521 |
splittedabspath.clear();
|
|
|
522 |
if (splittedrelpath.at(1) == "")
|
|
|
523 |
{
|
|
|
524 |
splittedabspath.push_back("");
|
|
|
525 |
splittedabspath.push_back("");
|
|
|
526 |
}
|
|
|
527 |
}
|
|
|
528 |
if (splittedrelpath.at(0) == "~")
|
|
|
529 |
{
|
|
|
530 |
splittedabspath = splittedhomepath;
|
|
|
531 |
splittedrelpath.erase(splittedrelpath.begin());
|
|
|
532 |
}
|
|
|
533 |
vector<string>::iterator dir = splittedrelpath.begin();
|
|
|
534 |
vector<string>::iterator dirend = splittedrelpath.end();
|
|
|
535 |
for(; dir != dirend; dir++)
|
|
|
536 |
{
|
|
|
537 |
string dirstr = *dir;
|
|
|
538 |
if (dirstr == "")
|
|
|
539 |
{
|
|
|
540 |
// Do nothing
|
|
|
541 |
} else if (dirstr == ".")
|
|
|
542 |
{
|
|
|
543 |
// Do nothing
|
|
|
544 |
} else if (dirstr == "..")
|
|
|
545 |
{
|
|
|
546 |
if (splittedabspath.size() > 1)
|
|
|
547 |
{
|
|
|
548 |
splittedabspath.pop_back();
|
|
|
549 |
}
|
|
|
550 |
} else
|
|
|
551 |
{
|
|
|
552 |
if (dirstr.find_first_not_of('.') == string::npos)
|
|
|
553 |
{
|
|
|
554 |
throw HAException("Illegal directory: '" + dirstr + "'\n");
|
|
|
555 |
}
|
|
|
556 |
splittedabspath.push_back(dirstr);
|
|
|
557 |
}
|
|
|
558 |
}
|
|
|
559 |
dir = splittedabspath.begin();
|
|
|
560 |
dirend = splittedabspath.end();
|
|
|
561 |
if (dir != dirend && *dir == "")
|
|
|
562 |
{
|
|
|
563 |
dir++;
|
|
|
564 |
}
|
|
|
565 |
aAbspath = "";
|
|
|
566 |
for(; dir != dirend; dir++)
|
|
|
567 |
{
|
|
|
568 |
aAbspath += '/';
|
|
|
569 |
aAbspath += *dir;
|
|
|
570 |
}
|
|
|
571 |
return true;
|
|
|
572 |
}
|
|
|
573 |
#endif
|
|
|
574 |
|
|
|
575 |
|
|
|
576 |
// ----------------------------------------------------------------------------
|
|
|
577 |
// BBCFileUtils::getDirsInDir
|
|
|
578 |
// Get subdirectories in directory
|
|
|
579 |
// ----------------------------------------------------------------------------
|
|
|
580 |
//
|
|
|
581 |
list<pair<string, string> > BBCFileUtils::getDirsInDir(string aRelPath, list<pair<string, string> >& aExcludes)
|
|
|
582 |
{
|
|
|
583 |
// STRAT -- Support for multiple header directories
|
|
|
584 |
list<pair<string, string> > ret;
|
|
|
585 |
list<pair<string, string> > dirs = BBCFileUtils::extractFilenames(iBaseDir);
|
|
|
586 |
list<pair<string, string> >::iterator dirbegin = dirs.begin();
|
|
|
587 |
for(; dirbegin != dirs.end(); dirbegin++)
|
|
|
588 |
{
|
|
|
589 |
// END -- Support for multiple header directories
|
|
|
590 |
string basedir = BBCFileUtils::getFullPath(dirbegin->first);
|
|
|
591 |
|
|
|
592 |
if (aRelPath != "")
|
|
|
593 |
{
|
|
|
594 |
iWildcard = basedir + DIR_SEPARATOR + aRelPath + DIR_SEPARATOR + WILDCARD_ALLFILES;
|
|
|
595 |
} else
|
|
|
596 |
{
|
|
|
597 |
iWildcard = basedir + DIR_SEPARATOR + WILDCARD_ALLFILES;
|
|
|
598 |
}
|
|
|
599 |
|
|
|
600 |
// search for all files which were specified in wildcard string
|
|
|
601 |
#ifdef __WIN__
|
|
|
602 |
_finddata_t finddata;
|
|
|
603 |
intptr_t searchsession =_findfirst(iWildcard.c_str(),&finddata);
|
|
|
604 |
int found = -1;
|
|
|
605 |
if (searchsession > 0)
|
|
|
606 |
{
|
|
|
607 |
found = 0;
|
|
|
608 |
}
|
|
|
609 |
while (found == 0)
|
|
|
610 |
{
|
|
|
611 |
string filename = finddata.name;
|
|
|
612 |
if (filename == "." || filename == "..")
|
|
|
613 |
{
|
|
|
614 |
// Do Nothing
|
|
|
615 |
} else
|
|
|
616 |
{
|
|
|
617 |
if (aRelPath != "")
|
|
|
618 |
{
|
|
|
619 |
filename = aRelPath + DIR_SEPARATOR + filename;
|
|
|
620 |
}
|
|
|
621 |
string fullpath = basedir + DIR_SEPARATOR + filename;
|
|
|
622 |
// Check if path is pointing to a directory
|
|
|
623 |
if (isValidDirectory(fullpath))
|
|
|
624 |
{
|
|
|
625 |
list<pair<string, string> >::const_iterator filereplace = FindFromList(toLowerCaseWin(filename), aExcludes, ERightValue);
|
|
|
626 |
if (filereplace == aExcludes.end())
|
|
|
627 |
{
|
|
|
628 |
filereplace = FindFromList(toLowerCaseWin(finddata.name), aExcludes, ERightValue);
|
|
|
629 |
}
|
|
|
630 |
if (filereplace == aExcludes.end())
|
|
|
631 |
{
|
|
|
632 |
pair<string, string> tempvar(filename, toLowerCaseWin(filename));
|
|
|
633 |
ret.push_back(tempvar);
|
|
|
634 |
}
|
|
|
635 |
}
|
|
|
636 |
}
|
|
|
637 |
found = _findnext(searchsession, &finddata);
|
|
|
638 |
}
|
|
|
639 |
_findclose(searchsession);
|
|
|
640 |
#else
|
|
|
641 |
//Wildcard.c_str());
|
|
|
642 |
glob_t p_glob;
|
|
|
643 |
|
|
|
644 |
int result = glob(iWildcard.c_str() , GLOB_ONLYDIR, NULL, &p_glob);
|
|
|
645 |
if (result == 0)
|
|
|
646 |
{
|
|
|
647 |
for ( unsigned int i = 0; i < p_glob.gl_pathc; i++ )
|
|
|
648 |
{
|
|
|
649 |
string path = p_glob.gl_pathv[i];
|
|
|
650 |
if (path.at(path.length() - 1) == '/')
|
|
|
651 |
{
|
|
|
652 |
path.resize(path.length() - 1);
|
|
|
653 |
}
|
|
|
654 |
// Check if path is pointing to a directory
|
|
|
655 |
if (isValidDirectory(path))
|
|
|
656 |
{
|
|
|
657 |
string dirname = BBCFileUtils::StripPath(path);
|
|
|
658 |
path = dirname;
|
|
|
659 |
if (aRelPath != "")
|
|
|
660 |
{
|
|
|
661 |
path = aRelPath + DIR_SEPARATOR + dirname;
|
|
|
662 |
}
|
|
|
663 |
list<pair<string, string> >::const_iterator filereplace = FindFromList(toLowerCaseWin(path), aExcludes,ERightValue);
|
|
|
664 |
if (filereplace == aExcludes.end())
|
|
|
665 |
{
|
|
|
666 |
filereplace = FindFromList(toLowerCaseWin(dirname), aExcludes,ERightValue);
|
|
|
667 |
}
|
|
|
668 |
if (filereplace == aExcludes.end())
|
|
|
669 |
{
|
|
|
670 |
pair<string, string> tempvar(path, toLowerCaseWin(path));
|
|
|
671 |
ret.push_back(tempvar);
|
|
|
672 |
}
|
|
|
673 |
}
|
|
|
674 |
}
|
|
|
675 |
globfree(&p_glob);
|
|
|
676 |
}
|
|
|
677 |
|
|
|
678 |
#endif
|
|
|
679 |
}
|
|
|
680 |
return ret;
|
|
|
681 |
}
|
|
|
682 |
|
|
|
683 |
|
|
|
684 |
// ----------------------------------------------------------------------------
|
|
|
685 |
// BBCFileUtils::StripPath
|
|
|
686 |
// Strip off path
|
|
|
687 |
// ----------------------------------------------------------------------------
|
|
|
688 |
//
|
|
|
689 |
string BBCFileUtils::StripPath(const string& aFilename)
|
|
|
690 |
{
|
|
|
691 |
size_t pos = aFilename.find_last_of("\\/");
|
|
|
692 |
|
|
|
693 |
if (pos != string::npos)
|
|
|
694 |
{
|
|
|
695 |
return string(aFilename.begin()+pos+1, aFilename.end());
|
|
|
696 |
}
|
|
|
697 |
return aFilename;
|
|
|
698 |
}
|
|
|
699 |
|
|
|
700 |
// ----------------------------------------------------------------------------
|
|
|
701 |
// BBCFileUtils::StripFilenameExtension
|
|
|
702 |
// Strip off filename extension
|
|
|
703 |
// ----------------------------------------------------------------------------
|
|
|
704 |
//
|
|
|
705 |
string BBCFileUtils::StripFilenameExtension(string aFilename)
|
|
|
706 |
{
|
|
|
707 |
string ret(aFilename);
|
|
|
708 |
size_t pos = ret.find_last_of("\\/.");
|
|
|
709 |
if (pos != string::npos)
|
|
|
710 |
{
|
|
|
711 |
string::iterator begin = ret.begin() + pos;
|
|
|
712 |
string::iterator end = ret.end();
|
|
|
713 |
if (*begin == '.')
|
|
|
714 |
ret.erase(begin,end);
|
|
|
715 |
}
|
|
|
716 |
return ret;
|
|
|
717 |
}
|
|
|
718 |
/**
|
|
|
719 |
* This function first splits strings in vectors. Then it finds the
|
|
|
720 |
* first part, the common part and the last part of the directories
|
|
|
721 |
* and combines them.
|
|
|
722 |
*/
|
|
|
723 |
// Changes done to this function to Support for multiple header directories. Return type changed to list.
|
|
|
724 |
list<pair<string, bool> > BBCFileUtils::MergeDirs(const string& rootDirs, const string& subDir)
|
|
|
725 |
{
|
|
|
726 |
// START -- Support for multiple header directories --
|
|
|
727 |
list<pair<string, bool> > retu;
|
|
|
728 |
list<pair<string, string> > dirs = BBCFileUtils::extractFilenames(rootDirs);
|
|
|
729 |
list<pair<string, string> >::iterator dirbegin = dirs.begin();
|
|
|
730 |
for(; dirbegin != dirs.end(); dirbegin++)
|
|
|
731 |
{
|
|
|
732 |
string ret;
|
|
|
733 |
string rootDir = BBCFileUtils::getFullPath(dirbegin->first);
|
|
|
734 |
// END -- Support for multiple header directories --
|
|
|
735 |
vector<string> rootVector(splitString(rootDir, DIR_SEPARATOR));
|
|
|
736 |
vector<string> subVector(splitString(subDir, DIR_SEPARATOR));
|
|
|
737 |
|
|
|
738 |
vector<string>::iterator rootIt = rootVector.begin();
|
|
|
739 |
vector<string>::iterator subIt = subVector.begin();
|
|
|
740 |
|
|
|
741 |
bool commonFound = false;
|
|
|
742 |
|
|
|
743 |
while( rootIt != rootVector.end() || subIt != subVector.end() )
|
|
|
744 |
{
|
|
|
745 |
string fixedRoot;
|
|
|
746 |
string fixedSub;
|
|
|
747 |
|
|
|
748 |
if( rootIt != rootVector.end() )
|
|
|
749 |
{
|
|
|
750 |
fixedRoot = *rootIt;
|
|
|
751 |
toLower(fixedRoot);
|
|
|
752 |
}
|
|
|
753 |
|
|
|
754 |
if( subIt != subVector.end() )
|
|
|
755 |
{
|
|
|
756 |
fixedSub = *subIt;
|
|
|
757 |
toLower(fixedSub);
|
|
|
758 |
}
|
|
|
759 |
|
|
|
760 |
if( rootIt != rootVector.end() &&
|
|
|
761 |
subIt != subVector.end() )
|
|
|
762 |
{
|
|
|
763 |
if( fixedRoot != fixedSub )
|
|
|
764 |
{
|
|
|
765 |
if( ret.size() > 0 )
|
|
|
766 |
ret += DIR_SEPARATOR;
|
|
|
767 |
|
|
|
768 |
if( commonFound == false )
|
|
|
769 |
{
|
|
|
770 |
ret += fixedRoot;
|
|
|
771 |
++rootIt;
|
|
|
772 |
}
|
|
|
773 |
else
|
|
|
774 |
{
|
|
|
775 |
ret += fixedSub;
|
|
|
776 |
++subIt;
|
|
|
777 |
}
|
|
|
778 |
}
|
|
|
779 |
else if( fixedRoot == fixedSub )
|
|
|
780 |
{
|
|
|
781 |
if( ret.size() > 0 )
|
|
|
782 |
ret += DIR_SEPARATOR;
|
|
|
783 |
ret += fixedSub;
|
|
|
784 |
|
|
|
785 |
++subIt;
|
|
|
786 |
++rootIt;
|
|
|
787 |
commonFound = true;
|
|
|
788 |
}
|
|
|
789 |
}
|
|
|
790 |
else if( subIt != subVector.end() )
|
|
|
791 |
{
|
|
|
792 |
if( ret.size() > 0 )
|
|
|
793 |
ret += DIR_SEPARATOR;
|
|
|
794 |
ret += fixedSub;
|
|
|
795 |
|
|
|
796 |
++subIt;
|
|
|
797 |
}
|
|
|
798 |
else
|
|
|
799 |
{
|
|
|
800 |
break;
|
|
|
801 |
}
|
|
|
802 |
}
|
|
|
803 |
#ifndef __WIN__
|
|
|
804 |
ret=DIR_SEPARATOR+ret;
|
|
|
805 |
#endif
|
|
|
806 |
// START -- Support for multiple header directories --
|
|
|
807 |
pair<string, bool> tempvar(ret, subIt == subVector.end());
|
|
|
808 |
retu.push_back(tempvar);
|
|
|
809 |
}
|
|
|
810 |
return retu;
|
|
|
811 |
// END -- Support for multiple header directories --
|
|
|
812 |
}
|
|
|
813 |
map<string, bool> BBCFileUtils::fileExistsCache = map<string, bool>();
|
|
|
814 |
bool BBCFileUtils::FileExists(const string& fileName)
|
|
|
815 |
{
|
|
|
816 |
map<string, bool>::const_iterator i = fileExistsCache.find(fileName);
|
|
|
817 |
if( i != fileExistsCache.end() )
|
|
|
818 |
{
|
|
|
819 |
return i->second;
|
|
|
820 |
}
|
|
|
821 |
fstream fin;
|
|
|
822 |
fin.open(fileName.c_str(),ios::in);
|
|
|
823 |
bool fileExists = fin.is_open();
|
|
|
824 |
fin.close();
|
|
|
825 |
fileExistsCache.insert(pair<string, bool>(fileName, fileExists));
|
|
|
826 |
return fileExists;
|
|
|
827 |
}
|
|
|
828 |
|
|
|
829 |
string BBCFileUtils::getCompilationError(string filename)
|
|
|
830 |
{
|
|
|
831 |
//Add compilation error to the report
|
|
|
832 |
std::ifstream file(filename.c_str());
|
|
|
833 |
string templine;
|
|
|
834 |
string compilationError;
|
|
|
835 |
int size =0;
|
|
|
836 |
int characterLimit = 2000;
|
|
|
837 |
// Keep the character count of total line to be displayed in report to 2000.
|
|
|
838 |
while(size <= characterLimit && std::getline(file,templine))
|
|
|
839 |
{
|
|
|
840 |
int index;
|
|
|
841 |
string replaceFrom = "\'";
|
|
|
842 |
string replaceTo = "`";
|
|
|
843 |
size += templine.size();
|
|
|
844 |
if(size > characterLimit)
|
|
|
845 |
compilationError.append(".........");
|
|
|
846 |
else
|
|
|
847 |
{
|
|
|
848 |
//Replace if any "'" with "`" , to work properly with stylesheet.
|
|
|
849 |
if((index = (int)templine.find(replaceFrom))!= string::npos)
|
|
|
850 |
templine.replace(index,replaceFrom.size(),replaceTo);
|
|
|
851 |
compilationError.append(templine);
|
|
|
852 |
compilationError.append("\n");
|
|
|
853 |
|
|
|
854 |
|
|
|
855 |
}
|
|
|
856 |
}
|
|
|
857 |
|
|
|
858 |
return compilationError;
|
|
|
859 |
}
|
|
|
860 |
|
|
|
861 |
// ----------------------------------------------------------------------------------------------------------
|
|
|
862 |
|
|
|
863 |
string& BBCFileUtils::TrimRight(string& s)
|
|
|
864 |
{
|
|
|
865 |
int pos(s.size());
|
|
|
866 |
for (; pos && (s[pos-1]==' ' || s[pos-1]=='\t' || s[pos-1]=='\r'); --pos);
|
|
|
867 |
s.erase(pos, s.size()-pos);
|
|
|
868 |
return s;
|
|
|
869 |
}
|
|
|
870 |
|
|
|
871 |
// ----------------------------------------------------------------------------------------------------------
|
|
|
872 |
|
|
|
873 |
string& BBCFileUtils::TrimLeft(string& s)
|
|
|
874 |
{
|
|
|
875 |
int pos(0);
|
|
|
876 |
for (; s[pos]==' ' || s[pos]=='\t' || s[pos]=='\r'; ++pos);
|
|
|
877 |
s.erase(0, pos);
|
|
|
878 |
return s;
|
|
|
879 |
}
|
|
|
880 |
|
|
|
881 |
// ----------------------------------------------------------------------------------------------------------
|
|
|
882 |
|
|
|
883 |
string& BBCFileUtils::TrimAll(string& s)
|
|
|
884 |
{
|
|
|
885 |
return TrimLeft(TrimRight(s));
|
|
|
886 |
}
|
|
|
887 |
|