1 /*
2  * Copyright (c) 2007-2009  Marko Kreen, Skype Technologies OÜ
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /**
18  * \file
19  * Theme include for strings.
20  */
21 
22 #ifndef _USUAL_STRING_H_
23 #define _USUAL_STRING_H_
24 
25 #include <usual/cxalloc.h>
26 
27 #include <string.h>
28 
29 /**
30  * @name  List of strings.
31  * @{
32  */
33 
34 /** Callback signature */
35 typedef bool (*str_cb)(void *arg, const char *s);
36 
37 struct StrList;
38 /** Allocate new string list */
39 struct StrList *strlist_new(CxMem *ca);
40 /** Free string string */
41 void strlist_free(struct StrList *slist);
42 /** Check if empty */
43 bool strlist_empty(struct StrList *slist);
44 /** Append copy of string. */
45 bool strlist_append(struct StrList *slist, const char *str);
46 /** Append reference, strlist now owns it. */
47 bool strlist_append_ref(struct StrList *slist, char *str);
48 /** Call function on each element */
49 bool strlist_foreach(const struct StrList *slist, str_cb cb_func, void *cb_arg);
50 /** Remove and return first element */
51 char *strlist_pop(struct StrList *slist);
52 /* @} */
53 
54 /** Parse comma-separated elements from string and launch callback for each of them. */
55 bool parse_word_list(const char *s, str_cb cb_func, void *cb_arg);
56 
57 #ifndef HAVE_STRNLEN
58 #undef strnlen
59 #define strnlen(a,b) usual_strnlen(a,b)
60 /** Compat: determine the length of a fixed-size string */
61 size_t strnlen(const char *string, size_t maxlen);
62 #endif
63 
64 #ifndef HAVE_STRLCPY
65 #undef strlcpy
66 #define strlcpy(a,b,c) usual_strlcpy(a,b,c)
67 /** Compat: Safely copy string to fixed-length buffer.  */
68 size_t strlcpy(char *dst, const char *src, size_t n);
69 #endif
70 
71 #ifndef HAVE_STRLCAT
72 #undef strlcat
73 #define strlcat(a,b,c) usual_strlcat(a,b,c)
74 /** Compat: Safely append string to fixed-length buffer. */
75 size_t strlcat(char *dst, const char *src, size_t n);
76 #endif
77 
78 #undef strpcpy
79 #define strpcpy(a,b,c) usual_strpcpy(a,b,c)
80 
81 /**
82  * Safe string copy.
83  *
84  * Returns pointer to end of string in dst or NULL
85  * if truncation occured.  Destination will be
86  * zero-terminated unless dstlen is 0.
87  */
88 char *strpcpy(char *dst, const char *src, size_t dstlen);
89 
90 #undef strpcat
91 #define strpcat(a,b,c) usual_strpcat(a,b,c)
92 
93 /**
94  * Safe string concat.
95  *
96  * Returns pointer to end of string in dst or NULL if truncation occured.
97  * Destination will be zero-terminated, unless dstlen is 0 or existing
98  * contents were not zero-terminated.
99  */
100 char *strpcat(char *dst, const char *src, size_t dstlen);
101 
102 
103 #ifndef HAVE_MEMRCHR
104 #undef memrchr
105 #define memrchr(a,b,c) usual_memrchr(a,b,c)
106 /** Compat: find byte in reverse direction */
107 void *memrchr(const void *s, int c, size_t n);
108 #endif
109 
110 #ifndef HAVE_MEMMEM
111 #undef memmem
112 #define memmem(a,b,c,d) usual_memmem(a,b,c,d)
113 /** Compat: find memory area */
114 void *memmem(const void *s, size_t slen, const void *q, size_t qlen);
115 #endif
116 
117 #ifndef HAVE_MEMPCPY
118 #undef mempcpy
119 #define mempcpy(a,b,c) usual_mempcpy(a,b,c)
120 /** Copy memory, return pointer to end. */
121 void *mempcpy(void *dst, const void *src, size_t len);
122 #endif
123 
124 /** Return position to first byte that is in 'find'. */
125 void *mempbrk(const void *data, size_t dlen, const void *find, size_t flen);
126 
127 /** Return number of bytes where none are in reject. */
128 size_t memcspn(const void *data, size_t dlen, const void *reject, size_t rlen);
129 
130 /** Return number of bytes where all are in accept. */
131 size_t memspn(const void *data, size_t dlen, const void *accept, size_t alen);
132 
133 #ifndef HAVE_BASENAME
134 #undef basename
135 #define basename(a) usual_basename(a)
136 /** Compat: Return pointer to last non-path element.
137     Never modifies path, returns either pointer inside path or static buffer.  */
138 const char *basename(const char *path);
139 #endif
140 
141 #ifndef HAVE_DIRNAME
142 #undef dirname
143 #define dirname(a) usual_dirname(a)
144 /** Compat: Return directory part of pathname.
145     Never modifies path, returns either pointer inside path or static buffer.  */
146 const char *dirname(const char *path);
147 #endif
148 
149 #ifndef HAVE_EXPLICIT_BZERO
150 #undef explicit_bzero
151 #define explicit_bzero(a,b) usual_explicit_bzero(a,b)
152 /** Definitely clear memory */
153 void explicit_bzero(void *buf, size_t len);
154 #endif
155 
156 /*
157  * strerror, strerror_r
158  */
159 
160 #ifdef WIN32
161 const char *win32_strerror(int e);
162 /** Compat: strerror() for win32 */
163 #define strerror(x) win32_strerror(x)
164 #endif
165 
166 const char *usual_strerror_r(int e, char *dst, size_t dstlen);
167 /** Compat: Provide GNU-style API: const char *strerror_r(int e, char *dst, size_t dstlen)  */
168 #define strerror_r(a,b,c) usual_strerror_r(a,b,c)
169 
170 /** strtod() that uses '.' as decimal separator */
171 double strtod_dot(const char *s, char **tokend);
172 
173 /** Convert double to string with '.' as decimal separator */
174 ssize_t dtostr_dot(char *buf, size_t buflen, double val);
175 
176 #ifndef HAVE_STRTONUM
177 #undef strtonum
178 #define strtonum(a,b,c,d) usual_strtonum(a,b,c,d)
179 /**
180  * Convert string to integer, check limits.
181  *
182  * Accepts only decimal numbers, no octal or hex.  Allows leading whitespace,
183  * but not tailing.
184  *
185  * On success, returns value that is minval <= res <= maxval.  If errstr_p is given
186  * it stores NULL there.  Keeps old errno value.
187  *
188  * On error, returns 0, sets errno, and if errstr_p is given, stores error reason there.
189  */
190 long long strtonum(const char *s, long long minval, long long maxval, const char **errstr_p);
191 #endif
192 
193 #ifndef HAVE_STRSEP
194 #undef strsep
195 #define strsep(a,b) usual_strsep(a,b)
196 /**
197  * Return next token from string.
198  *
199  * Tokens are separated by delim chars
200  * Modifies string in-place.
201  */
202 char *strsep(char **stringp, const char *delim);
203 #endif
204 
205 #ifndef HAVE_ASPRINTF
206 #undef asprintf
207 #define asprintf(dst_p, fmt, ...) usual_asprintf(dst_p, fmt, __VA_ARGS__)
208 int asprintf(char **dst_p, const char *fmt, ...) _PRINTF(2, 3);
209 #endif
210 
211 #ifndef HAVE_VASPRINTF
212 #undef vasprintf
213 #define vasprintf(dst_p, fmt, ap) usual_vasprintf(dst_p, fmt, ap)
214 int vasprintf(char **dst_p, const char *fmt, va_list ap) _PRINTF(2, 0);
215 #endif
216 
217 #endif
218