1 /*
2  * wzdftpd - a modular and cool ftp server
3  * Copyright (C) 2002-2004  Pierre Chifflier
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * As a special exemption, Pierre Chifflier
20  * and other respective copyright holders give permission to link this program
21  * with OpenSSL, and distribute the resulting executable, without including
22  * the source code for OpenSSL in the source distribution.
23  */
24 
25 #ifndef __WZD_STRING__
26 #define __WZD_STRING__
27 
28 #include <stdarg.h> /* va_list */
29 
30 typedef struct wzd_string_t wzd_string_t;
31 
32 wzd_string_t * str_allocate(void);
33 void str_deallocate(wzd_string_t *st);
34 
35 /** \brief Deallocates a NULL-terminated string list
36  */
37 void str_deallocate_array(wzd_string_t **array);
38 
39 /** returns a pointer to a new string which is a duplicate of the string str.
40  */
41 wzd_string_t * str_fromchar(const char *str);
42 
43 #define STR(x) str_fromchar((x))
44 
45 /** returns a pointer to a new string pointing to \a str
46  *
47  * \note \a str must not be freed, you must use str_deallocate() on the result
48  */
49 wzd_string_t * str_fromchar_raw(char *str);
50 
51 #define STR_RAW(x) str_fromchar_raw((x))
52 
53 /** returns a pointer to the data contained in the string str.
54  * These data must NOT be modified !
55  */
56 const char * str_tochar(const wzd_string_t *str);
57 
58 /** returns 1 if string exists and length is inside min and max (included)
59  */
60 unsigned int str_checklength(const wzd_string_t *str, size_t min, size_t max);
61 
62 /** Get the length of the given string, or -1 if error
63  */
64 size_t str_length(const wzd_string_t *str);
65 
66 /** \brief Store a copy of the argument into \a str
67  */
68 wzd_string_t * str_store(wzd_string_t * str, const char * s);
69 
70 /** \brief returns a pointer to a new string which is a duplicate of the string src.
71  */
72 wzd_string_t * str_dup(const wzd_string_t *src);
73 
74 /** \brief copies the string pointed to by src (including the terminating `\\0'
75  * character) to the array pointed to by  dest.
76  */
77 wzd_string_t * str_copy(wzd_string_t *dst, const wzd_string_t *src);
78 
79 /** \brief append \a tail to string pointed to by \a str
80  */
81 wzd_string_t * str_append(wzd_string_t * str, const char *tail);
82 
83 /** \brief append character \a c to string pointed to by str
84  */
85 wzd_string_t * str_append_c(wzd_string_t * str, const char c);
86 
87 /** \brief prepend 'head' to string pointed to by str
88  */
89 wzd_string_t * str_prepend(wzd_string_t * str, const char *head);
90 
91 /** \brief remove all leading and trailing spaces from input string
92  */
93 wzd_string_t * str_trim(wzd_string_t * str);
94 wzd_string_t * str_trim_left(wzd_string_t *str);
95 wzd_string_t * str_trim_right(wzd_string_t *str);
96 
97 /** \brief Removes \a len characters from a wzd_string_t, starting at position \a pos.
98  *
99  * The rest of the wzd_string_t is shifted down to fill the gap.
100  */
101 wzd_string_t * str_erase(wzd_string_t * str, size_t pos, int len);
102 
103 /** \brief Convert string to lower case
104  * \note
105  * This function modifies its input string
106  */
107 wzd_string_t * str_tolower(wzd_string_t *str);
108 
109 /** \brief Extract token from string str
110  * \note
111  * This function modifies its input string
112  */
113 wzd_string_t * str_tok(wzd_string_t *str, const char *delim);
114 
115 /** \brief str_read next token
116  * \return a pointer to the next token, or NULL if not found, or if there is
117  * only whitespaces, or if quotes are unbalanced
118  *
119  * Read next token separated by a whitespace, except if string begins
120  * with a ' or ", in this case it searches the matching character.
121  * Note: input string is modified as a \0 is written.
122  */
123 wzd_string_t * str_read_token(wzd_string_t *str);
124 
125 /** \brief Produce output according to format and variable number of arguments,
126  * and write output to str.
127  */
128 int str_sprintf(wzd_string_t *str, const char *format, ...);
129 
130 /** \brief Produce output according to \a format and variable number of arguments,
131  * and write output to \a str.
132  */
133 int str_vsprintf(wzd_string_t *str, const char *format, va_list ap);
134 
135 /** \brief Prepend formatted output to string
136  */
137 int str_prepend_printf(wzd_string_t *str, const char *format, ...);
138 
139 /** \brief Append formatted output to string
140  */
141 int str_append_printf(wzd_string_t *str, const char *format, ...);
142 
143 /** \brief Split \a str into a maximum of \a max_tokens pieces, separated by \a sep.
144  *
145  * If \a max_tokens is reached, the remainder of \a str is appended to the last token.
146  *
147  * \return a NULL-terminated string array, or NULL. The array must be freed using
148  * str_deallocate_array().
149  */
150 wzd_string_t ** str_split(wzd_string_t * str, const char * sep, int max_tokens);
151 
152 /** \brief Convert utf8 string to other charset
153  * \note
154  * Require unicode support
155  */
156 int str_utf8_to_local(wzd_string_t *str, const char * charset);
157 
158 /** \brief Convert charset to utf8 string
159  * \note
160  * Require unicode support
161  */
162 int str_local_to_utf8(wzd_string_t *str, const char * charset);
163 
164 /** \brief test if string is valid utf8
165  * \note
166  * require unicode support
167  */
168 int str_is_valid_utf8(wzd_string_t *str);
169 
170 #endif /* __WZD_STRING__ */
171 
172