]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/fileutils.h
Improve behaviour when running as root.
[user/henk/code/inspircd.git] / include / fileutils.h
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 #pragma once
21
22 /** Provides an easy method of reading a text file into memory. */
23 class CoreExport FileReader
24 {
25         /** The lines of text in the file. */
26         std::vector<std::string> lines;
27
28         /** File size in bytes. */
29         unsigned long totalSize;
30
31  public:
32         /** Initializes a new file reader. */
33         FileReader() : totalSize(0) { }
34
35         /** Initializes a new file reader and reads the specified file.
36          * @param filename The file to read into memory.
37          */
38         FileReader(const std::string& filename);
39
40         /** Loads a text file from disk.
41          * @param filename The file to read into memory.
42          * @throw CoreException The file can not be loaded.
43          */
44         void Load(const std::string& filename);
45
46         /** Retrieves the entire contents of the file cache as a single string. */
47         std::string GetString() const;
48
49         /** Retrieves the entire contents of the file cache as a vector of strings. */
50         const std::vector<std::string>& GetVector() const { return lines; }
51
52         /** Retrieves the total size in bytes of the file. */
53         unsigned long TotalSize() const { return totalSize; }
54 };
55
56 /** Implements methods for file system access */
57 class CoreExport FileSystem
58 {
59 private:
60         FileSystem() { }
61
62 public:
63         /** Expands a path fragment to a full path.
64          * @param base The base path to expand from
65          * @param fragment The path fragment to expand on top of base.
66          */
67         static std::string ExpandPath(const std::string& base, const std::string& fragment);
68
69         /**
70          * Checks whether a file with the specified name exists on the filesystem.
71          * @param path The path to a file.
72          * @return True if the file exists; otherwise, false.
73         */
74         static bool FileExists(const std::string& path);
75
76         /** Gets the file name segment of a path.
77          * @param path The path to extract the file name from.
78          * @return The file name segment of a path.
79          */
80         static std::string GetFileName(const std::string& path);
81
82         /** Gets a list of files which exist in the specified directory.
83          * @param directory The directory to retrieve files from.
84          * @param entries A vector which entries will be added to.
85          * @param match If defined then a glob match for files to be matched against.
86          * @return True if the directory could be opened; otherwise false.
87          */
88         static bool GetFileList(const std::string& directory, std::vector<std::string>& entries, const std::string& match = "*");
89
90         /** Determines whether the given path starts with a Windows drive letter.
91          * @param path The path to validate.
92          * @returns True if the path begins with a Windows drive letter; otherwise, false.
93          */
94         static bool StartsWithWindowsDriveLetter(const std::string& path);
95 };