Root/
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 <dirent.h> |
32 | #include <fstream> |
33 | #include <iostream> |
34 | #include <strings.h> |
35 | #include <unistd.h> |
36 | |
37 | using namespace std; |
38 | |
39 | bool case_less::operator()(const string &left, const string &right) const { |
40 | return strcasecmp(left.c_str(), right.c_str()) < 0; |
41 | } |
42 | |
43 | // General tool to strip spaces from both ends: |
44 | string trim(const string& s) { |
45 | if(s.length() == 0) |
46 | return s; |
47 | int b = s.find_first_not_of(" \t\r"); |
48 | int e = s.find_last_not_of(" \t\r"); |
49 | if(b == -1) // No non-spaces |
50 | return ""; |
51 | return string(s, b, e - b + 1); |
52 | } |
53 | |
54 | bool fileExists(const string &file) { |
55 | fstream fin; |
56 | fin.open(file.c_str() ,ios::in); |
57 | bool exists = fin.is_open(); |
58 | fin.close(); |
59 | |
60 | return exists; |
61 | } |
62 | |
63 | bool rmtree(string path) { |
64 | DIR *dirp; |
65 | struct stat st; |
66 | struct dirent *dptr; |
67 | string filepath; |
68 | |
69 | DEBUG("RMTREE: '%s'\n", path.c_str()); |
70 | |
71 | if ((dirp = opendir(path.c_str())) == NULL) return false; |
72 | if (path[path.length()-1]!='/') path += "/"; |
73 | |
74 | while ((dptr = readdir(dirp))) { |
75 | filepath = dptr->d_name; |
76 | if (filepath=="." || filepath=="..") continue; |
77 | filepath = path+filepath; |
78 | int statRet = stat(filepath.c_str(), &st); |
79 | if (statRet == -1) continue; |
80 | if (S_ISDIR(st.st_mode)) { |
81 | if (!rmtree(filepath)) return false; |
82 | } else { |
83 | if (unlink(filepath.c_str())!=0) return false; |
84 | } |
85 | } |
86 | |
87 | closedir(dirp); |
88 | return rmdir(path.c_str())==0; |
89 | } |
90 | |
91 | int constrain(int x, int imin, int imax) { |
92 | return min(imax, max(imin, x)); |
93 | } |
94 | |
95 | //Configuration parsing utilities |
96 | int evalIntConf (int val, int def, int imin, int imax) { |
97 | if (val==0 && (val<imin || val>imax)) |
98 | return def; |
99 | val = constrain(val, imin, imax); |
100 | return val; |
101 | } |
102 | int evalIntConf (int *val, int def, int imin, int imax) { |
103 | *val = evalIntConf(*val, def, imin, imax); |
104 | return *val; |
105 | } |
106 | |
107 | bool split (vector<string> &vec, const string &str, const string &delim, bool destructive) { |
108 | vec.clear(); |
109 | |
110 | if (delim.empty()) { |
111 | vec.push_back(str); |
112 | return false; |
113 | } |
114 | |
115 | std::string::size_type i = 0; |
116 | std::string::size_type j = 0; |
117 | |
118 | while(1) { |
119 | j = str.find(delim,i); |
120 | if (j==std::string::npos) { |
121 | vec.push_back(str.substr(i)); |
122 | break; |
123 | } |
124 | |
125 | if (!destructive) |
126 | j += delim.size(); |
127 | |
128 | vec.push_back(str.substr(i,j-i)); |
129 | |
130 | if (destructive) |
131 | i = j + delim.size(); |
132 | |
133 | if (i==str.size()) { |
134 | vec.push_back(std::string()); |
135 | break; |
136 | } |
137 | } |
138 | |
139 | return true; |
140 | } |
141 | |
142 | string strreplace (string orig, const string &search, const string &replace) { |
143 | string::size_type pos = orig.find( search, 0 ); |
144 | while (pos != string::npos) { |
145 | orig.replace(pos,search.length(),replace); |
146 | pos = orig.find( search, pos+replace.length() ); |
147 | } |
148 | return orig; |
149 | } |
150 | |
151 | string cmdclean (string cmdline) { |
152 | string spchars = "\\`$();|{}&'\"*?<>[]!^~-#\n\r "; |
153 | for (uint i=0; i<spchars.length(); i++) { |
154 | string curchar = spchars.substr(i,1); |
155 | cmdline = strreplace(cmdline, curchar, "\\"+curchar); |
156 | } |
157 | return cmdline; |
158 | } |
159 | |
160 | int intTransition(int from, int to, long tickStart, long duration, long tickNow) { |
161 | if (tickNow<0) tickNow = SDL_GetTicks(); |
162 | return constrain(((tickNow-tickStart) * (to-from)) / duration, from, to); |
163 | // elapsed increments |
164 | } |
165 | |
166 | void inject_user_event(enum EventCode code, void *data1, void *data2) |
167 | { |
168 | SDL_UserEvent e = { |
169 | .type = SDL_USEREVENT, |
170 | .code = code, |
171 | .data1 = data1, |
172 | .data2 = data2, |
173 | }; |
174 | |
175 | /* Inject an user event, that will be handled as a "repaint" |
176 | * event by the InputManager */ |
177 | SDL_PushEvent((SDL_Event *) &e); |
178 | DEBUG("Injecting event code %i\n", e.code); |
179 | } |
180 |
Branches:
install_locations
master
opkrun
packages