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