]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/fileutils.cpp
Add a method for getting a list of files in a directory.
[user/henk/code/inspircd.git] / src / fileutils.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Peter Powell <petpow@saberuk.com>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21
22 #include <fstream>
23
24 FileReader::FileReader(const std::string& filename)
25 {
26         Load(filename);
27 }
28
29 void FileReader::Load(const std::string& filename)
30 {
31         // If the file is stored in the file cache then we used that version instead.
32         ConfigFileCache::const_iterator it = ServerInstance->Config->Files.find(filename);
33         if (it != ServerInstance->Config->Files.end())
34         {
35                 this->lines = it->second;
36         }
37         else
38         {
39                 const std::string realName = ServerInstance->Config->Paths.PrependConfig(filename);
40                 lines.clear();
41
42                 std::ifstream stream(realName.c_str());
43                 if (!stream.is_open())
44                         throw CoreException(filename + " does not exist or is not readable!");
45
46                 std::string line;
47                 while (std::getline(stream, line))
48                 {
49                         lines.push_back(line);
50                         totalSize += line.size() + 2;
51                 }
52
53                 stream.close();
54         }
55 }
56
57 std::string FileReader::GetString() const
58 {
59         std::string buffer;
60         for (file_cache::const_iterator it = this->lines.begin(); it != this->lines.end(); ++it)
61         {
62                 buffer.append(*it);
63                 buffer.append("\r\n");
64         }
65         return buffer;
66 }
67
68 std::string FileSystem::ExpandPath(const std::string& base, const std::string& fragment)
69 {
70         // The fragment is an absolute path, don't modify it.
71         if (fragment[0] == '/' || FileSystem::StartsWithWindowsDriveLetter(fragment))
72                 return fragment;
73
74         return base + '/' + fragment;
75 }
76
77 bool FileSystem::FileExists(const std::string& file)
78 {
79         struct stat sb;
80         if (stat(file.c_str(), &sb) == -1)
81                 return false;
82
83         if ((sb.st_mode & S_IFDIR) > 0)
84                 return false;
85
86         return !access(file.c_str(), F_OK);
87 }
88
89 bool FileSystem::GetFileList(const std::string& directory, std::vector<std::string>& entries, const std::string& match)
90 {
91 #ifdef _WIN32
92         const std::string search_path = directory + "\\" + match;
93
94         WIN32_FIND_DATAA wfd;
95         HANDLE fh = FindFirstFileA(search_path.c_str(), &wfd);
96         if (fh == INVALID_HANDLE_VALUE)
97                 return false;
98
99         do
100         {
101                 entries.push_back(wfd.cFileName);
102         } while (FindNextFile(fh, &wfd) != 0);
103
104         FindClose(fh);
105         return true;
106 #else
107         DIR* library = opendir(directory.c_str());
108         if (!library)
109                 return false;
110
111         dirent* entry = NULL;
112         while ((entry = readdir(library)))
113         {
114                 if (InspIRCd::Match(entry->d_name, match, ascii_case_insensitive_map))
115                         entries.push_back(entry->d_name);
116         }
117         closedir(library);
118         return true;
119 #endif
120 }
121
122
123 std::string FileSystem::GetFileName(const std::string& name)
124 {
125 #ifdef _WIN32
126         size_t pos = name.find_last_of("\\/");
127 #else
128         size_t pos = name.rfind('/');
129 #endif
130         return pos == std::string::npos ? name : name.substr(++pos);
131 }
132
133 bool FileSystem::StartsWithWindowsDriveLetter(const std::string& path)
134 {
135         return (path.length() > 2 && isalpha(path[0]) && path[1] == ':');
136 }