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,
77 unsigned int *elements)
95 /* count tokens and check strlen(str) */
98 const char *d = delim, *d_end = d + dlen;
99 const char *tmp = src;
100 for (; (d < d_end) && (*tmp != '\0'); d++, tmp++)
102 if (EINA_LIKELY(*d != *tmp))
105 if (EINA_UNLIKELY(d == d_end))
115 if ((max_tokens > 0) && (tokens > (unsigned int)max_tokens))
118 str_array = malloc(sizeof(char *) * (tokens + 2));
137 /* copy tokens and string */
143 const char *d = delim, *d_end = d + dlen;
144 const char *tmp = src;
145 for (; (d < d_end) && (*tmp != '\0'); d++, tmp++)
147 if (EINA_LIKELY(*d != *tmp))
150 if (EINA_UNLIKELY(d == d_end))
156 str_array[tokens] = s;
166 str_array[tokens + 1] = NULL;
168 *elements = (tokens + 1);
177 /*============================================================================*
179 *============================================================================*/
181 /*============================================================================*
183 *============================================================================*/
186 * @addtogroup Eina_String_Group String
188 * @brief These functions provide useful C string management.
195 * @brief Copy a c-string to another.
197 * @param dst The destination string.
198 * @param src The source string.
199 * @param siz The size of the destination string.
200 * @return The length of the source string.
202 * This function copies up to @p siz - 1 characters from the
203 * NUL-terminated string @p src to @p dst, NUL-terminating the result
204 * (unless @p siz is equal to 0). The returned value is the length of
205 * @p src. If the returned value is greater than @p siz, truncation
209 eina_strlcpy(char *dst, const char *src, size_t siz)
212 return strlcpy(dst, src, siz);
218 /* Copy as many bytes as will fit */
222 if ((*d++ = *s++) == '\0')
226 /* Not enough room in dst, add NUL and traverse rest of src */
231 *d = '\0'; /* NUL-terminate dst */
238 return(s - src - 1); /* count does not include NUL */
243 * @brief Append a c-string.
245 * @param dst The destination string.
246 * @param src The source string.
247 * @param siz The size of the destination string.
248 * @return The length of the source string plus MIN(siz, strlen(initial dst))
250 * This function appends @p src to @p dst of size @p siz (unlike
251 * strncat, @p siz is the full size of @p dst, not space left). At
252 * most @p siz - 1 characters will be copied. Always NUL terminates
253 * (unless @p siz <= strlen(dst)). This function returns strlen(src) +
254 * MIN(siz, strlen(initial dst)). If the returned value is greater or
255 * equal than @p siz, truncation occurred.
258 eina_strlcat(char *dst, const char *src, size_t siz)
265 /* Find the end of dst and adjust bytes left but don't go past end */
266 while (n-- != 0 && *d != '\0')
272 return(dlen + strlen(s));
285 return(dlen + (s - src)); /* count does not include NUL */
289 * @brief Check if the given string has the given prefix.
291 * @param str The string to work with.
292 * @param prefix The prefix to check for.
293 * @return #EINA_TRUE if the string has the given prefix, #EINA_FALSE otherwise.
295 * This function returns #EINA_TRUE if @p str has the prefix
296 * @p prefix, #EINA_FALSE otherwise. If the length of @p prefix is
297 * greater than @p str, #EINA_FALSE is returned.
300 eina_str_has_prefix(const char *str, const char *prefix)
305 str_len = strlen(str);
306 prefix_len = eina_strlen_bounded(prefix, str_len);
307 if (prefix_len == (size_t)-1)
310 return (strncmp(str, prefix, prefix_len) == 0);
314 * @brief Check if the given string has the given suffix.
316 * @param str The string to work with.
317 * @param suffix The suffix to check for.
318 * @return #EINA_TRUE if the string has the given suffix, #EINA_FALSE otherwise.
320 * This function returns #EINA_TRUE if @p str has the suffix
321 * @p suffix, #EINA_FALSE otherwise. If the length of @p suffix is
322 * greater than @p str, #EINA_FALSE is returned.
325 * @param str the string to work with
326 * @param suffix the suffix to check for
327 * @return true if str has the given suffix
328 * @brief checks if the string has the given suffix
331 eina_str_has_suffix(const char *str, const char *suffix)
333 return eina_str_has_suffix_helper(str, suffix, strcmp);
337 * @brief Check if the given string has the given suffix.
339 * @param str The string to work with.
340 * @param ext The extension to check for.
341 * @return #EINA_TRUE if the string has the given extension, #EINA_FALSE otherwise.
343 * This function does the same like eina_str_has_suffix(), but with a
344 * case insensitive compare.
347 eina_str_has_extension(const char *str, const char *ext)
349 return eina_str_has_suffix_helper(str, ext, strcasecmp);
353 * @brief Split a string using a delimiter and returns number of elements.
355 * @param str The string to split.
356 * @param delim The string which specifies the places at which to split the string.
357 * @param max_tokens The maximum number of strings to split string into.
358 * @param elements Where to return the number of elements in returned
359 * array (not counting the terminating @c NULL). May be @c NULL.
360 * @return A newly-allocated NULL-terminated array of strings or NULL if it
361 * fails to allocate the array.
363 * This functin splits @p str into a maximum of @p max_tokens pieces,
364 * using the given delimiter @p delim. @p delim is not included in any
365 * of the resulting strings, unless @p max_tokens is reached. If
366 * @p max_tokens is less than @c 1, the string is splitted completely. If
367 * @p max_tokens is reached, the last string in the returned string
368 * array contains the remainder of string. The returned value is a
369 * newly allocated NULL-terminated array of strings or NULL if it fails to
370 * allocate the array. To free it, free the first element of the array and the
373 * @see eina_str_split()
376 eina_str_split_full(const char *str,
379 unsigned int *elements)
381 return eina_str_split_full_helper(str, delim, max_tokens, elements);
386 * @brief Split a string using a delimiter.
388 * @param str The string to split.
389 * @param delim The string which specifies the places at which to split the string.
390 * @param max_tokens The maximum number of strings to split string into.
391 * @return A newly-allocated NULL-terminated array of strings or NULL if it
392 * fails to allocate the array.
394 * This functin splits @p str into a maximum of @p max_tokens pieces,
395 * using the given delimiter @p delim. @p delim is not included in any
396 * of the resulting strings, unless @p max_tokens is reached. If
397 * @p max_tokens is less than @c 1, the string is splitted completely. If
398 * @p max_tokens is reached, the last string in the returned string
399 * array contains the remainder of string. The returned value is a
400 * newly allocated NULL-terminated array of strings or NULL if it fails to
401 * allocate the array. To free it, free the first element of the array and the
405 eina_str_split(const char *str, const char *delim, int max_tokens)
407 return eina_str_split_full_helper(str, delim, max_tokens, NULL);
411 * @brief Join two strings of known length.
413 * @param dst The buffer to store the result.
414 * @param size Size (in byte) of the buffer.
415 * @param sep The separator character to use.
416 * @param a First string to use, before @p sep.
417 * @param a_len length of @p a.
418 * @param b Second string to use, after @p sep.
419 * @param b_len length of @p b.
420 * @return The number of characters printed.
422 * This function joins the strings @p a and @p b (in that order) and
423 * separate them with @p sep. The result is stored in the buffer
424 * @p dst and at most @p size - 1 characters will be written and the
425 * string is NULL-terminated. @p a_len is the length of @p a (not
426 * including '\\0') and @p b_len is the length of @p b (not including
427 * '\\0'). This function returns the number of characters printed (not
428 * including the trailing '\\0' used to end output to strings). Just
429 * like snprintf(), it will not write more than @p size bytes, thus a
430 * returned value of @p size or more means that the output was
433 * @see eina_str_join()
434 * @see eina_str_join_static()
437 eina_str_join_len(char *dst,
445 size_t ret = a_len + b_len + 1;
453 memcpy(dst, a, size - 1);
454 dst[size - 1] = '\0';
458 memcpy(dst, a, a_len);
463 dst[size - 1] = '\0';
470 if (size <= off + b_len + 1)
472 memcpy(dst + off, b, size - off - 1);
473 dst[size - 1] = '\0';
477 memcpy(dst + off, b, b_len);
478 dst[off + b_len] = '\0';
483 * @brief Use iconv to convert a text string from one encoding to another
485 * @param enc_from encoding to convert from
486 * @param enc_to encoding to convert to
487 * @param text text to convert
492 eina_str_convert(const char *enc_from, const char *enc_to, const char *text)
495 char *new_txt, *inp, *outp;
496 size_t inb, outb, outlen, tob, outalloc;
501 ic = iconv_open(enc_to, enc_from);
502 if (ic == (iconv_t)(-1))
505 new_txt = malloc(64);
518 count = iconv(ic, &inp, &inb, &outp, &outb);
519 outlen += tob - outb;
520 if (count == (size_t)(-1))
524 new_txt = realloc(new_txt, outalloc + 64);
525 outp = new_txt + outlen;
529 else if (errno == EILSEQ)
537 else if (errno == EINVAL)
557 if (outalloc == outlen)
558 new_txt = realloc(new_txt, outalloc + 1);
569 eina_str_convert(const char *enc_from __UNUSED__,
570 const char *enc_to __UNUSED__,
571 const char *text __UNUSED__)
578 * @brief Put a \ before and Space( ), \ or ' in a string.
580 * @param str the string to escape
582 * A newly allocated string is returned.
585 eina_str_escape(const char *str)
590 s2 = malloc((strlen(str) * 2) + 1);
594 for (s = str, d = s2; *s != 0; s++, d++)
596 if ((*s == ' ') || (*s == '\\') || (*s == '\''))
609 * @brief Lowercase all the characters in range [A-Z] in the given string.
611 * @param str the string to lowercase
613 * This modifies the original string, changing all characters in [A-Z] to lowercase.
616 eina_str_tolower(char **str)
619 if ((!str) || (!(*str)))
622 for (p = *str; (*p); p++)
627 * @brief Uppercase all the characters in range [a-z] in the given string.
629 * @param str the string to uppercase
631 * This modifies the original string, changing all characters in [a-z] to uppercase.
634 eina_str_toupper(char **str)
637 if ((!str) || (!(*str)))
640 for (p = *str; (*p); p++)