| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,47 +0,0 @@ |
| 1 |
-//taken from fceux on 10/27/08 |
|
| 2 |
-//subsequently modified for desmume |
|
| 3 |
- |
|
| 4 |
-/* |
|
| 5 |
- Copyright (C) 2008-2009 DeSmuME team |
|
| 6 |
- |
|
| 7 |
- This file is free software: you can redistribute it and/or modify |
|
| 8 |
- it under the terms of the GNU General Public License as published by |
|
| 9 |
- the Free Software Foundation, either version 2 of the License, or |
|
| 10 |
- (at your option) any later version. |
|
| 11 |
- |
|
| 12 |
- This file is distributed in the hope that it will be useful, |
|
| 13 |
- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 14 |
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 15 |
- GNU General Public License for more details. |
|
| 16 |
- |
|
| 17 |
- You should have received a copy of the GNU General Public License |
|
| 18 |
- along with the this software. If not, see <http://www.gnu.org/licenses/>. |
|
| 19 |
-*/ |
|
| 20 |
- |
|
| 21 |
-#include <string> |
|
| 22 |
-#include <vector> |
|
| 23 |
- |
|
| 24 |
-#include "xstring.h" |
|
| 25 |
- |
|
| 26 |
-std::vector<std::string> tokenize_str(const std::string &str, const std::string &delims = ", \t") |
|
| 27 |
-{
|
|
| 28 |
- // Skip delims at beginning, find start of first token |
|
| 29 |
- auto lastPos = str.find_first_not_of(delims, 0); |
|
| 30 |
- // Find next delimiter @ end of token |
|
| 31 |
- auto pos = str.find_first_of(delims, lastPos); |
|
| 32 |
- |
|
| 33 |
- // output vector |
|
| 34 |
- std::vector<std::string> tokens; |
|
| 35 |
- |
|
| 36 |
- while (pos != std::string::npos || lastPos != std::string::npos) |
|
| 37 |
- {
|
|
| 38 |
- // Found a token, add it to the vector. |
|
| 39 |
- tokens.push_back(str.substr(lastPos, pos - lastPos)); |
|
| 40 |
- // Skip delims. Note the "not_of". this is beginning of token |
|
| 41 |
- lastPos = str.find_first_not_of(delims, pos); |
|
| 42 |
- // Find next delimiter at end of token. |
|
| 43 |
- pos = str.find_first_of(delims, lastPos); |
|
| 44 |
- } |
|
| 45 |
- |
|
| 46 |
- return tokens; |
|
| 47 |
-} |
| ... | ... |
@@ -23,724 +23,25 @@ |
| 23 | 23 |
|
| 24 | 24 |
#include "xstring.h" |
| 25 | 25 |
|
| 26 |
-//a vc-style substring operation (very kind and lenient) |
|
| 27 |
-/*std::string strsub(const std::string& str, int pos, int len) {
|
|
| 28 |
- int strlen = str.size(); |
|
| 29 |
- |
|
| 30 |
- if(strlen==0) return str; //empty strings always return empty strings |
|
| 31 |
- if(pos>=strlen) return str; //if you start past the end of the string, return the entire string. this is unusual, but there you have it |
|
| 32 |
- |
|
| 33 |
- //clipping |
|
| 34 |
- if(pos<0) {
|
|
| 35 |
- len += pos; |
|
| 36 |
- pos = 0; |
|
| 37 |
- } |
|
| 38 |
- |
|
| 39 |
- if (pos+len>=strlen) |
|
| 40 |
- len=strlen-pos+1; |
|
| 41 |
- |
|
| 42 |
- //return str.str().substr(pos,len); |
|
| 43 |
- return str.substr(pos,len); |
|
| 44 |
-}*/ |
|
| 45 |
- |
|
| 46 |
-//std::string strmid(const std::string& str, int pos, int len) { return strsub(str,pos,len); }
|
|
| 47 |
-//std::string strleft(const std::string& str, int len) { return strsub(str,0,len); }
|
|
| 48 |
-//std::string strright(const std::string& str, int len) { return len ? strsub(str,str.size()-len,len) : ""; }
|
|
| 49 |
-/*std::string toupper(const std::string& str) |
|
| 50 |
-{
|
|
| 51 |
- std::string ret = str; |
|
| 52 |
- for(uint32_t i=0;i<str.size();i++) |
|
| 53 |
- ret[i] = toupper(ret[i]); |
|
| 54 |
- return ret; |
|
| 55 |
-}*/ |
|
| 56 |
- |
|
| 57 |
-///Upper case routine. Returns number of characters modified |
|
| 58 |
-/*int str_ucase(char *str) {
|
|
| 59 |
- uint32_t i=0,j=0; |
|
| 60 |
- |
|
| 61 |
- while (i < strlen(str)) {
|
|
| 62 |
- if ((str[i] >= 'a') && (str[i] <= 'z')) {
|
|
| 63 |
- str[i] &= ~0x20; |
|
| 64 |
- j++; |
|
| 65 |
- } |
|
| 66 |
- i++; |
|
| 67 |
- } |
|
| 68 |
- return j; |
|
| 69 |
-}*/ |
|
| 70 |
- |
|
| 71 |
- |
|
| 72 |
-///Lower case routine. Returns number of characters modified |
|
| 73 |
-/*int str_lcase(char *str) {
|
|
| 74 |
- uint32_t i=0,j=0; |
|
| 75 |
- |
|
| 76 |
- while (i < strlen(str)) {
|
|
| 77 |
- if ((str[i] >= 'A') && (str[i] <= 'Z')) {
|
|
| 78 |
- str[i] |= 0x20; |
|
| 79 |
- j++; |
|
| 80 |
- } |
|
| 81 |
- i++; |
|
| 82 |
- } |
|
| 83 |
- return j; |
|
| 84 |
-}*/ |
|
| 85 |
- |
|
| 86 |
- |
|
| 87 |
-///White space-trimming routine |
|
| 88 |
- |
|
| 89 |
-///Removes whitespace from left side of string, depending on the flags set (See STRIP_x definitions in xstring.h) |
|
| 90 |
-///Returns number of characters removed |
|
| 91 |
-/*int str_ltrim(char *str, int flags) {
|
|
| 92 |
- uint32_t i=0; |
|
| 93 |
- |
|
| 94 |
- while (str[0]) {
|
|
| 95 |
- if ((str[0] != ' ') || (str[0] != '\t') || (str[0] != '\r') || (str[0] != '\n')) break; |
|
| 96 |
- |
|
| 97 |
- if ((flags & STRIP_SP) && (str[0] == ' ')) {
|
|
| 98 |
- i++; |
|
| 99 |
- strcpy(str,str+1); |
|
| 100 |
- } |
|
| 101 |
- if ((flags & STRIP_TAB) && (str[0] == '\t')) {
|
|
| 102 |
- i++; |
|
| 103 |
- strcpy(str,str+1); |
|
| 104 |
- } |
|
| 105 |
- if ((flags & STRIP_CR) && (str[0] == '\r')) {
|
|
| 106 |
- i++; |
|
| 107 |
- strcpy(str,str+1); |
|
| 108 |
- } |
|
| 109 |
- if ((flags & STRIP_LF) && (str[0] == '\n')) {
|
|
| 110 |
- i++; |
|
| 111 |
- strcpy(str,str+1); |
|
| 112 |
- } |
|
| 113 |
- } |
|
| 114 |
- return i; |
|
| 115 |
-}*/ |
|
| 116 |
- |
|
| 117 |
- |
|
| 118 |
-///White space-trimming routine |
|
| 119 |
- |
|
| 120 |
-///Removes whitespace from right side of string, depending on the flags set (See STRIP_x definitions in xstring.h) |
|
| 121 |
-///Returns number of characters removed |
|
| 122 |
-/*int str_rtrim(char *str, int flags) {
|
|
| 123 |
- uint32_t i=0; |
|
| 124 |
- |
|
| 125 |
- while (strlen(str)) {
|
|
| 126 |
- if ((str[strlen(str)-1] != ' ') || |
|
| 127 |
- (str[strlen(str)-1] != '\t') || |
|
| 128 |
- (str[strlen(str)-1] != '\r') || |
|
| 129 |
- (str[strlen(str)-1] != '\n')) break; |
|
| 130 |
- |
|
| 131 |
- if ((flags & STRIP_SP) && (str[0] == ' ')) {
|
|
| 132 |
- i++; |
|
| 133 |
- str[strlen(str)-1] = 0; |
|
| 134 |
- } |
|
| 135 |
- if ((flags & STRIP_TAB) && (str[0] == '\t')) {
|
|
| 136 |
- i++; |
|
| 137 |
- str[strlen(str)-1] = 0; |
|
| 138 |
- } |
|
| 139 |
- if ((flags & STRIP_CR) && (str[0] == '\r')) {
|
|
| 140 |
- i++; |
|
| 141 |
- str[strlen(str)-1] = 0; |
|
| 142 |
- } |
|
| 143 |
- if ((flags & STRIP_LF) && (str[0] == '\n')) {
|
|
| 144 |
- i++; |
|
| 145 |
- str[strlen(str)-1] = 0; |
|
| 146 |
- } |
|
| 147 |
- } |
|
| 148 |
- return i; |
|
| 149 |
-}*/ |
|
| 150 |
- |
|
| 151 |
- |
|
| 152 |
-///White space-stripping routine |
|
| 153 |
- |
|
| 154 |
-///Removes whitespace depending on the flags set (See STRIP_x definitions in xstring.h) |
|
| 155 |
-///Returns number of characters removed, or -1 on error |
|
| 156 |
-/*int str_strip(char *str, int flags) {
|
|
| 157 |
- uint32_t i=0,j=0; |
|
| 158 |
- char *astr,chr; |
|
| 159 |
- |
|
| 160 |
- if (!strlen(str)) return -1; |
|
| 161 |
- if (!(flags & (STRIP_SP|STRIP_TAB|STRIP_CR|STRIP_LF))) return -1; |
|
| 162 |
- if (!(astr = (char*)malloc(strlen(str)+1))) return -1; |
|
| 163 |
- while (i < strlen(str)) {
|
|
| 164 |
- chr = str[i++]; |
|
| 165 |
- if ((flags & STRIP_SP) && (chr == ' ')) chr = 0; |
|
| 166 |
- if ((flags & STRIP_TAB) && (chr == '\t')) chr = 0; |
|
| 167 |
- if ((flags & STRIP_CR) && (chr == '\r')) chr = 0; |
|
| 168 |
- if ((flags & STRIP_LF) && (chr == '\n')) chr = 0; |
|
| 169 |
- |
|
| 170 |
- if (chr) astr[j++] = chr; |
|
| 171 |
- } |
|
| 172 |
- astr[j] = 0; |
|
| 173 |
- strcpy(str,astr); |
|
| 174 |
- free(astr); |
|
| 175 |
- return j; |
|
| 176 |
-}*/ |
|
| 177 |
- |
|
| 178 |
- |
|
| 179 |
-///Character replacement routine |
|
| 180 |
- |
|
| 181 |
-///Replaces all instances of 'search' with 'replace' |
|
| 182 |
-///Returns number of characters modified |
|
| 183 |
-/*int chr_replace(char *str, char search, char replace) {
|
|
| 184 |
- uint32_t i=0,j=0; |
|
| 185 |
- |
|
| 186 |
- while (i < strlen(str)) {
|
|
| 187 |
- if (str[i] == search) {
|
|
| 188 |
- str[i] = replace; |
|
| 189 |
- j++; |
|
| 190 |
- } |
|
| 191 |
- i++; |
|
| 192 |
- } |
|
| 193 |
- return j; |
|
| 194 |
-}*/ |
|
| 195 |
- |
|
| 196 |
- |
|
| 197 |
-///Sub-String replacement routine |
|
| 198 |
- |
|
| 199 |
-///Replaces all instances of 'search' with 'replace' |
|
| 200 |
-///Returns number of sub-strings modified, or -1 on error |
|
| 201 |
-/*int str_replace(char *str, char *search, char *replace) {
|
|
| 202 |
- uint32_t i=0,j=0; |
|
| 203 |
- int searchlen,replacelen; |
|
| 204 |
- char *astr; |
|
| 205 |
- |
|
| 206 |
- searchlen = strlen(search); |
|
| 207 |
- replacelen = strlen(replace); |
|
| 208 |
- if ((!strlen(str)) || (!searchlen)) return -1; //note: allow *replace to have a length of zero! |
|
| 209 |
- if (!(astr = (char*)malloc(strlen(str)+1))) return -1; |
|
| 210 |
- while (i < strlen(str)) {
|
|
| 211 |
- if (!strncmp(str+i,search,searchlen)) {
|
|
| 212 |
- if (replacelen) memcpy(astr+j,replace,replacelen); |
|
| 213 |
- i += searchlen; |
|
| 214 |
- j += replacelen; |
|
| 215 |
- } |
|
| 216 |
- else astr[j++] = str[i++]; |
|
| 217 |
- } |
|
| 218 |
- astr[j] = 0; |
|
| 219 |
- strcpy(str,astr); |
|
| 220 |
- free(astr); |
|
| 221 |
- return j; |
|
| 222 |
-}*/ |
|
| 223 |
- |
|
| 224 |
-/*static const struct Base64Table |
|
| 225 |
-{
|
|
| 226 |
- Base64Table() |
|
| 227 |
- {
|
|
| 228 |
- size_t a=0; |
|
| 229 |
- for(a=0; a<256; ++a) data[a] = 0xFF; // mark everything as invalid by default |
|
| 230 |
- // create value->ascii mapping |
|
| 231 |
- a=0; |
|
| 232 |
- for(unsigned char c='A'; c<='Z'; ++c) data[a++] = c; // 0..25 |
|
| 233 |
- for(unsigned char c='a'; c<='z'; ++c) data[a++] = c; // 26..51 |
|
| 234 |
- for(unsigned char c='0'; c<='9'; ++c) data[a++] = c; // 52..61 |
|
| 235 |
- data[62] = '+'; // 62 |
|
| 236 |
- data[63] = '/'; // 63 |
|
| 237 |
- // create ascii->value mapping (but due to overlap, write it to highbit region) |
|
| 238 |
- for(a=0; a<64; ++a) data[data[a]^0x80] = a; // |
|
| 239 |
- data[((unsigned char)'=') ^ 0x80] = 0; |
|
| 240 |
- } |
|
| 241 |
- unsigned char operator[] (size_t pos) const { return data[pos]; }
|
|
| 242 |
-private: |
|
| 243 |
- unsigned char data[256]; |
|
| 244 |
-} Base64Table;*/ |
|
| 245 |
- |
|
| 246 |
-/*std::string u32ToHexString(uint32_t val) |
|
| 26 |
+std::vector<std::string> tokenize_str(const std::string &str, const std::string &delims = ", \t") |
|
| 247 | 27 |
{
|
| 248 |
- char temp[16]; |
|
| 249 |
- sprintf(temp,"%08X",val); |
|
| 250 |
- return temp; |
|
| 251 |
-}*/ |
|
| 28 |
+ // Skip delims at beginning, find start of first token |
|
| 29 |
+ auto lastPos = str.find_first_not_of(delims, 0); |
|
| 30 |
+ // Find next delimiter @ end of token |
|
| 31 |
+ auto pos = str.find_first_of(delims, lastPos); |
|
| 252 | 32 |
|
| 253 |
-///Converts the provided data to a string in a standard, user-friendly, round-trippable format |
|
| 254 |
-/*std::string BytesToString(const void* data, int len) |
|
| 255 |
-{
|
|
| 256 |
- char temp[16]; |
|
| 257 |
- if(len==1) {
|
|
| 258 |
- sprintf(temp,"%d",*(const unsigned char*)data); |
|
| 259 |
- return temp; |
|
| 260 |
- } else if(len==2) {
|
|
| 261 |
- sprintf(temp,"%d",*(const unsigned short*)data); |
|
| 262 |
- return temp; |
|
| 263 |
- } else if(len==4) {
|
|
| 264 |
- sprintf(temp,"%d",*(const unsigned int*)data); |
|
| 265 |
- return temp; |
|
| 266 |
- } |
|
| 33 |
+ // output vector |
|
| 34 |
+ std::vector<std::string> tokens; |
|
| 267 | 35 |
|
| 268 |
- std::string ret; |
|
| 269 |
- if(1) // use base64 |
|
| 270 |
- {
|
|
| 271 |
- const unsigned char* src = (const unsigned char*)data; |
|
| 272 |
- ret = "base64:"; |
|
| 273 |
- for(int n; len > 0; len -= n) |
|
| 274 |
- {
|
|
| 275 |
- unsigned char input[3] = {0,0,0};
|
|
| 276 |
- for(n=0; n<3 && n<len; ++n) |
|
| 277 |
- input[n] = *src++; |
|
| 278 |
- unsigned char output[4] = |
|
| 279 |
- {
|
|
| 280 |
- Base64Table[ input[0] >> 2 ], |
|
| 281 |
- Base64Table[ ((input[0] & 0x03) << 4) | (input[1] >> 4) ], |
|
| 282 |
- n<2 ? '=' : Base64Table[ ((input[1] & 0x0F) << 2) | (input[2] >> 6) ], |
|
| 283 |
- n<3 ? '=' : Base64Table[ input[2] & 0x3F ] |
|
| 284 |
- }; |
|
| 285 |
- ret.append(output, output+4); |
|
| 286 |
- } |
|
| 287 |
- } |
|
| 288 |
- else // use hex |
|
| 289 |
- {
|
|
| 290 |
- ret.resize(len*2+2); |
|
| 291 |
- ret[0] = '0'; |
|
| 292 |
- ret[1] = 'x'; |
|
| 293 |
- for(int i=0;i<len;i++) |
|
| 294 |
- {
|
|
| 295 |
- int a = (((const unsigned char*)data)[i]>>4); |
|
| 296 |
- int b = (((const unsigned char*)data)[i])&15; |
|
| 297 |
- if(a>9) a += 'A'-10; |
|
| 298 |
- else a += '0'; |
|
| 299 |
- if(b>9) b += 'A'-10; |
|
| 300 |
- else b += '0'; |
|
| 301 |
- ret[2+i*2] = a; |
|
| 302 |
- ret[2+i*2+1] = b; |
|
| 303 |
- } |
|
| 304 |
- } |
|
| 305 |
- return ret; |
|
| 306 |
-}*/ |
|
| 307 |
- |
|
| 308 |
-///returns -1 if this is not a hex string |
|
| 309 |
-/*int HexStringToBytesLength(const std::string& str) |
|
| 310 |
-{
|
|
| 311 |
- if(str.size()>2 && str[0] == '0' && toupper(str[1]) == 'X') |
|
| 312 |
- return str.size()/2-1; |
|
| 313 |
- else return -1; |
|
| 314 |
-}*/ |
|
| 315 |
- |
|
| 316 |
-/*int Base64StringToBytesLength(const std::string& str) |
|
| 317 |
-{
|
|
| 318 |
- if(str.size() < 7 || (str.size()-7) % 4 || str.substr(0,7) != "base64:") return -1; |
|
| 319 |
- |
|
| 320 |
- size_t c = ((str.size() - 7) / 4) * 3; |
|
| 321 |
- if(str[str.size()-1] == '=') { --c;
|
|
| 322 |
- if(str[str.size()-2] == '=') --c; } |
|
| 323 |
- return c; |
|
| 324 |
-}*/ |
|
| 325 |
- |
|
| 326 |
-///parses a string in the same format as BytesToString |
|
| 327 |
-///returns true if success. |
|
| 328 |
-/*bool StringToBytes(const std::string& str, void* data, int len) |
|
| 329 |
-{
|
|
| 330 |
- if(str.substr(0,7) == "base64:") |
|
| 36 |
+ while (pos != std::string::npos || lastPos != std::string::npos) |
|
| 331 | 37 |
{
|
| 332 |
- // base64 |
|
| 333 |
- unsigned char* tgt = (unsigned char*)data; |
|
| 334 |
- for(size_t pos = 7; pos < str.size() && len > 0; ) |
|
| 335 |
- {
|
|
| 336 |
- unsigned char input[4], converted[4]; |
|
| 337 |
- for(int i=0; i<4; ++i) |
|
| 338 |
- {
|
|
| 339 |
- if(pos >= str.size() && i > 0) return false; // invalid data |
|
| 340 |
- input[i] = str[pos++]; |
|
| 341 |
- if(input[i] & 0x80) return false; // illegal character |
|
| 342 |
- converted[i] = Base64Table[input[i]^0x80]; |
|
| 343 |
- if(converted[i] & 0x80) return false; // illegal character |
|
| 344 |
- } |
|
| 345 |
- unsigned char outpacket[3] = |
|
| 346 |
- {
|
|
| 347 |
- (converted[0] << 2) | (converted[1] >> 4), |
|
| 348 |
- (converted[1] << 4) | (converted[2] >> 2), |
|
| 349 |
- (converted[2] << 6) | (converted[3]) |
|
| 350 |
- }; |
|
| 351 |
- int outlen = (input[2] == '=') ? 1 : (input[3] == '=' ? 2 : 3); |
|
| 352 |
- if(outlen > len) outlen = len; |
|
| 353 |
- memcpy(tgt, outpacket, outlen); |
|
| 354 |
- tgt += outlen; |
|
| 355 |
- len -= outlen; |
|
| 356 |
- } |
|
| 357 |
- return true; |
|
| 38 |
+ // Found a token, add it to the vector. |
|
| 39 |
+ tokens.push_back(str.substr(lastPos, pos - lastPos)); |
|
| 40 |
+ // Skip delims. Note the "not_of". this is beginning of token |
|
| 41 |
+ lastPos = str.find_first_not_of(delims, pos); |
|
| 42 |
+ // Find next delimiter at end of token. |
|
| 43 |
+ pos = str.find_first_of(delims, lastPos); |
|
| 358 | 44 |
} |
| 359 |
- if(str.size()>2 && str[0] == '0' && toupper(str[1]) == 'X') |
|
| 360 |
- {
|
|
| 361 |
- // hex |
|
| 362 |
- int amt = len; |
|
| 363 |
- int bytesAvailable = str.size()/2; |
|
| 364 |
- if(bytesAvailable < amt) |
|
| 365 |
- amt = bytesAvailable; |
|
| 366 |
- const char* cstr = str.c_str()+2; |
|
| 367 |
- for(int i=0;i<amt;i++) {
|
|
| 368 |
- char a = toupper(cstr[i*2]); |
|
| 369 |
- char b = toupper(cstr[i*2+1]); |
|
| 370 |
- if(a>='A') a=a-'A'+10; |
|
| 371 |
- else a-='0'; |
|
| 372 |
- if(b>='A') b=b-'A'+10; |
|
| 373 |
- else b-='0'; |
|
| 374 |
- unsigned char val = ((unsigned char)a<<4)|(unsigned char)b; |
|
| 375 |
- ((unsigned char*)data)[i] = val; |
|
| 376 |
- } |
|
| 377 |
- return true; |
|
| 378 |
- } |
|
| 379 |
- |
|
| 380 |
- if(len==1) {
|
|
| 381 |
- int x = atoi(str.c_str()); |
|
| 382 |
- *(unsigned char*)data = x; |
|
| 383 |
- return true; |
|
| 384 |
- } else if(len==2) {
|
|
| 385 |
- int x = atoi(str.c_str()); |
|
| 386 |
- *(unsigned short*)data = x; |
|
| 387 |
- return true; |
|
| 388 |
- } else if(len==4) {
|
|
| 389 |
- int x = atoi(str.c_str()); |
|
| 390 |
- *(unsigned int*)data = x; |
|
| 391 |
- return true; |
|
| 392 |
- } |
|
| 393 |
- //we can't handle it |
|
| 394 |
- return false; |
|
| 395 |
-}*/ |
|
| 396 |
- |
|
| 397 |
-/// \brief convert input string into vector of string tokens |
|
| 398 |
-/// |
|
| 399 |
-/// \note consecutive delimiters will be treated as single delimiter |
|
| 400 |
-/// \note delimiters are _not_ included in return data |
|
| 401 |
-/// |
|
| 402 |
-/// \param input string to be parsed |
|
| 403 |
-/// \param delims list of delimiters. |
|
| 404 |
- |
|
| 405 |
-std::vector<std::string> tokenize_str(const std::string & str, |
|
| 406 |
- const std::string & delims=", \t") |
|
| 407 |
-{
|
|
| 408 |
- using namespace std; |
|
| 409 |
- // Skip delims at beginning, find start of first token |
|
| 410 |
- string::size_type lastPos = str.find_first_not_of(delims, 0); |
|
| 411 |
- // Find next delimiter @ end of token |
|
| 412 |
- string::size_type pos = str.find_first_of(delims, lastPos); |
|
| 413 |
- |
|
| 414 |
- // output vector |
|
| 415 |
- vector<string> tokens; |
|
| 416 | 45 |
|
| 417 |
- while (string::npos != pos || string::npos != lastPos) |
|
| 418 |
- {
|
|
| 419 |
- // Found a token, add it to the vector. |
|
| 420 |
- tokens.push_back(str.substr(lastPos, pos - lastPos)); |
|
| 421 |
- // Skip delims. Note the "not_of". this is beginning of token |
|
| 422 |
- lastPos = str.find_first_not_of(delims, pos); |
|
| 423 |
- // Find next delimiter at end of token. |
|
| 424 |
- pos = str.find_first_of(delims, lastPos); |
|
| 425 |
- } |
|
| 426 |
- |
|
| 427 |
- return tokens; |
|
| 46 |
+ return tokens; |
|
| 428 | 47 |
} |
| 429 |
- |
|
| 430 |
-//this code was taken from WINE (LGPL) |
|
| 431 |
-//http://google.com/codesearch?hl=en&q=+lang:c+splitpath+show:CPvw9Z-euls:_RSotQzmLeU:KGzljMEYFbY&sa=N&cd=9&ct=rc&cs_p=http://gentoo.osuosl.org/distfiles/Wine-20050524.tar.gz&cs_f=wine-20050524/programs/winefile/splitpath.c |
|
| 432 |
-/*void splitpath(const char* path, char* drv, char* dir, char* name, char* ext) |
|
| 433 |
-{
|
|
| 434 |
- const char* end;*/ /* end of processed string */ |
|
| 435 |
- //const char* p; /* search pointer */ |
|
| 436 |
- //const char* s; /* copy pointer */ |
|
| 437 |
- |
|
| 438 |
- /* extract drive name */ |
|
| 439 |
- /*if (path[0] && path[1]==':') {
|
|
| 440 |
- if (drv) {
|
|
| 441 |
- *drv++ = *path++; |
|
| 442 |
- *drv++ = *path++; |
|
| 443 |
- *drv = '\0'; |
|
| 444 |
- } else path+=2; |
|
| 445 |
- } else if (drv) |
|
| 446 |
- *drv = '\0';*/ |
|
| 447 |
- |
|
| 448 |
- /* search for end of string or stream separator */ |
|
| 449 |
- /*for(end=path; *end && *end!=':'; ) |
|
| 450 |
- end++;*/ |
|
| 451 |
- |
|
| 452 |
- /* search for begin of file extension */ |
|
| 453 |
- /*for(p=end; p>path && *--p!='\\' && *p!='/'; ) |
|
| 454 |
- if (*p == '.') {
|
|
| 455 |
- end = p; |
|
| 456 |
- break; |
|
| 457 |
- } |
|
| 458 |
- |
|
| 459 |
- if (ext) |
|
| 460 |
- for(s=end; (*ext=*s++); ) |
|
| 461 |
- ext++; |
|
| 462 |
- else |
|
| 463 |
- for(s=end; *s++; ) {}*/
|
|
| 464 |
- |
|
| 465 |
- /* search for end of directory name */ |
|
| 466 |
- /*for(p=end; p>path; ) |
|
| 467 |
- if (*--p=='\\' || *p=='/') {
|
|
| 468 |
- p++; |
|
| 469 |
- break; |
|
| 470 |
- } |
|
| 471 |
- |
|
| 472 |
- if (name) {
|
|
| 473 |
- for(s=p; s<end; ) |
|
| 474 |
- *name++ = *s++; |
|
| 475 |
- |
|
| 476 |
- *name = '\0'; |
|
| 477 |
- } else |
|
| 478 |
- for(s=p; s<end; ) |
|
| 479 |
- s++; |
|
| 480 |
- |
|
| 481 |
- if (dir) {
|
|
| 482 |
- for(s=path; s<p; ) |
|
| 483 |
- *dir++ = *s++; |
|
| 484 |
- |
|
| 485 |
- *dir = '\0'; |
|
| 486 |
- } |
|
| 487 |
-}*/ |
|
| 488 |
- |
|
| 489 |
-//mbg 5/12/08 |
|
| 490 |
-//for the curious, I tested U16ToHexStr and it was 10x faster than printf. |
|
| 491 |
-//so the author of these dedicated functions is not insane, and I will leave them. |
|
| 492 |
- |
|
| 493 |
-//static char TempArray[11]; |
|
| 494 |
- |
|
| 495 |
-/*uint16_t FastStrToU16(char* s, bool& valid) |
|
| 496 |
-{
|
|
| 497 |
- int i; |
|
| 498 |
- uint16_t v=0; |
|
| 499 |
- for(i=0; i < 4; i++) |
|
| 500 |
- {
|
|
| 501 |
- if(s[i] == 0) return v; |
|
| 502 |
- v<<=4; |
|
| 503 |
- if(s[i] >= '0' && s[i] <= '9') |
|
| 504 |
- {
|
|
| 505 |
- v+=s[i]-'0'; |
|
| 506 |
- } |
|
| 507 |
- else if(s[i] >= 'a' && s[i] <= 'f') |
|
| 508 |
- {
|
|
| 509 |
- v+=s[i]-'a'+10; |
|
| 510 |
- } |
|
| 511 |
- else if(s[i] >= 'A' && s[i] <= 'F') |
|
| 512 |
- {
|
|
| 513 |
- v+=s[i]-'A'+10; |
|
| 514 |
- } |
|
| 515 |
- else |
|
| 516 |
- {
|
|
| 517 |
- valid = false; |
|
| 518 |
- return 0xFFFF; |
|
| 519 |
- } |
|
| 520 |
- } |
|
| 521 |
- valid = true; |
|
| 522 |
- return v; |
|
| 523 |
-}*/ |
|
| 524 |
- |
|
| 525 |
-/*char *U8ToDecStr(uint8_t a) |
|
| 526 |
-{
|
|
| 527 |
- TempArray[0] = '0' + a/100; |
|
| 528 |
- TempArray[1] = '0' + (a%100)/10; |
|
| 529 |
- TempArray[2] = '0' + (a%10); |
|
| 530 |
- TempArray[3] = 0; |
|
| 531 |
- return TempArray; |
|
| 532 |
-}*/ |
|
| 533 |
- |
|
| 534 |
-/*char *U16ToDecStr(uint16_t a) |
|
| 535 |
-{
|
|
| 536 |
- TempArray[0] = '0' + a/10000; |
|
| 537 |
- TempArray[1] = '0' + (a%10000)/1000; |
|
| 538 |
- TempArray[2] = '0' + (a%1000)/100; |
|
| 539 |
- TempArray[3] = '0' + (a%100)/10; |
|
| 540 |
- TempArray[4] = '0' + (a%10); |
|
| 541 |
- TempArray[5] = 0; |
|
| 542 |
- return TempArray; |
|
| 543 |
-}*/ |
|
| 544 |
- |
|
| 545 |
-/*char *U32ToDecStr(char* buf, uint32_t a) |
|
| 546 |
-{
|
|
| 547 |
- buf[0] = '0' + a/1000000000; |
|
| 548 |
- buf[1] = '0' + (a%1000000000)/100000000; |
|
| 549 |
- buf[2] = '0' + (a%100000000)/10000000; |
|
| 550 |
- buf[3] = '0' + (a%10000000)/1000000; |
|
| 551 |
- buf[4] = '0' + (a%1000000)/100000; |
|
| 552 |
- buf[5] = '0' + (a%100000)/10000; |
|
| 553 |
- buf[6] = '0' + (a%10000)/1000; |
|
| 554 |
- buf[7] = '0' + (a%1000)/100; |
|
| 555 |
- buf[8] = '0' + (a%100)/10; |
|
| 556 |
- buf[9] = '0' + (a%10); |
|
| 557 |
- buf[10] = 0; |
|
| 558 |
- return buf; |
|
| 559 |
-}*/ |
|
| 560 |
-/*char *U32ToDecStr(uint32_t a) |
|
| 561 |
-{
|
|
| 562 |
- return U32ToDecStr(TempArray,a); |
|
| 563 |
-}*/ |
|
| 564 |
- |
|
| 565 |
-/*char *U16ToHexStr(uint16_t a) |
|
| 566 |
-{
|
|
| 567 |
- TempArray[0] = a/4096 > 9?'A'+a/4096-10:'0' + a/4096; |
|
| 568 |
- TempArray[1] = (a%4096)/256 > 9?'A'+(a%4096)/256 - 10:'0' + (a%4096)/256; |
|
| 569 |
- TempArray[2] = (a%256)/16 > 9?'A'+(a%256)/16 - 10:'0' + (a%256)/16; |
|
| 570 |
- TempArray[3] = a%16 > 9?'A'+(a%16) - 10:'0' + (a%16); |
|
| 571 |
- TempArray[4] = 0; |
|
| 572 |
- return TempArray; |
|
| 573 |
-}*/ |
|
| 574 |
- |
|
| 575 |
-/*char *U8ToHexStr(uint8_t a) |
|
| 576 |
-{
|
|
| 577 |
- TempArray[0] = a/16 > 9?'A'+a/16 - 10:'0' + a/16; |
|
| 578 |
- TempArray[1] = a%16 > 9?'A'+(a%16) - 10:'0' + (a%16); |
|
| 579 |
- TempArray[2] = 0; |
|
| 580 |
- return TempArray; |
|
| 581 |
-}*/ |
|
| 582 |
- |
|
| 583 |
-/*std::string stditoa(int n) |
|
| 584 |
-{
|
|
| 585 |
- char tempbuf[16]; |
|
| 586 |
- sprintf(tempbuf, "%d", n); |
|
| 587 |
- return tempbuf; |
|
| 588 |
-}*/ |
|
| 589 |
- |
|
| 590 |
- |
|
| 591 |
-/*std::string readNullTerminatedAscii(std::istream* is) |
|
| 592 |
-{
|
|
| 593 |
- std::string ret; |
|
| 594 |
- ret.reserve(50); |
|
| 595 |
- for(;;) |
|
| 596 |
- {
|
|
| 597 |
- int c = is->get(); |
|
| 598 |
- if(c == 0) break; |
|
| 599 |
- else ret += (char)c; |
|
| 600 |
- } |
|
| 601 |
- return ret; |
|
| 602 |
-}*/ |
|
| 603 |
- |
|
| 604 |
-// replace all instances of victim with replacement |
|
| 605 |
-/*std::string mass_replace(const std::string &source, const std::string &victim, const std::string &replacement) |
|
| 606 |
-{
|
|
| 607 |
- std::string answer = source; |
|
| 608 |
- std::string::size_type j = 0; |
|
| 609 |
- while ((j = answer.find(victim, j)) != std::string::npos ) |
|
| 610 |
- {
|
|
| 611 |
- answer.replace(j, victim.length(), replacement); |
|
| 612 |
- j+= replacement.length(); |
|
| 613 |
- } |
|
| 614 |
- return answer; |
|
| 615 |
-}*/ |
|
| 616 |
- |
|
| 617 |
-//http://www.codeproject.com/KB/string/UtfConverter.aspx |
|
| 618 |
-/*#include "ConvertUTF.h" |
|
| 619 |
-namespace UtfConverter |
|
| 620 |
-{
|
|
| 621 |
- static std::wstring FromUtf8(const std::string& utf8string) |
|
| 622 |
- {
|
|
| 623 |
- size_t widesize = utf8string.length(); |
|
| 624 |
- if (sizeof(wchar_t) == 2) |
|
| 625 |
- {
|
|
| 626 |
- wchar_t* widestringnative = new wchar_t[widesize+1]; |
|
| 627 |
- const UTF8* sourcestart = reinterpret_cast<const UTF8*>(utf8string.c_str()); |
|
| 628 |
- const UTF8* sourceend = sourcestart + widesize; |
|
| 629 |
- UTF16* targetstart = reinterpret_cast<UTF16*>(widestringnative); |
|
| 630 |
- UTF16* targetend = targetstart + widesize+1; |
|
| 631 |
- ConversionResult res = ConvertUTF8toUTF16(&sourcestart, sourceend, &targetstart, targetend, strictConversion); |
|
| 632 |
- if (res != conversionOK) |
|
| 633 |
- {
|
|
| 634 |
- delete [] widestringnative; |
|
| 635 |
- throw std::exception(); |
|
| 636 |
- } |
|
| 637 |
- *targetstart = 0; |
|
| 638 |
- std::wstring resultstring(widestringnative); |
|
| 639 |
- delete [] widestringnative; |
|
| 640 |
- return resultstring; |
|
| 641 |
- } |
|
| 642 |
- else if (sizeof(wchar_t) == 4) |
|
| 643 |
- {
|
|
| 644 |
- wchar_t* widestringnative = new wchar_t[widesize+1]; |
|
| 645 |
- const UTF8* sourcestart = reinterpret_cast<const UTF8*>(utf8string.c_str()); |
|
| 646 |
- const UTF8* sourceend = sourcestart + widesize; |
|
| 647 |
- UTF32* targetstart = reinterpret_cast<UTF32*>(widestringnative); |
|
| 648 |
- UTF32* targetend = targetstart + widesize; |
|
| 649 |
- ConversionResult res = ConvertUTF8toUTF32(&sourcestart, sourceend, &targetstart, targetend, strictConversion); |
|
| 650 |
- if (res != conversionOK) |
|
| 651 |
- {
|
|
| 652 |
- delete [] widestringnative; |
|
| 653 |
- throw std::exception(); |
|
| 654 |
- } |
|
| 655 |
- *targetstart = 0; |
|
| 656 |
- std::wstring resultstring(widestringnative); |
|
| 657 |
- delete [] widestringnative; |
|
| 658 |
- return resultstring; |
|
| 659 |
- } |
|
| 660 |
- else |
|
| 661 |
- {
|
|
| 662 |
- throw std::exception(); |
|
| 663 |
- } |
|
| 664 |
- return L""; |
|
| 665 |
- } |
|
| 666 |
- |
|
| 667 |
- static std::string ToUtf8(const std::wstring& widestring) |
|
| 668 |
- {
|
|
| 669 |
- size_t widesize = widestring.length(); |
|
| 670 |
- |
|
| 671 |
- if (sizeof(wchar_t) == 2) |
|
| 672 |
- {
|
|
| 673 |
- size_t utf8size = 3 * widesize + 1; |
|
| 674 |
- char* utf8stringnative = new char[utf8size]; |
|
| 675 |
- const UTF16* sourcestart = reinterpret_cast<const UTF16*>(widestring.c_str()); |
|
| 676 |
- const UTF16* sourceend = sourcestart + widesize; |
|
| 677 |
- UTF8* targetstart = reinterpret_cast<UTF8*>(utf8stringnative); |
|
| 678 |
- UTF8* targetend = targetstart + utf8size; |
|
| 679 |
- ConversionResult res = ConvertUTF16toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion); |
|
| 680 |
- if (res != conversionOK) |
|
| 681 |
- {
|
|
| 682 |
- delete [] utf8stringnative; |
|
| 683 |
- throw std::exception(); |
|
| 684 |
- } |
|
| 685 |
- *targetstart = 0; |
|
| 686 |
- std::string resultstring(utf8stringnative); |
|
| 687 |
- delete [] utf8stringnative; |
|
| 688 |
- return resultstring; |
|
| 689 |
- } |
|
| 690 |
- else if (sizeof(wchar_t) == 4) |
|
| 691 |
- {
|
|
| 692 |
- size_t utf8size = 4 * widesize + 1; |
|
| 693 |
- char* utf8stringnative = new char[utf8size]; |
|
| 694 |
- const UTF32* sourcestart = reinterpret_cast<const UTF32*>(widestring.c_str()); |
|
| 695 |
- const UTF32* sourceend = sourcestart + widesize; |
|
| 696 |
- UTF8* targetstart = reinterpret_cast<UTF8*>(utf8stringnative); |
|
| 697 |
- UTF8* targetend = targetstart + utf8size; |
|
| 698 |
- ConversionResult res = ConvertUTF32toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion); |
|
| 699 |
- if (res != conversionOK) |
|
| 700 |
- {
|
|
| 701 |
- delete [] utf8stringnative; |
|
| 702 |
- throw std::exception(); |
|
| 703 |
- } |
|
| 704 |
- *targetstart = 0; |
|
| 705 |
- std::string resultstring(utf8stringnative); |
|
| 706 |
- delete [] utf8stringnative; |
|
| 707 |
- return resultstring; |
|
| 708 |
- } |
|
| 709 |
- else |
|
| 710 |
- {
|
|
| 711 |
- throw std::exception(); |
|
| 712 |
- } |
|
| 713 |
- return ""; |
|
| 714 |
- } |
|
| 715 |
-}*/ |
|
| 716 |
- |
|
| 717 |
-//convert a std::string to std::wstring |
|
| 718 |
-/*std::wstring mbstowcs(std::string str) |
|
| 719 |
-{
|
|
| 720 |
- try {
|
|
| 721 |
- return UtfConverter::FromUtf8(str); |
|
| 722 |
- } catch(std::exception) {
|
|
| 723 |
- return L"(failed UTF-8 conversion)"; |
|
| 724 |
- } |
|
| 725 |
-}*/ |
|
| 726 |
- |
|
| 727 |
-/*std::string wcstombs(std::wstring str) |
|
| 728 |
-{
|
|
| 729 |
- return UtfConverter::ToUtf8(str); |
|
| 730 |
-}*/ |
|
| 731 |
- |
|
| 732 |
- |
|
| 733 |
-//TODO - dont we already have another function that can do this |
|
| 734 |
-/*std::string getExtension(const char* input) {
|
|
| 735 |
- char buf[1024]; |
|
| 736 |
- strcpy(buf,input); |
|
| 737 |
- char* dot=strrchr(buf,'.'); |
|
| 738 |
- if(!dot) |
|
| 739 |
- return ""; |
|
| 740 |
- char ext [512]; |
|
| 741 |
- strcpy(ext, dot+1); |
|
| 742 |
- int k, extlen=strlen(ext); |
|
| 743 |
- for(k=0;k<extlen;k++) |
|
| 744 |
- ext[k]=tolower(ext[k]); |
|
| 745 |
- return ext; |
|
| 746 |
-}*/ |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,746 @@ |
| 1 |
+//taken from fceux on 10/27/08 |
|
| 2 |
+//subsequently modified for desmume |
|
| 3 |
+ |
|
| 4 |
+/* |
|
| 5 |
+ Copyright (C) 2008-2009 DeSmuME team |
|
| 6 |
+ |
|
| 7 |
+ This file is free software: you can redistribute it and/or modify |
|
| 8 |
+ it under the terms of the GNU General Public License as published by |
|
| 9 |
+ the Free Software Foundation, either version 2 of the License, or |
|
| 10 |
+ (at your option) any later version. |
|
| 11 |
+ |
|
| 12 |
+ This file is distributed in the hope that it will be useful, |
|
| 13 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 14 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 15 |
+ GNU General Public License for more details. |
|
| 16 |
+ |
|
| 17 |
+ You should have received a copy of the GNU General Public License |
|
| 18 |
+ along with the this software. If not, see <http://www.gnu.org/licenses/>. |
|
| 19 |
+*/ |
|
| 20 |
+ |
|
| 21 |
+#include <string> |
|
| 22 |
+#include <vector> |
|
| 23 |
+ |
|
| 24 |
+#include "xstring.h" |
|
| 25 |
+ |
|
| 26 |
+//a vc-style substring operation (very kind and lenient) |
|
| 27 |
+/*std::string strsub(const std::string& str, int pos, int len) {
|
|
| 28 |
+ int strlen = str.size(); |
|
| 29 |
+ |
|
| 30 |
+ if(strlen==0) return str; //empty strings always return empty strings |
|
| 31 |
+ if(pos>=strlen) return str; //if you start past the end of the string, return the entire string. this is unusual, but there you have it |
|
| 32 |
+ |
|
| 33 |
+ //clipping |
|
| 34 |
+ if(pos<0) {
|
|
| 35 |
+ len += pos; |
|
| 36 |
+ pos = 0; |
|
| 37 |
+ } |
|
| 38 |
+ |
|
| 39 |
+ if (pos+len>=strlen) |
|
| 40 |
+ len=strlen-pos+1; |
|
| 41 |
+ |
|
| 42 |
+ //return str.str().substr(pos,len); |
|
| 43 |
+ return str.substr(pos,len); |
|
| 44 |
+}*/ |
|
| 45 |
+ |
|
| 46 |
+//std::string strmid(const std::string& str, int pos, int len) { return strsub(str,pos,len); }
|
|
| 47 |
+//std::string strleft(const std::string& str, int len) { return strsub(str,0,len); }
|
|
| 48 |
+//std::string strright(const std::string& str, int len) { return len ? strsub(str,str.size()-len,len) : ""; }
|
|
| 49 |
+/*std::string toupper(const std::string& str) |
|
| 50 |
+{
|
|
| 51 |
+ std::string ret = str; |
|
| 52 |
+ for(uint32_t i=0;i<str.size();i++) |
|
| 53 |
+ ret[i] = toupper(ret[i]); |
|
| 54 |
+ return ret; |
|
| 55 |
+}*/ |
|
| 56 |
+ |
|
| 57 |
+///Upper case routine. Returns number of characters modified |
|
| 58 |
+/*int str_ucase(char *str) {
|
|
| 59 |
+ uint32_t i=0,j=0; |
|
| 60 |
+ |
|
| 61 |
+ while (i < strlen(str)) {
|
|
| 62 |
+ if ((str[i] >= 'a') && (str[i] <= 'z')) {
|
|
| 63 |
+ str[i] &= ~0x20; |
|
| 64 |
+ j++; |
|
| 65 |
+ } |
|
| 66 |
+ i++; |
|
| 67 |
+ } |
|
| 68 |
+ return j; |
|
| 69 |
+}*/ |
|
| 70 |
+ |
|
| 71 |
+ |
|
| 72 |
+///Lower case routine. Returns number of characters modified |
|
| 73 |
+/*int str_lcase(char *str) {
|
|
| 74 |
+ uint32_t i=0,j=0; |
|
| 75 |
+ |
|
| 76 |
+ while (i < strlen(str)) {
|
|
| 77 |
+ if ((str[i] >= 'A') && (str[i] <= 'Z')) {
|
|
| 78 |
+ str[i] |= 0x20; |
|
| 79 |
+ j++; |
|
| 80 |
+ } |
|
| 81 |
+ i++; |
|
| 82 |
+ } |
|
| 83 |
+ return j; |
|
| 84 |
+}*/ |
|
| 85 |
+ |
|
| 86 |
+ |
|
| 87 |
+///White space-trimming routine |
|
| 88 |
+ |
|
| 89 |
+///Removes whitespace from left side of string, depending on the flags set (See STRIP_x definitions in xstring.h) |
|
| 90 |
+///Returns number of characters removed |
|
| 91 |
+/*int str_ltrim(char *str, int flags) {
|
|
| 92 |
+ uint32_t i=0; |
|
| 93 |
+ |
|
| 94 |
+ while (str[0]) {
|
|
| 95 |
+ if ((str[0] != ' ') || (str[0] != '\t') || (str[0] != '\r') || (str[0] != '\n')) break; |
|
| 96 |
+ |
|
| 97 |
+ if ((flags & STRIP_SP) && (str[0] == ' ')) {
|
|
| 98 |
+ i++; |
|
| 99 |
+ strcpy(str,str+1); |
|
| 100 |
+ } |
|
| 101 |
+ if ((flags & STRIP_TAB) && (str[0] == '\t')) {
|
|
| 102 |
+ i++; |
|
| 103 |
+ strcpy(str,str+1); |
|
| 104 |
+ } |
|
| 105 |
+ if ((flags & STRIP_CR) && (str[0] == '\r')) {
|
|
| 106 |
+ i++; |
|
| 107 |
+ strcpy(str,str+1); |
|
| 108 |
+ } |
|
| 109 |
+ if ((flags & STRIP_LF) && (str[0] == '\n')) {
|
|
| 110 |
+ i++; |
|
| 111 |
+ strcpy(str,str+1); |
|
| 112 |
+ } |
|
| 113 |
+ } |
|
| 114 |
+ return i; |
|
| 115 |
+}*/ |
|
| 116 |
+ |
|
| 117 |
+ |
|
| 118 |
+///White space-trimming routine |
|
| 119 |
+ |
|
| 120 |
+///Removes whitespace from right side of string, depending on the flags set (See STRIP_x definitions in xstring.h) |
|
| 121 |
+///Returns number of characters removed |
|
| 122 |
+/*int str_rtrim(char *str, int flags) {
|
|
| 123 |
+ uint32_t i=0; |
|
| 124 |
+ |
|
| 125 |
+ while (strlen(str)) {
|
|
| 126 |
+ if ((str[strlen(str)-1] != ' ') || |
|
| 127 |
+ (str[strlen(str)-1] != '\t') || |
|
| 128 |
+ (str[strlen(str)-1] != '\r') || |
|
| 129 |
+ (str[strlen(str)-1] != '\n')) break; |
|
| 130 |
+ |
|
| 131 |
+ if ((flags & STRIP_SP) && (str[0] == ' ')) {
|
|
| 132 |
+ i++; |
|
| 133 |
+ str[strlen(str)-1] = 0; |
|
| 134 |
+ } |
|
| 135 |
+ if ((flags & STRIP_TAB) && (str[0] == '\t')) {
|
|
| 136 |
+ i++; |
|
| 137 |
+ str[strlen(str)-1] = 0; |
|
| 138 |
+ } |
|
| 139 |
+ if ((flags & STRIP_CR) && (str[0] == '\r')) {
|
|
| 140 |
+ i++; |
|
| 141 |
+ str[strlen(str)-1] = 0; |
|
| 142 |
+ } |
|
| 143 |
+ if ((flags & STRIP_LF) && (str[0] == '\n')) {
|
|
| 144 |
+ i++; |
|
| 145 |
+ str[strlen(str)-1] = 0; |
|
| 146 |
+ } |
|
| 147 |
+ } |
|
| 148 |
+ return i; |
|
| 149 |
+}*/ |
|
| 150 |
+ |
|
| 151 |
+ |
|
| 152 |
+///White space-stripping routine |
|
| 153 |
+ |
|
| 154 |
+///Removes whitespace depending on the flags set (See STRIP_x definitions in xstring.h) |
|
| 155 |
+///Returns number of characters removed, or -1 on error |
|
| 156 |
+/*int str_strip(char *str, int flags) {
|
|
| 157 |
+ uint32_t i=0,j=0; |
|
| 158 |
+ char *astr,chr; |
|
| 159 |
+ |
|
| 160 |
+ if (!strlen(str)) return -1; |
|
| 161 |
+ if (!(flags & (STRIP_SP|STRIP_TAB|STRIP_CR|STRIP_LF))) return -1; |
|
| 162 |
+ if (!(astr = (char*)malloc(strlen(str)+1))) return -1; |
|
| 163 |
+ while (i < strlen(str)) {
|
|
| 164 |
+ chr = str[i++]; |
|
| 165 |
+ if ((flags & STRIP_SP) && (chr == ' ')) chr = 0; |
|
| 166 |
+ if ((flags & STRIP_TAB) && (chr == '\t')) chr = 0; |
|
| 167 |
+ if ((flags & STRIP_CR) && (chr == '\r')) chr = 0; |
|
| 168 |
+ if ((flags & STRIP_LF) && (chr == '\n')) chr = 0; |
|
| 169 |
+ |
|
| 170 |
+ if (chr) astr[j++] = chr; |
|
| 171 |
+ } |
|
| 172 |
+ astr[j] = 0; |
|
| 173 |
+ strcpy(str,astr); |
|
| 174 |
+ free(astr); |
|
| 175 |
+ return j; |
|
| 176 |
+}*/ |
|
| 177 |
+ |
|
| 178 |
+ |
|
| 179 |
+///Character replacement routine |
|
| 180 |
+ |
|
| 181 |
+///Replaces all instances of 'search' with 'replace' |
|
| 182 |
+///Returns number of characters modified |
|
| 183 |
+/*int chr_replace(char *str, char search, char replace) {
|
|
| 184 |
+ uint32_t i=0,j=0; |
|
| 185 |
+ |
|
| 186 |
+ while (i < strlen(str)) {
|
|
| 187 |
+ if (str[i] == search) {
|
|
| 188 |
+ str[i] = replace; |
|
| 189 |
+ j++; |
|
| 190 |
+ } |
|
| 191 |
+ i++; |
|
| 192 |
+ } |
|
| 193 |
+ return j; |
|
| 194 |
+}*/ |
|
| 195 |
+ |
|
| 196 |
+ |
|
| 197 |
+///Sub-String replacement routine |
|
| 198 |
+ |
|
| 199 |
+///Replaces all instances of 'search' with 'replace' |
|
| 200 |
+///Returns number of sub-strings modified, or -1 on error |
|
| 201 |
+/*int str_replace(char *str, char *search, char *replace) {
|
|
| 202 |
+ uint32_t i=0,j=0; |
|
| 203 |
+ int searchlen,replacelen; |
|
| 204 |
+ char *astr; |
|
| 205 |
+ |
|
| 206 |
+ searchlen = strlen(search); |
|
| 207 |
+ replacelen = strlen(replace); |
|
| 208 |
+ if ((!strlen(str)) || (!searchlen)) return -1; //note: allow *replace to have a length of zero! |
|
| 209 |
+ if (!(astr = (char*)malloc(strlen(str)+1))) return -1; |
|
| 210 |
+ while (i < strlen(str)) {
|
|
| 211 |
+ if (!strncmp(str+i,search,searchlen)) {
|
|
| 212 |
+ if (replacelen) memcpy(astr+j,replace,replacelen); |
|
| 213 |
+ i += searchlen; |
|
| 214 |
+ j += replacelen; |
|
| 215 |
+ } |
|
| 216 |
+ else astr[j++] = str[i++]; |
|
| 217 |
+ } |
|
| 218 |
+ astr[j] = 0; |
|
| 219 |
+ strcpy(str,astr); |
|
| 220 |
+ free(astr); |
|
| 221 |
+ return j; |
|
| 222 |
+}*/ |
|
| 223 |
+ |
|
| 224 |
+/*static const struct Base64Table |
|
| 225 |
+{
|
|
| 226 |
+ Base64Table() |
|
| 227 |
+ {
|
|
| 228 |
+ size_t a=0; |
|
| 229 |
+ for(a=0; a<256; ++a) data[a] = 0xFF; // mark everything as invalid by default |
|
| 230 |
+ // create value->ascii mapping |
|
| 231 |
+ a=0; |
|
| 232 |
+ for(unsigned char c='A'; c<='Z'; ++c) data[a++] = c; // 0..25 |
|
| 233 |
+ for(unsigned char c='a'; c<='z'; ++c) data[a++] = c; // 26..51 |
|
| 234 |
+ for(unsigned char c='0'; c<='9'; ++c) data[a++] = c; // 52..61 |
|
| 235 |
+ data[62] = '+'; // 62 |
|
| 236 |
+ data[63] = '/'; // 63 |
|
| 237 |
+ // create ascii->value mapping (but due to overlap, write it to highbit region) |
|
| 238 |
+ for(a=0; a<64; ++a) data[data[a]^0x80] = a; // |
|
| 239 |
+ data[((unsigned char)'=') ^ 0x80] = 0; |
|
| 240 |
+ } |
|
| 241 |
+ unsigned char operator[] (size_t pos) const { return data[pos]; }
|
|
| 242 |
+private: |
|
| 243 |
+ unsigned char data[256]; |
|
| 244 |
+} Base64Table;*/ |
|
| 245 |
+ |
|
| 246 |
+/*std::string u32ToHexString(uint32_t val) |
|
| 247 |
+{
|
|
| 248 |
+ char temp[16]; |
|
| 249 |
+ sprintf(temp,"%08X",val); |
|
| 250 |
+ return temp; |
|
| 251 |
+}*/ |
|
| 252 |
+ |
|
| 253 |
+///Converts the provided data to a string in a standard, user-friendly, round-trippable format |
|
| 254 |
+/*std::string BytesToString(const void* data, int len) |
|
| 255 |
+{
|
|
| 256 |
+ char temp[16]; |
|
| 257 |
+ if(len==1) {
|
|
| 258 |
+ sprintf(temp,"%d",*(const unsigned char*)data); |
|
| 259 |
+ return temp; |
|
| 260 |
+ } else if(len==2) {
|
|
| 261 |
+ sprintf(temp,"%d",*(const unsigned short*)data); |
|
| 262 |
+ return temp; |
|
| 263 |
+ } else if(len==4) {
|
|
| 264 |
+ sprintf(temp,"%d",*(const unsigned int*)data); |
|
| 265 |
+ return temp; |
|
| 266 |
+ } |
|
| 267 |
+ |
|
| 268 |
+ std::string ret; |
|
| 269 |
+ if(1) // use base64 |
|
| 270 |
+ {
|
|
| 271 |
+ const unsigned char* src = (const unsigned char*)data; |
|
| 272 |
+ ret = "base64:"; |
|
| 273 |
+ for(int n; len > 0; len -= n) |
|
| 274 |
+ {
|
|
| 275 |
+ unsigned char input[3] = {0,0,0};
|
|
| 276 |
+ for(n=0; n<3 && n<len; ++n) |
|
| 277 |
+ input[n] = *src++; |
|
| 278 |
+ unsigned char output[4] = |
|
| 279 |
+ {
|
|
| 280 |
+ Base64Table[ input[0] >> 2 ], |
|
| 281 |
+ Base64Table[ ((input[0] & 0x03) << 4) | (input[1] >> 4) ], |
|
| 282 |
+ n<2 ? '=' : Base64Table[ ((input[1] & 0x0F) << 2) | (input[2] >> 6) ], |
|
| 283 |
+ n<3 ? '=' : Base64Table[ input[2] & 0x3F ] |
|
| 284 |
+ }; |
|
| 285 |
+ ret.append(output, output+4); |
|
| 286 |
+ } |
|
| 287 |
+ } |
|
| 288 |
+ else // use hex |
|
| 289 |
+ {
|
|
| 290 |
+ ret.resize(len*2+2); |
|
| 291 |
+ ret[0] = '0'; |
|
| 292 |
+ ret[1] = 'x'; |
|
| 293 |
+ for(int i=0;i<len;i++) |
|
| 294 |
+ {
|
|
| 295 |
+ int a = (((const unsigned char*)data)[i]>>4); |
|
| 296 |
+ int b = (((const unsigned char*)data)[i])&15; |
|
| 297 |
+ if(a>9) a += 'A'-10; |
|
| 298 |
+ else a += '0'; |
|
| 299 |
+ if(b>9) b += 'A'-10; |
|
| 300 |
+ else b += '0'; |
|
| 301 |
+ ret[2+i*2] = a; |
|
| 302 |
+ ret[2+i*2+1] = b; |
|
| 303 |
+ } |
|
| 304 |
+ } |
|
| 305 |
+ return ret; |
|
| 306 |
+}*/ |
|
| 307 |
+ |
|
| 308 |
+///returns -1 if this is not a hex string |
|
| 309 |
+/*int HexStringToBytesLength(const std::string& str) |
|
| 310 |
+{
|
|
| 311 |
+ if(str.size()>2 && str[0] == '0' && toupper(str[1]) == 'X') |
|
| 312 |
+ return str.size()/2-1; |
|
| 313 |
+ else return -1; |
|
| 314 |
+}*/ |
|
| 315 |
+ |
|
| 316 |
+/*int Base64StringToBytesLength(const std::string& str) |
|
| 317 |
+{
|
|
| 318 |
+ if(str.size() < 7 || (str.size()-7) % 4 || str.substr(0,7) != "base64:") return -1; |
|
| 319 |
+ |
|
| 320 |
+ size_t c = ((str.size() - 7) / 4) * 3; |
|
| 321 |
+ if(str[str.size()-1] == '=') { --c;
|
|
| 322 |
+ if(str[str.size()-2] == '=') --c; } |
|
| 323 |
+ return c; |
|
| 324 |
+}*/ |
|
| 325 |
+ |
|
| 326 |
+///parses a string in the same format as BytesToString |
|
| 327 |
+///returns true if success. |
|
| 328 |
+/*bool StringToBytes(const std::string& str, void* data, int len) |
|
| 329 |
+{
|
|
| 330 |
+ if(str.substr(0,7) == "base64:") |
|
| 331 |
+ {
|
|
| 332 |
+ // base64 |
|
| 333 |
+ unsigned char* tgt = (unsigned char*)data; |
|
| 334 |
+ for(size_t pos = 7; pos < str.size() && len > 0; ) |
|
| 335 |
+ {
|
|
| 336 |
+ unsigned char input[4], converted[4]; |
|
| 337 |
+ for(int i=0; i<4; ++i) |
|
| 338 |
+ {
|
|
| 339 |
+ if(pos >= str.size() && i > 0) return false; // invalid data |
|
| 340 |
+ input[i] = str[pos++]; |
|
| 341 |
+ if(input[i] & 0x80) return false; // illegal character |
|
| 342 |
+ converted[i] = Base64Table[input[i]^0x80]; |
|
| 343 |
+ if(converted[i] & 0x80) return false; // illegal character |
|
| 344 |
+ } |
|
| 345 |
+ unsigned char outpacket[3] = |
|
| 346 |
+ {
|
|
| 347 |
+ (converted[0] << 2) | (converted[1] >> 4), |
|
| 348 |
+ (converted[1] << 4) | (converted[2] >> 2), |
|
| 349 |
+ (converted[2] << 6) | (converted[3]) |
|
| 350 |
+ }; |
|
| 351 |
+ int outlen = (input[2] == '=') ? 1 : (input[3] == '=' ? 2 : 3); |
|
| 352 |
+ if(outlen > len) outlen = len; |
|
| 353 |
+ memcpy(tgt, outpacket, outlen); |
|
| 354 |
+ tgt += outlen; |
|
| 355 |
+ len -= outlen; |
|
| 356 |
+ } |
|
| 357 |
+ return true; |
|
| 358 |
+ } |
|
| 359 |
+ if(str.size()>2 && str[0] == '0' && toupper(str[1]) == 'X') |
|
| 360 |
+ {
|
|
| 361 |
+ // hex |
|
| 362 |
+ int amt = len; |
|
| 363 |
+ int bytesAvailable = str.size()/2; |
|
| 364 |
+ if(bytesAvailable < amt) |
|
| 365 |
+ amt = bytesAvailable; |
|
| 366 |
+ const char* cstr = str.c_str()+2; |
|
| 367 |
+ for(int i=0;i<amt;i++) {
|
|
| 368 |
+ char a = toupper(cstr[i*2]); |
|
| 369 |
+ char b = toupper(cstr[i*2+1]); |
|
| 370 |
+ if(a>='A') a=a-'A'+10; |
|
| 371 |
+ else a-='0'; |
|
| 372 |
+ if(b>='A') b=b-'A'+10; |
|
| 373 |
+ else b-='0'; |
|
| 374 |
+ unsigned char val = ((unsigned char)a<<4)|(unsigned char)b; |
|
| 375 |
+ ((unsigned char*)data)[i] = val; |
|
| 376 |
+ } |
|
| 377 |
+ return true; |
|
| 378 |
+ } |
|
| 379 |
+ |
|
| 380 |
+ if(len==1) {
|
|
| 381 |
+ int x = atoi(str.c_str()); |
|
| 382 |
+ *(unsigned char*)data = x; |
|
| 383 |
+ return true; |
|
| 384 |
+ } else if(len==2) {
|
|
| 385 |
+ int x = atoi(str.c_str()); |
|
| 386 |
+ *(unsigned short*)data = x; |
|
| 387 |
+ return true; |
|
| 388 |
+ } else if(len==4) {
|
|
| 389 |
+ int x = atoi(str.c_str()); |
|
| 390 |
+ *(unsigned int*)data = x; |
|
| 391 |
+ return true; |
|
| 392 |
+ } |
|
| 393 |
+ //we can't handle it |
|
| 394 |
+ return false; |
|
| 395 |
+}*/ |
|
| 396 |
+ |
|
| 397 |
+/// \brief convert input string into vector of string tokens |
|
| 398 |
+/// |
|
| 399 |
+/// \note consecutive delimiters will be treated as single delimiter |
|
| 400 |
+/// \note delimiters are _not_ included in return data |
|
| 401 |
+/// |
|
| 402 |
+/// \param input string to be parsed |
|
| 403 |
+/// \param delims list of delimiters. |
|
| 404 |
+ |
|
| 405 |
+std::vector<std::string> tokenize_str(const std::string & str, |
|
| 406 |
+ const std::string & delims=", \t") |
|
| 407 |
+{
|
|
| 408 |
+ using namespace std; |
|
| 409 |
+ // Skip delims at beginning, find start of first token |
|
| 410 |
+ string::size_type lastPos = str.find_first_not_of(delims, 0); |
|
| 411 |
+ // Find next delimiter @ end of token |
|
| 412 |
+ string::size_type pos = str.find_first_of(delims, lastPos); |
|
| 413 |
+ |
|
| 414 |
+ // output vector |
|
| 415 |
+ vector<string> tokens; |
|
| 416 |
+ |
|
| 417 |
+ while (string::npos != pos || string::npos != lastPos) |
|
| 418 |
+ {
|
|
| 419 |
+ // Found a token, add it to the vector. |
|
| 420 |
+ tokens.push_back(str.substr(lastPos, pos - lastPos)); |
|
| 421 |
+ // Skip delims. Note the "not_of". this is beginning of token |
|
| 422 |
+ lastPos = str.find_first_not_of(delims, pos); |
|
| 423 |
+ // Find next delimiter at end of token. |
|
| 424 |
+ pos = str.find_first_of(delims, lastPos); |
|
| 425 |
+ } |
|
| 426 |
+ |
|
| 427 |
+ return tokens; |
|
| 428 |
+} |
|
| 429 |
+ |
|
| 430 |
+//this code was taken from WINE (LGPL) |
|
| 431 |
+//http://google.com/codesearch?hl=en&q=+lang:c+splitpath+show:CPvw9Z-euls:_RSotQzmLeU:KGzljMEYFbY&sa=N&cd=9&ct=rc&cs_p=http://gentoo.osuosl.org/distfiles/Wine-20050524.tar.gz&cs_f=wine-20050524/programs/winefile/splitpath.c |
|
| 432 |
+/*void splitpath(const char* path, char* drv, char* dir, char* name, char* ext) |
|
| 433 |
+{
|
|
| 434 |
+ const char* end;*/ /* end of processed string */ |
|
| 435 |
+ //const char* p; /* search pointer */ |
|
| 436 |
+ //const char* s; /* copy pointer */ |
|
| 437 |
+ |
|
| 438 |
+ /* extract drive name */ |
|
| 439 |
+ /*if (path[0] && path[1]==':') {
|
|
| 440 |
+ if (drv) {
|
|
| 441 |
+ *drv++ = *path++; |
|
| 442 |
+ *drv++ = *path++; |
|
| 443 |
+ *drv = '\0'; |
|
| 444 |
+ } else path+=2; |
|
| 445 |
+ } else if (drv) |
|
| 446 |
+ *drv = '\0';*/ |
|
| 447 |
+ |
|
| 448 |
+ /* search for end of string or stream separator */ |
|
| 449 |
+ /*for(end=path; *end && *end!=':'; ) |
|
| 450 |
+ end++;*/ |
|
| 451 |
+ |
|
| 452 |
+ /* search for begin of file extension */ |
|
| 453 |
+ /*for(p=end; p>path && *--p!='\\' && *p!='/'; ) |
|
| 454 |
+ if (*p == '.') {
|
|
| 455 |
+ end = p; |
|
| 456 |
+ break; |
|
| 457 |
+ } |
|
| 458 |
+ |
|
| 459 |
+ if (ext) |
|
| 460 |
+ for(s=end; (*ext=*s++); ) |
|
| 461 |
+ ext++; |
|
| 462 |
+ else |
|
| 463 |
+ for(s=end; *s++; ) {}*/
|
|
| 464 |
+ |
|
| 465 |
+ /* search for end of directory name */ |
|
| 466 |
+ /*for(p=end; p>path; ) |
|
| 467 |
+ if (*--p=='\\' || *p=='/') {
|
|
| 468 |
+ p++; |
|
| 469 |
+ break; |
|
| 470 |
+ } |
|
| 471 |
+ |
|
| 472 |
+ if (name) {
|
|
| 473 |
+ for(s=p; s<end; ) |
|
| 474 |
+ *name++ = *s++; |
|
| 475 |
+ |
|
| 476 |
+ *name = '\0'; |
|
| 477 |
+ } else |
|
| 478 |
+ for(s=p; s<end; ) |
|
| 479 |
+ s++; |
|
| 480 |
+ |
|
| 481 |
+ if (dir) {
|
|
| 482 |
+ for(s=path; s<p; ) |
|
| 483 |
+ *dir++ = *s++; |
|
| 484 |
+ |
|
| 485 |
+ *dir = '\0'; |
|
| 486 |
+ } |
|
| 487 |
+}*/ |
|
| 488 |
+ |
|
| 489 |
+//mbg 5/12/08 |
|
| 490 |
+//for the curious, I tested U16ToHexStr and it was 10x faster than printf. |
|
| 491 |
+//so the author of these dedicated functions is not insane, and I will leave them. |
|
| 492 |
+ |
|
| 493 |
+//static char TempArray[11]; |
|
| 494 |
+ |
|
| 495 |
+/*uint16_t FastStrToU16(char* s, bool& valid) |
|
| 496 |
+{
|
|
| 497 |
+ int i; |
|
| 498 |
+ uint16_t v=0; |
|
| 499 |
+ for(i=0; i < 4; i++) |
|
| 500 |
+ {
|
|
| 501 |
+ if(s[i] == 0) return v; |
|
| 502 |
+ v<<=4; |
|
| 503 |
+ if(s[i] >= '0' && s[i] <= '9') |
|
| 504 |
+ {
|
|
| 505 |
+ v+=s[i]-'0'; |
|
| 506 |
+ } |
|
| 507 |
+ else if(s[i] >= 'a' && s[i] <= 'f') |
|
| 508 |
+ {
|
|
| 509 |
+ v+=s[i]-'a'+10; |
|
| 510 |
+ } |
|
| 511 |
+ else if(s[i] >= 'A' && s[i] <= 'F') |
|
| 512 |
+ {
|
|
| 513 |
+ v+=s[i]-'A'+10; |
|
| 514 |
+ } |
|
| 515 |
+ else |
|
| 516 |
+ {
|
|
| 517 |
+ valid = false; |
|
| 518 |
+ return 0xFFFF; |
|
| 519 |
+ } |
|
| 520 |
+ } |
|
| 521 |
+ valid = true; |
|
| 522 |
+ return v; |
|
| 523 |
+}*/ |
|
| 524 |
+ |
|
| 525 |
+/*char *U8ToDecStr(uint8_t a) |
|
| 526 |
+{
|
|
| 527 |
+ TempArray[0] = '0' + a/100; |
|
| 528 |
+ TempArray[1] = '0' + (a%100)/10; |
|
| 529 |
+ TempArray[2] = '0' + (a%10); |
|
| 530 |
+ TempArray[3] = 0; |
|
| 531 |
+ return TempArray; |
|
| 532 |
+}*/ |
|
| 533 |
+ |
|
| 534 |
+/*char *U16ToDecStr(uint16_t a) |
|
| 535 |
+{
|
|
| 536 |
+ TempArray[0] = '0' + a/10000; |
|
| 537 |
+ TempArray[1] = '0' + (a%10000)/1000; |
|
| 538 |
+ TempArray[2] = '0' + (a%1000)/100; |
|
| 539 |
+ TempArray[3] = '0' + (a%100)/10; |
|
| 540 |
+ TempArray[4] = '0' + (a%10); |
|
| 541 |
+ TempArray[5] = 0; |
|
| 542 |
+ return TempArray; |
|
| 543 |
+}*/ |
|
| 544 |
+ |
|
| 545 |
+/*char *U32ToDecStr(char* buf, uint32_t a) |
|
| 546 |
+{
|
|
| 547 |
+ buf[0] = '0' + a/1000000000; |
|
| 548 |
+ buf[1] = '0' + (a%1000000000)/100000000; |
|
| 549 |
+ buf[2] = '0' + (a%100000000)/10000000; |
|
| 550 |
+ buf[3] = '0' + (a%10000000)/1000000; |
|
| 551 |
+ buf[4] = '0' + (a%1000000)/100000; |
|
| 552 |
+ buf[5] = '0' + (a%100000)/10000; |
|
| 553 |
+ buf[6] = '0' + (a%10000)/1000; |
|
| 554 |
+ buf[7] = '0' + (a%1000)/100; |
|
| 555 |
+ buf[8] = '0' + (a%100)/10; |
|
| 556 |
+ buf[9] = '0' + (a%10); |
|
| 557 |
+ buf[10] = 0; |
|
| 558 |
+ return buf; |
|
| 559 |
+}*/ |
|
| 560 |
+/*char *U32ToDecStr(uint32_t a) |
|
| 561 |
+{
|
|
| 562 |
+ return U32ToDecStr(TempArray,a); |
|
| 563 |
+}*/ |
|
| 564 |
+ |
|
| 565 |
+/*char *U16ToHexStr(uint16_t a) |
|
| 566 |
+{
|
|
| 567 |
+ TempArray[0] = a/4096 > 9?'A'+a/4096-10:'0' + a/4096; |
|
| 568 |
+ TempArray[1] = (a%4096)/256 > 9?'A'+(a%4096)/256 - 10:'0' + (a%4096)/256; |
|
| 569 |
+ TempArray[2] = (a%256)/16 > 9?'A'+(a%256)/16 - 10:'0' + (a%256)/16; |
|
| 570 |
+ TempArray[3] = a%16 > 9?'A'+(a%16) - 10:'0' + (a%16); |
|
| 571 |
+ TempArray[4] = 0; |
|
| 572 |
+ return TempArray; |
|
| 573 |
+}*/ |
|
| 574 |
+ |
|
| 575 |
+/*char *U8ToHexStr(uint8_t a) |
|
| 576 |
+{
|
|
| 577 |
+ TempArray[0] = a/16 > 9?'A'+a/16 - 10:'0' + a/16; |
|
| 578 |
+ TempArray[1] = a%16 > 9?'A'+(a%16) - 10:'0' + (a%16); |
|
| 579 |
+ TempArray[2] = 0; |
|
| 580 |
+ return TempArray; |
|
| 581 |
+}*/ |
|
| 582 |
+ |
|
| 583 |
+/*std::string stditoa(int n) |
|
| 584 |
+{
|
|
| 585 |
+ char tempbuf[16]; |
|
| 586 |
+ sprintf(tempbuf, "%d", n); |
|
| 587 |
+ return tempbuf; |
|
| 588 |
+}*/ |
|
| 589 |
+ |
|
| 590 |
+ |
|
| 591 |
+/*std::string readNullTerminatedAscii(std::istream* is) |
|
| 592 |
+{
|
|
| 593 |
+ std::string ret; |
|
| 594 |
+ ret.reserve(50); |
|
| 595 |
+ for(;;) |
|
| 596 |
+ {
|
|
| 597 |
+ int c = is->get(); |
|
| 598 |
+ if(c == 0) break; |
|
| 599 |
+ else ret += (char)c; |
|
| 600 |
+ } |
|
| 601 |
+ return ret; |
|
| 602 |
+}*/ |
|
| 603 |
+ |
|
| 604 |
+// replace all instances of victim with replacement |
|
| 605 |
+/*std::string mass_replace(const std::string &source, const std::string &victim, const std::string &replacement) |
|
| 606 |
+{
|
|
| 607 |
+ std::string answer = source; |
|
| 608 |
+ std::string::size_type j = 0; |
|
| 609 |
+ while ((j = answer.find(victim, j)) != std::string::npos ) |
|
| 610 |
+ {
|
|
| 611 |
+ answer.replace(j, victim.length(), replacement); |
|
| 612 |
+ j+= replacement.length(); |
|
| 613 |
+ } |
|
| 614 |
+ return answer; |
|
| 615 |
+}*/ |
|
| 616 |
+ |
|
| 617 |
+//http://www.codeproject.com/KB/string/UtfConverter.aspx |
|
| 618 |
+/*#include "ConvertUTF.h" |
|
| 619 |
+namespace UtfConverter |
|
| 620 |
+{
|
|
| 621 |
+ static std::wstring FromUtf8(const std::string& utf8string) |
|
| 622 |
+ {
|
|
| 623 |
+ size_t widesize = utf8string.length(); |
|
| 624 |
+ if (sizeof(wchar_t) == 2) |
|
| 625 |
+ {
|
|
| 626 |
+ wchar_t* widestringnative = new wchar_t[widesize+1]; |
|
| 627 |
+ const UTF8* sourcestart = reinterpret_cast<const UTF8*>(utf8string.c_str()); |
|
| 628 |
+ const UTF8* sourceend = sourcestart + widesize; |
|
| 629 |
+ UTF16* targetstart = reinterpret_cast<UTF16*>(widestringnative); |
|
| 630 |
+ UTF16* targetend = targetstart + widesize+1; |
|
| 631 |
+ ConversionResult res = ConvertUTF8toUTF16(&sourcestart, sourceend, &targetstart, targetend, strictConversion); |
|
| 632 |
+ if (res != conversionOK) |
|
| 633 |
+ {
|
|
| 634 |
+ delete [] widestringnative; |
|
| 635 |
+ throw std::exception(); |
|
| 636 |
+ } |
|
| 637 |
+ *targetstart = 0; |
|
| 638 |
+ std::wstring resultstring(widestringnative); |
|
| 639 |
+ delete [] widestringnative; |
|
| 640 |
+ return resultstring; |
|
| 641 |
+ } |
|
| 642 |
+ else if (sizeof(wchar_t) == 4) |
|
| 643 |
+ {
|
|
| 644 |
+ wchar_t* widestringnative = new wchar_t[widesize+1]; |
|
| 645 |
+ const UTF8* sourcestart = reinterpret_cast<const UTF8*>(utf8string.c_str()); |
|
| 646 |
+ const UTF8* sourceend = sourcestart + widesize; |
|
| 647 |
+ UTF32* targetstart = reinterpret_cast<UTF32*>(widestringnative); |
|
| 648 |
+ UTF32* targetend = targetstart + widesize; |
|
| 649 |
+ ConversionResult res = ConvertUTF8toUTF32(&sourcestart, sourceend, &targetstart, targetend, strictConversion); |
|
| 650 |
+ if (res != conversionOK) |
|
| 651 |
+ {
|
|
| 652 |
+ delete [] widestringnative; |
|
| 653 |
+ throw std::exception(); |
|
| 654 |
+ } |
|
| 655 |
+ *targetstart = 0; |
|
| 656 |
+ std::wstring resultstring(widestringnative); |
|
| 657 |
+ delete [] widestringnative; |
|
| 658 |
+ return resultstring; |
|
| 659 |
+ } |
|
| 660 |
+ else |
|
| 661 |
+ {
|
|
| 662 |
+ throw std::exception(); |
|
| 663 |
+ } |
|
| 664 |
+ return L""; |
|
| 665 |
+ } |
|
| 666 |
+ |
|
| 667 |
+ static std::string ToUtf8(const std::wstring& widestring) |
|
| 668 |
+ {
|
|
| 669 |
+ size_t widesize = widestring.length(); |
|
| 670 |
+ |
|
| 671 |
+ if (sizeof(wchar_t) == 2) |
|
| 672 |
+ {
|
|
| 673 |
+ size_t utf8size = 3 * widesize + 1; |
|
| 674 |
+ char* utf8stringnative = new char[utf8size]; |
|
| 675 |
+ const UTF16* sourcestart = reinterpret_cast<const UTF16*>(widestring.c_str()); |
|
| 676 |
+ const UTF16* sourceend = sourcestart + widesize; |
|
| 677 |
+ UTF8* targetstart = reinterpret_cast<UTF8*>(utf8stringnative); |
|
| 678 |
+ UTF8* targetend = targetstart + utf8size; |
|
| 679 |
+ ConversionResult res = ConvertUTF16toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion); |
|
| 680 |
+ if (res != conversionOK) |
|
| 681 |
+ {
|
|
| 682 |
+ delete [] utf8stringnative; |
|
| 683 |
+ throw std::exception(); |
|
| 684 |
+ } |
|
| 685 |
+ *targetstart = 0; |
|
| 686 |
+ std::string resultstring(utf8stringnative); |
|
| 687 |
+ delete [] utf8stringnative; |
|
| 688 |
+ return resultstring; |
|
| 689 |
+ } |
|
| 690 |
+ else if (sizeof(wchar_t) == 4) |
|
| 691 |
+ {
|
|
| 692 |
+ size_t utf8size = 4 * widesize + 1; |
|
| 693 |
+ char* utf8stringnative = new char[utf8size]; |
|
| 694 |
+ const UTF32* sourcestart = reinterpret_cast<const UTF32*>(widestring.c_str()); |
|
| 695 |
+ const UTF32* sourceend = sourcestart + widesize; |
|
| 696 |
+ UTF8* targetstart = reinterpret_cast<UTF8*>(utf8stringnative); |
|
| 697 |
+ UTF8* targetend = targetstart + utf8size; |
|
| 698 |
+ ConversionResult res = ConvertUTF32toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion); |
|
| 699 |
+ if (res != conversionOK) |
|
| 700 |
+ {
|
|
| 701 |
+ delete [] utf8stringnative; |
|
| 702 |
+ throw std::exception(); |
|
| 703 |
+ } |
|
| 704 |
+ *targetstart = 0; |
|
| 705 |
+ std::string resultstring(utf8stringnative); |
|
| 706 |
+ delete [] utf8stringnative; |
|
| 707 |
+ return resultstring; |
|
| 708 |
+ } |
|
| 709 |
+ else |
|
| 710 |
+ {
|
|
| 711 |
+ throw std::exception(); |
|
| 712 |
+ } |
|
| 713 |
+ return ""; |
|
| 714 |
+ } |
|
| 715 |
+}*/ |
|
| 716 |
+ |
|
| 717 |
+//convert a std::string to std::wstring |
|
| 718 |
+/*std::wstring mbstowcs(std::string str) |
|
| 719 |
+{
|
|
| 720 |
+ try {
|
|
| 721 |
+ return UtfConverter::FromUtf8(str); |
|
| 722 |
+ } catch(std::exception) {
|
|
| 723 |
+ return L"(failed UTF-8 conversion)"; |
|
| 724 |
+ } |
|
| 725 |
+}*/ |
|
| 726 |
+ |
|
| 727 |
+/*std::string wcstombs(std::wstring str) |
|
| 728 |
+{
|
|
| 729 |
+ return UtfConverter::ToUtf8(str); |
|
| 730 |
+}*/ |
|
| 731 |
+ |
|
| 732 |
+ |
|
| 733 |
+//TODO - dont we already have another function that can do this |
|
| 734 |
+/*std::string getExtension(const char* input) {
|
|
| 735 |
+ char buf[1024]; |
|
| 736 |
+ strcpy(buf,input); |
|
| 737 |
+ char* dot=strrchr(buf,'.'); |
|
| 738 |
+ if(!dot) |
|
| 739 |
+ return ""; |
|
| 740 |
+ char ext [512]; |
|
| 741 |
+ strcpy(ext, dot+1); |
|
| 742 |
+ int k, extlen=strlen(ext); |
|
| 743 |
+ for(k=0;k<extlen;k++) |
|
| 744 |
+ ext[k]=tolower(ext[k]); |
|
| 745 |
+ return ext; |
|
| 746 |
+}*/ |