EAPI
[framework/uifw/eina.git] / src / lib / eina_str.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4 /* Leave the OpenBSD version below so we can track upstream fixes */
5 /*      $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $        */
6
7 /*
8  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
9  *
10  * Permission to use, copy, modify, and distribute this software for any
11  * purpose with or without fee is hereby granted, provided that the above
12  * copyright notice and this permission notice appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 /*
24  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <limits.h>
34
35 #include "eina_private.h"
36
37 static int eina_str_has_suffix_helper(const char *str, const char *suffix,
38                 int (*cmp)(const char *, const char *));
39 /**
40  * @param dst the destination
41  * @param src the source
42  * @param siz the size of the destination
43  * @return the length of the source string
44  * @brief copy a c-string
45  *
46  * Copy src to string dst of size siz.  At most siz-1 characters
47  * will be copied.  Always NUL terminates (unless siz == 0).
48  * Returns strlen(src); if retval >= siz, truncation occurred.
49  */
50 EAPI size_t
51 eina_strlcpy(char *dst, const char *src, size_t siz)
52 {
53 #ifdef HAVE_STRLCPY
54    return strlcpy(dst, src, siz);
55 #else
56    char *d = dst;
57    const char *s = src;
58    size_t n = siz;
59
60    /* Copy as many bytes as will fit */
61    if (n != 0)
62      {
63         while (--n != 0)
64           {
65              if ((*d++ = *s++) == '\0')
66                break;
67           }
68      }
69
70    /* Not enough room in dst, add NUL and traverse rest of src */
71    if (n == 0)
72      {
73         if (siz != 0)
74           *d = '\0';                /* NUL-terminate dst */
75         while (*s++)
76           ;
77      }
78
79    return(s - src - 1);        /* count does not include NUL */
80 #endif
81 }
82
83 /**
84  * @param dst the destination
85  * @param src the source
86  * @param siz the size of the destination
87  * @return the length of the source string plus MIN(siz, strlen(initial dst))
88  * @brief append a c-string
89  *
90  * Appends src to string dst of size siz (unlike strncat, siz is the
91  * full size of dst, not space left).  At most siz-1 characters
92  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
93  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
94  * If retval >= siz, truncation occurred.
95  */
96 EAPI size_t
97 eina_strlcat(char *dst, const char *src, size_t siz)
98 {
99    char *d = dst;
100    const char *s = src;
101    size_t n = siz;
102    size_t dlen;
103
104    /* Find the end of dst and adjust bytes left but don't go past end */
105    while (n-- != 0 && *d != '\0')
106      d++;
107    dlen = d - dst;
108    n = siz - dlen;
109
110    if (n == 0)
111      return(dlen + strlen(s));
112    while (*s != '\0') {
113         if (n != 1) {
114              *d++ = *s;
115              n--;
116         }
117         s++;
118    }
119    *d = '\0';
120
121    return(dlen + (s - src));        /* count does not include NUL */
122 }
123
124 /**
125  * @param str the string to work with
126  * @param prefix the prefix to check for
127  * @return true if str has the given prefix
128  * @brief checks if the string has the given prefix
129  */
130 EAPI int
131 eina_str_has_prefix(const char *str, const char *prefix)
132 {
133    size_t str_len;
134    size_t prefix_len;
135
136    str_len = strlen(str);
137    prefix_len = strlen(prefix);
138    if (prefix_len > str_len)
139      return 0;
140
141    return (strncmp(str, prefix, prefix_len) == 0);
142 }
143
144 /**
145  * @param str the string to work with
146  * @param suffix the suffix to check for
147  * @return true if str has the given suffix
148  * @brief checks if the string has the given suffix
149  */
150 EAPI int
151 eina_str_has_suffix(const char *str, const char *suffix)
152 {
153    return eina_str_has_suffix_helper(str, suffix, strcmp);
154 }
155
156 /**
157  * This function does the same like eina_str_has_suffix(), but with a
158  * case insensitive compare.
159  *
160  * @param str the string to work with
161  * @param ext the  extension to check for
162  * @return true if str has the given extension
163  * @brief checks if the string has the given extension
164  */
165 EAPI int
166 eina_str_has_extension(const char *str, const char *ext)
167 {
168    return eina_str_has_suffix_helper(str, ext, strcasecmp);
169 }
170
171 /*
172  * Internal helper function used by eina_str_has_suffix() and
173  * eina_str_has_extension()
174  */
175 static int
176 eina_str_has_suffix_helper(const char *str, const char *suffix,
177                 int (*cmp)(const char *, const char *))
178 {
179    size_t str_len;
180    size_t suffix_len;
181
182    str_len = strlen(str);
183    suffix_len = strlen(suffix);
184    if (suffix_len > str_len)
185      return 0;
186
187    return cmp(str + str_len - suffix_len, suffix) == 0;
188 }
189
190 /**
191  * Splits a string into a maximum of max_tokens pieces, using the given
192  * delimiter. If max_tokens is reached, the final string in the returned
193  * string array contains the remainder of string.
194  *
195  * @param str         A string to split.
196  * @param delim       A string which specifies the places at which to split the
197  *                    string. The delimiter is not included in any of the
198  *                    resulting strings, unless max_tokens is reached.
199  * @param max_tokens  The maximum number of strings to split string into.
200  *                    If this is less than 1, the string is split completely.
201  * @return            A newly-allocated NULL-terminated array of strings.
202  *                    To free it: free the first element of the array
203  *                    and the array itself.
204  */
205 EAPI char **
206 eina_str_split(const char *str, const char *delim, int max_tokens)
207 {
208    char *s, *sep, **str_array;
209    size_t len, dlen;
210    int i;
211
212    if (*delim == '\0')
213      return NULL;
214
215    max_tokens = ((max_tokens <= 0) ? (INT_MAX) : (max_tokens - 1));
216    len = strlen(str);
217    dlen = strlen(delim);
218    s = strdup(str);
219    str_array = malloc(sizeof(char *) * (len + 1));
220    for (i = 0; (i < max_tokens) && (sep = strstr(s, delim)); i++)
221      {
222         str_array[i] = s;
223         s = sep + dlen;
224         *sep = 0;
225      }
226
227    str_array[i++] = s;
228    str_array = realloc(str_array, sizeof(char *) * (i + 1));
229    str_array[i] = NULL;
230
231    return str_array;
232 }
233
234 /**
235  * Join two strings of known length and store the result in @a dst buffer.
236  *
237  * @param dst where to store the result.
238  * @param size byte size of dst, will write at most (size - 1)
239  *     characters and then the '\0' (null terminator).
240  * @param sep separator character to use.
241  * @param a first string to use, before @a sep.
242  * @param a_len length of @a a, not including '\0' (strlen()-like)
243  * @param b second string to use, after @a sep.
244  * @param b_len length of @a b, not including '\0' (strlen()-like)
245  *
246  * @return the number of characters printed (not including the
247  *     trailing '\0' used to end output to strings). Just like
248  *     snprintf(), it will not write more than @a size bytes, thus a
249  *     return value of @a size or more means that the output was
250  *     truncated.
251  *
252  * @see eina_str_join() and eina_str_join_static()
253  */
254 EAPI size_t
255 eina_str_join_len(char *dst, size_t size, char sep, const char *a, size_t a_len, const char *b, size_t b_len)
256 {
257    size_t ret = a_len + b_len + 1;
258    size_t off;
259
260    if (size < 1) return ret;
261
262    if (size <= a_len)
263      {
264         memcpy(dst, a, size - 1);
265         dst[size - 1] = '\0';
266         return ret;
267      }
268
269    memcpy(dst, a, a_len);
270    off = a_len;
271
272    if (size <= off + 1)
273      {
274         dst[size - 1] = '\0';
275         return ret;
276      }
277
278    dst[off] = sep;
279    off++;
280
281    if (size <= off + b_len + 1)
282      {
283         memcpy(dst + off, b, size - off - 1);
284         dst[size - 1] = '\0';
285         return ret;
286      }
287
288    memcpy(dst + off, b, b_len);
289    dst[off + b_len] = '\0';
290    return ret;
291 }