expstring.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @file expstring.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_MISC_EXPSTRING_H
  30. #define BADVPN_MISC_EXPSTRING_H
  31. #include <stddef.h>
  32. #include <misc/debug.h>
  33. #include <misc/exparray.h>
  34. #include <misc/bsize.h>
  35. typedef struct {
  36. struct ExpArray arr;
  37. size_t n;
  38. } ExpString;
  39. static int ExpString_Init (ExpString *c);
  40. static void ExpString_Free (ExpString *c);
  41. static int ExpString_Append (ExpString *c, const char *str);
  42. static int ExpString_AppendChar (ExpString *c, char ch);
  43. static char * ExpString_Get (ExpString *c);
  44. int ExpString_Init (ExpString *c)
  45. {
  46. if (!ExpArray_init(&c->arr, 1, 16)) {
  47. return 0;
  48. }
  49. c->n = 0;
  50. ((char *)c->arr.v)[c->n] = '\0';
  51. return 1;
  52. }
  53. void ExpString_Free (ExpString *c)
  54. {
  55. free(c->arr.v);
  56. }
  57. int ExpString_Append (ExpString *c, const char *str)
  58. {
  59. ASSERT(str)
  60. size_t l = strlen(str);
  61. bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_add(bsize_fromsize(l), bsize_fromint(1)));
  62. if (newsize.is_overflow || !ExpArray_resize(&c->arr, newsize.value)) {
  63. return 0;
  64. }
  65. memcpy((char *)c->arr.v + c->n, str, l);
  66. c->n += l;
  67. ((char *)c->arr.v)[c->n] = '\0';
  68. return 1;
  69. }
  70. int ExpString_AppendChar (ExpString *c, char ch)
  71. {
  72. ASSERT(ch != '\0')
  73. bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_fromint(2));
  74. if (newsize.is_overflow || !ExpArray_resize(&c->arr, newsize.value)) {
  75. return 0;
  76. }
  77. ((char *)c->arr.v)[c->n] = ch;
  78. c->n++;
  79. ((char *)c->arr.v)[c->n] = '\0';
  80. return 1;
  81. }
  82. char * ExpString_Get (ExpString *c)
  83. {
  84. return (char *)c->arr.v;
  85. }
  86. #endif