2 * String functions for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #define _CUPS_STRING_C_
21 #include "cups-private.h"
30 static _cups_mutex_t sp_mutex = _CUPS_MUTEX_INITIALIZER;
31 /* Mutex to control access to pool */
32 static cups_array_t *stringpool = NULL;
33 /* Global string pool */
40 static int compare_sp_items(_cups_sp_item_t *a, _cups_sp_item_t *b);
44 * '_cupsStrAlloc()' - Allocate/reference a string.
47 char * /* O - String pointer */
48 _cupsStrAlloc(const char *s) /* I - String */
50 size_t slen; /* Length of string */
51 _cups_sp_item_t *item, /* String pool item */
52 *key; /* Search key */
56 * Range check input...
63 * Get the string pool...
66 _cupsMutexLock(&sp_mutex);
69 stringpool = cupsArrayNew((cups_array_func_t)compare_sp_items, NULL);
73 _cupsMutexUnlock(&sp_mutex);
79 * See if the string is already in the pool...
82 key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str));
84 if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL)
87 * Found it, return the cached string...
93 DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, "
94 "ref_count=%d", item, item->str, s, item->guard,
97 if (item->guard != _CUPS_STR_GUARD)
99 #endif /* DEBUG_GUARDS */
101 _cupsMutexUnlock(&sp_mutex);
107 * Not found, so allocate a new one...
111 item = (_cups_sp_item_t *)calloc(1, sizeof(_cups_sp_item_t) + slen);
114 _cupsMutexUnlock(&sp_mutex);
120 memcpy(item->str, s, slen + 1);
123 item->guard = _CUPS_STR_GUARD;
125 DEBUG_printf(("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, "
126 "ref_count=%d", item, item->str, s, item->guard,
128 #endif /* DEBUG_GUARDS */
131 * Add the string to the pool and return it...
134 cupsArrayAdd(stringpool, item);
136 _cupsMutexUnlock(&sp_mutex);
143 * '_cupsStrDate()' - Return a localized date for a given time value.
145 * This function works around the locale encoding issues of strftime...
148 char * /* O - Buffer */
149 _cupsStrDate(char *buf, /* I - Buffer */
150 size_t bufsize, /* I - Size of buffer */
151 time_t timeval) /* I - Time value */
153 struct tm *dateval; /* Local date/time */
154 char temp[1024]; /* Temporary buffer */
155 _cups_globals_t *cg = _cupsGlobals(); /* Per-thread globals */
158 if (!cg->lang_default)
159 cg->lang_default = cupsLangDefault();
161 dateval = localtime(&timeval);
163 if (cg->lang_default->encoding != CUPS_UTF8)
165 strftime(temp, sizeof(temp), "%c", dateval);
166 cupsCharsetToUTF8((cups_utf8_t *)buf, temp, (int)bufsize, cg->lang_default->encoding);
169 strftime(buf, bufsize, "%c", dateval);
176 * '_cupsStrFlush()' - Flush the string pool.
182 _cups_sp_item_t *item; /* Current item */
185 DEBUG_printf(("4_cupsStrFlush: %d strings in array",
186 cupsArrayCount(stringpool)));
188 _cupsMutexLock(&sp_mutex);
190 for (item = (_cups_sp_item_t *)cupsArrayFirst(stringpool);
192 item = (_cups_sp_item_t *)cupsArrayNext(stringpool))
195 cupsArrayDelete(stringpool);
198 _cupsMutexUnlock(&sp_mutex);
203 * '_cupsStrFormatd()' - Format a floating-point number.
206 char * /* O - Pointer to end of string */
207 _cupsStrFormatd(char *buf, /* I - String */
208 char *bufend, /* I - End of string buffer */
209 double number, /* I - Number to format */
210 struct lconv *loc) /* I - Locale data */
212 char *bufptr, /* Pointer into buffer */
213 temp[1024], /* Temporary string */
214 *tempdec, /* Pointer to decimal point */
215 *tempptr; /* Pointer into temporary string */
216 const char *dec; /* Decimal point */
217 int declen; /* Length of decimal point */
221 * Format the number using the "%.12f" format and then eliminate
222 * unnecessary trailing 0's.
225 snprintf(temp, sizeof(temp), "%.12f", number);
226 for (tempptr = temp + strlen(temp) - 1;
227 tempptr > temp && *tempptr == '0';
231 * Next, find the decimal point...
234 if (loc && loc->decimal_point)
236 dec = loc->decimal_point;
237 declen = (int)strlen(dec);
246 tempdec = strchr(temp, *dec);
248 tempdec = strstr(temp, dec);
251 * Copy everything up to the decimal point...
256 for (tempptr = temp, bufptr = buf;
257 tempptr < tempdec && bufptr < bufend;
258 *bufptr++ = *tempptr++);
262 if (*tempptr && bufptr < bufend)
266 while (*tempptr && bufptr < bufend)
267 *bufptr++ = *tempptr++;
274 strlcpy(buf, temp, (size_t)(bufend - buf + 1));
275 bufptr = buf + strlen(buf);
283 * '_cupsStrFree()' - Free/dereference a string.
287 _cupsStrFree(const char *s) /* I - String to free */
289 _cups_sp_item_t *item, /* String pool item */
290 *key; /* Search key */
294 * Range check input...
301 * Check the string pool...
303 * We don't need to lock the mutex yet, as we only want to know if
304 * the stringpool is initialized. The rest of the code will still
305 * work if it is initialized before we lock...
312 * See if the string is already in the pool...
315 _cupsMutexLock(&sp_mutex);
317 key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str));
320 if (key->guard != _CUPS_STR_GUARD)
322 DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, "
323 "ref_count=%d", key, key->str, key->guard, key->ref_count));
326 #endif /* DEBUG_GUARDS */
328 if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL &&
332 * Found it, dereference...
337 if (!item->ref_count)
343 cupsArrayRemove(stringpool, item);
349 _cupsMutexUnlock(&sp_mutex);
354 * '_cupsStrRetain()' - Increment the reference count of a string.
356 * Note: This function does not verify that the passed pointer is in the
357 * string pool, so any calls to it MUST know they are passing in a
361 char * /* O - Pointer to string */
362 _cupsStrRetain(const char *s) /* I - String to retain */
364 _cups_sp_item_t *item; /* Pointer to string pool item */
369 item = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str));
372 if (item->guard != _CUPS_STR_GUARD)
374 DEBUG_printf(("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, "
375 "ref_count=%d", item, s, item->guard, item->ref_count));
378 #endif /* DEBUG_GUARDS */
380 _cupsMutexLock(&sp_mutex);
384 _cupsMutexUnlock(&sp_mutex);
392 * '_cupsStrScand()' - Scan a string for a floating-point number.
394 * This function handles the locale-specific BS so that a decimal
395 * point is always the period (".")...
398 double /* O - Number */
399 _cupsStrScand(const char *buf, /* I - Pointer to number */
400 char **bufptr, /* O - New pointer or NULL on error */
401 struct lconv *loc) /* I - Locale data */
403 char temp[1024], /* Temporary buffer */
404 *tempptr; /* Pointer into temporary buffer */
408 * Range check input...
415 * Skip leading whitespace...
418 while (_cups_isspace(*buf))
422 * Copy leading sign, numbers, period, and then numbers...
426 if (*buf == '-' || *buf == '+')
429 while (isdigit(*buf & 255))
430 if (tempptr < (temp + sizeof(temp) - 1))
443 * Read fractional portion of number...
448 if (loc && loc->decimal_point)
450 strlcpy(tempptr, loc->decimal_point, sizeof(temp) - (size_t)(tempptr - temp));
451 tempptr += strlen(tempptr);
453 else if (tempptr < (temp + sizeof(temp) - 1))
463 while (isdigit(*buf & 255))
464 if (tempptr < (temp + sizeof(temp) - 1))
475 if (*buf == 'e' || *buf == 'E')
481 if (tempptr < (temp + sizeof(temp) - 1))
491 if (*buf == '+' || *buf == '-')
493 if (tempptr < (temp + sizeof(temp) - 1))
504 while (isdigit(*buf & 255))
505 if (tempptr < (temp + sizeof(temp) - 1))
517 * Nul-terminate the temporary string and return the value...
521 *bufptr = (char *)buf;
525 return (strtod(temp, NULL));
530 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
533 size_t /* O - Number of strings */
534 _cupsStrStatistics(size_t *alloc_bytes, /* O - Allocated bytes */
535 size_t *total_bytes) /* O - Total string bytes */
537 size_t count, /* Number of strings */
538 abytes, /* Allocated string bytes */
539 tbytes, /* Total string bytes */
540 len; /* Length of string */
541 _cups_sp_item_t *item; /* Current item */
545 * Loop through strings in pool, counting everything up...
548 _cupsMutexLock(&sp_mutex);
550 for (count = 0, abytes = 0, tbytes = 0,
551 item = (_cups_sp_item_t *)cupsArrayFirst(stringpool);
553 item = (_cups_sp_item_t *)cupsArrayNext(stringpool))
556 * Count allocated memory, using a 64-bit aligned buffer as a basis.
559 count += item->ref_count;
560 len = (strlen(item->str) + 8) & (size_t)~7;
561 abytes += sizeof(_cups_sp_item_t) + len;
562 tbytes += item->ref_count * len;
565 _cupsMutexUnlock(&sp_mutex);
572 *alloc_bytes = abytes;
575 *total_bytes = tbytes;
582 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
586 _cups_strcpy(char *dst, /* I - Destination string */
587 const char *src) /* I - Source string */
597 * '_cups_strdup()' - Duplicate a string.
601 char * /* O - New string pointer */
602 _cups_strdup(const char *s) /* I - String to duplicate */
604 char *t; /* New string pointer */
605 size_t slen; /* Length of string */
612 if ((t = malloc(slen + 1)) == NULL)
615 return (memcpy(t, s, slen + 1));
617 #endif /* !HAVE_STRDUP */
621 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
624 int /* O - Result of comparison (-1, 0, or 1) */
625 _cups_strcasecmp(const char *s, /* I - First string */
626 const char *t) /* I - Second string */
628 while (*s != '\0' && *t != '\0')
630 if (_cups_tolower(*s) < _cups_tolower(*t))
632 else if (_cups_tolower(*s) > _cups_tolower(*t))
639 if (*s == '\0' && *t == '\0')
648 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
651 int /* O - Result of comparison (-1, 0, or 1) */
652 _cups_strncasecmp(const char *s, /* I - First string */
653 const char *t, /* I - Second string */
654 size_t n) /* I - Maximum number of characters to compare */
656 while (*s != '\0' && *t != '\0' && n > 0)
658 if (_cups_tolower(*s) < _cups_tolower(*t))
660 else if (_cups_tolower(*s) > _cups_tolower(*t))
670 else if (*s == '\0' && *t == '\0')
681 * '_cups_strlcat()' - Safely concatenate two strings.
684 size_t /* O - Length of string */
685 _cups_strlcat(char *dst, /* O - Destination string */
686 const char *src, /* I - Source string */
687 size_t size) /* I - Size of destination string buffer */
689 size_t srclen; /* Length of source string */
690 size_t dstlen; /* Length of destination string */
694 * Figure out how much room is left...
697 dstlen = strlen(dst);
701 return (dstlen); /* No room, return immediately... */
704 * Figure out how much room is needed...
707 srclen = strlen(src);
710 * Copy the appropriate amount...
716 memmove(dst + dstlen, src, srclen);
717 dst[dstlen + srclen] = '\0';
719 return (dstlen + srclen);
721 #endif /* !HAVE_STRLCAT */
726 * '_cups_strlcpy()' - Safely copy two strings.
729 size_t /* O - Length of string */
730 _cups_strlcpy(char *dst, /* O - Destination string */
731 const char *src, /* I - Source string */
732 size_t size) /* I - Size of destination string buffer */
734 size_t srclen; /* Length of source string */
738 * Figure out how much room is needed...
743 srclen = strlen(src);
746 * Copy the appropriate amount...
752 memmove(dst, src, srclen);
757 #endif /* !HAVE_STRLCPY */
761 * 'compare_sp_items()' - Compare two string pool items...
764 static int /* O - Result of comparison */
765 compare_sp_items(_cups_sp_item_t *a, /* I - First item */
766 _cups_sp_item_t *b) /* I - Second item */
768 return (strcmp(a->str, b->str));