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