[169] | 1 | #ifndef SH_STRING_H
|
---|
| 2 | #define SH_STRING_H
|
---|
| 3 |
|
---|
| 4 |
|
---|
| 5 | /* String definition and utility functions.
|
---|
| 6 | */
|
---|
| 7 | typedef struct sh_str_struct
|
---|
| 8 | {
|
---|
| 9 | char * str; /* always NULL terminated */
|
---|
| 10 | size_t len; /* without terminating \0 */
|
---|
| 11 | size_t siz; /* size of allocated buffer */
|
---|
| 12 | } sh_string;
|
---|
| 13 |
|
---|
| 14 | sh_string * sh_string_new(size_t size);
|
---|
| 15 | void sh_string_destroy(sh_string ** s);
|
---|
| 16 | #define sh_string_str(a) ((a)->str)
|
---|
| 17 | #define sh_string_len(a) ((a)->len)
|
---|
| 18 |
|
---|
| 19 | /* concat string to sh_string
|
---|
| 20 | */
|
---|
[183] | 21 | sh_string * sh_string_cat_lchar(sh_string * s, const char * str, size_t len);
|
---|
[169] | 22 |
|
---|
[183] | 23 | /* add char array to end of string */
|
---|
| 24 | sh_string * sh_string_add_from_char(sh_string * s, const char * str);
|
---|
| 25 |
|
---|
[169] | 26 | /* set sh_string from string
|
---|
| 27 | */
|
---|
[183] | 28 | sh_string * sh_string_set_from_char(sh_string * s, const char * str);
|
---|
[169] | 29 |
|
---|
| 30 | /* create new sh_string from array of given length
|
---|
| 31 | */
|
---|
[183] | 32 | sh_string * sh_string_new_from_lchar(const char * str, size_t len);
|
---|
[169] | 33 |
|
---|
[183] | 34 | #define sh_string_copy(a) ((a) ? sh_string_new_from_lchar(((a)->str), ((a)->len)) : NULL)
|
---|
[185] | 35 | #define sh_string_add(a,b) ((a && b) ? sh_string_cat_lchar((a), ((b)->str), ((b)->len)) : NULL)
|
---|
[169] | 36 |
|
---|
| 37 | /* create new sh_string from three arrays of given length
|
---|
| 38 | */
|
---|
[183] | 39 | sh_string * sh_string_new_from_lchar3(const char * str1, size_t len1,
|
---|
| 40 | const char * str2, size_t len2,
|
---|
| 41 | const char * str3, size_t len3);
|
---|
[169] | 42 |
|
---|
| 43 | /* Truncate to desired length.
|
---|
| 44 | */
|
---|
| 45 | sh_string * sh_string_truncate(sh_string * s, size_t len);
|
---|
| 46 |
|
---|
| 47 | /* If requested increase is zero, increase by default amount.
|
---|
| 48 | */
|
---|
| 49 | sh_string * sh_string_grow(sh_string * s, size_t increase);
|
---|
| 50 |
|
---|
[183] | 51 | /* Read a string from a file, with maxlen. Return 0 on EOF,
|
---|
| 52 | * -1 on error, and -2 if a line exceeds maxlen.
|
---|
| 53 | */
|
---|
| 54 | size_t sh_string_read(sh_string * s, FILE * fp, size_t maxlen);
|
---|
| 55 |
|
---|
[185] | 56 | /* Read a string from a file, with maxlen. Return 0 on EOF,
|
---|
| 57 | * -1 on error, and -2 if a line exceeds maxlen.
|
---|
| 58 | * If 'cont' != NULL, continuation lines starting with a char
|
---|
| 59 | * in 'cont' are concatenated.
|
---|
| 60 | */
|
---|
| 61 | size_t sh_string_read_cont(sh_string * s, FILE * fp, size_t maxlen, char *cont);
|
---|
| 62 |
|
---|
[183] | 63 | /* Split array at delim in at most nfields fields.
|
---|
| 64 | * Empty fields are returned as empty (zero-length) strings.
|
---|
| 65 | * Leading and trailing WS are removed from token.
|
---|
| 66 | * The number of fields is returned in 'nfields', their
|
---|
| 67 | * lengths in 'lengths'.
|
---|
| 68 | * A single delimiter will return two empty fields.
|
---|
| 69 | */
|
---|
| 70 | char ** split_array(char *line, unsigned int * nfields,
|
---|
| 71 | char delim, size_t * lengths);
|
---|
| 72 |
|
---|
| 73 | /* Split array at whitespace in at most nfields fields.
|
---|
| 74 | * Multiple whitespaces are collapsed.
|
---|
| 75 | * Empty fields are returned as empty (zero-length) strings.
|
---|
| 76 | * The number of fields is returned in nfields.
|
---|
| 77 | * An empty string will return zero fields.
|
---|
| 78 | * If nfields < actual fields, last string will be remainder.
|
---|
| 79 | */
|
---|
| 80 | char ** split_array_ws(char *line, unsigned int * nfields, size_t * lengths);
|
---|
| 81 |
|
---|
[215] | 82 | /* Same as above, but split on [space, tab, comma]
|
---|
| 83 | */
|
---|
| 84 | char ** split_array_list(char *line, unsigned int * nfields, size_t * lengths);
|
---|
| 85 |
|
---|
[260] | 86 | /* Return a split_array_list() of a list contained in 'PREFIX\s*( list ).*'
|
---|
| 87 | */
|
---|
| 88 | char ** split_array_braced (char *line, const char * prefix,
|
---|
| 89 | unsigned int * nfields, size_t * lengths);
|
---|
| 90 |
|
---|
[183] | 91 | /* Replaces fields in s with 'replacement'. Fields are given
|
---|
| 92 | * in the ordered array ovector, comprising ovecnum pairs
|
---|
| 93 | * ovector[i], ovector[i+1] which list offset of first char
|
---|
| 94 | * of field, offset of first char after field (this is how
|
---|
| 95 | * the pcre library does it).
|
---|
| 96 | */
|
---|
| 97 | sh_string * sh_string_replace(const sh_string * s,
|
---|
| 98 | const int * ovector, int ovecnum,
|
---|
| 99 | const char * replacement, size_t rlen);
|
---|
| 100 |
|
---|
[169] | 101 | #endif
|
---|