2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
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 $ */
8 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
24 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
42 #include "eina_private.h"
45 /*============================================================================*
47 *============================================================================*/
54 * Internal helper function used by eina_str_has_suffix() and
55 * eina_str_has_extension()
57 static inline Eina_Bool
58 eina_str_has_suffix_helper(const char *str,
60 int (*cmp)(const char *, const char *))
65 str_len = strlen(str);
66 suffix_len = eina_strlen_bounded(suffix, str_len);
67 if (suffix_len == (size_t)-1)
70 return cmp(str + str_len - suffix_len, suffix) == 0;
74 eina_str_split_full_helper(const char *str, const char *delim, int max_tokens, unsigned int *elements)
84 if (elements) *elements = 0;
90 /* count tokens and check strlen(str) */
93 const char *d = delim, *d_end = d + dlen;
94 const char *tmp = src;
95 for (; (d < d_end) && (*tmp != '\0'); d++, tmp++)
97 if (EINA_LIKELY(*d != *tmp))
100 if (EINA_UNLIKELY(d == d_end))
110 if ((max_tokens > 0) && (tokens > (unsigned int)max_tokens))
113 str_array = malloc(sizeof(char *) * (tokens + 2));
116 if (elements) *elements = 0;
124 if (elements) *elements = 0;
128 /* copy tokens and string */
134 const char *d = delim, *d_end = d + dlen;
135 const char *tmp = src;
136 for (; (d < d_end) && (*tmp != '\0'); d++, tmp++)
138 if (EINA_LIKELY(*d != *tmp))
141 if (EINA_UNLIKELY(d == d_end))
147 str_array[tokens] = s;
157 str_array[tokens + 1] = NULL;
158 if (elements) *elements = (tokens + 1);
166 /*============================================================================*
168 *============================================================================*/
170 /*============================================================================*
172 *============================================================================*/
175 * @addtogroup Eina_String_Group String
177 * @brief These functions provide useful C string management.
184 * @brief Copy a c-string to another.
186 * @param dst The destination string.
187 * @param src The source string.
188 * @param siz The size of the destination string.
189 * @return The length of the source string.
191 * This function copies up to @p siz - 1 characters from the
192 * NUL-terminated string @p src to @p dst, NUL-terminating the result
193 * (unless @p siz is equal to 0). The returned value is the length of
194 * @p src. If the returned value is greater than @p siz, truncation
198 eina_strlcpy(char *dst, const char *src, size_t siz)
201 return strlcpy(dst, src, siz);
207 /* Copy as many bytes as will fit */
212 if ((*d++ = *s++) == '\0')
217 /* Not enough room in dst, add NUL and traverse rest of src */
221 *d = '\0'; /* NUL-terminate dst */
226 return(s - src - 1); /* count does not include NUL */
231 * @brief Append a c-string.
233 * @param dst The destination string.
234 * @param src The source string.
235 * @param siz The size of the destination string.
236 * @return The length of the source string plus MIN(siz, strlen(initial dst))
238 * This function appends @p src to @p dst of size @p siz (unlike
239 * strncat, @p siz is the full size of @p dst, not space left). At
240 * most @p siz - 1 characters will be copied. Always NUL terminates
241 * (unless @p siz <= strlen(dst)). This function returns strlen(src) +
242 * MIN(siz, strlen(initial dst)). If the returned value is greater or
243 * equal than @p siz, truncation occurred.
246 eina_strlcat(char *dst, const char *src, size_t siz)
253 /* Find the end of dst and adjust bytes left but don't go past end */
254 while (n-- != 0 && *d != '\0')
260 return(dlen + strlen(s));
270 return(dlen + (s - src)); /* count does not include NUL */
274 * @brief Check if the given string has the given prefix.
276 * @param str The string to work with.
277 * @param prefix The prefix to check for.
278 * @return #EINA_TRUE if the string has the given prefix, #EINA_FALSE otherwise.
280 * This function returns #EINA_TRUE if @p str has the prefix
281 * @p prefix, #EINA_FALSE otherwise. If the length of @p prefix is
282 * greater than @p str, #EINA_FALSE is returned.
285 eina_str_has_prefix(const char *str, const char *prefix)
290 str_len = strlen(str);
291 prefix_len = eina_strlen_bounded(prefix, str_len);
292 if (prefix_len == (size_t)-1)
295 return (strncmp(str, prefix, prefix_len) == 0);
299 * @brief Check if the given string has the given suffix.
301 * @param str The string to work with.
302 * @param suffix The suffix to check for.
303 * @return #EINA_TRUE if the string has the given suffix, #EINA_FALSE otherwise.
305 * This function returns #EINA_TRUE if @p str has the suffix
306 * @p suffix, #EINA_FALSE otherwise. If the length of @p suffix is
307 * greater than @p str, #EINA_FALSE is returned.
310 * @param str the string to work with
311 * @param suffix the suffix to check for
312 * @return true if str has the given suffix
313 * @brief checks if the string has the given suffix
316 eina_str_has_suffix(const char *str, const char *suffix)
318 return eina_str_has_suffix_helper(str, suffix, strcmp);
322 * @brief Check if the given string has the given suffix.
324 * @param str The string to work with.
325 * @param ext The extension to check for.
326 * @return #EINA_TRUE if the string has the given extension, #EINA_FALSE otherwise.
328 * This function does the same like eina_str_has_suffix(), but with a
329 * case insensitive compare.
332 eina_str_has_extension(const char *str, const char *ext)
334 return eina_str_has_suffix_helper(str, ext, strcasecmp);
338 * @brief Split a string using a delimiter and returns number of elements.
340 * @param str The string to split.
341 * @param delim The string which specifies the places at which to split the string.
342 * @param max_tokens The maximum number of strings to split string into.
343 * @param elements Where to return the number of elements in returned
344 * array (not counting the terminating @c NULL). May be @c NULL.
345 * @return A newly-allocated NULL-terminated array of strings or NULL if it
346 * fails to allocate the array.
348 * This functin splits @p str into a maximum of @p max_tokens pieces,
349 * using the given delimiter @p delim. @p delim is not included in any
350 * of the resulting strings, unless @p max_tokens is reached. If
351 * @p max_tokens is less than @c 1, the string is splitted completely. If
352 * @p max_tokens is reached, the last string in the returned string
353 * array contains the remainder of string. The returned value is a
354 * newly allocated NULL-terminated array of strings or NULL if it fails to
355 * allocate the array. To free it, free the first element of the array and the
358 * @see eina_str_split()
361 eina_str_split_full(const char *str, const char *delim, int max_tokens, unsigned int *elements)
363 return eina_str_split_full_helper(str, delim, max_tokens, elements);
368 * @brief Split a string using a delimiter.
370 * @param str The string to split.
371 * @param delim The string which specifies the places at which to split the string.
372 * @param max_tokens The maximum number of strings to split string into.
373 * @return A newly-allocated NULL-terminated array of strings or NULL if it
374 * fails to allocate the array.
376 * This functin splits @p str into a maximum of @p max_tokens pieces,
377 * using the given delimiter @p delim. @p delim is not included in any
378 * of the resulting strings, unless @p max_tokens is reached. If
379 * @p max_tokens is less than @c 1, the string is splitted completely. If
380 * @p max_tokens is reached, the last string in the returned string
381 * array contains the remainder of string. The returned value is a
382 * newly allocated NULL-terminated array of strings or NULL if it fails to
383 * allocate the array. To free it, free the first element of the array and the
387 eina_str_split(const char *str, const char *delim, int max_tokens)
389 return eina_str_split_full_helper(str, delim, max_tokens, NULL);
393 * @brief Join two strings of known length.
395 * @param dst The buffer to store the result.
396 * @param size Size (in byte) of the buffer.
397 * @param sep The separator character to use.
398 * @param a First string to use, before @p sep.
399 * @param a_len length of @p a.
400 * @param b Second string to use, after @p sep.
401 * @param b_len length of @p b.
402 * @return The number of characters printed.
404 * This function joins the strings @p a and @p b (in that order) and
405 * separate them with @p sep. The result is stored in the buffer
406 * @p dst and at most @p size - 1 characters will be written and the
407 * string is NULL-terminated. @p a_len is the length of @p a (not
408 * including '\\0') and @p b_len is the length of @p b (not including
409 * '\\0'). This function returns the number of characters printed (not
410 * including the trailing '\\0' used to end output to strings). Just
411 * like snprintf(), it will not write more than @p size bytes, thus a
412 * returned value of @p size or more means that the output was
415 * @see eina_str_join()
416 * @see eina_str_join_static()
419 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)
421 size_t ret = a_len + b_len + 1;
424 if (size < 1) return ret;
428 memcpy(dst, a, size - 1);
429 dst[size - 1] = '\0';
433 memcpy(dst, a, a_len);
438 dst[size - 1] = '\0';
445 if (size <= off + b_len + 1)
447 memcpy(dst + off, b, size - off - 1);
448 dst[size - 1] = '\0';
452 memcpy(dst + off, b, b_len);
453 dst[off + b_len] = '\0';
458 * @brief Use iconv to convert a text string from one encoding to another
460 * @param enc_from encoding to convert from
461 * @param enc_to encoding to convert to
462 * @param text text to convert
467 eina_str_convert(const char *enc_from, const char *enc_to, const char *text)
470 char *new_txt, *inp, *outp;
471 size_t inb, outb, outlen, tob, outalloc;
473 if (!text) return NULL;
474 ic = iconv_open(enc_to, enc_from);
475 if (ic == (iconv_t)(-1)) return NULL;
476 new_txt = malloc(64);
489 count = iconv(ic, &inp, &inb, &outp, &outb);
490 outlen += tob - outb;
491 if (count == (size_t)(-1))
495 new_txt = realloc(new_txt, outalloc + 64);
496 outp = new_txt + outlen;
500 else if (errno == EILSEQ)
502 if (new_txt) free(new_txt);
506 else if (errno == EINVAL)
508 if (new_txt) free(new_txt);
514 if (new_txt) free(new_txt);
521 if (outalloc == outlen) new_txt = realloc(new_txt, outalloc + 1);
531 eina_str_convert(const char *enc_from __UNUSED__, const char *enc_to __UNUSED__, const char *text __UNUSED__)
538 * @brief Put a \ before and Space( ), \ or ' in a string.
540 * @param str the string to escape
542 * A newly allocated string is returned.
545 eina_str_escape(const char *str)
550 s2 = malloc((strlen(str) * 2) + 1);
551 if (!s2) return NULL;
552 for (s = str, d = s2; *s != 0; s++, d++)
554 if ((*s == ' ') || (*s == '\\') || (*s == '\''))
566 * @brief Lowercase all the characters in range [A-Z] in the given string.
568 * @param str the string to lowercase
570 * This modifies the original string, changing all characters in [A-Z] to lowercase.
573 eina_str_tolower(char **str)
576 if ((!str) || (!(*str))) return;
578 for (p = *str; (*p); p++)
579 if ((*p >= 'A') && (*p <= 'Z'))