Mercurial > hg > th-libs
comparison th_string.c @ 559:6021ef8fc341
Return proper THERR_ error codes from th_pstr_*()
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 05 Jan 2020 20:17:59 +0200 |
parents | 3a852e9f70a6 |
children | b75f42ca08ef |
comparison
equal
deleted
inserted
replaced
558:7b7872212afd | 559:6021ef8fc341 |
---|---|
448 int th_pstr_cpy(char **pdst, const char *src) | 448 int th_pstr_cpy(char **pdst, const char *src) |
449 { | 449 { |
450 assert(pdst != NULL); | 450 assert(pdst != NULL); |
451 | 451 |
452 if (src == NULL) | 452 if (src == NULL) |
453 return -1; | 453 return THERR_NULLPTR; |
454 | 454 |
455 th_free(*pdst); | 455 th_free(*pdst); |
456 if ((*pdst = th_malloc(strlen(src) + 1)) == NULL) | 456 |
457 return -2; | 457 size_t slen = strlen(src); |
458 | 458 if ((*pdst = th_malloc(slen + 1)) == NULL) |
459 strcpy(*pdst, src); | 459 return THERR_MALLOC; |
460 return 0; | 460 |
461 memcpy(*pdst, src, slen + 1); | |
462 | |
463 return THERR_OK; | |
461 } | 464 } |
462 | 465 |
463 | 466 |
464 /* Concatenates a given string into string pointed by *pdst. | 467 /* Concatenates a given string into string pointed by *pdst. |
465 */ | 468 */ |
466 int th_pstr_cat(char **pdst, const char *src) | 469 int th_pstr_cat(char **pdst, const char *src) |
467 { | 470 { |
468 assert(pdst != NULL); | 471 assert(pdst != NULL); |
469 | 472 |
470 if (src == NULL) | 473 if (src == NULL) |
471 return -1; | 474 return THERR_NULLPTR; |
472 | 475 |
473 if (*pdst != NULL) | 476 if (*pdst != NULL) |
474 { | 477 { |
475 *pdst = th_realloc(*pdst, strlen(*pdst) + strlen(src) + 1); | 478 size_t dlen = strlen(*pdst), slen = strlen(src); |
476 if (*pdst == NULL) | 479 if ((*pdst = th_realloc(*pdst, dlen + slen + 1)) == NULL) |
477 return -1; | 480 return THERR_MALLOC; |
478 | 481 |
479 strcat(*pdst, src); | 482 memcpy((*pdst) + dlen, src, slen + 1); |
480 } | 483 } |
481 else | 484 else |
482 { | 485 { |
483 *pdst = th_malloc(strlen(src) + 1); | 486 size_t slen = strlen(src); |
484 if (*pdst == NULL) | 487 if ((*pdst = th_malloc(slen + 1)) == NULL) |
485 return -1; | 488 return THERR_MALLOC; |
486 | 489 |
487 strcpy(*pdst, src); | 490 memcpy(*pdst, src, slen + 1); |
488 } | 491 } |
489 | 492 |
490 return 0; | 493 return THERR_OK; |
491 } | 494 } |
492 | 495 |
493 | 496 |
494 /* Find next non-whitespace character in string. | 497 /* Find next non-whitespace character in string. |
495 * Updates iPos into the position of such character and | 498 * Updates iPos into the position of such character and |