]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/fileutils.cpp
Merge insp20
[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 FileReader::FileReader(const std::string& filename)
25 {
26         Load(filename);
27 }
28
29 void FileReader::Load(const std::string& filename)
30 {
31         // If the file is stored in the file cache then we used that version instead.
32         ConfigFileCache::const_iterator it = ServerInstance->Config->Files.find(filename);
33         if (it != ServerInstance->Config->Files.end())
34         {
35                 this->lines = it->second;
36         }
37         else
38         {
39                 const std::string realName = ServerInstance->Config->Paths.PrependConfig(filename);
40                 lines.clear();
41
42                 std::ifstream stream(realName.c_str());
43                 if (!stream.is_open())
44                         throw CoreException(filename + " does not exist or is not readable!");
45
46                 std::string line;
47                 while (std::getline(stream, line))
48                 {
49                         lines.push_back(line);
50                         totalSize += line.size() + 2;
51                 }
52
53                 stream.close();
54         }
55 }
56
57 std::string FileReader::GetString() const
58 {
59         std::string buffer;
60         for (file_cache::const_iterator it = this->lines.begin(); it != this->lines.end(); ++it)
61         {
62                 buffer.append(*it);
63                 buffer.append("\r\n");
64         }
65         return buffer;
66 }
67
68 std::string FileSystem::ExpandPath(const std::string& base, const std::string& fragment)
69 {
70         // The fragment is an absolute path, don't modify it.
71         if (fragment[0] == '/' || FileSystem::StartsWithWindowsDriveLetter(fragment))
72                 return fragment;
73
74         return base + '/' + fragment;
75 }
76
77 bool FileSystem::FileExists(const std::string& file)
78 {
79         struct stat sb;
80         if (stat(file.c_str(), &sb) == -1)
81                 return false;
82
83         if ((sb.st_mode & S_IFDIR) > 0)
84                 return false;
85
86         return !access(file.c_str(), F_OK);
87 }
88
89 std::string FileSystem::GetFileName(const std::string& name)
90 {
91 #ifdef _WIN32
92         size_t pos = name.find_last_of("\\/");
93 #else
94         size_t pos = name.rfind('/');
95 #endif
96         return pos == std::string::npos ? name : name.substr(++pos);
97 }
98
99 bool FileSystem::StartsWithWindowsDriveLetter(const std::string& path)
100 {
101         return (path.length() > 2 && isalpha(path[0]) && path[1] == ':');
102 }