]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/string.c
2 * String functions for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #define _CUPS_STRING_C_
15 #include "cups-private.h"
24 static _cups_mutex_t sp_mutex
= _CUPS_MUTEX_INITIALIZER
;
25 /* Mutex to control access to pool */
26 static cups_array_t
*stringpool
= NULL
;
27 /* Global string pool */
34 static int compare_sp_items(_cups_sp_item_t
*a
, _cups_sp_item_t
*b
);
38 * '_cupsStrAlloc()' - Allocate/reference a string.
41 char * /* O - String pointer */
42 _cupsStrAlloc(const char *s
) /* I - String */
44 size_t slen
; /* Length of string */
45 _cups_sp_item_t
*item
, /* String pool item */
46 *key
; /* Search key */
50 * Range check input...
57 * Get the string pool...
60 _cupsMutexLock(&sp_mutex
);
63 stringpool
= cupsArrayNew((cups_array_func_t
)compare_sp_items
, NULL
);
67 _cupsMutexUnlock(&sp_mutex
);
73 * See if the string is already in the pool...
76 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
78 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
)
81 * Found it, return the cached string...
87 DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, "
88 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
91 if (item
->guard
!= _CUPS_STR_GUARD
)
93 #endif /* DEBUG_GUARDS */
95 _cupsMutexUnlock(&sp_mutex
);
101 * Not found, so allocate a new one...
105 item
= (_cups_sp_item_t
*)calloc(1, sizeof(_cups_sp_item_t
) + slen
);
108 _cupsMutexUnlock(&sp_mutex
);
114 memcpy(item
->str
, s
, slen
+ 1);
117 item
->guard
= _CUPS_STR_GUARD
;
119 DEBUG_printf(("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, "
120 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
122 #endif /* DEBUG_GUARDS */
125 * Add the string to the pool and return it...
128 cupsArrayAdd(stringpool
, item
);
130 _cupsMutexUnlock(&sp_mutex
);
137 * '_cupsStrDate()' - Return a localized date for a given time value.
139 * This function works around the locale encoding issues of strftime...
142 char * /* O - Buffer */
143 _cupsStrDate(char *buf
, /* I - Buffer */
144 size_t bufsize
, /* I - Size of buffer */
145 time_t timeval
) /* I - Time value */
147 struct tm
*dateval
; /* Local date/time */
148 char temp
[1024]; /* Temporary buffer */
149 _cups_globals_t
*cg
= _cupsGlobals(); /* Per-thread globals */
152 if (!cg
->lang_default
)
153 cg
->lang_default
= cupsLangDefault();
155 dateval
= localtime(&timeval
);
157 if (cg
->lang_default
->encoding
!= CUPS_UTF8
)
159 strftime(temp
, sizeof(temp
), "%c", dateval
);
160 cupsCharsetToUTF8((cups_utf8_t
*)buf
, temp
, (int)bufsize
, cg
->lang_default
->encoding
);
163 strftime(buf
, bufsize
, "%c", dateval
);
170 * '_cupsStrFlush()' - Flush the string pool.
176 _cups_sp_item_t
*item
; /* Current item */
179 DEBUG_printf(("4_cupsStrFlush: %d strings in array",
180 cupsArrayCount(stringpool
)));
182 _cupsMutexLock(&sp_mutex
);
184 for (item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
186 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
189 cupsArrayDelete(stringpool
);
192 _cupsMutexUnlock(&sp_mutex
);
197 * '_cupsStrFormatd()' - Format a floating-point number.
200 char * /* O - Pointer to end of string */
201 _cupsStrFormatd(char *buf
, /* I - String */
202 char *bufend
, /* I - End of string buffer */
203 double number
, /* I - Number to format */
204 struct lconv
*loc
) /* I - Locale data */
206 char *bufptr
, /* Pointer into buffer */
207 temp
[1024], /* Temporary string */
208 *tempdec
, /* Pointer to decimal point */
209 *tempptr
; /* Pointer into temporary string */
210 const char *dec
; /* Decimal point */
211 int declen
; /* Length of decimal point */
215 * Format the number using the "%.12f" format and then eliminate
216 * unnecessary trailing 0's.
219 snprintf(temp
, sizeof(temp
), "%.12f", number
);
220 for (tempptr
= temp
+ strlen(temp
) - 1;
221 tempptr
> temp
&& *tempptr
== '0';
225 * Next, find the decimal point...
228 if (loc
&& loc
->decimal_point
)
230 dec
= loc
->decimal_point
;
231 declen
= (int)strlen(dec
);
240 tempdec
= strchr(temp
, *dec
);
242 tempdec
= strstr(temp
, dec
);
245 * Copy everything up to the decimal point...
250 for (tempptr
= temp
, bufptr
= buf
;
251 tempptr
< tempdec
&& bufptr
< bufend
;
252 *bufptr
++ = *tempptr
++);
256 if (*tempptr
&& bufptr
< bufend
)
260 while (*tempptr
&& bufptr
< bufend
)
261 *bufptr
++ = *tempptr
++;
268 strlcpy(buf
, temp
, (size_t)(bufend
- buf
+ 1));
269 bufptr
= buf
+ strlen(buf
);
277 * '_cupsStrFree()' - Free/dereference a string.
281 _cupsStrFree(const char *s
) /* I - String to free */
283 _cups_sp_item_t
*item
, /* String pool item */
284 *key
; /* Search key */
288 * Range check input...
295 * Check the string pool...
297 * We don't need to lock the mutex yet, as we only want to know if
298 * the stringpool is initialized. The rest of the code will still
299 * work if it is initialized before we lock...
306 * See if the string is already in the pool...
309 _cupsMutexLock(&sp_mutex
);
311 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
314 if (key
->guard
!= _CUPS_STR_GUARD
)
316 DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, "
317 "ref_count=%d", key
, key
->str
, key
->guard
, key
->ref_count
));
320 #endif /* DEBUG_GUARDS */
322 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
&&
326 * Found it, dereference...
331 if (!item
->ref_count
)
337 cupsArrayRemove(stringpool
, item
);
343 _cupsMutexUnlock(&sp_mutex
);
348 * '_cupsStrRetain()' - Increment the reference count of a string.
350 * Note: This function does not verify that the passed pointer is in the
351 * string pool, so any calls to it MUST know they are passing in a
355 char * /* O - Pointer to string */
356 _cupsStrRetain(const char *s
) /* I - String to retain */
358 _cups_sp_item_t
*item
; /* Pointer to string pool item */
363 item
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
366 if (item
->guard
!= _CUPS_STR_GUARD
)
368 DEBUG_printf(("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, "
369 "ref_count=%d", item
, s
, item
->guard
, item
->ref_count
));
372 #endif /* DEBUG_GUARDS */
374 _cupsMutexLock(&sp_mutex
);
378 _cupsMutexUnlock(&sp_mutex
);
386 * '_cupsStrScand()' - Scan a string for a floating-point number.
388 * This function handles the locale-specific BS so that a decimal
389 * point is always the period (".")...
392 double /* O - Number */
393 _cupsStrScand(const char *buf
, /* I - Pointer to number */
394 char **bufptr
, /* O - New pointer or NULL on error */
395 struct lconv
*loc
) /* I - Locale data */
397 char temp
[1024], /* Temporary buffer */
398 *tempptr
; /* Pointer into temporary buffer */
402 * Range check input...
409 * Skip leading whitespace...
412 while (_cups_isspace(*buf
))
416 * Copy leading sign, numbers, period, and then numbers...
420 if (*buf
== '-' || *buf
== '+')
423 while (isdigit(*buf
& 255))
424 if (tempptr
< (temp
+ sizeof(temp
) - 1))
437 * Read fractional portion of number...
442 if (loc
&& loc
->decimal_point
)
444 strlcpy(tempptr
, loc
->decimal_point
, sizeof(temp
) - (size_t)(tempptr
- temp
));
445 tempptr
+= strlen(tempptr
);
447 else if (tempptr
< (temp
+ sizeof(temp
) - 1))
457 while (isdigit(*buf
& 255))
458 if (tempptr
< (temp
+ sizeof(temp
) - 1))
469 if (*buf
== 'e' || *buf
== 'E')
475 if (tempptr
< (temp
+ sizeof(temp
) - 1))
485 if (*buf
== '+' || *buf
== '-')
487 if (tempptr
< (temp
+ sizeof(temp
) - 1))
498 while (isdigit(*buf
& 255))
499 if (tempptr
< (temp
+ sizeof(temp
) - 1))
511 * Nul-terminate the temporary string and return the value...
515 *bufptr
= (char *)buf
;
519 return (strtod(temp
, NULL
));
524 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
527 size_t /* O - Number of strings */
528 _cupsStrStatistics(size_t *alloc_bytes
, /* O - Allocated bytes */
529 size_t *total_bytes
) /* O - Total string bytes */
531 size_t count
, /* Number of strings */
532 abytes
, /* Allocated string bytes */
533 tbytes
, /* Total string bytes */
534 len
; /* Length of string */
535 _cups_sp_item_t
*item
; /* Current item */
539 * Loop through strings in pool, counting everything up...
542 _cupsMutexLock(&sp_mutex
);
544 for (count
= 0, abytes
= 0, tbytes
= 0,
545 item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
547 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
550 * Count allocated memory, using a 64-bit aligned buffer as a basis.
553 count
+= item
->ref_count
;
554 len
= (strlen(item
->str
) + 8) & (size_t)~7;
555 abytes
+= sizeof(_cups_sp_item_t
) + len
;
556 tbytes
+= item
->ref_count
* len
;
559 _cupsMutexUnlock(&sp_mutex
);
566 *alloc_bytes
= abytes
;
569 *total_bytes
= tbytes
;
576 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
580 _cups_strcpy(char *dst
, /* I - Destination string */
581 const char *src
) /* I - Source string */
591 * '_cups_strdup()' - Duplicate a string.
595 char * /* O - New string pointer */
596 _cups_strdup(const char *s
) /* I - String to duplicate */
598 char *t
; /* New string pointer */
599 size_t slen
; /* Length of string */
606 if ((t
= malloc(slen
+ 1)) == NULL
)
609 return (memcpy(t
, s
, slen
+ 1));
611 #endif /* !HAVE_STRDUP */
615 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
618 int /* O - Result of comparison (-1, 0, or 1) */
619 _cups_strcasecmp(const char *s
, /* I - First string */
620 const char *t
) /* I - Second string */
622 while (*s
!= '\0' && *t
!= '\0')
624 if (_cups_tolower(*s
) < _cups_tolower(*t
))
626 else if (_cups_tolower(*s
) > _cups_tolower(*t
))
633 if (*s
== '\0' && *t
== '\0')
642 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
645 int /* O - Result of comparison (-1, 0, or 1) */
646 _cups_strncasecmp(const char *s
, /* I - First string */
647 const char *t
, /* I - Second string */
648 size_t n
) /* I - Maximum number of characters to compare */
650 while (*s
!= '\0' && *t
!= '\0' && n
> 0)
652 if (_cups_tolower(*s
) < _cups_tolower(*t
))
654 else if (_cups_tolower(*s
) > _cups_tolower(*t
))
664 else if (*s
== '\0' && *t
== '\0')
675 * '_cups_strlcat()' - Safely concatenate two strings.
678 size_t /* O - Length of string */
679 _cups_strlcat(char *dst
, /* O - Destination string */
680 const char *src
, /* I - Source string */
681 size_t size
) /* I - Size of destination string buffer */
683 size_t srclen
; /* Length of source string */
684 size_t dstlen
; /* Length of destination string */
688 * Figure out how much room is left...
691 dstlen
= strlen(dst
);
693 if (size
< (dstlen
+ 1))
694 return (dstlen
); /* No room, return immediately... */
699 * Figure out how much room is needed...
702 srclen
= strlen(src
);
705 * Copy the appropriate amount...
711 memmove(dst
+ dstlen
, src
, srclen
);
712 dst
[dstlen
+ srclen
] = '\0';
714 return (dstlen
+ srclen
);
716 #endif /* !HAVE_STRLCAT */
721 * '_cups_strlcpy()' - Safely copy two strings.
724 size_t /* O - Length of string */
725 _cups_strlcpy(char *dst
, /* O - Destination string */
726 const char *src
, /* I - Source string */
727 size_t size
) /* I - Size of destination string buffer */
729 size_t srclen
; /* Length of source string */
733 * Figure out how much room is needed...
738 srclen
= strlen(src
);
741 * Copy the appropriate amount...
747 memmove(dst
, src
, srclen
);
752 #endif /* !HAVE_STRLCPY */
756 * 'compare_sp_items()' - Compare two string pool items...
759 static int /* O - Result of comparison */
760 compare_sp_items(_cups_sp_item_t
*a
, /* I - First item */
761 _cups_sp_item_t
*b
) /* I - Second item */
763 return (strcmp(a
->str
, b
->str
));