2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013 Peter Powell <petpow@saberuk.com>
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.
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
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/>.
28 FileReader::FileReader(const std::string& filename)
33 void FileReader::Load(const std::string& filename)
35 // If the file is stored in the file cache then we used that version instead.
36 ConfigFileCache::const_iterator it = ServerInstance->Config->Files.find(filename);
37 if (it != ServerInstance->Config->Files.end())
39 this->lines = it->second;
43 const std::string realName = ServerInstance->Config->Paths.PrependConfig(filename);
46 std::ifstream stream(realName.c_str());
47 if (!stream.is_open())
48 throw CoreException(filename + " does not exist or is not readable!");
51 while (std::getline(stream, line))
53 lines.push_back(line);
54 totalSize += line.size() + 2;
61 std::string FileReader::GetString() const
64 for (file_cache::const_iterator it = this->lines.begin(); it != this->lines.end(); ++it)
67 buffer.append("\r\n");
72 std::string FileSystem::ExpandPath(const std::string& base, const std::string& fragment)
74 // The fragment is an absolute path, don't modify it.
75 if (fragment[0] == '/' || FileSystem::StartsWithWindowsDriveLetter(fragment))
78 return base + '/' + fragment;
81 bool FileSystem::FileExists(const std::string& file)
84 if (stat(file.c_str(), &sb) == -1)
87 if ((sb.st_mode & S_IFDIR) > 0)
90 return !access(file.c_str(), F_OK);
93 bool FileSystem::GetFileList(const std::string& directory, std::vector<std::string>& entries, const std::string& match)
96 const std::string search_path = directory + "\\" + match;
99 HANDLE fh = FindFirstFileA(search_path.c_str(), &wfd);
100 if (fh == INVALID_HANDLE_VALUE)
105 entries.push_back(wfd.cFileName);
106 } while (FindNextFile(fh, &wfd) != 0);
111 DIR* library = opendir(directory.c_str());
115 dirent* entry = NULL;
116 while ((entry = readdir(library)))
118 if (InspIRCd::Match(entry->d_name, match, ascii_case_insensitive_map))
119 entries.push_back(entry->d_name);
127 std::string FileSystem::GetFileName(const std::string& name)
130 size_t pos = name.find_last_of("\\/");
132 size_t pos = name.rfind('/');
134 return pos == std::string::npos ? name : name.substr(++pos);
137 bool FileSystem::StartsWithWindowsDriveLetter(const std::string& path)
139 return (path.length() > 2 && isalpha(path[0]) && path[1] == ':');