Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : #include "collectdircontent.hxx"
3 :
4 : using namespace std;
5 :
6 297 : PathFilePair IncludesCollection::split_path(const string& filePath) {
7 297 : string sepU = "/";
8 297 : string sepW = "\\";
9 297 : string::size_type pos = filePath.rfind (sepU);
10 297 : string::size_type posW = filePath.rfind (sepW);
11 297 : if ((posW != string::npos) && ((posW > pos) || (pos == string::npos))) pos = posW;
12 297 : if (pos != string::npos) {
13 281 : string dirName = filePath.substr(0, pos);
14 281 : return PathFilePair(dirName, filePath.substr(pos + 1, filePath.length()));
15 : } else
16 16 : return PathFilePair(".", filePath);
17 : }
18 :
19 61 : void IncludesCollection::add_to_collection(const string& dirPath) {
20 61 : DirContent dirContent;
21 : #if defined( WNT )
22 : WIN32_FIND_DATA FindFileData;
23 : HANDLE hFind;
24 : hFind = FindFirstFile((dirPath + "\\*").c_str(), &FindFileData);
25 : if (hFind == INVALID_HANDLE_VALUE) {
26 : // Invalid File Handle - no need to try it anymore
27 : allIncludes.insert(EntriesPair(dirPath, DirContent()));
28 : return;
29 : };
30 : do {
31 : string winFileName(FindFileData.cFileName);
32 : transform(winFileName.begin(), winFileName.end(), winFileName.begin(), ::tolower);
33 : dirContent.insert(winFileName);
34 : } while (FindNextFile(hFind, &FindFileData));
35 : #else
36 : DIR *pdir;
37 : dirent *pent;
38 61 : pdir = opendir(dirPath.c_str()); //"." refers to the current dir
39 61 : if (!pdir) {
40 : // Invalid File Handle - no need to try it anymore
41 50 : allIncludes.insert(EntriesPair(dirPath, DirContent()));
42 61 : return;
43 : }
44 335 : while ((pent = readdir(pdir))) {
45 313 : dirContent.insert(pent->d_name);
46 : };
47 11 : closedir(pdir);
48 : #endif // defined( WNT )
49 11 : allIncludes.insert(EntriesPair(dirPath, dirContent));
50 : }
51 :
52 297 : bool IncludesCollection::exists(string filePath) {
53 : #if defined( WNT )
54 : transform(filePath.begin(), filePath.end(), filePath.begin(), ::tolower);
55 : #endif // defined( WNT )
56 297 : PathFilePair dirFile = split_path(filePath);
57 297 : string dirPath = dirFile.first;
58 297 : string fileName = dirFile.second;
59 297 : DirMap::iterator mapIter = allIncludes.find(dirPath);
60 297 : if (mapIter == allIncludes.end()) {
61 61 : add_to_collection(dirPath);
62 61 : mapIter = allIncludes.find(dirPath);
63 : };
64 297 : DirContent dirContent = (*mapIter).second;
65 297 : DirContent::iterator dirIter = dirContent.find(fileName);
66 297 : if (dirIter == dirContent.end()) {
67 288 : return false;
68 : } else {
69 9 : return true;
70 297 : };
71 : }
72 :
73 : extern "C" {
74 :
75 2 : IncludesCollection * create_IncludesCollection() {
76 2 : return new IncludesCollection;
77 : }
78 :
79 2 : void delete_IncludesCollection(IncludesCollection *m) {
80 2 : delete m;
81 2 : }
82 :
83 297 : int call_IncludesCollection_exists(IncludesCollection* m, const char * filePath) {
84 297 : return m->exists(filePath);
85 : }
86 6 : }
87 :
88 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|