2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
5 * Copyright (C) 2013, 2019 Sadie Powell <sadie@witchery.services>
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.
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
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/>.
23 /** Provides an easy method of reading a text file into memory. */
24 class CoreExport FileReader
26 /** The lines of text in the file. */
27 std::vector<std::string> lines;
29 /** File size in bytes. */
30 unsigned long totalSize;
33 /** Initializes a new file reader. */
34 FileReader() : totalSize(0) { }
36 /** Initializes a new file reader and reads the specified file.
37 * @param filename The file to read into memory.
39 FileReader(const std::string& filename);
41 /** Loads a text file from disk.
42 * @param filename The file to read into memory.
43 * @throw CoreException The file can not be loaded.
45 void Load(const std::string& filename);
47 /** Retrieves the entire contents of the file cache as a single string. */
48 std::string GetString() const;
50 /** Retrieves the entire contents of the file cache as a vector of strings. */
51 const std::vector<std::string>& GetVector() const { return lines; }
53 /** Retrieves the total size in bytes of the file. */
54 unsigned long TotalSize() const { return totalSize; }
57 /** Implements methods for file system access */
58 class CoreExport FileSystem
64 /** Expands a path fragment to a full path.
65 * @param base The base path to expand from
66 * @param fragment The path fragment to expand on top of base.
68 static std::string ExpandPath(const std::string& base, const std::string& fragment);
71 * Checks whether a file with the specified name exists on the filesystem.
72 * @param path The path to a file.
73 * @return True if the file exists; otherwise, false.
75 static bool FileExists(const std::string& path);
77 /** Gets the file name segment of a path.
78 * @param path The path to extract the file name from.
79 * @return The file name segment of a path.
81 static std::string GetFileName(const std::string& path);
83 /** Gets a list of files which exist in the specified directory.
84 * @param directory The directory to retrieve files from.
85 * @param entries A vector which entries will be added to.
86 * @param match If defined then a glob match for files to be matched against.
87 * @return True if the directory could be opened; otherwise false.
89 static bool GetFileList(const std::string& directory, std::vector<std::string>& entries, const std::string& match = "*");
91 /** Determines whether the given path starts with a Windows drive letter.
92 * @param path The path to validate.
93 * @returns True if the path begins with a Windows drive letter; otherwise, false.
95 static bool StartsWithWindowsDriveLetter(const std::string& path);