Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

163 lines
6.5 KiB

  1. #ifndef STRALLOC_H
  2. #define STRALLOC_H
  3. #ifdef __dietlibc__
  4. #include <sys/cdefs.h>
  5. #endif
  6. #ifndef __pure__
  7. #define __pure__
  8. #endif
  9. /* stralloc is the internal data structure all functions are working on.
  10. * s is the string.
  11. * len is the used length of the string.
  12. * a is the allocated length of the string.
  13. */
  14. typedef struct stralloc {
  15. char* s;
  16. unsigned long int len;
  17. unsigned long int a;
  18. } stralloc;
  19. /* stralloc_init will initialize a stralloc.
  20. * Previously allocated memory will not be freed; use stralloc_free for
  21. * that. To assign an empty string, use stralloc_copys(sa,""). */
  22. void stralloc_init(stralloc* sa);
  23. /* stralloc_ready makes sure that sa has enough space allocated to hold
  24. * len bytes: If sa is not allocated, stralloc_ready allocates at least
  25. * len bytes of space, and returns 1. If sa is already allocated, but
  26. * not enough to hold len bytes, stralloc_ready allocates at least len
  27. * bytes of space, copies the old string into the new space, frees the
  28. * old space, and returns 1. Note that this changes sa.s. If the
  29. * allocation fails, stralloc_ready leaves sa alone and returns 0. */
  30. int stralloc_ready(stralloc* sa,unsigned long int len);
  31. /* stralloc_readyplus is like stralloc_ready except that, if sa is
  32. * already allocated, stralloc_readyplus adds the current length of sa
  33. * to len. */
  34. int stralloc_readyplus(stralloc* sa,unsigned long int len);
  35. /* stralloc_copyb copies the string buf[0], buf[1], ..., buf[len-1] into
  36. * sa, allocating space if necessary, and returns 1. If it runs out of
  37. * memory, stralloc_copyb leaves sa alone and returns 0. */
  38. int stralloc_copyb(stralloc* sa,const char* buf,unsigned long int len);
  39. /* stralloc_copys copies a \0-terminated string from buf into sa,
  40. * without the \0. It is the same as
  41. * stralloc_copyb(&sa,buf,str_len(buf)). */
  42. int stralloc_copys(stralloc* sa,const char* buf);
  43. /* stralloc_copy copies the string stored in sa2 into sa. It is the same
  44. * as stralloc_copyb(&sa,sa2.s,sa2.len). sa2 must already be allocated. */
  45. int stralloc_copy(stralloc* sa,const stralloc* sa2);
  46. /* stralloc_catb adds the string buf[0], buf[1], ... buf[len-1] to the
  47. * end of the string stored in sa, allocating space if necessary, and
  48. * returns 1. If sa is unallocated, stralloc_catb is the same as
  49. * stralloc_copyb. If it runs out of memory, stralloc_catb leaves sa
  50. * alone and returns 0. */
  51. int stralloc_catb(stralloc* sa,const char* in,unsigned long int len);
  52. /* stralloc_cats is analogous to stralloc_copys */
  53. int stralloc_cats(stralloc* sa,const char* in);
  54. void stralloc_zero(stralloc* sa);
  55. /* like stralloc_cats but can cat more than one string at once */
  56. int stralloc_catm_internal(stralloc* sa,...);
  57. #define stralloc_catm(sa,...) stralloc_catm_internal(sa,__VA_ARGS__,0)
  58. #define stralloc_copym(sa,...) (stralloc_zero(sa) && stralloc_catm_internal(sa,__VA_ARGS__,0))
  59. /* stralloc_cat is analogous to stralloc_copy */
  60. int stralloc_cat(stralloc* sa,stralloc* in);
  61. /* stralloc_append adds one byte in[0] to the end of the string stored
  62. * in sa. It is the same as stralloc_catb(&sa,in,1). */
  63. int stralloc_append(stralloc* sa,const char* in); /* beware: this takes a pointer to 1 char */
  64. /* stralloc_starts returns 1 if the \0-terminated string in "in", without
  65. * the terminating \0, is a prefix of the string stored in sa. Otherwise
  66. * it returns 0. sa must already be allocated. */
  67. int stralloc_starts(stralloc* sa,const char* in) __pure__;
  68. /* stralloc_diff returns negative, 0, or positive, depending on whether
  69. * a is lexicographically smaller than, equal to, or greater than the
  70. * string b. */
  71. int stralloc_diff(const stralloc* a,const stralloc* b) __pure__;
  72. /* stralloc_diffs returns negative, 0, or positive, depending on whether
  73. * a is lexicographically smaller than, equal to, or greater than the
  74. * string b[0], b[1], ..., b[n]=='\0'. */
  75. int stralloc_diffs(const stralloc* a,const char* b) __pure__;
  76. #define stralloc_equal(a,b) (!stralloc_diff((a),(b)))
  77. #define stralloc_equals(a,b) (!stralloc_diffs((a),(b)))
  78. /* stralloc_0 appends \0 */
  79. #define stralloc_0(sa) stralloc_append(sa,"")
  80. /* stralloc_catulong0 appends a '0' padded ASCII representation of in */
  81. int stralloc_catulong0(stralloc* sa,unsigned long int in,unsigned long int n);
  82. /* stralloc_catlong0 appends a '0' padded ASCII representation of in */
  83. int stralloc_catlong0(stralloc* sa,signed long int in,unsigned long int n);
  84. /* stralloc_free frees the storage associated with sa */
  85. void stralloc_free(stralloc* sa);
  86. #define stralloc_catlong(sa,l) (stralloc_catlong0((sa),(l),0))
  87. #define stralloc_catuint0(sa,i,n) (stralloc_catulong0((sa),(i),(n)))
  88. #define stralloc_catint0(sa,i,n) (stralloc_catlong0((sa),(i),(n)))
  89. #define stralloc_catint(sa,i) (stralloc_catlong0((sa),(i),0))
  90. /* remove last char. Return removed byte as unsigned char (or -1 if stralloc was empty). */
  91. int stralloc_chop(stralloc* sa);
  92. /* remove trailing "\r\n", "\n" or "\r". Return number of removed chars (0,1 or 2) */
  93. int stralloc_chomp(stralloc* sa);
  94. #ifdef BUFFER_H
  95. /* write stralloc to buffer */
  96. int buffer_putsa(buffer* b,stralloc* sa);
  97. /* write stralloc to buffer and flush */
  98. int buffer_putsaflush(buffer* b,stralloc* sa);
  99. /* these "read token" functions return 0 if the token was complete or
  100. * EOF was hit or -1 on error. In contrast to the non-stralloc token
  101. * functions, the separator is also put in the stralloc; use
  102. * stralloc_chop or stralloc_chomp to get rid of it. */
  103. /* WARNING! These token reading functions will not clear the stralloc!
  104. * They _append_ the token to the contents of the stralloc. The idea is
  105. * that this way these functions can be used on non-blocking sockets;
  106. * when you get signalled EAGAIN, just call the functions again when new
  107. * data is available. */
  108. /* read token from buffer to stralloc */
  109. int buffer_get_token_sa(buffer* b,stralloc* sa,const char* charset,unsigned long int setlen);
  110. /* read line from buffer to stralloc */
  111. int buffer_getline_sa(buffer* b,stralloc* sa);
  112. /* same as buffer_get_token_sa but empty sa first */
  113. int buffer_get_new_token_sa(buffer* b,stralloc* sa,const char* charset,unsigned long int setlen);
  114. /* same as buffer_getline_sa but empty sa first */
  115. int buffer_getnewline_sa(buffer* b,stralloc* sa);
  116. typedef int (*sa_predicate)(stralloc* sa);
  117. /* like buffer_get_token_sa but the token ends when your predicate says so */
  118. int buffer_get_token_sa_pred(buffer* b,stralloc* sa,sa_predicate p);
  119. /* same, but clear sa first */
  120. int buffer_get_new_token_sa_pred(buffer* b,stralloc* sa,sa_predicate p);
  121. /* make a buffer from a stralloc.
  122. * Do not change the stralloc after this! */
  123. void buffer_fromsa(buffer* b,stralloc* sa);
  124. #endif
  125. #endif