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