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