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/>.
24 FileReader::FileReader(const std::string& filename)
29 void FileReader::Load(const std::string& filename)
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())
35 this->lines = it->second;
39 const std::string realName = ServerInstance->Config->Paths.PrependConfig(filename);
42 std::ifstream stream(realName.c_str());
43 if (!stream.is_open())
44 throw CoreException(filename + " does not exist or is not readable!");
47 while (std::getline(stream, line))
49 lines.push_back(line);
50 totalSize += line.size() + 2;
57 std::string FileReader::GetString() const
60 for (file_cache::const_iterator it = this->lines.begin(); it != this->lines.end(); ++it)
63 buffer.append("\r\n");
68 std::string FileSystem::ExpandPath(const std::string& base, const std::string& fragment)
70 // The fragment is an absolute path, don't modify it.
71 if (fragment[0] == '/' || FileSystem::StartsWithWindowsDriveLetter(fragment))
74 return base + '/' + fragment;
77 bool FileSystem::FileExists(const std::string& file)
80 if (stat(file.c_str(), &sb) == -1)
83 if ((sb.st_mode & S_IFDIR) > 0)
86 return !access(file.c_str(), F_OK);
89 bool FileSystem::GetFileList(const std::string& directory, std::vector<std::string>& entries, const std::string& match)
92 const std::string search_path = directory + "\\" + match;
95 HANDLE fh = FindFirstFileA(search_path.c_str(), &wfd);
96 if (fh == INVALID_HANDLE_VALUE)
101 entries.push_back(wfd.cFileName);
102 } while (FindNextFile(fh, &wfd) != 0);
107 DIR* library = opendir(directory.c_str());
111 dirent* entry = NULL;
112 while ((entry = readdir(library)))
114 if (InspIRCd::Match(entry->d_name, match, ascii_case_insensitive_map))
115 entries.push_back(entry->d_name);
123 std::string FileSystem::GetFileName(const std::string& name)
126 size_t pos = name.find_last_of("\\/");
128 size_t pos = name.rfind('/');
130 return pos == std::string::npos ? name : name.substr(++pos);
133 bool FileSystem::StartsWithWindowsDriveLetter(const std::string& path)
135 return (path.length() > 2 && isalpha(path[0]) && path[1] == ':');