| 1 | /* |
| 2 | * Copyright (C) 1998 Mark Baysinger ([email protected]) |
| 3 | * Copyright (C) 1998,1999,2000,2001 Ross Combs ([email protected]) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (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 Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | #include "common/setup_before.h" |
| 20 | #include "common/util.h" |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <cstdlib> |
| 24 | #include <cctype> |
| 25 | #include <cstdio> |
| 26 | #include <string> |
| 27 | |
| 28 | #include "compat/strcasecmp.h" |
| 29 | #include "compat/strncasecmp.h" |
| 30 | #include "common/xalloc.h" |
| 31 | #include "common/setup_after.h" |
| 32 | |
| 33 | |
| 34 | namespace pvpgn |
| 35 | { |
| 36 | |
| 37 | extern int strstart(char const * full, char const * part) |
| 38 | { |
| 39 | std::size_t strlen_part; |
| 40 | int compare_result; |
| 41 | |
| 42 | if (!full || !part) |
| 43 | return 1; |
| 44 | |
| 45 | strlen_part = std::strlen(part); |
| 46 | compare_result = strncasecmp(full, part, strlen_part); |
| 47 | |
| 48 | /* If there is more than the command, make sure it is separated */ |
| 49 | if (compare_result != 0) |
| 50 | return compare_result; |
| 51 | else if (full[strlen_part] != ' ' && full[strlen_part] != '\0') |
| 52 | return 1; |
| 53 | else return compare_result; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | #define DEF_LEN 64 |
| 58 | #define INC_LEN 16 |
| 59 | |
| 60 | extern char * file_get_line(std::FILE * fp) |
| 61 | { |
| 62 | static char * line = NULL; |
| 63 | static unsigned int len = 0; |
| 64 | unsigned int pos = 0; |
| 65 | int prev_char, curr_char; |
| 66 | |
| 67 | // use file_get_line with NULL argument to clear the buffer |
| 68 | if (!(fp)) |
| 69 | { |
| 70 | len = 0; |
| 71 | if ((line)) |
| 72 | xfree((void *)line); |
| 73 | line = NULL; |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | if (!(line)) |
| 78 | { |
| 79 | line = (char*)xmalloc(DEF_LEN); |
| 80 | len = DEF_LEN; |
| 81 | } |
| 82 | |
| 83 | prev_char = '\0'; |
| 84 | while ((curr_char = std::fgetc(fp)) != EOF) |
| 85 | { |
| 86 | if (((char)curr_char) == '\r') |
| 87 | continue; /* make DOS line endings look Unix-like */ |
| 88 | if (((char)curr_char) == '\n') |
| 89 | { |
| 90 | if (pos < 1 || ((char)prev_char) != '\\') |
| 91 | break; |
| 92 | pos--; /* throw away the backslash */ |
| 93 | prev_char = '\0'; |
| 94 | continue; |
| 95 | } |
| 96 | prev_char = curr_char; |
| 97 | |
| 98 | line[pos++] = (char)curr_char; |
| 99 | if ((pos + 1) >= len) |
| 100 | { |
| 101 | len += INC_LEN; |
| 102 | line = (char*)xrealloc(line, len); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (curr_char == EOF && pos < 1) /* not even an empty line */ |
| 107 | { |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | line[pos] = '\0'; |
| 112 | |
| 113 | return line; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | extern char * strreverse(char * str) |
| 118 | { |
| 119 | std::reverse(str, str + std::strlen(str)); |
| 120 | return str; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | extern int str_to_uint(char const * str, unsigned int * num) |
| 125 | { |
| 126 | unsigned int val; |
| 127 | unsigned int pval; |
| 128 | char * pos; |
| 129 | |
| 130 | if (!str || !num) |
| 131 | return -1; |
| 132 | for (pos = (char *)str; *pos == ' ' || *pos == '\t'; pos++); |
| 133 | if (*pos == '+') |
| 134 | pos++; |
| 135 | |
| 136 | val = 0; |
| 137 | for (; *pos != '\0'; pos++) |
| 138 | { |
| 139 | pval = val; |
| 140 | val *= 10; |
| 141 | if (val / 10 != pval) /* check for overflow */ |
| 142 | return -1; |
| 143 | |
| 144 | pval = val; |
| 145 | if (std::isdigit(*pos)) |
| 146 | val += *pos - '0'; |
| 147 | else |
| 148 | return -1; |
| 149 | |
| 150 | if (val < pval) /* check for overflow */ |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | *num = val; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | extern int str_to_ushort(char const * str, unsigned short * num) |
| 160 | { |
| 161 | unsigned short val; |
| 162 | unsigned short pval; |
| 163 | char * pos; |
| 164 | |
| 165 | if (!str || !num) |
| 166 | return -1; |
| 167 | for (pos = (char *)str; *pos == ' ' || *pos == '\t'; pos++); |
| 168 | if (*pos == '+') |
| 169 | pos++; |
| 170 | |
| 171 | val = 0; |
| 172 | for (; *pos != '\0'; pos++) |
| 173 | { |
| 174 | pval = val; |
| 175 | val *= 10; |
| 176 | if (val / 10 != pval) /* check for overflow */ |
| 177 | return -1; |
| 178 | |
| 179 | pval = val; |
| 180 | |
| 181 | if (std::isdigit(*pos)) |
| 182 | val += *pos - '0'; |
| 183 | else |
| 184 | return -1; |
| 185 | |
| 186 | if (val < pval) /* check for overflow */ |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | *num = val; |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /* This routine assumes ASCII like control chars. |
| 196 | If len is zero, it will print all characters up to the first NUL, |
| 197 | otherwise it will print exactly that many characters. */ |
| 198 | int str_print_term(std::FILE * fp, char const * str, unsigned int len, int allow_nl) |
| 199 | { |
| 200 | unsigned int i; |
| 201 | |
| 202 | if (!fp) |
| 203 | return -1; |
| 204 | if (!str) |
| 205 | return -1; |
| 206 | |
| 207 | if (len == 0) |
| 208 | len = std::strlen(str); |
| 209 | for (i = 0; i < len; i++) |
| 210 | if ((str[i] == '\177' || (str[i] >= '\000' && str[i] < '\040')) && |
| 211 | (!allow_nl || (str[i] != '\r' && str[i] != '\n'))) |
| 212 | std::fprintf(fp, "^%c", str[i] + 64); |
| 213 | else |
| 214 | std::fputc((int)str[i], fp); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | extern int str_get_bool(char const * str) |
| 221 | { |
| 222 | if (!str) |
| 223 | return -1; |
| 224 | |
| 225 | if (strcasecmp(str, "true") == 0 || |
| 226 | strcasecmp(str, "yes") == 0 || |
| 227 | strcasecmp(str, "on") == 0 || |
| 228 | std::strcmp(str, "1") == 0) |
| 229 | return 1; |
| 230 | |
| 231 | if (strcasecmp(str, "false") == 0 || |
| 232 | strcasecmp(str, "no") == 0 || |
| 233 | strcasecmp(str, "off") == 0 || |
| 234 | std::strcmp(str, "0") == 0) |
| 235 | return 0; |
| 236 | |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | extern char const * seconds_to_timestr(unsigned int totsecs) |
| 242 | { |
| 243 | static char temp[256]; |
| 244 | int days; |
| 245 | int hours; |
| 246 | int minutes; |
| 247 | int seconds; |
| 248 | |
| 249 | days = totsecs / (24 * 60 * 60); |
| 250 | hours = (totsecs / (60 * 60)) % 24; |
| 251 | minutes = (totsecs / 60) % 60; |
| 252 | seconds = totsecs % 60; |
| 253 | |
| 254 | if (days > 0) |
| 255 | std::sprintf(temp, "%d day%s %d hour%s %d minute%s %d second%s", |
| 256 | days, days == 1 ? "" : "s", |
| 257 | hours, hours == 1 ? "" : "s", |
| 258 | minutes, minutes == 1 ? "" : "s", |
| 259 | seconds, seconds == 1 ? "" : "s"); |
| 260 | else if (hours > 0) |
| 261 | std::sprintf(temp, "%d hour%s %d minute%s %d second%s", |
| 262 | hours, hours == 1 ? "" : "s", |
| 263 | minutes, minutes == 1 ? "" : "s", |
| 264 | seconds, seconds == 1 ? "" : "s"); |
| 265 | else if (minutes > 0) |
| 266 | std::sprintf(temp, "%d minute%s %d second%s", |
| 267 | minutes, minutes == 1 ? "" : "s", |
| 268 | seconds, seconds == 1 ? "" : "s"); |
| 269 | else |
| 270 | std::sprintf(temp, "%d second%s.", |
| 271 | seconds, seconds == 1 ? "" : "s"); |
| 272 | |
| 273 | return temp; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | extern int clockstr_to_seconds(char const * clockstr, unsigned int * totsecs) |
| 278 | { |
| 279 | unsigned int i, j; |
| 280 | unsigned int temp; |
| 281 | |
| 282 | if (!clockstr) |
| 283 | return -1; |
| 284 | if (!totsecs) |
| 285 | return -1; |
| 286 | |
| 287 | for (i = j = temp = 0; j < std::strlen(clockstr); j++) |
| 288 | { |
| 289 | switch (clockstr[j]) |
| 290 | { |
| 291 | case ':': |
| 292 | temp *= 60; |
| 293 | temp += std::strtoul(&clockstr[i], NULL, 10); |
| 294 | i = j + 1; |
| 295 | break; |
| 296 | case '0': |
| 297 | case '1': |
| 298 | case '2': |
| 299 | case '3': |
| 300 | case '4': |
| 301 | case '5': |
| 302 | case '6': |
| 303 | case '7': |
| 304 | case '8': |
| 305 | case '9': |
| 306 | break; |
| 307 | default: |
| 308 | return -1; |
| 309 | } |
| 310 | } |
| 311 | if (i < j) |
| 312 | { |
| 313 | temp *= 60; |
| 314 | temp += std::strtoul(&clockstr[i], NULL, 10); |
| 315 | } |
| 316 | |
| 317 | *totsecs = temp; |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | extern char * escape_fs_chars(char const * in, unsigned int len) |
| 323 | { |
| 324 | char * out; |
| 325 | unsigned int inpos; |
| 326 | unsigned int outpos; |
| 327 | |
| 328 | if (!in) |
| 329 | return NULL; |
| 330 | out = (char*)xmalloc(len * 3 + 1); /* if all turn into %XX */ |
| 331 | |
| 332 | for (inpos = 0, outpos = 0; inpos < len; inpos++) |
| 333 | { |
| 334 | if (in[inpos] == '\0' || in[inpos] == '%' || in[inpos] == '/' || |
| 335 | in[inpos] == '\\' || in[inpos] == ':') /* FIXME: what other characters does Windows not allow? */ |
| 336 | { |
| 337 | out[outpos++] = '%'; |
| 338 | /* always 00 through FF hex */ |
| 339 | std::sprintf(&out[outpos], "%02X", (unsigned int)(unsigned char)in[inpos]); |
| 340 | outpos += 2; |
| 341 | } |
| 342 | else |
| 343 | out[outpos++] = in[inpos]; |
| 344 | } |
| 345 | /* if outpos >= len*3+1 then the buffer was overflowed */ |
| 346 | out[outpos] = '\0'; |
| 347 | |
| 348 | return out; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | extern char * escape_chars(char const * in, unsigned int len) |
| 353 | { |
| 354 | char * out; |
| 355 | unsigned int inpos; |
| 356 | unsigned int outpos; |
| 357 | |
| 358 | if (!in) |
| 359 | return NULL; |
| 360 | out = (char*)xmalloc(len * 4 + 1); /* if all turn into \xxx */ |
| 361 | |
| 362 | for (inpos = 0, outpos = 0; inpos < len; inpos++) |
| 363 | { |
| 364 | if (in[inpos] == '\\') |
| 365 | { |
| 366 | out[outpos++] = '\\'; |
| 367 | out[outpos++] = '\\'; |
| 368 | } |
| 369 | else if (in[inpos] == '"') |
| 370 | { |
| 371 | out[outpos++] = '\\'; |
| 372 | out[outpos++] = '"'; |
| 373 | } |
| 374 | else if (std::isprint((unsigned char)in[inpos])) |
| 375 | out[outpos++] = in[inpos]; |
| 376 | else if (in[inpos] == '\a') |
| 377 | { |
| 378 | out[outpos++] = '\\'; |
| 379 | out[outpos++] = 'a'; |
| 380 | } |
| 381 | else if (in[inpos] == '\b') |
| 382 | { |
| 383 | out[outpos++] = '\\'; |
| 384 | out[outpos++] = 'b'; |
| 385 | } |
| 386 | else if (in[inpos] == '\t') |
| 387 | { |
| 388 | out[outpos++] = '\\'; |
| 389 | out[outpos++] = 't'; |
| 390 | } |
| 391 | else if (in[inpos] == '\n') |
| 392 | { |
| 393 | out[outpos++] = '\\'; |
| 394 | out[outpos++] = 'n'; |
| 395 | } |
| 396 | else if (in[inpos] == '\v') |
| 397 | { |
| 398 | out[outpos++] = '\\'; |
| 399 | out[outpos++] = 'v'; |
| 400 | } |
| 401 | else if (in[inpos] == '\f') |
| 402 | { |
| 403 | out[outpos++] = '\\'; |
| 404 | out[outpos++] = 'f'; |
| 405 | } |
| 406 | else if (in[inpos] == '\r') |
| 407 | { |
| 408 | out[outpos++] = '\\'; |
| 409 | out[outpos++] = 'r'; |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | out[outpos++] = '\\'; |
| 414 | /* always 001 through 377 octal */ |
| 415 | std::sprintf(&out[outpos], "%03o", (unsigned int)(unsigned char)in[inpos]); |
| 416 | outpos += 3; |
| 417 | } |
| 418 | } |
| 419 | /* if outpos >= len*4+1 then the buffer was overflowed */ |
| 420 | out[outpos] = '\0'; |
| 421 | |
| 422 | return out; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | extern char * unescape_chars(char const * in) |
| 427 | { |
| 428 | char * out; |
| 429 | unsigned int inpos; |
| 430 | unsigned int outpos; |
| 431 | unsigned int inlen; |
| 432 | |
| 433 | if (!in) |
| 434 | return NULL; |
| 435 | |
| 436 | inlen = std::strlen(in); |
| 437 | out = (char*)xmalloc(inlen + 1); |
| 438 | |
| 439 | for (inpos = 0, outpos = 0; inpos < inlen; inpos++) |
| 440 | { |
| 441 | if (in[inpos] != '\\') |
| 442 | out[outpos++] = in[inpos]; |
| 443 | else |
| 444 | switch (in[++inpos]) |
| 445 | { |
| 446 | case '\\': |
| 447 | out[outpos++] = '\\'; |
| 448 | break; |
| 449 | case '"': |
| 450 | out[outpos++] = '"'; |
| 451 | break; |
| 452 | case 'a': |
| 453 | out[outpos++] = '\a'; |
| 454 | break; |
| 455 | case 'b': |
| 456 | out[outpos++] = '\b'; |
| 457 | break; |
| 458 | case 't': |
| 459 | out[outpos++] = '\t'; |
| 460 | break; |
| 461 | case 'n': |
| 462 | out[outpos++] = '\n'; |
| 463 | break; |
| 464 | case 'v': |
| 465 | out[outpos++] = '\v'; |
| 466 | break; |
| 467 | case 'f': |
| 468 | out[outpos++] = '\f'; |
| 469 | break; |
| 470 | case 'r': |
| 471 | out[outpos++] = '\r'; |
| 472 | break; |
| 473 | default: |
| 474 | { |
| 475 | char temp[4]; |
| 476 | unsigned int i; |
| 477 | unsigned int num; |
| 478 | |
| 479 | for (i = 0; i < 3; i++) |
| 480 | { |
| 481 | if (in[inpos] != '0' && |
| 482 | in[inpos] != '1' && |
| 483 | in[inpos] != '2' && |
| 484 | in[inpos] != '3' && |
| 485 | in[inpos] != '4' && |
| 486 | in[inpos] != '5' && |
| 487 | in[inpos] != '6' && |
| 488 | in[inpos] != '7') |
| 489 | break; |
| 490 | temp[i] = in[inpos++]; |
| 491 | } |
| 492 | temp[i] = '\0'; |
| 493 | inpos--; |
| 494 | |
| 495 | num = std::strtoul(temp, NULL, 8); |
| 496 | if (i < 3 || num<1 || num>255) /* bad escape (including \000), leave it as-is */ |
| 497 | { |
| 498 | out[outpos++] = '\\'; |
| 499 | std::strcpy(&out[outpos], temp); |
| 500 | outpos += std::strlen(temp); |
| 501 | } |
| 502 | else |
| 503 | out[outpos++] = (unsigned char)num; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | out[outpos] = '\0'; |
| 508 | |
| 509 | return out; |
| 510 | } |
| 511 | |
| 512 | |
| 513 | extern void str_to_hex(char * target, char const * data, int datalen) |
| 514 | { |
| 515 | unsigned char c; |
| 516 | int i; |
| 517 | for (i = 0; i < datalen; i++) |
| 518 | { |
| 519 | c = (data[i]) & 0xff; |
| 520 | |
| 521 | std::sprintf(target + i * 3, "%02X ", c); |
| 522 | target[i * 3 + 3] = '\0'; |
| 523 | |
| 524 | /* std::fprintf(stderr, "str_to_hex %d | '%02x' '%s'\n", i, c, target); */ |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | |
| 529 | extern int hex_to_str(char const * source, char * data, int datalen) |
| 530 | { |
| 531 | /* |
| 532 | * TODO: We really need a more robust function here, |
| 533 | * for now, I'll just use this hack for a quick evaluation |
| 534 | */ |
| 535 | char byte; |
| 536 | char c; |
| 537 | int i; |
| 538 | |
| 539 | for (i = 0; i < datalen; i++) |
| 540 | { |
| 541 | byte = 0; |
| 542 | |
| 543 | /* std::fprintf(stderr, "hex_to_str %d | '%02x'", i, byte); */ |
| 544 | |
| 545 | c = source[i * 3 + 0]; |
| 546 | byte += 16 * (c > '9' ? (c - 'A' + 10) : (c - '0')); |
| 547 | |
| 548 | /* std::fprintf(stderr, " | '%c' '%02x'", c, byte); */ |
| 549 | |
| 550 | c = source[i * 3 + 1]; |
| 551 | byte += 1 * (c > '9' ? (c - 'A' + 10) : (c - '0')); |
| 552 | |
| 553 | /* std::fprintf(stderr, " | '%c' '%02x'", c, byte); */ |
| 554 | |
| 555 | /* std::fprintf(stderr, "\n"); */ |
| 556 | |
| 557 | data[i] = byte; |
| 558 | } |
| 559 | |
| 560 | /* std::fprintf(stderr, "finished, returning %d from '%s'\n", i, source); */ |
| 561 | |
| 562 | return i; |
| 563 | } |
| 564 | |
| 565 | /* convert a time string to time_t |
| 566 | time string format is: |
| 567 | yyyy/mm/dd or yyyy-mm-dd or yyyy.mm.dd |
| 568 | hh:mm:ss |
| 569 | */ |
| 570 | extern int timestr_to_time(char const * timestr, std::time_t* ptime) |
| 571 | { |
| 572 | char const * p; |
| 573 | char ch; |
| 574 | struct std::tm when; |
| 575 | int day_s, time_s, last; |
| 576 | |
| 577 | if (!timestr) return -1; |
| 578 | if (!timestr[0]) { |
| 579 | *ptime = 0; |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | p = timestr; |
| 584 | day_s = time_s = 0; |
| 585 | last = 0; |
| 586 | std::memset(&when, 0, sizeof(when)); |
| 587 | when.tm_mday = 1; |
| 588 | when.tm_isdst = -1; |
| 589 | while (1) { |
| 590 | ch = *timestr; |
| 591 | timestr++; |
| 592 | switch (ch) { |
| 593 | case '/': |
| 594 | case '-': |
| 595 | case '.': |
| 596 | if (day_s == 0) { |
| 597 | when.tm_year = std::atoi(p) - 1900; |
| 598 | } |
| 599 | else if (day_s == 1) { |
| 600 | when.tm_mon = std::atoi(p) - 1; |
| 601 | } |
| 602 | else if (day_s == 2) { |
| 603 | when.tm_mday = std::atoi(p); |
| 604 | } |
| 605 | time_s = 0; |
| 606 | day_s++; |
| 607 | p = timestr; |
| 608 | last = 1; |
| 609 | break; |
| 610 | case ':': |
| 611 | if (time_s == 0) { |
| 612 | when.tm_hour = std::atoi(p); |
| 613 | } |
| 614 | else if (time_s == 1) { |
| 615 | when.tm_min = std::atoi(p); |
| 616 | } |
| 617 | else if (time_s == 2) { |
| 618 | when.tm_sec = std::atoi(p); |
| 619 | } |
| 620 | day_s = 0; |
| 621 | time_s++; |
| 622 | p = timestr; |
| 623 | last = 2; |
| 624 | break; |
| 625 | case ' ': |
| 626 | case '\t': |
| 627 | case '\x0': |
| 628 | if (last == 1) { |
| 629 | if (day_s == 0) { |
| 630 | when.tm_year = std::atoi(p) - 1900; |
| 631 | } |
| 632 | else if (day_s == 1) { |
| 633 | when.tm_mon = std::atoi(p) - 1; |
| 634 | } |
| 635 | else if (day_s == 2) { |
| 636 | when.tm_mday = std::atoi(p); |
| 637 | } |
| 638 | } |
| 639 | else if (last == 2) { |
| 640 | if (time_s == 0) { |
| 641 | when.tm_hour = std::atoi(p); |
| 642 | } |
| 643 | else if (time_s == 1) { |
| 644 | when.tm_min = std::atoi(p); |
| 645 | } |
| 646 | else if (time_s == 2) { |
| 647 | when.tm_sec = std::atoi(p); |
| 648 | } |
| 649 | } |
| 650 | time_s = day_s = 0; |
| 651 | p = timestr; |
| 652 | last = 0; |
| 653 | break; |
| 654 | default: |
| 655 | break; |
| 656 | } |
| 657 | if (!ch) break; |
| 658 | } |
| 659 | |
| 660 | *ptime = std::mktime(&when); |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | } |