Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / soltools / mkdepend / collectdircontent.cxx
blob0041857ee879e4814c17a811159a12cffb7e81ac
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 #include "collectdircontent.hxx"
4 using namespace std;
6 PathFilePair IncludesCollection::split_path(const string& filePath) {
7 string sepU = "/";
8 string sepW = "\\";
9 string::size_type pos = filePath.rfind (sepU);
10 string::size_type posW = filePath.rfind (sepW);
11 if ((posW != string::npos) && ((posW > pos) || (pos == string::npos))) pos = posW;
12 if (pos != string::npos) {
13 string dirName = filePath.substr(0, pos);
14 return PathFilePair(dirName, filePath.substr(pos + 1, filePath.length()));
15 } else
16 return PathFilePair(".", filePath);
19 void IncludesCollection::add_to_collection(const string& dirPath) {
20 DirContent dirContent;
21 #if defined(_WIN32)
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;
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 pdir = opendir(dirPath.c_str()); //"." refers to the current dir
39 if (!pdir) {
40 // Invalid File Handle - no need to try it anymore
41 allIncludes.insert(EntriesPair(dirPath, DirContent()));
42 return;
44 while ((pent = readdir(pdir))) {
45 dirContent.insert(pent->d_name);
47 closedir(pdir);
48 #endif // defined( WNT )
49 allIncludes.insert(EntriesPair(dirPath, dirContent));
52 bool IncludesCollection::exists(string filePath) {
53 #if defined(_WIN32)
54 transform(filePath.begin(), filePath.end(), filePath.begin(), ::tolower);
55 #endif // defined( WNT )
56 PathFilePair dirFile = split_path(filePath);
57 string dirPath = dirFile.first;
58 string fileName = dirFile.second;
59 DirMap::iterator mapIter = allIncludes.find(dirPath);
60 if (mapIter == allIncludes.end()) {
61 add_to_collection(dirPath);
62 mapIter = allIncludes.find(dirPath);
64 DirContent dirContent = (*mapIter).second;
65 DirContent::iterator dirIter = dirContent.find(fileName);
66 if (dirIter == dirContent.end()) {
67 return false;
68 } else {
69 return true;
73 extern "C" {
75 IncludesCollection * create_IncludesCollection() {
76 return new IncludesCollection;
79 void delete_IncludesCollection(IncludesCollection *m) {
80 delete m;
83 int call_IncludesCollection_exists(IncludesCollection* m, const char * filePath) {
84 return m->exists(filePath);
88 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */