]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/fileutils.h
Use IsCTCP in blockcolor for ignoring CTCPs.
[user/henk/code/inspircd.git] / include / fileutils.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2013, 2019 Sadie Powell <sadie@witchery.services>
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 #pragma once
22
23 /** Provides an easy method of reading a text file into memory. */
24 class CoreExport FileReader
25 {
26         /** The lines of text in the file. */
27         std::vector<std::string> lines;
28
29         /** File size in bytes. */
30         unsigned long totalSize;
31
32  public:
33         /** Initializes a new file reader. */
34         FileReader() : totalSize(0) { }
35
36         /** Initializes a new file reader and reads the specified file.
37          * @param filename The file to read into memory.
38          */
39         FileReader(const std::string& filename);
40
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.
44          */
45         void Load(const std::string& filename);
46
47         /** Retrieves the entire contents of the file cache as a single string. */
48         std::string GetString() const;
49
50         /** Retrieves the entire contents of the file cache as a vector of strings. */
51         const std::vector<std::string>& GetVector() const { return lines; }
52
53         /** Retrieves the total size in bytes of the file. */
54         unsigned long TotalSize() const { return totalSize; }
55 };
56
57 /** Implements methods for file system access */
58 class CoreExport FileSystem
59 {
60 private:
61         FileSystem() { }
62
63 public:
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.
67          */
68         static std::string ExpandPath(const std::string& base, const std::string& fragment);
69
70         /**
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.
74         */
75         static bool FileExists(const std::string& path);
76
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.
80          */
81         static std::string GetFileName(const std::string& path);
82
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.
88          */
89         static bool GetFileList(const std::string& directory, std::vector<std::string>& entries, const std::string& match = "*");
90
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.
94          */
95         static bool StartsWithWindowsDriveLetter(const std::string& path);
96 };