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.
 
 
 
 

202 lines
7.4 KiB

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