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