]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/string.c
4 * String functions for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
22 #define _CUPS_STRING_C_
23 #include "cups-private.h"
32 static _cups_mutex_t sp_mutex
= _CUPS_MUTEX_INITIALIZER
;
33 /* Mutex to control access to pool */
34 static cups_array_t
*stringpool
= NULL
;
35 /* Global string pool */
42 static int compare_sp_items(_cups_sp_item_t
*a
, _cups_sp_item_t
*b
);
46 * '_cupsStrAlloc()' - Allocate/reference a string.
49 char * /* O - String pointer */
50 _cupsStrAlloc(const char *s
) /* I - String */
52 size_t slen
; /* Length of string */
53 _cups_sp_item_t
*item
, /* String pool item */
54 *key
; /* Search key */
58 * Range check input...
65 * Get the string pool...
68 _cupsMutexLock(&sp_mutex
);
71 stringpool
= cupsArrayNew((cups_array_func_t
)compare_sp_items
, NULL
);
75 _cupsMutexUnlock(&sp_mutex
);
81 * See if the string is already in the pool...
84 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
86 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
)
89 * Found it, return the cached string...
95 DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, "
96 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
99 if (item
->guard
!= _CUPS_STR_GUARD
)
101 #endif /* DEBUG_GUARDS */
103 _cupsMutexUnlock(&sp_mutex
);
109 * Not found, so allocate a new one...
113 item
= (_cups_sp_item_t
*)calloc(1, sizeof(_cups_sp_item_t
) + slen
);
116 _cupsMutexUnlock(&sp_mutex
);
122 memcpy(item
->str
, s
, slen
+ 1);
125 item
->guard
= _CUPS_STR_GUARD
;
127 DEBUG_printf(("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, "
128 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
130 #endif /* DEBUG_GUARDS */
133 * Add the string to the pool and return it...
136 cupsArrayAdd(stringpool
, item
);
138 _cupsMutexUnlock(&sp_mutex
);
145 * '_cupsStrDate()' - Return a localized date for a given time value.
147 * This function works around the locale encoding issues of strftime...
150 char * /* O - Buffer */
151 _cupsStrDate(char *buf
, /* I - Buffer */
152 size_t bufsize
, /* I - Size of buffer */
153 time_t timeval
) /* I - Time value */
155 struct tm
*dateval
; /* Local date/time */
156 char temp
[1024]; /* Temporary buffer */
157 _cups_globals_t
*cg
= _cupsGlobals(); /* Per-thread globals */
160 if (!cg
->lang_default
)
161 cg
->lang_default
= cupsLangDefault();
163 dateval
= localtime(&timeval
);
165 if (cg
->lang_default
->encoding
!= CUPS_UTF8
)
167 strftime(temp
, sizeof(temp
), "%c", dateval
);
168 cupsCharsetToUTF8((cups_utf8_t
*)buf
, temp
, (int)bufsize
, cg
->lang_default
->encoding
);
171 strftime(buf
, bufsize
, "%c", dateval
);
178 * '_cupsStrFlush()' - Flush the string pool.
184 _cups_sp_item_t
*item
; /* Current item */
187 DEBUG_printf(("4_cupsStrFlush: %d strings in array",
188 cupsArrayCount(stringpool
)));
190 _cupsMutexLock(&sp_mutex
);
192 for (item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
194 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
197 cupsArrayDelete(stringpool
);
200 _cupsMutexUnlock(&sp_mutex
);
205 * '_cupsStrFormatd()' - Format a floating-point number.
208 char * /* O - Pointer to end of string */
209 _cupsStrFormatd(char *buf
, /* I - String */
210 char *bufend
, /* I - End of string buffer */
211 double number
, /* I - Number to format */
212 struct lconv
*loc
) /* I - Locale data */
214 char *bufptr
, /* Pointer into buffer */
215 temp
[1024], /* Temporary string */
216 *tempdec
, /* Pointer to decimal point */
217 *tempptr
; /* Pointer into temporary string */
218 const char *dec
; /* Decimal point */
219 int declen
; /* Length of decimal point */
223 * Format the number using the "%.12f" format and then eliminate
224 * unnecessary trailing 0's.
227 snprintf(temp
, sizeof(temp
), "%.12f", number
);
228 for (tempptr
= temp
+ strlen(temp
) - 1;
229 tempptr
> temp
&& *tempptr
== '0';
233 * Next, find the decimal point...
236 if (loc
&& loc
->decimal_point
)
238 dec
= loc
->decimal_point
;
239 declen
= (int)strlen(dec
);
248 tempdec
= strchr(temp
, *dec
);
250 tempdec
= strstr(temp
, dec
);
253 * Copy everything up to the decimal point...
258 for (tempptr
= temp
, bufptr
= buf
;
259 tempptr
< tempdec
&& bufptr
< bufend
;
260 *bufptr
++ = *tempptr
++);
264 if (*tempptr
&& bufptr
< bufend
)
268 while (*tempptr
&& bufptr
< bufend
)
269 *bufptr
++ = *tempptr
++;
276 strlcpy(buf
, temp
, (size_t)(bufend
- buf
+ 1));
277 bufptr
= buf
+ strlen(buf
);
285 * '_cupsStrFree()' - Free/dereference a string.
289 _cupsStrFree(const char *s
) /* I - String to free */
291 _cups_sp_item_t
*item
, /* String pool item */
292 *key
; /* Search key */
296 * Range check input...
303 * Check the string pool...
305 * We don't need to lock the mutex yet, as we only want to know if
306 * the stringpool is initialized. The rest of the code will still
307 * work if it is initialized before we lock...
314 * See if the string is already in the pool...
317 _cupsMutexLock(&sp_mutex
);
319 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
322 if (key
->guard
!= _CUPS_STR_GUARD
)
324 DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, "
325 "ref_count=%d", key
, key
->str
, key
->guard
, key
->ref_count
));
328 #endif /* DEBUG_GUARDS */
330 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
&&
334 * Found it, dereference...
339 if (!item
->ref_count
)
345 cupsArrayRemove(stringpool
, item
);
351 _cupsMutexUnlock(&sp_mutex
);
356 * '_cupsStrRetain()' - Increment the reference count of a string.
358 * Note: This function does not verify that the passed pointer is in the
359 * string pool, so any calls to it MUST know they are passing in a
363 char * /* O - Pointer to string */
364 _cupsStrRetain(const char *s
) /* I - String to retain */
366 _cups_sp_item_t
*item
; /* Pointer to string pool item */
371 item
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
374 if (item
->guard
!= _CUPS_STR_GUARD
)
376 DEBUG_printf(("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, "
377 "ref_count=%d", item
, s
, item
->guard
, item
->ref_count
));
380 #endif /* DEBUG_GUARDS */
382 _cupsMutexLock(&sp_mutex
);
386 _cupsMutexUnlock(&sp_mutex
);
394 * '_cupsStrScand()' - Scan a string for a floating-point number.
396 * This function handles the locale-specific BS so that a decimal
397 * point is always the period (".")...
400 double /* O - Number */
401 _cupsStrScand(const char *buf
, /* I - Pointer to number */
402 char **bufptr
, /* O - New pointer or NULL on error */
403 struct lconv
*loc
) /* I - Locale data */
405 char temp
[1024], /* Temporary buffer */
406 *tempptr
; /* Pointer into temporary buffer */
410 * Range check input...
417 * Skip leading whitespace...
420 while (_cups_isspace(*buf
))
424 * Copy leading sign, numbers, period, and then numbers...
428 if (*buf
== '-' || *buf
== '+')
431 while (isdigit(*buf
& 255))
432 if (tempptr
< (temp
+ sizeof(temp
) - 1))
445 * Read fractional portion of number...
450 if (loc
&& loc
->decimal_point
)
452 strlcpy(tempptr
, loc
->decimal_point
, sizeof(temp
) - (size_t)(tempptr
- temp
));
453 tempptr
+= strlen(tempptr
);
455 else if (tempptr
< (temp
+ sizeof(temp
) - 1))
465 while (isdigit(*buf
& 255))
466 if (tempptr
< (temp
+ sizeof(temp
) - 1))
477 if (*buf
== 'e' || *buf
== 'E')
483 if (tempptr
< (temp
+ sizeof(temp
) - 1))
493 if (*buf
== '+' || *buf
== '-')
495 if (tempptr
< (temp
+ sizeof(temp
) - 1))
506 while (isdigit(*buf
& 255))
507 if (tempptr
< (temp
+ sizeof(temp
) - 1))
519 * Nul-terminate the temporary string and return the value...
523 *bufptr
= (char *)buf
;
527 return (strtod(temp
, NULL
));
532 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
535 size_t /* O - Number of strings */
536 _cupsStrStatistics(size_t *alloc_bytes
, /* O - Allocated bytes */
537 size_t *total_bytes
) /* O - Total string bytes */
539 size_t count
, /* Number of strings */
540 abytes
, /* Allocated string bytes */
541 tbytes
, /* Total string bytes */
542 len
; /* Length of string */
543 _cups_sp_item_t
*item
; /* Current item */
547 * Loop through strings in pool, counting everything up...
550 _cupsMutexLock(&sp_mutex
);
552 for (count
= 0, abytes
= 0, tbytes
= 0,
553 item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
555 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
558 * Count allocated memory, using a 64-bit aligned buffer as a basis.
561 count
+= item
->ref_count
;
562 len
= (strlen(item
->str
) + 8) & (size_t)~7;
563 abytes
+= sizeof(_cups_sp_item_t
) + len
;
564 tbytes
+= item
->ref_count
* len
;
567 _cupsMutexUnlock(&sp_mutex
);
574 *alloc_bytes
= abytes
;
577 *total_bytes
= tbytes
;
584 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
588 _cups_strcpy(char *dst
, /* I - Destination string */
589 const char *src
) /* I - Source string */
599 * '_cups_strdup()' - Duplicate a string.
603 char * /* O - New string pointer */
604 _cups_strdup(const char *s
) /* I - String to duplicate */
606 char *t
; /* New string pointer */
607 size_t slen
; /* Length of string */
614 if ((t
= malloc(slen
+ 1)) == NULL
)
617 return (memcpy(t
, s
, slen
+ 1));
619 #endif /* !HAVE_STRDUP */
623 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
626 int /* O - Result of comparison (-1, 0, or 1) */
627 _cups_strcasecmp(const char *s
, /* I - First string */
628 const char *t
) /* I - Second string */
630 while (*s
!= '\0' && *t
!= '\0')
632 if (_cups_tolower(*s
) < _cups_tolower(*t
))
634 else if (_cups_tolower(*s
) > _cups_tolower(*t
))
641 if (*s
== '\0' && *t
== '\0')
650 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
653 int /* O - Result of comparison (-1, 0, or 1) */
654 _cups_strncasecmp(const char *s
, /* I - First string */
655 const char *t
, /* I - Second string */
656 size_t n
) /* I - Maximum number of characters to compare */
658 while (*s
!= '\0' && *t
!= '\0' && n
> 0)
660 if (_cups_tolower(*s
) < _cups_tolower(*t
))
662 else if (_cups_tolower(*s
) > _cups_tolower(*t
))
672 else if (*s
== '\0' && *t
== '\0')
683 * '_cups_strlcat()' - Safely concatenate two strings.
686 size_t /* O - Length of string */
687 _cups_strlcat(char *dst
, /* O - Destination string */
688 const char *src
, /* I - Source string */
689 size_t size
) /* I - Size of destination string buffer */
691 size_t srclen
; /* Length of source string */
692 size_t dstlen
; /* Length of destination string */
696 * Figure out how much room is left...
699 dstlen
= strlen(dst
);
703 return (dstlen
); /* No room, return immediately... */
706 * Figure out how much room is needed...
709 srclen
= strlen(src
);
712 * Copy the appropriate amount...
718 memmove(dst
+ dstlen
, src
, srclen
);
719 dst
[dstlen
+ srclen
] = '\0';
721 return (dstlen
+ srclen
);
723 #endif /* !HAVE_STRLCAT */
728 * '_cups_strlcpy()' - Safely copy two strings.
731 size_t /* O - Length of string */
732 _cups_strlcpy(char *dst
, /* O - Destination string */
733 const char *src
, /* I - Source string */
734 size_t size
) /* I - Size of destination string buffer */
736 size_t srclen
; /* Length of source string */
740 * Figure out how much room is needed...
745 srclen
= strlen(src
);
748 * Copy the appropriate amount...
754 memmove(dst
, src
, srclen
);
759 #endif /* !HAVE_STRLCPY */
763 * 'compare_sp_items()' - Compare two string pool items...
766 static int /* O - Result of comparison */
767 compare_sp_items(_cups_sp_item_t
*a
, /* I - First item */
768 _cups_sp_item_t
*b
) /* I - Second item */
770 return (strcmp(a
->str
, b
->str
));