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