Root/src/utilities.cpp

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

Archive Download this file



interactive