| 1 | /* |
| 2 | * Copyright (C) 2000,2001 Onlyer ([email protected]) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #include "common/setup_before.h" |
| 19 | #include "xstring.h" |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <cstdio> |
| 23 | #include <cctype> |
| 24 | #include <cstring> |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | #include <sstream> |
| 28 | #include <iomanip> |
| 29 | |
| 30 | #include "compat/strdup.h" |
| 31 | #include "compat/strcasecmp.h" |
| 32 | #include "common/xalloc.h" |
| 33 | #include "common/setup_after.h" |
| 34 | |
| 35 | namespace pvpgn |
| 36 | { |
| 37 | |
| 38 | extern char * strtolower(char * str) |
| 39 | { |
| 40 | unsigned int i; |
| 41 | unsigned char ch; |
| 42 | |
| 43 | if (!str) return NULL; |
| 44 | for (i = 0; (ch = str[i]); i++) { |
| 45 | if ((std::isupper(ch))) str[i] = ch + ('a' - 'A'); |
| 46 | } |
| 47 | return str; |
| 48 | } |
| 49 | |
| 50 | extern unsigned char xtoi(unsigned char ch) |
| 51 | { |
| 52 | unsigned char retval; |
| 53 | |
| 54 | if (std::isalpha(ch)) retval = safe_tolower(ch); |
| 55 | else retval = ch; |
| 56 | if (retval < 'A') retval -= ('0' - 0); |
| 57 | else retval -= ('a' - 0xa); |
| 58 | return retval; |
| 59 | } |
| 60 | |
| 61 | extern char * str_strip_affix(char * str, char const * affix) |
| 62 | { |
| 63 | unsigned int i, j, n; |
| 64 | int match; |
| 65 | |
| 66 | if (!str) return NULL; |
| 67 | if (!affix) return str; |
| 68 | for (i = 0; str[i]; i++) { |
| 69 | match = 0; |
| 70 | for (n = 0; affix[n]; n++) { |
| 71 | if (str[i] == affix[n]) { |
| 72 | match = 1; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | if (!match) break; |
| 77 | } |
| 78 | for (j = std::strlen(str) - 1; j >= i; j--) { |
| 79 | match = 0; |
| 80 | for (n = 0; affix[n]; n++) { |
| 81 | if (str[j] == affix[n]) { |
| 82 | match = 1; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | if (!match) break; |
| 87 | } |
| 88 | if (i > j) { |
| 89 | str[0] = '\0'; |
| 90 | } |
| 91 | else { |
| 92 | std::memmove(str, str + i, j - i + 1); |
| 93 | str[j - i + 1] = '\0'; |
| 94 | } |
| 95 | return str; |
| 96 | } |
| 97 | |
| 98 | extern char * hexstrdup(unsigned char const * src) |
| 99 | { |
| 100 | char * dest; |
| 101 | int len; |
| 102 | |
| 103 | if (!src) return NULL; |
| 104 | dest = xstrdup((const char*)src); |
| 105 | len = hexstrtoraw(src, dest, std::strlen(dest) + 1); |
| 106 | dest[len] = '\0'; |
| 107 | return dest; |
| 108 | } |
| 109 | |
| 110 | extern unsigned int hexstrtoraw(unsigned char const * src, char * data, unsigned int datalen) |
| 111 | { |
| 112 | unsigned char ch; |
| 113 | unsigned int i, j; |
| 114 | |
| 115 | for (i = 0, j = 0; j < datalen; i++) { |
| 116 | ch = src[i]; |
| 117 | if (!ch) break; |
| 118 | if (ch == '\\') { |
| 119 | i++; |
| 120 | ch = src[i]; |
| 121 | if (!ch) { |
| 122 | break; |
| 123 | } |
| 124 | else if (ch == '\\') { |
| 125 | data[j++] = ch; |
| 126 | } |
| 127 | else if (ch == 'x') { |
| 128 | if (std::isxdigit(src[i + 1])) { |
| 129 | if (std::isxdigit(src[i + 2])) { |
| 130 | data[j++] = xtoi(src[i + 1]) * 0x10 + xtoi(src[i + 2]); |
| 131 | i += 2; |
| 132 | } |
| 133 | else { |
| 134 | data[j++] = xtoi(src[i + 1]); |
| 135 | i++; |
| 136 | } |
| 137 | } |
| 138 | else { |
| 139 | data[j++] = ch; |
| 140 | } |
| 141 | } |
| 142 | else if (ch == 'n') { |
| 143 | data[j++] = '\n'; |
| 144 | } |
| 145 | else if (ch == 'r') { |
| 146 | data[j++] = '\r'; |
| 147 | } |
| 148 | else if (ch == 'a') { |
| 149 | data[j++] = '\a'; |
| 150 | } |
| 151 | else if (ch == 't') { |
| 152 | data[j++] = '\t'; |
| 153 | } |
| 154 | else if (ch == 'b') { |
| 155 | data[j++] = '\b'; |
| 156 | } |
| 157 | else if (ch == 'f') { |
| 158 | data[j++] = '\f'; |
| 159 | } |
| 160 | else if (ch == 'v') { |
| 161 | data[j++] = '\v'; |
| 162 | } |
| 163 | else { |
| 164 | data[j++] = ch; |
| 165 | } |
| 166 | } |
| 167 | else { |
| 168 | data[j++] = ch; |
| 169 | continue; |
| 170 | } |
| 171 | } |
| 172 | return j; |
| 173 | } |
| 174 | |
| 175 | #define SPLIT_STRING_INIT_COUNT 32 |
| 176 | #define SPLIT_STRING_INCREASEMENT 32 |
| 177 | extern char * * strtoargv(char const * str, unsigned int * count) |
| 178 | { |
| 179 | unsigned int n, index_size; |
| 180 | char * temp; |
| 181 | unsigned int i; |
| 182 | int j; |
| 183 | int * pindex; |
| 184 | void ** ptrindex; |
| 185 | char * result; |
| 186 | |
| 187 | if (!str || !count) return NULL; |
| 188 | temp = (char*)xmalloc(std::strlen(str) + 1); |
| 189 | n = SPLIT_STRING_INIT_COUNT; |
| 190 | pindex = (int*)xmalloc(n * sizeof (int)); |
| 191 | |
| 192 | i = j = 0; |
| 193 | *count = 0; |
| 194 | while (str[i]) { |
| 195 | while (str[i] == ' ' || str[i] == '\t') i++; |
| 196 | if (!str[i]) break; |
| 197 | if (*count >= n) { |
| 198 | n += SPLIT_STRING_INCREASEMENT; |
| 199 | pindex = (int *)xrealloc(pindex, n * sizeof(int)); |
| 200 | } |
| 201 | pindex[*count] = j; |
| 202 | (*count)++; |
| 203 | if (str[i] == '"') { |
| 204 | i++; |
| 205 | while (str[i]) { |
| 206 | if (str[i] == '\\') { |
| 207 | i++; |
| 208 | if (!str[i]) break; |
| 209 | } |
| 210 | else if (str[i] == '"') { |
| 211 | i++; |
| 212 | break; |
| 213 | } |
| 214 | temp[j++] = str[i++]; |
| 215 | } |
| 216 | } |
| 217 | else { |
| 218 | while (str[i] && str[i] != ' ' && str[i] != '\t') { |
| 219 | temp[j++] = str[i++]; |
| 220 | } |
| 221 | } |
| 222 | temp[j++] = '\0'; |
| 223 | } |
| 224 | index_size = *count * sizeof(char *); |
| 225 | if (!index_size) { |
| 226 | xfree(temp); |
| 227 | xfree(pindex); |
| 228 | return NULL; |
| 229 | } |
| 230 | result = (char*)xmalloc(j + index_size); |
| 231 | std::memcpy(result + index_size, temp, j); |
| 232 | |
| 233 | ptrindex = (void**)xmalloc(*count * sizeof (char*)); |
| 234 | for (i = 0; i < *count; i++) { |
| 235 | ptrindex[i] = result + index_size + pindex[i]; |
| 236 | } |
| 237 | std::memcpy(result, ptrindex, index_size); |
| 238 | xfree(temp); |
| 239 | xfree(pindex); |
| 240 | xfree(ptrindex); |
| 241 | return (char * *)result; |
| 242 | } |
| 243 | |
| 244 | #define COMBINE_STRING_INIT_LEN 1024 |
| 245 | #define COMBINE_STRING_INCREASEMENT 1024 |
| 246 | extern char * arraytostr(char * * array, char const * delim, int count) |
| 247 | { |
| 248 | int i; |
| 249 | unsigned int n; |
| 250 | char * result; |
| 251 | int need_delim; |
| 252 | |
| 253 | if (!delim || !array) return NULL; |
| 254 | |
| 255 | n = COMBINE_STRING_INIT_LEN; |
| 256 | result = (char*)xmalloc(n); |
| 257 | result[0] = '\0'; |
| 258 | |
| 259 | need_delim = 0; |
| 260 | for (i = 0; i < count; i++) { |
| 261 | if (!array[i]) continue; |
| 262 | if (std::strlen(result) + std::strlen(array[i]) + std::strlen(delim) >= n) { |
| 263 | n += COMBINE_STRING_INCREASEMENT; |
| 264 | result = (char*)xrealloc(result, n); |
| 265 | } |
| 266 | if (need_delim) { |
| 267 | std::strcat(result, delim); |
| 268 | } |
| 269 | std::strcat(result, array[i]); |
| 270 | need_delim = 1; |
| 271 | } |
| 272 | result = (char*)xrealloc(result, std::strlen(result) + 1); |
| 273 | return result; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | // You must free the result if result is non-NULL. |
| 278 | extern char *str_replace(char *orig, char *rep, char *with) |
| 279 | { |
| 280 | if (!orig) |
| 281 | return nullptr; |
| 282 | |
| 283 | if (!rep) |
| 284 | std::strcpy(rep, ""); |
| 285 | int len_rep = std::strlen(rep); |
| 286 | |
| 287 | if (!with) |
| 288 | std::strcpy(with, ""); |
| 289 | int len_with = std::strlen(with); |
| 290 | |
| 291 | // number of replacements |
| 292 | int count = 0; |
| 293 | |
| 294 | // next insert point |
| 295 | char *ins = orig; |
| 296 | char *tmp = nullptr; |
| 297 | for (count = 0; tmp = std::strstr(ins, rep); ++count) |
| 298 | ins = tmp + len_rep; |
| 299 | |
| 300 | // the return string |
| 301 | char *result = nullptr; |
| 302 | |
| 303 | // first time through the loop, all the variable are set correctly |
| 304 | // from here on, |
| 305 | // tmp points to the end of the result string |
| 306 | // ins points to the next occurrence of rep in orig |
| 307 | // orig points to the remainder of orig after "end of rep" |
| 308 | tmp = result = (char*)xmalloc(std::strlen(orig) + (len_with - len_rep) * count + 1); |
| 309 | |
| 310 | if (!result) |
| 311 | return nullptr; |
| 312 | |
| 313 | // distance between rep and end of last rep |
| 314 | int len_front = 0; |
| 315 | while (count--) |
| 316 | { |
| 317 | ins = std::strstr(orig, rep); |
| 318 | len_front = ins - orig; |
| 319 | tmp = std::strncpy(tmp, orig, len_front) + len_front; |
| 320 | tmp = std::strcpy(tmp, with) + len_with; |
| 321 | orig += len_front + len_rep; // move to next "end of rep" |
| 322 | } |
| 323 | std::strcpy(tmp, orig); |
| 324 | return result; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /* Replace "\n" in string to a new line character '\n' */ |
| 329 | extern std::string str_replace_nl(char const * text) |
| 330 | { |
| 331 | std::string s(text); |
| 332 | size_t pos = 0; |
| 333 | while ((pos = s.find("\\n", pos)) != std::string::npos) { |
| 334 | s.replace(pos, 2, "\n"); |
| 335 | } |
| 336 | return s; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | // search substring in input string |
| 341 | // (case insensitive) |
| 342 | extern bool find_substr(char * input, const char * find) |
| 343 | { |
| 344 | std::string str1(input); |
| 345 | std::string str2(find); |
| 346 | |
| 347 | return std::lexicographical_compare( |
| 348 | str1.begin(), str1.end(), |
| 349 | str2.begin(), str2.end(), |
| 350 | [](char ch1, char ch2) { return std::toupper(ch1) == std::toupper(ch2); } |
| 351 | ); |
| 352 | } |
| 353 | } |