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

Archive Download this file



interactive