1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 #include "collectdircontent.hxx"
3 #include <rtl/character.hxx>
5 PathFilePair
IncludesCollection::split_path(const std::string
& filePath
) {
6 std::string sepU
= "/";
7 std::string sepW
= "\\";
8 std::string::size_type pos
= filePath
.rfind (sepU
);
9 std::string::size_type posW
= filePath
.rfind (sepW
);
10 if ((posW
!= std::string::npos
) && ((posW
> pos
) || (pos
== std::string::npos
))) pos
= posW
;
11 if (pos
!= std::string::npos
) {
12 std::string dirName
= filePath
.substr(0, pos
);
13 return PathFilePair(dirName
, filePath
.substr(pos
+ 1, filePath
.length()));
15 return PathFilePair(".", filePath
);
18 void IncludesCollection::add_to_collection(const std::string
& dirPath
) {
19 DirContent dirContent
;
21 WIN32_FIND_DATA FindFileData
;
23 hFind
= FindFirstFile((dirPath
+ "\\*").c_str(), &FindFileData
);
24 if (hFind
== INVALID_HANDLE_VALUE
) {
25 // Invalid File Handle - no need to try it anymore
26 allIncludes
.insert(EntriesPair(dirPath
, DirContent()));
30 std::string
winFileName(FindFileData
.cFileName
);
32 winFileName
.begin(), winFileName
.end(), winFileName
.begin(),
34 return rtl::toAsciiLowerCase(static_cast<unsigned char>(c
));
36 dirContent
.insert(winFileName
);
37 } while (FindNextFile(hFind
, &FindFileData
));
41 pdir
= opendir(dirPath
.c_str()); //"." refers to the current dir
43 // Invalid File Handle - no need to try it anymore
44 allIncludes
.insert(EntriesPair(dirPath
, DirContent()));
47 while ((pent
= readdir(pdir
))) {
48 dirContent
.insert(pent
->d_name
);
51 #endif // defined( _WIN32 )
52 allIncludes
.insert(EntriesPair(dirPath
, dirContent
));
55 bool IncludesCollection::exists(std::string filePath
) {
58 filePath
.begin(), filePath
.end(), filePath
.begin(),
60 return rtl::toAsciiLowerCase(static_cast<unsigned char>(c
));
62 #endif // defined( _WIN32 )
63 PathFilePair dirFile
= split_path(filePath
);
64 std::string dirPath
= dirFile
.first
;
65 std::string fileName
= dirFile
.second
;
66 DirMap::iterator mapIter
= allIncludes
.find(dirPath
);
67 if (mapIter
== allIncludes
.end()) {
68 add_to_collection(dirPath
);
69 mapIter
= allIncludes
.find(dirPath
);
71 assert(mapIter
!= allIncludes
.end());
72 DirContent dirContent
= (*mapIter
).second
;
73 DirContent::iterator dirIter
= dirContent
.find(fileName
);
74 return dirIter
!= dirContent
.end();
79 IncludesCollection
* create_IncludesCollection() {
80 return new IncludesCollection
;
83 void delete_IncludesCollection(IncludesCollection
*m
) {
87 int call_IncludesCollection_exists(IncludesCollection
* m
, const char * filePath
) {
88 return m
->exists(filePath
);
92 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */