1 /*
2    String utility functions
3    Copyright (C) 1999-2002, Joe Orton <joe@manyfish.co.uk>
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9 
10    This library 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 GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public
16    License along with this library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18    MA 02111-1307, USA
19 
20 */
21 
22 #ifndef NE_STRING_H
23 #define NE_STRING_H
24 
25 #include "ne_defs.h"
26 #include "ne_alloc.h"
27 
28 #include <stdarg.h>
29 
30 BEGIN_NEON_DECLS
31 
32 /* ne_token and ne_qtoken return the next token in *str between *str
33  * and separator character 'sep' or the NUL terminator. ne_qtoken
34  * skips over any parts quoted using a pair of any one of the
35  * characters given in 'quotes'.  After returning, *str will point to
36  * the next character after the separator, or NULL if no more
37  * separator characters were found.
38  *
39  * ne_qtoken may return NULL if unterminated quotes are found. */
40 char *ne_token(char **str, char sep);
41 char *ne_qtoken(char **str, char sep, const char *quotes);
42 
43 /* Return portion of 'str' with any characters in 'whitespace' shaved
44  * off the beginning and end.  Modifies str. */
45 char *ne_shave(char *str, const char *whitespace);
46 
47 /* Cleanse 'str' of non-printable characters.  'str' is modified
48  * in-place, and returned. */
49 char *ne_strclean(char *str);
50 
51 /* A base64 encoder: converts 'len' bytes of 'text' to base64.
52  * Returns malloc-allocated buffer; caller must free(). */
53 char *ne_base64(const unsigned char *text, size_t len);
54 
55 /* Base64 decoder; decodes NUL-terminated base64-encoded string
56  * 'data', places malloc-allocated raw data in '*out', returns length,
57  * or zero on decode error (in which case *out is undefined). */
58 size_t ne_unbase64(const char *data, unsigned char **out);
59 
60 /*** OBSOLETE INTERFACES ***/
61 char **split_string(const char *str, const char seperator,
62 		    const char *quotes, const char *whitespace);
63 char **split_string_c(const char *str, const char seperator,
64 		      const char *quotes, const char *whitespace, int *count);
65 char **pair_string(const char *str, const char compsep, const char kvsep,
66 		   const char *quotes, const char *whitespace);
67 void split_string_free(char **components);
68 void pair_string_free(char **pairs);
69 /*** END OF OBSOLETE INTERFACES */
70 
71 /* String buffer handling. (Strings are zero-terminated still).  A
72  * string buffer ne_buffer * which grows dynamically with the
73  * string. */
74 
75 typedef struct {
76     char *data; /* contents: null-terminated string. */
77     size_t used; /* used bytes in buffer */
78     size_t length; /* length of buffer */
79 } ne_buffer;
80 
81 /* Returns size of data in buffer, equiv to strlen(ne_buffer_data(buf)) */
82 #define ne_buffer_size(buf) ((buf)->used - 1)
83 
84 /* Concatenate all given strings onto the end of the buffer.  The
85  * strings must all be NUL-terminated, and MUST be followed by a NULL
86  * argument marking the end of the list.  */
87 void ne_buffer_concat(ne_buffer *buf, ...);
88 
89 /* Create a new ne_buffer. */
90 ne_buffer *ne_buffer_create(void);
91 
92 /* Create a new ne_buffer of given minimum size. */
93 ne_buffer *ne_buffer_ncreate(size_t size);
94 
95 /* Destroys (deallocates) a buffer */
96 void ne_buffer_destroy(ne_buffer *buf);
97 
98 /* Append a NUL-terminated string 'str' to buf. */
99 void ne_buffer_zappend(ne_buffer *buf, const char *str);
100 
101 /* Append 'len' bytes of 'data' to buf.  'data' does not need to be
102  * NUL-terminated. The resultant string will have a NUL-terminator,
103  * either way.  */
104 void ne_buffer_append(ne_buffer *buf, const char *data, size_t len);
105 
106 /* Empties the contents of buf; makes the buffer zero-length. */
107 void ne_buffer_clear(ne_buffer *buf);
108 
109 /* Grows the ne_buffer to a minimum size. */
110 void ne_buffer_grow(ne_buffer *buf, size_t size);
111 
112 void ne_buffer_altered(ne_buffer *buf);
113 
114 /* Destroys a buffer, WITHOUT freeing the data, and returns the
115  * data. */
116 char *ne_buffer_finish(ne_buffer *buf);
117 
118 /* Thread-safe strerror() wrapper; place system error for errno value
119  * 'errnum' in 'buffer', which is of length 'buflen'.  Returns
120  * 'buffer'. */
121 char *ne_strerror(int errnum, char *buffer, size_t buflen);
122 
123 /* ne_strnzcpy copies at most 'n'-1 bytes of 'src' to 'dest', and
124  * ensures that 'dest' is subsequently NUL-terminated. */
125 #define ne_strnzcpy(dest, src, n) do { \
126 strncpy(dest, src, n-1); dest[n-1] = '\0'; } while (0)
127 
128 /* Return malloc-allocated concatenation of all NUL-terminated string
129  * arguments, up to a terminating NULL. */
130 char *ne_concat(const char *str, ...);
131 
132 #define NE_ASC2HEX(x) (((x) <= '9') ? ((x) - '0') : (tolower((x)) + 10 - 'a'))
133 #define NE_HEX2ASC(x) ((char) ((x) > 9 ? ((x) - 10 + 'a') : ((x) + '0')))
134 
135 END_NEON_DECLS
136 
137 #endif /* NE_STRING_H */
138