sd: keep a non-owning pointer to the OverridingShell
[LibreOffice.git] / soltools / mkdepend / collectdircontent.cxx
blob2f64520b773a577301afbc5481d456303206a894
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()));
14 } else
15 return PathFilePair(".", filePath);
18 void IncludesCollection::add_to_collection(const std::string& dirPath) {
19 DirContent dirContent;
20 #if defined(_WIN32)
21 WIN32_FIND_DATA FindFileData;
22 HANDLE hFind;
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()));
27 return;
29 do {
30 std::string winFileName(FindFileData.cFileName);
31 transform(
32 winFileName.begin(), winFileName.end(), winFileName.begin(),
33 [](char c) {
34 return rtl::toAsciiLowerCase(static_cast<unsigned char>(c));
35 });
36 dirContent.insert(winFileName);
37 } while (FindNextFile(hFind, &FindFileData));
38 #else
39 DIR *pdir;
40 dirent *pent;
41 pdir = opendir(dirPath.c_str()); //"." refers to the current dir
42 if (!pdir) {
43 // Invalid File Handle - no need to try it anymore
44 allIncludes.insert(EntriesPair(dirPath, DirContent()));
45 return;
47 while ((pent = readdir(pdir))) {
48 dirContent.insert(pent->d_name);
50 closedir(pdir);
51 #endif // defined( _WIN32 )
52 allIncludes.insert(EntriesPair(dirPath, dirContent));
55 bool IncludesCollection::exists(std::string filePath) {
56 #if defined(_WIN32)
57 transform(
58 filePath.begin(), filePath.end(), filePath.begin(),
59 [](char c) {
60 return rtl::toAsciiLowerCase(static_cast<unsigned char>(c));
61 });
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();
77 extern "C" {
79 IncludesCollection * create_IncludesCollection() {
80 return new IncludesCollection;
83 void delete_IncludesCollection(IncludesCollection *m) {
84 delete m;
87 int call_IncludesCollection_exists(IncludesCollection* m, const char * filePath) {
88 return m->exists(filePath);
92 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */