| 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"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 19 | #include "xstring.h"cxx . . $ `<file>/src/common/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"cxx . . $ `<file>/src/compat/strdup.h`/ |
| 31 | #include "compat/strcasecmp.h"cxx . . $ `<file>/src/compat/strcasecmp.h`/ |
| 32 | #include "common/xalloc.h"cxx . . $ `<file>/src/common/xalloc.h`/ |
| 33 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 34 | |
| 35 | namespace pvpgncxx . . $ pvpgn/ |
| 36 | { |
| 37 | |
| 38 | extern char * strtolowercxx . . $ pvpgn/strtolower(7a9722d75b371431).(char * str) |
| 39 | { |
| 40 | unsigned int i; |
| 41 | unsigned char ch; |
| 42 | |
| 43 | if (!str) return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 44 | for (i = 0; (ch = str[i]); i++) { |
| 45 | if ((stdcxx . . $ std/::isuppercxx . . $ std/isupper(a1851578aea3f541).(ch))) str[i] = ch + ('a' - 'A'); |
| 46 | } |
| 47 | return str; |
| 48 | } |
| 49 | |
| 50 | extern unsigned char xtoicxx . . $ pvpgn/xtoi(50106d451684defa).(unsigned char ch) |
| 51 | { |
| 52 | unsigned char retval; |
| 53 | |
| 54 | if (stdcxx . . $ std/::isalphacxx . . $ std/isalpha(a1851578aea3f541).(ch)) retval = safe_tolowercxx . . $ `src/common/xstring.h:41:10`!(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_affixcxx . . $ pvpgn/str_strip_affix(b7b3a83273496486).(char * str, char const * affix) |
| 62 | { |
| 63 | unsigned int i, j, n; |
| 64 | int match; |
| 65 | |
| 66 | if (!str) return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 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 = stdcxx . . $ 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 | stdcxx . . $ std/::memmove(str, str + i, j - i + 1); |
| 93 | str[j - i + 1] = '\0'; |
| 94 | } |
| 95 | return str; |
| 96 | } |
| 97 | |
| 98 | extern char * hexstrdupcxx . . $ pvpgn/hexstrdup(978de2d1319386df).(unsigned char const * src) |
| 99 | { |
| 100 | char * dest; |
| 101 | int len; |
| 102 | |
| 103 | if (!src) return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 104 | dest = xstrdupcxx . . $ `src/common/xalloc.h:49:9`!((const char*)src); |
| 105 | len = hexstrtorawcxx . . $ pvpgn/hexstrtoraw(3560bc29fc3dba48).(src, dest, stdcxx . . $ std/::strlen(dest) + 1); |
| 106 | dest[len] = '\0'; |
| 107 | return dest; |
| 108 | } |
| 109 | |
| 110 | extern unsigned int hexstrtorawcxx . . $ pvpgn/hexstrtoraw(3560bc29fc3dba48).(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 (stdcxx . . $ std/::isxdigitcxx . . $ std/isxdigit(a1851578aea3f541).(src[i + 1])) { |
| 129 | if (stdcxx . . $ std/::isxdigitcxx . . $ std/isxdigit(a1851578aea3f541).(src[i + 2])) { |
| 130 | data[j++] = xtoicxx . . $ pvpgn/xtoi(50106d451684defa).(src[i + 1]) * 0x10 + xtoicxx . . $ pvpgn/xtoi(50106d451684defa).(src[i + 2]); |
| 131 | i += 2; |
| 132 | } |
| 133 | else { |
| 134 | data[j++] = xtoicxx . . $ pvpgn/xtoi(50106d451684defa).(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_COUNTcxx . . $ `src/common/xstring.cpp:175:9`! 32 |
| 176 | #define SPLIT_STRING_INCREASEMENTcxx . . $ `src/common/xstring.cpp:176:9`! 32 |
| 177 | extern char * * strtoargvcxx . . $ pvpgn/strtoargv(40c3717ab9e55081).(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 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 188 | temp = (char*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(std::strlen(str) + 1); |
| 189 | n = SPLIT_STRING_INIT_COUNTcxx . . $ `src/common/xstring.cpp:175:9`!; |
| 190 | pindex = (int*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(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_INCREASEMENTcxx . . $ `src/common/xstring.cpp:176:9`!; |
| 199 | pindex = (int *)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(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 | xfreecxx . . $ `src/common/xalloc.h:51:9`!(temp); |
| 227 | xfreecxx . . $ `src/common/xalloc.h:51:9`!(pindex); |
| 228 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 229 | } |
| 230 | result = (char*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(j + index_size); |
| 231 | stdcxx . . $ std/::memcpy(result + index_size, temp, j); |
| 232 | |
| 233 | ptrindex = (void**)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(*count * sizeof (char*)); |
| 234 | for (i = 0; i < *count; i++) { |
| 235 | ptrindex[i] = result + index_size + pindex[i]; |
| 236 | } |
| 237 | stdcxx . . $ std/::memcpy(result, ptrindex, index_size); |
| 238 | xfreecxx . . $ `src/common/xalloc.h:51:9`!(temp); |
| 239 | xfreecxx . . $ `src/common/xalloc.h:51:9`!(pindex); |
| 240 | xfreecxx . . $ `src/common/xalloc.h:51:9`!(ptrindex); |
| 241 | return (char * *)result; |
| 242 | } |
| 243 | |
| 244 | #define COMBINE_STRING_INIT_LENcxx . . $ `src/common/xstring.cpp:244:9`! 1024 |
| 245 | #define COMBINE_STRING_INCREASEMENTcxx . . $ `src/common/xstring.cpp:245:9`! 1024 |
| 246 | extern char * arraytostrcxx . . $ pvpgn/arraytostr(2633cd5eeccb56c6).(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 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 254 | |
| 255 | n = COMBINE_STRING_INIT_LENcxx . . $ `src/common/xstring.cpp:244:9`!; |
| 256 | result = (char*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(n); |
| 257 | result[0] = '\0'; |
| 258 | |
| 259 | need_delim = 0; |
| 260 | for (i = 0; i < count; i++) { |
| 261 | if (!array[i]) continue; |
| 262 | if (stdcxx . . $ std/::strlen(result) + stdcxx . . $ std/::strlen(array[i]) + stdcxx . . $ std/::strlen(delim) >= n) { |
| 263 | n += COMBINE_STRING_INCREASEMENTcxx . . $ `src/common/xstring.cpp:245:9`!; |
| 264 | result = (char*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(result, n); |
| 265 | } |
| 266 | if (need_delim) { |
| 267 | stdcxx . . $ std/::strcat(result, delim); |
| 268 | } |
| 269 | stdcxx . . $ std/::strcat(result, array[i]); |
| 270 | need_delim = 1; |
| 271 | } |
| 272 | result = (char*)xrealloccxx . . $ `src/common/xalloc.h:47:9`!(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_replacecxx . . $ pvpgn/str_replace(a1df38fec57fccd9).(char *orig, char *rep, char *with) |
| 279 | { |
| 280 | if (!orig) |
| 281 | return nullptr; |
| 282 | |
| 283 | if (!rep) |
| 284 | stdcxx . . $ std/::strcpy(rep, ""); |
| 285 | int len_rep = stdcxx . . $ std/::strlen(rep); |
| 286 | |
| 287 | if (!with) |
| 288 | stdcxx . . $ std/::strcpy(with, ""); |
| 289 | int len_with = stdcxx . . $ 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 = stdcxx . . $ 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*)xmalloccxx . . $ `src/common/xalloc.h:43:9`!(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 = stdcxx . . $ std/::strstr(orig, rep); |
| 318 | len_front = ins - orig; |
| 319 | tmp = stdcxx . . $ std/::strncpy(tmp, orig, len_front) + len_front; |
| 320 | tmp = stdcxx . . $ std/::strcpy(tmp, with) + len_with; |
| 321 | orig += len_front + len_rep; // move to next "end of rep" |
| 322 | } |
| 323 | stdcxx . . $ std/::strcpy(tmp, orig); |
| 324 | return result; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /* Replace "\n" in string to a new line character '\n' */ |
| 329 | extern stdcxx . . $ std/::stringcxx . . $ std/string# str_replace_nlcxx . . $ pvpgn/str_replace_nl(d06bf7adff8c4caf).(char const * text) |
| 330 | { |
| 331 | stdcxx . . $ std/::stringcxx . . $ std/string# scxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).(text); |
| 332 | size_t pos = 0; |
| 333 | while ((pos = s.findcxx . . $ std/__cxx11/basic_string#find(b733e800f6f8fb8b).("\\n", pos)) != stdcxx . . $ std/::stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos.) { |
| 334 | s.replacecxx . . $ std/__cxx11/basic_string#replace(5a2f4129e55e86e6).(pos, 2, "\n"); |
| 335 | } |
| 336 | return scxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | // search substring in input string |
| 341 | // (case insensitive) |
| 342 | extern bool find_substrcxx . . $ pvpgn/find_substr(9efbdc942cde3d3d).(char * input, const char * find) |
| 343 | { |
| 344 | stdcxx . . $ std/::stringcxx . . $ std/string# str1cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).(input); |
| 345 | stdcxx . . $ std/::stringcxx . . $ std/string# str2cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).(find); |
| 346 | |
| 347 | return stdcxx . . $ std/::lexicographical_comparecxx . . $ std/lexicographical_compare(4e72d593e94d210f).( |
| 348 | str1.begincxx . . $ std/__cxx11/basic_string#begin(417acd00ce8503f4).(), str1.endcxx . . $ std/__cxx11/basic_string#end(417acd00ce8503f4).(), |
| 349 | str2.begincxx . . $ std/__cxx11/basic_string#begin(417acd00ce8503f4).(), str2.endcxx . . $ std/__cxx11/basic_string#end(417acd00ce8503f4).(), |
| 350 | [](char ch1, char ch2) { return stdcxx . . $ std/::touppercxx . . $ std/toupper(a1851578aea3f541).(ch1) == stdcxx . . $ std/::touppercxx . . $ std/toupper(a1851578aea3f541).(ch2); } |
| 351 | ); |
| 352 | } |
| 353 | } |