source: trunk/include/sh_string.h@ 553

Last change on this file since 553 was 265, checked in by katerina, 15 years ago

Enhance logfile monitoring (tickets #183, #184, #185).

File size: 3.8 KB
Line 
1#ifndef SH_STRING_H
2#define SH_STRING_H
3
4#include <stdio.h>
5
6/* String definition and utility functions.
7 */
8typedef 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
15sh_string * sh_string_new(size_t size);
16void 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 */
22sh_string * sh_string_cat_lchar(sh_string * s, const char * str, size_t len);
23
24/* add char array to end of string */
25sh_string * sh_string_add_from_char(sh_string * s, const char * str);
26
27/* set sh_string from string
28 */
29sh_string * sh_string_set_from_char(sh_string * s, const char * str);
30
31/* create new sh_string from array of given length
32 */
33sh_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 */
40sh_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 */
46sh_string * sh_string_truncate(sh_string * s, size_t len);
47
48/* If requested increase is zero, increase by default amount.
49 */
50sh_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 */
55size_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 */
62size_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 */
71char ** 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 */
81char ** split_array_ws(char *line, unsigned int * nfields, size_t * lengths);
82
83/* Same as above, but split on [space, tab, comma]
84 */
85char ** split_array_list(char *line, unsigned int * nfields, size_t * lengths);
86
87/* Same as above, but split on delimiter list (token)
88 */
89char ** split_array_token (char *line,
90 unsigned int * nfields, size_t * lengths,
91 const char * token);
92
93/* Return a split_array_list() of a list contained in 'PREFIX\s*( list ).*'
94 */
95char ** split_array_braced (char *line, const char * prefix,
96 unsigned int * nfields, size_t * lengths);
97
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 */
104sh_string * sh_string_replace(const sh_string * s,
105 const int * ovector, int ovecnum,
106 const char * replacement, size_t rlen);
107
108#endif
Note: See TracBrowser for help on using the repository browser.