1 | #ifndef SH_STRING_H
|
---|
2 | #define SH_STRING_H
|
---|
3 |
|
---|
4 | #include <stdio.h>
|
---|
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 | */
|
---|
22 | sh_string * sh_string_cat_lchar(sh_string * s, const char * str, size_t len);
|
---|
23 |
|
---|
24 | /* add char array to end of string */
|
---|
25 | sh_string * sh_string_add_from_char(sh_string * s, const char * str);
|
---|
26 |
|
---|
27 | /* set sh_string from string
|
---|
28 | */
|
---|
29 | sh_string * sh_string_set_from_char(sh_string * s, const char * str);
|
---|
30 |
|
---|
31 | /* create new sh_string from array of given length
|
---|
32 | */
|
---|
33 | sh_string * sh_string_new_from_lchar(const char * str, size_t len);
|
---|
34 |
|
---|
35 | #define sh_string_copy(a) ((a) ? sh_string_new_from_lchar(((a)->str), ((a)->len)) : NULL)
|
---|
36 | #define sh_string_add(a,b) ((a && b) ? sh_string_cat_lchar((a), ((b)->str), ((b)->len)) : NULL)
|
---|
37 |
|
---|
38 | /* create new sh_string from three arrays of given length
|
---|
39 | */
|
---|
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);
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|
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 | * The returned array is allocated memory, and its fields
|
---|
71 | * are modified parts of the 'line' parameter.
|
---|
72 | */
|
---|
73 | char ** split_array(char *line, unsigned int * nfields,
|
---|
74 | char delim, size_t * lengths);
|
---|
75 |
|
---|
76 | /* Split array at whitespace in at most nfields fields.
|
---|
77 | * Multiple whitespaces are collapsed.
|
---|
78 | * Empty fields are returned as empty (zero-length) strings.
|
---|
79 | * The number of fields is returned in nfields.
|
---|
80 | * An empty string will return zero fields.
|
---|
81 | * If nfields < actual fields, last string will be remainder.
|
---|
82 | * The returned array is allocated memory, and its fields
|
---|
83 | * are modified parts of the 'line' parameter.
|
---|
84 | */
|
---|
85 | char ** split_array_ws(char *line, unsigned int * nfields, size_t * lengths);
|
---|
86 |
|
---|
87 | /* Same as above, but split on [space, tab, comma]
|
---|
88 | */
|
---|
89 | char ** split_array_list(char *line, unsigned int * nfields, size_t * lengths);
|
---|
90 |
|
---|
91 | /* Same as above, but split on delimiter list (token)
|
---|
92 | */
|
---|
93 | char ** split_array_token (char *line,
|
---|
94 | unsigned int * nfields, size_t * lengths,
|
---|
95 | const char * token);
|
---|
96 |
|
---|
97 | /* Return a split_array_list() of a list contained in 'PREFIX\s*( list ).*'
|
---|
98 | */
|
---|
99 | char ** split_array_braced (char *line, const char * prefix,
|
---|
100 | unsigned int * nfields, size_t * lengths);
|
---|
101 |
|
---|
102 | /* Replaces fields in s with 'replacement'. Fields are given
|
---|
103 | * in the ordered array ovector, comprising ovecnum pairs
|
---|
104 | * ovector[i], ovector[i+1] which list offset of first char
|
---|
105 | * of field, offset of first char after field (this is how
|
---|
106 | * the pcre library does it).
|
---|
107 | */
|
---|
108 | sh_string * sh_string_replace(const sh_string * s,
|
---|
109 | const int * ovector, int ovecnum,
|
---|
110 | const char * replacement, size_t rlen);
|
---|
111 |
|
---|
112 | #endif
|
---|