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.
 
 
 
 

121 lines
4.9 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 int len;
  17. unsigned 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. extern 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. extern int stralloc_ready(stralloc* sa,unsigned 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. extern int stralloc_readyplus(stralloc* sa,unsigned 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. extern int stralloc_copyb(stralloc* sa,const char* buf,unsigned 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. extern 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. extern 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. extern int stralloc_catb(stralloc* sa,const char* in,unsigned int len);
  52. /* stralloc_cats is analogous to stralloc_copys */
  53. extern int stralloc_cats(stralloc* sa,const char* in);
  54. /* stralloc_cat is analogous to stralloc_copy */
  55. extern int stralloc_cat(stralloc* sa,stralloc* in);
  56. /* stralloc_append adds one byte in[0] to the end of the string stored
  57. * in sa. It is the same as stralloc_catb(&sa,in,1). */
  58. extern int stralloc_append(stralloc* sa,const char* in); /* beware: this takes a pointer to 1 char */
  59. /* stralloc_starts returns 1 if the \0-terminated string in "in", without
  60. * the terminating \0, is a prefix of the string stored in sa. Otherwise
  61. * it returns 0. sa must already be allocated. */
  62. extern int stralloc_starts(stralloc* sa,const char* in) __pure__;
  63. /* stralloc_diff returns negative, 0, or positive, depending on whether
  64. * a is lexicographically smaller than, equal to, or greater than the
  65. * string b. */
  66. extern int stralloc_diff(const stralloc* a,const stralloc* b) __pure__;
  67. /* stralloc_diffs returns negative, 0, or positive, depending on whether
  68. * a is lexicographically smaller than, equal to, or greater than the
  69. * string b[0], b[1], ..., b[n]=='\0'. */
  70. extern int stralloc_diffs(const stralloc* a,const char* b) __pure__;
  71. /* stralloc_0 appends \0 */
  72. #define stralloc_0(sa) stralloc_append(sa,"")
  73. /* stralloc_catulong0 appends a '0' padded ASCII representation of in */
  74. extern int stralloc_catulong0(stralloc* sa,unsigned long in,unsigned int n);
  75. /* stralloc_catlong0 appends a '0' padded ASCII representation of in */
  76. extern int stralloc_catlong0(stralloc* sa,signed long in,unsigned int n);
  77. /* stralloc_free frees the storage associated with sa */
  78. extern void stralloc_free(stralloc* sa);
  79. #define stralloc_catlong(sa,l) (stralloc_catlong0((sa),(l),0))
  80. #define stralloc_catuint0(sa,i,n) (stralloc_catulong0((sa),(i),(n)))
  81. #define stralloc_catint0(sa,i,n) (stralloc_catlong0((sa),(i),(n)))
  82. #define stralloc_catint(sa,i) (stralloc_catlong0((sa),(i),0))
  83. /* remove last char. Return removed byte as unsigned char (or -1 if stralloc was empty). */
  84. extern int stralloc_chop(stralloc* sa);
  85. /* remove trailing "\r\n", "\n" or "\r". Return number of removed chars (0,1 or 2) */
  86. extern int stralloc_chomp(stralloc* sa);
  87. #ifdef BUFFER_H
  88. /* write stralloc to buffer */
  89. extern int buffer_putsa(buffer* b,stralloc* sa);
  90. /* read token from buffer to stralloc */
  91. extern int buffer_get_token_sa(buffer* b,stralloc* sa,const char* charset,unsigned int setlen);
  92. /* read line from buffer to stralloc */
  93. extern int buffer_getline_sa(buffer* b,stralloc* sa);
  94. #endif
  95. #endif