]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/fileutils.cpp
Ignore clients on ulined servers when reporting stats in LUSERS.
[user/henk/code/inspircd.git] / src / fileutils.cpp
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 #include "inspircd.h"
21
22 #include <fstream>
23
24 #ifndef _WIN32
25 # include <dirent.h>
26 #endif
27
28 FileReader::FileReader(const std::string& filename)
29 {
30         Load(filename);
31 }
32
33 void FileReader::Load(const std::string& filename)
34 {
35         // If the file is stored in the file cache then we used that version instead.
36         ConfigFileCache::const_iterator it = ServerInstance->Config->Files.find(filename);
37         if (it != ServerInstance->Config->Files.end())
38         {
39                 this->lines = it->second;
40         }
41         else
42         {
43                 const std::string realName = ServerInstance->Config->Paths.PrependConfig(filename);
44                 lines.clear();
45
46                 std::ifstream stream(realName.c_str());
47                 if (!stream.is_open())
48                         throw CoreException(filename + " does not exist or is not readable!");
49
50                 std::string line;
51                 while (std::getline(stream, line))
52                 {
53                         lines.push_back(line);
54                         totalSize += line.size() + 2;
55                 }
56
57                 stream.close();
58         }
59 }
60
61 std::string FileReader::GetString() const
62 {
63         std::string buffer;
64         for (file_cache::const_iterator it = this->lines.begin(); it != this->lines.end(); ++it)
65         {
66                 buffer.append(*it);
67                 buffer.append("\r\n");
68         }
69         return buffer;
70 }
71
72 std::string FileSystem::ExpandPath(const std::string& base, const std::string& fragment)
73 {
74         // The fragment is an absolute path, don't modify it.
75         if (fragment[0] == '/' || FileSystem::StartsWithWindowsDriveLetter(fragment))
76                 return fragment;
77
78         return base + '/' + fragment;
79 }
80
81 bool FileSystem::FileExists(const std::string& file)
82 {
83         struct stat sb;
84         if (stat(file.c_str(), &sb) == -1)
85                 return false;
86
87         if ((sb.st_mode & S_IFDIR) > 0)
88                 return false;
89
90         return !access(file.c_str(), F_OK);
91 }
92
93 bool FileSystem::GetFileList(const std::string& directory, std::vector<std::string>& entries, const std::string& match)
94 {
95 #ifdef _WIN32
96         const std::string search_path = directory + "\\" + match;
97
98         WIN32_FIND_DATAA wfd;
99         HANDLE fh = FindFirstFileA(search_path.c_str(), &wfd);
100         if (fh == INVALID_HANDLE_VALUE)
101                 return false;
102
103         do
104         {
105                 entries.push_back(wfd.cFileName);
106         } while (FindNextFile(fh, &wfd) != 0);
107
108         FindClose(fh);
109         return true;
110 #else
111         DIR* library = opendir(directory.c_str());
112         if (!library)
113                 return false;
114
115         dirent* entry = NULL;
116         while ((entry = readdir(library)))
117         {
118                 if (InspIRCd::Match(entry->d_name, match, ascii_case_insensitive_map))
119                         entries.push_back(entry->d_name);
120         }
121         closedir(library);
122         return true;
123 #endif
124 }
125
126
127 std::string FileSystem::GetFileName(const std::string& name)
128 {
129 #ifdef _WIN32
130         size_t pos = name.find_last_of("\\/");
131 #else
132         size_t pos = name.rfind('/');
133 #endif
134         return pos == std::string::npos ? name : name.substr(++pos);
135 }
136
137 bool FileSystem::StartsWithWindowsDriveLetter(const std::string& path)
138 {
139         return (path.length() > 2 && isalpha(path[0]) && path[1] == ':');
140 }