Root/
Source at commit 73ceffa51d3450d2d20ce933e7fc6da187e09fc8 created 8 years 11 months ago. By Maarten ter Huurne, Fix bug in section directory creation method | |
---|---|
1 | /*************************************************************************** |
2 | * Copyright (C) 2006 by Massimiliano Torromeo * |
3 | * massimiliano.torromeo@gmail.com * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | * * |
10 | * This program is distributed in the hope that it will be useful, * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * GNU General Public License for more details. * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the * |
17 | * Free Software Foundation, Inc., * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | ***************************************************************************/ |
20 | |
21 | #include "utilities.h" |
22 | |
23 | #include "debug.h" |
24 | |
25 | #include <SDL.h> |
26 | #include <algorithm> |
27 | |
28 | //for browsing the filesystem |
29 | #include <sys/stat.h> |
30 | #include <sys/types.h> |
31 | #include <fcntl.h> |
32 | #include <dirent.h> |
33 | #include <fstream> |
34 | #include <iostream> |
35 | #include <strings.h> |
36 | #include <unistd.h> |
37 | |
38 | using namespace std; |
39 | |
40 | bool case_less::operator()(const string &left, const string &right) const { |
41 | return strcasecmp(left.c_str(), right.c_str()) < 0; |
42 | } |
43 | |
44 | string trim(const string& s) { |
45 | auto b = s.find_first_not_of(" \t\r"); |
46 | auto e = s.find_last_not_of(" \t\r"); |
47 | return b == string::npos ? "" : string(s, b, e + 1 - b); |
48 | } |
49 | |
50 | string ltrim(const string& s) { |
51 | auto b = s.find_first_not_of(" \t\r"); |
52 | return b == string::npos ? "" : string(s, b); |
53 | } |
54 | |
55 | string rtrim(const string& s) { |
56 | auto e = s.find_last_not_of(" \t\r"); |
57 | return e == string::npos ? "" : string(s, 0, e + 1); |
58 | } |
59 | |
60 | // See this article for a performance comparison of different approaches: |
61 | // http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html |
62 | string readFileAsString(string const& filename) { |
63 | ifstream in(filename, ios::in | ios::binary); |
64 | if (!in) { |
65 | return "<error opening " + string(filename) + ">"; |
66 | } |
67 | |
68 | // Get file size. |
69 | in.seekg(0, ios::end); |
70 | auto size = max(int(in.tellg()), 0); // tellg() returns -1 on errors |
71 | in.seekg(0, ios::beg); |
72 | |
73 | string contents(size, '\0'); |
74 | in.read(&contents[0], contents.size()); |
75 | in.close(); |
76 | |
77 | if (in.fail()) { |
78 | return "<error reading " + string(filename) + ">"; |
79 | } else { |
80 | contents.shrink_to_fit(); |
81 | return contents; |
82 | } |
83 | } |
84 | |
85 | constexpr int writeOpenFlags = |
86 | #ifdef O_CLOEXEC |
87 | O_CLOEXEC | // Linux |
88 | #endif |
89 | O_CREAT | O_WRONLY | O_TRUNC; |
90 | |
91 | // Use C functions since STL doesn't seem to have any way of applying fsync(). |
92 | bool writeStringToFile(string const& filename, string const& data) { |
93 | // Open temporary file. |
94 | string tempname = filename + '~'; |
95 | int fd = open(tempname.c_str(), writeOpenFlags, S_IRUSR | S_IWUSR); |
96 | if (fd < 0) { |
97 | return false; |
98 | } |
99 | |
100 | // Write temporary file. |
101 | const char *bytes = data.c_str(); |
102 | size_t remaining = data.size(); |
103 | bool ok = true; |
104 | while (remaining != 0) { |
105 | ssize_t written = write(fd, bytes, remaining); |
106 | if (written <= 0) { |
107 | ok = false; |
108 | break; |
109 | } else { |
110 | bytes += written; |
111 | remaining -= written; |
112 | } |
113 | } |
114 | if (ok) { |
115 | ok = fsync(fd) == 0; |
116 | } |
117 | |
118 | // Close temporary file. |
119 | ok &= close(fd) == 0; |
120 | |
121 | // Replace actual output file with temporary file. |
122 | if (ok) { |
123 | ok = rename(tempname.c_str(), filename.c_str()) == 0; |
124 | } |
125 | |
126 | return ok; |
127 | } |
128 | |
129 | constexpr int dirOpenFlags = |
130 | #ifdef O_DIRECTORY |
131 | O_DIRECTORY | // Linux |
132 | #endif |
133 | #ifdef O_CLOEXEC |
134 | O_CLOEXEC | // Linux |
135 | #endif |
136 | O_RDONLY; |
137 | |
138 | bool syncDir(string const& dirname) |
139 | { |
140 | int fd = open(dirname.c_str(), dirOpenFlags); |
141 | if (fd < 0) { |
142 | return false; |
143 | } |
144 | |
145 | bool ok = fsync(fd) == 0; |
146 | |
147 | ok &= close(fd) == 0; |
148 | |
149 | return ok; |
150 | } |
151 | |
152 | string parentDir(string const& dir) { |
153 | // Note that size() is unsigned, so for short strings the '- 2' wraps |
154 | // around and as a result the entire string is searched, which is fine. |
155 | auto p = dir.rfind('/', dir.size() - 2); |
156 | return p == string::npos ? "/" : dir.substr(0, p + 1); |
157 | } |
158 | |
159 | bool fileExists(const string &file) { |
160 | return access(file.c_str(), F_OK) == 0; |
161 | } |
162 | |
163 | int constrain(int x, int imin, int imax) { |
164 | return min(imax, max(imin, x)); |
165 | } |
166 | |
167 | //Configuration parsing utilities |
168 | int evalIntConf (ConfIntHash& hash, const std::string &key, int def, int imin, int imax) { |
169 | auto it = hash.find(key); |
170 | if (it == hash.end()) { |
171 | return hash[key] = def; |
172 | } else { |
173 | return it->second = constrain(it->second, imin, imax); |
174 | } |
175 | } |
176 | |
177 | void split(vector<string>& vec, string const& str, string const& delim) { |
178 | vec.clear(); |
179 | |
180 | if (delim.empty()) { |
181 | vec.push_back(str); |
182 | return; |
183 | } |
184 | |
185 | string::size_type i = 0, j; |
186 | while ((j = str.find(delim, i)) != string::npos) { |
187 | vec.push_back(str.substr(i, j - i)); |
188 | i = j + delim.size(); |
189 | } |
190 | vec.push_back(str.substr(i)); |
191 | } |
192 | |
193 | string strreplace (string orig, const string &search, const string &replace) { |
194 | string::size_type pos = orig.find( search, 0 ); |
195 | while (pos != string::npos) { |
196 | orig.replace(pos,search.length(),replace); |
197 | pos = orig.find( search, pos+replace.length() ); |
198 | } |
199 | return orig; |
200 | } |
201 | |
202 | string cmdclean (string cmdline) { |
203 | string spchars = "\\`$();|{}&'\"*?<>[]!^~-#\n\r "; |
204 | for (uint i=0; i<spchars.length(); i++) { |
205 | string curchar = spchars.substr(i,1); |
206 | cmdline = strreplace(cmdline, curchar, "\\"+curchar); |
207 | } |
208 | return cmdline; |
209 | } |
210 | |
211 | int intTransition(int from, int to, long tickStart, long duration, long tickNow) { |
212 | if (tickNow<0) tickNow = SDL_GetTicks(); |
213 | return constrain(((tickNow-tickStart) * (to-from)) / duration, from, to); |
214 | // elapsed increments |
215 | } |
216 | |
217 | void inject_user_event(enum EventCode code, void *data1, void *data2) |
218 | { |
219 | SDL_UserEvent e = { |
220 | .type = SDL_USEREVENT, |
221 | .code = code, |
222 | .data1 = data1, |
223 | .data2 = data2, |
224 | }; |
225 | |
226 | /* Inject an user event, that will be handled as a "repaint" |
227 | * event by the InputManager */ |
228 | SDL_PushEvent((SDL_Event *) &e); |
229 | DEBUG("Injecting event code %i\n", e.code); |
230 | } |
231 |
Branches:
install_locations
master
opkrun
packages