Move ecore_str and ecore_strbuf to eina
[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 static int eina_str_has_suffix_helper(const char *str, const char *suffix,
36                 int (*cmp)(const char *, const char *));
37 /**
38  * @param dst the destination
39  * @param src the source
40  * @param siz the size of the destination
41  * @return the length of the source string
42  * @brief copy a c-string
43  *
44  * Copy src to string dst of size siz.  At most siz-1 characters
45  * will be copied.  Always NUL terminates (unless siz == 0).
46  * Returns strlen(src); if retval >= siz, truncation occurred.
47  */
48 size_t
49 eina_strlcpy(char *dst, const char *src, size_t siz)
50 {
51 #ifdef HAVE_STRLCPY
52    return strlcpy(dst, src, siz);
53 #else
54    char *d = dst;
55    const char *s = src;
56    size_t n = siz;
57
58    /* Copy as many bytes as will fit */
59    if (n != 0)
60      {
61         while (--n != 0)
62           {
63              if ((*d++ = *s++) == '\0')
64                break;
65           }
66      }
67
68    /* Not enough room in dst, add NUL and traverse rest of src */
69    if (n == 0)
70      {
71         if (siz != 0)
72           *d = '\0';                /* NUL-terminate dst */
73         while (*s++)
74           ;
75      }
76
77    return(s - src - 1);        /* count does not include NUL */
78 #endif
79 }
80
81 /**
82  * @param dst the destination
83  * @param src the source
84  * @param siz the size of the destination
85  * @return the length of the source string plus MIN(siz, strlen(initial dst))
86  * @brief append a c-string
87  *
88  * Appends src to string dst of size siz (unlike strncat, siz is the
89  * full size of dst, not space left).  At most siz-1 characters
90  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
91  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
92  * If retval >= siz, truncation occurred.
93  */
94 size_t
95 eina_strlcat(char *dst, const char *src, size_t siz)
96 {
97    char *d = dst;
98    const char *s = src;
99    size_t n = siz;
100    size_t dlen;
101
102    /* Find the end of dst and adjust bytes left but don't go past end */
103    while (n-- != 0 && *d != '\0')
104      d++;
105    dlen = d - dst;
106    n = siz - dlen;
107
108    if (n == 0)
109      return(dlen + strlen(s));
110    while (*s != '\0') {
111         if (n != 1) {
112              *d++ = *s;
113              n--;
114         }
115         s++;
116    }
117    *d = '\0';
118
119    return(dlen + (s - src));        /* count does not include NUL */
120 }
121
122 /**
123  * @param str the string to work with
124  * @param prefix the prefix to check for
125  * @return true if str has the given prefix
126  * @brief checks if the string has the given prefix
127  */
128 int
129 eina_str_has_prefix(const char *str, const char *prefix)
130 {
131    size_t str_len;
132    size_t prefix_len;
133
134    str_len = strlen(str);
135    prefix_len = strlen(prefix);
136    if (prefix_len > str_len)
137      return 0;
138
139    return (strncmp(str, prefix, prefix_len) == 0);
140 }
141
142 /**
143  * @param str the string to work with
144  * @param suffix the suffix to check for
145  * @return true if str has the given suffix
146  * @brief checks if the string has the given suffix
147  */
148 int
149 eina_str_has_suffix(const char *str, const char *suffix)
150 {
151    return eina_str_has_suffix_helper(str, suffix, strcmp);
152 }
153
154 /**
155  * This function does the same like eina_str_has_suffix(), but with a
156  * case insensitive compare.
157  *
158  * @param str the string to work with
159  * @param ext the  extension to check for
160  * @return true if str has the given extension
161  * @brief checks if the string has the given extension
162  */
163 int
164 eina_str_has_extension(const char *str, const char *ext)
165 {
166    return eina_str_has_suffix_helper(str, ext, strcasecmp);
167 }
168
169 /*
170  * Internal helper function used by eina_str_has_suffix() and
171  * eina_str_has_extension()
172  */
173 static int
174 eina_str_has_suffix_helper(const char *str, const char *suffix,
175                 int (*cmp)(const char *, const char *))
176 {
177    size_t str_len;
178    size_t suffix_len;
179
180    str_len = strlen(str);
181    suffix_len = strlen(suffix);
182    if (suffix_len > str_len)
183      return 0;
184
185    return cmp(str + str_len - suffix_len, suffix) == 0;
186 }
187
188 /**
189  * Splits a string into a maximum of max_tokens pieces, using the given
190  * delimiter. If max_tokens is reached, the final string in the returned
191  * string array contains the remainder of string.
192  *
193  * @param str         A string to split.
194  * @param delim       A string which specifies the places at which to split the
195  *                    string. The delimiter is not included in any of the
196  *                    resulting strings, unless max_tokens is reached.
197  * @param max_tokens  The maximum number of strings to split string into.
198  *                    If this is less than 1, the string is split completely.
199  * @return            A newly-allocated NULL-terminated array of strings.
200  *                    To free it: free the first element of the array
201  *                    and the array itself.
202  */
203 char **
204 eina_str_split(const char *str, const char *delim, int max_tokens)
205 {
206    char *s, *sep, **str_array;
207    size_t len, dlen;
208    int i;
209
210    if (*delim == '\0')
211      return NULL;
212
213    max_tokens = ((max_tokens <= 0) ? (INT_MAX) : (max_tokens - 1));
214    len = strlen(str);
215    dlen = strlen(delim);
216    s = strdup(str);
217    str_array = malloc(sizeof(char *) * (len + 1));
218    for (i = 0; (i < max_tokens) && (sep = strstr(s, delim)); i++)
219      {
220         str_array[i] = s;
221         s = sep + dlen;
222         *sep = 0;
223      }
224
225    str_array[i++] = s;
226    str_array = realloc(str_array, sizeof(char *) * (i + 1));
227    str_array[i] = NULL;
228
229    return str_array;
230 }
231
232 /**
233  * Join two strings of known length and store the result in @a dst buffer.
234  *
235  * @param dst where to store the result.
236  * @param size byte size of dst, will write at most (size - 1)
237  *     characters and then the '\0' (null terminator).
238  * @param sep separator character to use.
239  * @param a first string to use, before @a sep.
240  * @param a_len length of @a a, not including '\0' (strlen()-like)
241  * @param b second string to use, after @a sep.
242  * @param b_len length of @a b, not including '\0' (strlen()-like)
243  *
244  * @return the number of characters printed (not including the
245  *     trailing '\0' used to end output to strings). Just like
246  *     snprintf(), it will not write more than @a size bytes, thus a
247  *     return value of @a size or more means that the output was
248  *     truncated.
249  *
250  * @see eina_str_join() and eina_str_join_static()
251  */
252 size_t
253 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)
254 {
255    size_t ret = a_len + b_len + 1;
256    size_t off;
257
258    if (size < 1) return ret;
259
260    if (size <= a_len)
261      {
262         memcpy(dst, a, size - 1);
263         dst[size - 1] = '\0';
264         return ret;
265      }
266
267    memcpy(dst, a, a_len);
268    off = a_len;
269
270    if (size <= off + 1)
271      {
272         dst[size - 1] = '\0';
273         return ret;
274      }
275
276    dst[off] = sep;
277    off++;
278
279    if (size <= off + b_len + 1)
280      {
281         memcpy(dst + off, b, size - off - 1);
282         dst[size - 1] = '\0';
283         return ret;
284      }
285
286    memcpy(dst + off, b, b_len);
287    dst[off + b_len] = '\0';
288    return ret;
289 }