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 | */
|
---|
21 | sh_string * sh_string_cat_lchar(sh_string * s, const char * str, size_t len);
|
---|
22 |
|
---|
23 | /* add char array to end of string */
|
---|
24 | sh_string * sh_string_add_from_char(sh_string * s, const char * str);
|
---|
25 |
|
---|
26 | /* set sh_string from string
|
---|
27 | */
|
---|
28 | sh_string * sh_string_set_from_char(sh_string * s, const char * str);
|
---|
29 |
|
---|
30 | /* create new sh_string from array of given length
|
---|
31 | */
|
---|
32 | sh_string * sh_string_new_from_lchar(const char * str, size_t len);
|
---|
33 |
|
---|
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_cat_lchar((a), ((b)->str), ((b)->len)) : NULL)
|
---|
36 |
|
---|
37 | /* create new sh_string from three arrays of given length
|
---|
38 | */
|
---|
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);
|
---|
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 |
|
---|
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 | /* 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 |
|
---|
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 |
|
---|
82 | /* Replaces fields in s with 'replacement'. Fields are given
|
---|
83 | * in the ordered array ovector, comprising ovecnum pairs
|
---|
84 | * ovector[i], ovector[i+1] which list offset of first char
|
---|
85 | * of field, offset of first char after field (this is how
|
---|
86 | * the pcre library does it).
|
---|
87 | */
|
---|
88 | sh_string * sh_string_replace(const sh_string * s,
|
---|
89 | const int * ovector, int ovecnum,
|
---|
90 | const char * replacement, size_t rlen);
|
---|
91 |
|
---|
92 | #endif
|
---|