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 0 : PathFilePair IncludesCollection::split_path(const string& filePath) {
7 0 : string sepU = "/";
8 0 : string sepW = "\\";
9 0 : string::size_type pos = filePath.rfind (sepU);
10 0 : string::size_type posW = filePath.rfind (sepW);
11 0 : if ((posW != string::npos) && ((posW > pos) || (pos == string::npos))) pos = posW;
12 0 : if (pos != string::npos) {
13 0 : string dirName = filePath.substr(0, pos);
14 0 : return PathFilePair(dirName, filePath.substr(pos + 1, filePath.length()));
15 : } else
16 0 : return PathFilePair(".", filePath);
17 : }
18 :
19 0 : void IncludesCollection::add_to_collection(const string& dirPath) {
20 0 : 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 0 : pdir = opendir(dirPath.c_str()); //"." refers to the current dir
39 0 : if (!pdir) {
40 : // Invalid File Handle - no need to try it anymore
41 0 : allIncludes.insert(EntriesPair(dirPath, DirContent()));
42 0 : return;
43 : }
44 0 : while ((pent = readdir(pdir))) {
45 0 : dirContent.insert(pent->d_name);
46 : }
47 0 : closedir(pdir);
48 : #endif // defined( WNT )
49 0 : allIncludes.insert(EntriesPair(dirPath, dirContent));
50 : }
51 :
52 0 : bool IncludesCollection::exists(string filePath) {
53 : #if defined( WNT )
54 : transform(filePath.begin(), filePath.end(), filePath.begin(), ::tolower);
55 : #endif // defined( WNT )
56 0 : PathFilePair dirFile = split_path(filePath);
57 0 : string dirPath = dirFile.first;
58 0 : string fileName = dirFile.second;
59 0 : DirMap::iterator mapIter = allIncludes.find(dirPath);
60 0 : if (mapIter == allIncludes.end()) {
61 0 : add_to_collection(dirPath);
62 0 : mapIter = allIncludes.find(dirPath);
63 : }
64 0 : DirContent dirContent = (*mapIter).second;
65 0 : DirContent::iterator dirIter = dirContent.find(fileName);
66 0 : if (dirIter == dirContent.end()) {
67 0 : return false;
68 : } else {
69 0 : return true;
70 0 : };
71 : }
72 :
73 : extern "C" {
74 :
75 0 : IncludesCollection * create_IncludesCollection() {
76 0 : return new IncludesCollection;
77 : }
78 :
79 0 : void delete_IncludesCollection(IncludesCollection *m) {
80 0 : delete m;
81 0 : }
82 :
83 0 : int call_IncludesCollection_exists(IncludesCollection* m, const char * filePath) {
84 0 : return m->exists(filePath);
85 : }
86 0 : }
87 :
88 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|