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