lugre_robstring.cpp
Go to the documentation of this file.00001
00002 #include <stdarg.h>
00003 #include <string.h>
00004 #include <assert.h>
00005 #include "lugre_profile.h"
00006
00007 #include "lugre_robstring.h"
00008
00009
00010
00011
00012 namespace Lugre {
00013
00014
00015
00016 char gRobStringBuffer[kRobStringBufferSize] = "";
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 void explodestr (const char* separator,const char* str,std::vector<std::string>& res) { PROFILE
00035 assert(separator); if (!separator) return;
00036 assert(str); if (!str) return;
00037 assert(strlen(separator)>0); if (strlen(separator)==0) return;
00038 const char* end;
00039 do {
00040 end = strstr(str,separator);
00041 if (end) {
00042 res.push_back(std::string(str,end-str));
00043 str = end + strlen(separator);
00044 if (!*str) res.push_back(std::string());
00045 } else {
00046 res.push_back(std::string(str));
00047 return;
00048 }
00049 } while (*str) ;
00050 }
00051
00052
00053
00054
00055
00056
00057
00058 bool charmatchrange (const char c,const char* r) { PROFILE
00059 for (;*r;r++)
00060 if (*r == '\\')
00061 if (c == r[1]) return true;
00062 else r += 1;
00063 else if (c == *r) return true;
00064 else if (r[1] == '-')
00065 if (c >= *r && c <= r[2]) return true;
00066 else r += 2;
00067 return false;
00068 }
00069
00070
00071 int cinrange (const char* str,const char* range) { PROFILE
00072 int c = 0;
00073 for (;*str && charmatchrange(*str,range);str++) c++;
00074 return c;
00075 }
00076
00077
00078 int coutrange (const char* str,const char* range) { PROFILE
00079 int c = 0;
00080 for (;*str && !charmatchrange(*str,range);str++) c++;
00081 return c;
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091 unsigned int stringhash (const char* str) { PROFILE
00092 if (!str) return 0;
00093 int res = 0;
00094 for (;*str;str++)
00095 res = (res + *str)*31;
00096 return res;
00097 }
00098
00099
00100 std::string addslashes (const char* str) { PROFILE
00101 std::string res;
00102 for (;*str;str++) {
00103 if (strchr("\\\"'",*str))
00104 res += "\\";
00105 res += *str;
00106 }
00107 return res;
00108 }
00109
00110
00111
00112
00113
00114
00115
00116 const char* directory_dividers = "\\/:";
00117
00118
00119 std::string pathgetdir (const std::string &path) { PROFILE
00120 return path.substr(0,path.find_last_of(directory_dividers));
00121 }
00122
00123
00124 std::string pathgetfile (const std::string &path) { PROFILE
00125 return path.substr(path.find_last_of(directory_dividers)+1);
00126 }
00127
00128
00129 std::string pathgetext (const std::string &path) { PROFILE
00130 std::string file = pathgetfile(path);
00131 int pos = file.rfind(".");
00132 if (pos >= file.npos) return std::string("");
00133 return file.substr(pos);
00134 }
00135
00136
00137 char pathgetdirslash (const std::string &path) { PROFILE
00138 int pos = path.find_last_of(directory_dividers);
00139 if (pos >= path.npos) return 0;
00140 return path[pos];
00141 }
00142
00143
00144 char pathgetwindrive (const std::string &path) { PROFILE
00145 if (path.length() >= 2 && path[1] == ':')
00146 return path[0];
00147 else return 0;
00148 }
00149
00150
00151 bool pathisabsolute (const std::string &path) { PROFILE
00152 return pathgetwindrive(path) || path.find_first_of(directory_dividers) == 0;
00153 }
00154
00155
00156 std::string pathadd (const std::string &base,std::string &add) { PROFILE
00157
00158
00159
00160 if (pathisabsolute(add)) return add;
00161 char baseslash = pathgetdirslash(base);
00162 std::string s = pathgetdir(base);
00163 do {
00164 if (add.compare(0,2,"..") == 0) {
00165
00166 add = add.substr(3);
00167 s = pathgetdir(s);
00168 } else if (add.compare(0,1,".") == 0) {
00169
00170 add = add.substr(2);
00171 } else {
00172
00173 int pos = add.find_first_of(directory_dividers);
00174 if (pos == add.npos) break;
00175 s += baseslash + add.substr(0,pos);
00176 add = add.substr(pos+1);
00177 }
00178 } while (1) ;
00179 return s + baseslash + add;
00180 }
00181
00182
00183 bool pathissubpath (const std::string &base,std::string &path) { PROFILE
00184
00185
00186 std::string b = base,p = path;
00187 do {
00188 int posa = b.find_first_of(directory_dividers);
00189 int posb = p.find_first_of(directory_dividers);
00190 if (posa == b.npos) return true;
00191 if (posb == p.npos) return false;
00192 if (b.compare(0,posa,p,0,posb) != 0) return false;
00193 b = b.substr(posa+1);
00194 p = p.substr(posb+1);
00195 } while (1) ;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204 #if 0
00205
00206 string test = "blaa44 ARGH! 'igitt' sprach der \"weise\" \\n ##";
00207 output("strtest "+test+"\n");
00208 output(string("addslashes ")+addslashes(test.c_str())+"\n");
00209 output(string("strtolower ")+strtolower(test.c_str())+"\n");
00210 output(string("strtoupper ")+strtoupper(test.c_str())+"\n");
00211
00212
00213
00214
00215
00216
00217
00218
00219 string path,add;
00220 path = "C:\\zeug\\grafik\\datei.txt";
00221 add = "..\\..\\.\\zeug2\\monster\\dump.sql";
00222 output("pathadd("+path+" , "+add+") "+pathadd(path,add)+"\n");
00223 add = "../.././zeug2/../zeug3/./monster/dump.sql";
00224 output("pathadd("+path+" , "+add+") "+pathadd(path,add)+"\n");
00225
00226
00227
00228
00229 string patharr[] = {"C:\\zeug\\grafik\\datei.txt" ,
00230 "C:\\zeug\\grafik\\datei" ,
00231 "C:\\zeug\\grafik\\" ,
00232 "C:\\zeug\\grafik" ,
00233 "C:/zeug/grafik/datei.txt" ,
00234 "C:/zeug/grafik/datei" ,
00235 "C:/zeug/grafik/" ,
00236 "C:/zeug/grafik" };
00237 for (i=0;i<8;i++) {
00238 string path = patharr[i];
00239 output("pathgetdir("+path+") "+pathgetdir(path)+"\n");
00240 output("pathgetfile("+path+") "+pathgetfile(path)+"\n");
00241 output("pathgetext("+path+") "+pathgetext(path)+"\n");
00242 output("____\n");
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 output(pathissubpath("C:\\a\\b\\","C:/a/c")?"true":"false");
00280 output(pathissubpath("C:\\a\\b\\","C:/a/b/d")?"true":"false");
00281
00282 #endif
00283
00284 };
00285
00286