[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)
|
---|
| 35 | #define sh_string_add(a,b) ((a && b) ? sh_string_add_from_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 |
|
---|
| 56 | /* Split array at delim in at most nfields fields.
|
---|
| 57 | * Empty fields are returned as empty (zero-length) strings.
|
---|
| 58 | * Leading and trailing WS are removed from token.
|
---|
| 59 | * The number of fields is returned in 'nfields', their
|
---|
| 60 | * lengths in 'lengths'.
|
---|
| 61 | * A single delimiter will return two empty fields.
|
---|
| 62 | */
|
---|
| 63 | char ** split_array(char *line, unsigned int * nfields,
|
---|
| 64 | char delim, size_t * lengths);
|
---|
| 65 |
|
---|
| 66 | /* Split array at whitespace in at most nfields fields.
|
---|
| 67 | * Multiple whitespaces are collapsed.
|
---|
| 68 | * Empty fields are returned as empty (zero-length) strings.
|
---|
| 69 | * The number of fields is returned in nfields.
|
---|
| 70 | * An empty string will return zero fields.
|
---|
| 71 | * If nfields < actual fields, last string will be remainder.
|
---|
| 72 | */
|
---|
| 73 | char ** split_array_ws(char *line, unsigned int * nfields, size_t * lengths);
|
---|
| 74 |
|
---|
| 75 | /* Replaces fields in s with 'replacement'. Fields are given
|
---|
| 76 | * in the ordered array ovector, comprising ovecnum pairs
|
---|
| 77 | * ovector[i], ovector[i+1] which list offset of first char
|
---|
| 78 | * of field, offset of first char after field (this is how
|
---|
| 79 | * the pcre library does it).
|
---|
| 80 | */
|
---|
| 81 | sh_string * sh_string_replace(const sh_string * s,
|
---|
| 82 | const int * ovector, int ovecnum,
|
---|
| 83 | const char * replacement, size_t rlen);
|
---|
| 84 |
|
---|
[169] | 85 | #endif
|
---|