Mercurial > hg > th-libs
comparison th_datastruct.c @ 457:85fa3d333556
Actually, revert the boolean changes .. meh.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 02 Jan 2018 23:09:29 +0200 |
parents | 347bfd3e017e |
children | 761724e01c02 |
comparison
equal
deleted
inserted
replaced
456:1bf886fa9db5 | 457:85fa3d333556 |
---|---|
333 | 333 |
334 return res; | 334 return res; |
335 } | 335 } |
336 | 336 |
337 | 337 |
338 bool th_ringbuf_grow(th_ringbuf_t *buf, const size_t n) | 338 BOOL th_ringbuf_grow(th_ringbuf_t *buf, const size_t n) |
339 { | 339 { |
340 buf->data = (char **) th_realloc(buf->data, (buf->size + n) * sizeof(char *)); | 340 buf->data = (char **) th_realloc(buf->data, (buf->size + n) * sizeof(char *)); |
341 if (buf->data != NULL) | 341 if (buf->data != NULL) |
342 { | 342 { |
343 memset(buf->data + buf->size, 0, sizeof(char *) * n); | 343 memset(buf->data + buf->size, 0, sizeof(char *) * n); |
344 buf->size += n; | 344 buf->size += n; |
345 return true; | 345 return TRUE; |
346 } else | 346 } else |
347 return false; | 347 return FALSE; |
348 } | 348 } |
349 | 349 |
350 | 350 |
351 void th_ringbuf_free(th_ringbuf_t *buf) | 351 void th_ringbuf_free(th_ringbuf_t *buf) |
352 { | 352 { |
398 | 398 |
399 if ((buf = th_malloc(sizeof(th_growbuf_t))) == NULL) | 399 if ((buf = th_malloc(sizeof(th_growbuf_t))) == NULL) |
400 return NULL; | 400 return NULL; |
401 | 401 |
402 th_growbuf_init(buf, mingrow); | 402 th_growbuf_init(buf, mingrow); |
403 buf->allocated = true; | 403 buf->allocated = TRUE; |
404 | 404 |
405 return buf; | 405 return buf; |
406 } | 406 } |
407 | 407 |
408 | 408 |
413 if (buf->allocated) | 413 if (buf->allocated) |
414 th_free(buf); | 414 th_free(buf); |
415 } | 415 } |
416 | 416 |
417 | 417 |
418 bool th_growbuf_grow(th_growbuf_t *buf, const size_t grow) | 418 BOOL th_growbuf_grow(th_growbuf_t *buf, const size_t grow) |
419 { | 419 { |
420 if (buf == NULL) | 420 if (buf == NULL) |
421 return false; | 421 return FALSE; |
422 | 422 |
423 if (buf->data == NULL || buf->len + grow >= buf->size) | 423 if (buf->data == NULL || buf->len + grow >= buf->size) |
424 { | 424 { |
425 buf->size += grow + (buf->mingrow > 0 ? buf->mingrow : TH_BUFGROW); | 425 buf->size += grow + (buf->mingrow > 0 ? buf->mingrow : TH_BUFGROW); |
426 buf->data = (uint8_t *) th_realloc(buf->data, buf->size); | 426 buf->data = (uint8_t *) th_realloc(buf->data, buf->size); |
427 if (buf->data == NULL) | 427 if (buf->data == NULL) |
428 return false; | 428 return FALSE; |
429 } | 429 } |
430 return true; | 430 return TRUE; |
431 } | 431 } |
432 | 432 |
433 | 433 |
434 bool th_growbuf_puts(th_growbuf_t *buf, const char *str, bool eos) | 434 BOOL th_growbuf_puts(th_growbuf_t *buf, const char *str, BOOL eos) |
435 { | 435 { |
436 size_t slen; | 436 size_t slen; |
437 if (str == NULL) | 437 if (str == NULL) |
438 return false; | 438 return FALSE; |
439 | 439 |
440 slen = strlen(str); | 440 slen = strlen(str); |
441 if (!th_growbuf_grow(buf, slen + 1)) | 441 if (!th_growbuf_grow(buf, slen + 1)) |
442 return false; | 442 return FALSE; |
443 | 443 |
444 memcpy(buf->data + buf->len, str, slen + 1); | 444 memcpy(buf->data + buf->len, str, slen + 1); |
445 buf->len += eos ? (slen + 1) : slen; | 445 buf->len += eos ? (slen + 1) : slen; |
446 | 446 |
447 return true; | 447 return TRUE; |
448 } | 448 } |
449 | 449 |
450 | 450 |
451 bool th_growbuf_putch(th_growbuf_t *buf, const char ch) | 451 BOOL th_growbuf_putch(th_growbuf_t *buf, const char ch) |
452 { | 452 { |
453 if (!th_growbuf_grow(buf, sizeof(char))) | 453 if (!th_growbuf_grow(buf, sizeof(char))) |
454 return false; | 454 return FALSE; |
455 | 455 |
456 buf->data[buf->len++] = (uint8_t) ch; | 456 buf->data[buf->len++] = (uint8_t) ch; |
457 | 457 |
458 return true; | 458 return TRUE; |
459 } | 459 } |
460 | 460 |
461 | 461 |
462 bool th_growbuf_put_str(th_growbuf_t *buf, const void *s, const size_t len) | 462 BOOL th_growbuf_put_str(th_growbuf_t *buf, const void *s, const size_t len) |
463 { | 463 { |
464 if (s == NULL) | 464 if (s == NULL) |
465 return false; | 465 return FALSE; |
466 | 466 |
467 if (!th_growbuf_grow(buf, len + 1)) | 467 if (!th_growbuf_grow(buf, len + 1)) |
468 return false; | 468 return FALSE; |
469 | 469 |
470 memcpy(buf->data + buf->len, s, len + 1); | 470 memcpy(buf->data + buf->len, s, len + 1); |
471 buf->len += len; | 471 buf->len += len; |
472 | 472 |
473 return true; | 473 return TRUE; |
474 } | 474 } |
475 | 475 |
476 | 476 |
477 bool th_growbuf_put_u8(th_growbuf_t *buf, const uint8_t val) | 477 BOOL th_growbuf_put_u8(th_growbuf_t *buf, const uint8_t val) |
478 { | 478 { |
479 if (!th_growbuf_grow(buf, sizeof(uint8_t))) | 479 if (!th_growbuf_grow(buf, sizeof(uint8_t))) |
480 return false; | 480 return FALSE; |
481 | 481 |
482 buf->data[buf->len++] = val; | 482 buf->data[buf->len++] = val; |
483 | 483 |
484 return true; | 484 return TRUE; |
485 } | 485 } |
486 | 486 |
487 | 487 |
488 bool th_growbuf_put_u16_be(th_growbuf_t *buf, const uint16_t val) | 488 BOOL th_growbuf_put_u16_be(th_growbuf_t *buf, const uint16_t val) |
489 { | 489 { |
490 if (!th_growbuf_grow(buf, sizeof(uint16_t))) | 490 if (!th_growbuf_grow(buf, sizeof(uint16_t))) |
491 return false; | 491 return FALSE; |
492 | 492 |
493 buf->data[buf->len++] = (val >> 8) & 0xff; | 493 buf->data[buf->len++] = (val >> 8) & 0xff; |
494 buf->data[buf->len++] = val & 0xff; | 494 buf->data[buf->len++] = val & 0xff; |
495 | 495 |
496 return true; | 496 return TRUE; |
497 } | 497 } |
498 | 498 |
499 | 499 |
500 bool th_growbuf_put_u16_le(th_growbuf_t *buf, const uint16_t val) | 500 BOOL th_growbuf_put_u16_le(th_growbuf_t *buf, const uint16_t val) |
501 { | 501 { |
502 if (!th_growbuf_grow(buf, sizeof(uint16_t))) | 502 if (!th_growbuf_grow(buf, sizeof(uint16_t))) |
503 return false; | 503 return FALSE; |
504 | 504 |
505 buf->data[buf->len++] = val & 0xff; | 505 buf->data[buf->len++] = val & 0xff; |
506 buf->data[buf->len++] = (val >> 8) & 0xff; | 506 buf->data[buf->len++] = (val >> 8) & 0xff; |
507 | 507 |
508 return true; | 508 return TRUE; |
509 } | 509 } |
510 | 510 |
511 | 511 |
512 bool th_growbuf_put_u32_be(th_growbuf_t *buf, const uint32_t val) | 512 BOOL th_growbuf_put_u32_be(th_growbuf_t *buf, const uint32_t val) |
513 { | 513 { |
514 if (!th_growbuf_grow(buf, sizeof(uint32_t))) | 514 if (!th_growbuf_grow(buf, sizeof(uint32_t))) |
515 return false; | 515 return FALSE; |
516 | 516 |
517 buf->data[buf->len++] = (val >> 24) & 0xff; | 517 buf->data[buf->len++] = (val >> 24) & 0xff; |
518 buf->data[buf->len++] = (val >> 16) & 0xff; | 518 buf->data[buf->len++] = (val >> 16) & 0xff; |
519 buf->data[buf->len++] = (val >> 8) & 0xff; | 519 buf->data[buf->len++] = (val >> 8) & 0xff; |
520 buf->data[buf->len++] = val & 0xff; | 520 buf->data[buf->len++] = val & 0xff; |
521 | 521 |
522 return true; | 522 return TRUE; |
523 } | 523 } |
524 | 524 |
525 | 525 |
526 bool th_growbuf_put_u32_le(th_growbuf_t *buf, const uint32_t val) | 526 BOOL th_growbuf_put_u32_le(th_growbuf_t *buf, const uint32_t val) |
527 { | 527 { |
528 if (!th_growbuf_grow(buf, sizeof(uint32_t))) | 528 if (!th_growbuf_grow(buf, sizeof(uint32_t))) |
529 return false; | 529 return FALSE; |
530 | 530 |
531 buf->data[buf->len++] = val & 0xff; | 531 buf->data[buf->len++] = val & 0xff; |
532 buf->data[buf->len++] = (val >> 8) & 0xff; | 532 buf->data[buf->len++] = (val >> 8) & 0xff; |
533 buf->data[buf->len++] = (val >> 16) & 0xff; | 533 buf->data[buf->len++] = (val >> 16) & 0xff; |
534 buf->data[buf->len++] = (val >> 24) & 0xff; | 534 buf->data[buf->len++] = (val >> 24) & 0xff; |
535 | 535 |
536 return true; | 536 return TRUE; |
537 } | 537 } |
538 | 538 |
539 | 539 |
540 /* | 540 /* |
541 * Simple legacy string growing buffer | 541 * Simple legacy string growing buffer |
542 */ | 542 */ |
543 bool th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, size_t grow) | 543 BOOL th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, size_t grow) |
544 { | 544 { |
545 if (*buf == NULL) | 545 if (*buf == NULL) |
546 *bufsize = *len = 0; | 546 *bufsize = *len = 0; |
547 | 547 |
548 if (*buf == NULL || *len + grow >= *bufsize) | 548 if (*buf == NULL || *len + grow >= *bufsize) |
549 { | 549 { |
550 *bufsize += grow + TH_BUFGROW; | 550 *bufsize += grow + TH_BUFGROW; |
551 *buf = th_realloc(*buf, *bufsize); | 551 *buf = th_realloc(*buf, *bufsize); |
552 if (*buf == NULL) | 552 if (*buf == NULL) |
553 return false; | 553 return FALSE; |
554 } | 554 } |
555 return true; | 555 return TRUE; |
556 } | 556 } |
557 | 557 |
558 | 558 |
559 bool th_strbuf_putch(char **buf, size_t *bufsize, size_t *len, const char ch) | 559 BOOL th_strbuf_putch(char **buf, size_t *bufsize, size_t *len, const char ch) |
560 { | 560 { |
561 if (!th_strbuf_grow(buf, bufsize, len, 1)) | 561 if (!th_strbuf_grow(buf, bufsize, len, 1)) |
562 return false; | 562 return FALSE; |
563 | 563 |
564 (*buf)[*len] = ch; | 564 (*buf)[*len] = ch; |
565 (*len)++; | 565 (*len)++; |
566 | 566 |
567 return true; | 567 return TRUE; |
568 } | 568 } |
569 | 569 |
570 | 570 |
571 bool th_strbuf_putsn(char **buf, size_t *bufsize, size_t *len, const char *str, const size_t slen) | 571 BOOL th_strbuf_putsn(char **buf, size_t *bufsize, size_t *len, const char *str, const size_t slen) |
572 { | 572 { |
573 if (str == NULL) | 573 if (str == NULL) |
574 return false; | 574 return FALSE; |
575 | 575 |
576 if (!th_strbuf_grow(buf, bufsize, len, slen + 1)) | 576 if (!th_strbuf_grow(buf, bufsize, len, slen + 1)) |
577 return false; | 577 return FALSE; |
578 | 578 |
579 memcpy(*buf + *len, str, slen); | 579 memcpy(*buf + *len, str, slen); |
580 (*len) += slen; | 580 (*len) += slen; |
581 *(buf + *len + slen) = 0; | 581 *(buf + *len + slen) = 0; |
582 | 582 |
583 return true; | 583 return TRUE; |
584 } | 584 } |
585 | 585 |
586 | 586 |
587 bool th_strbuf_puts(char **buf, size_t *bufsize, size_t *len, const char *str) | 587 BOOL th_strbuf_puts(char **buf, size_t *bufsize, size_t *len, const char *str) |
588 { | 588 { |
589 size_t slen; | 589 size_t slen; |
590 if (str == NULL) | 590 if (str == NULL) |
591 return false; | 591 return FALSE; |
592 | 592 |
593 slen = strlen(str); | 593 slen = strlen(str); |
594 if (!th_strbuf_grow(buf, bufsize, len, slen + 1)) | 594 if (!th_strbuf_grow(buf, bufsize, len, slen + 1)) |
595 return false; | 595 return FALSE; |
596 | 596 |
597 memcpy(*buf + *len, str, slen + 1); | 597 memcpy(*buf + *len, str, slen + 1); |
598 (*len) += slen; | 598 (*len) += slen; |
599 | 599 |
600 return true; | 600 return TRUE; |
601 } | 601 } |