]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/string.c
2 * String functions for CUPS.
4 * Copyright © 2007-2019 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #define _CUPS_STRING_C_
16 #include "cups-private.h"
17 #include "debug-internal.h"
26 static _cups_mutex_t sp_mutex
= _CUPS_MUTEX_INITIALIZER
;
27 /* Mutex to control access to pool */
28 static cups_array_t
*stringpool
= NULL
;
29 /* Global string pool */
36 static int compare_sp_items(_cups_sp_item_t
*a
, _cups_sp_item_t
*b
);
40 * '_cupsStrAlloc()' - Allocate/reference a string.
43 char * /* O - String pointer */
44 _cupsStrAlloc(const char *s
) /* I - String */
46 size_t slen
; /* Length of string */
47 _cups_sp_item_t
*item
, /* String pool item */
48 *key
; /* Search key */
52 * Range check input...
59 * Get the string pool...
62 _cupsMutexLock(&sp_mutex
);
65 stringpool
= cupsArrayNew((cups_array_func_t
)compare_sp_items
, NULL
);
69 _cupsMutexUnlock(&sp_mutex
);
75 * See if the string is already in the pool...
78 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
80 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
)
83 * Found it, return the cached string...
89 DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, "
90 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
93 if (item
->guard
!= _CUPS_STR_GUARD
)
95 #endif /* DEBUG_GUARDS */
97 _cupsMutexUnlock(&sp_mutex
);
103 * Not found, so allocate a new one...
107 item
= (_cups_sp_item_t
*)calloc(1, sizeof(_cups_sp_item_t
) + slen
);
110 _cupsMutexUnlock(&sp_mutex
);
116 memcpy(item
->str
, s
, slen
+ 1);
119 item
->guard
= _CUPS_STR_GUARD
;
121 DEBUG_printf(("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, "
122 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
124 #endif /* DEBUG_GUARDS */
127 * Add the string to the pool and return it...
130 cupsArrayAdd(stringpool
, item
);
132 _cupsMutexUnlock(&sp_mutex
);
139 * '_cupsStrDate()' - Return a localized date for a given time value.
141 * This function works around the locale encoding issues of strftime...
144 char * /* O - Buffer */
145 _cupsStrDate(char *buf
, /* I - Buffer */
146 size_t bufsize
, /* I - Size of buffer */
147 time_t timeval
) /* I - Time value */
149 struct tm
*dateval
; /* Local date/time */
150 char temp
[1024]; /* Temporary buffer */
151 _cups_globals_t
*cg
= _cupsGlobals(); /* Per-thread globals */
154 if (!cg
->lang_default
)
155 cg
->lang_default
= cupsLangDefault();
157 dateval
= localtime(&timeval
);
159 if (cg
->lang_default
->encoding
!= CUPS_UTF8
)
161 strftime(temp
, sizeof(temp
), "%c", dateval
);
162 cupsCharsetToUTF8((cups_utf8_t
*)buf
, temp
, (int)bufsize
, cg
->lang_default
->encoding
);
165 strftime(buf
, bufsize
, "%c", dateval
);
172 * '_cupsStrFlush()' - Flush the string pool.
178 _cups_sp_item_t
*item
; /* Current item */
181 DEBUG_printf(("4_cupsStrFlush: %d strings in array",
182 cupsArrayCount(stringpool
)));
184 _cupsMutexLock(&sp_mutex
);
186 for (item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
188 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
191 cupsArrayDelete(stringpool
);
194 _cupsMutexUnlock(&sp_mutex
);
199 * '_cupsStrFormatd()' - Format a floating-point number.
202 char * /* O - Pointer to end of string */
203 _cupsStrFormatd(char *buf
, /* I - String */
204 char *bufend
, /* I - End of string buffer */
205 double number
, /* I - Number to format */
206 struct lconv
*loc
) /* I - Locale data */
208 char *bufptr
, /* Pointer into buffer */
209 temp
[1024], /* Temporary string */
210 *tempdec
, /* Pointer to decimal point */
211 *tempptr
; /* Pointer into temporary string */
212 const char *dec
; /* Decimal point */
213 int declen
; /* Length of decimal point */
217 * Format the number using the "%.12f" format and then eliminate
218 * unnecessary trailing 0's.
221 snprintf(temp
, sizeof(temp
), "%.12f", number
);
222 for (tempptr
= temp
+ strlen(temp
) - 1;
223 tempptr
> temp
&& *tempptr
== '0';
227 * Next, find the decimal point...
230 if (loc
&& loc
->decimal_point
)
232 dec
= loc
->decimal_point
;
233 declen
= (int)strlen(dec
);
242 tempdec
= strchr(temp
, *dec
);
244 tempdec
= strstr(temp
, dec
);
247 * Copy everything up to the decimal point...
252 for (tempptr
= temp
, bufptr
= buf
;
253 tempptr
< tempdec
&& bufptr
< bufend
;
254 *bufptr
++ = *tempptr
++);
258 if (*tempptr
&& bufptr
< bufend
)
262 while (*tempptr
&& bufptr
< bufend
)
263 *bufptr
++ = *tempptr
++;
270 strlcpy(buf
, temp
, (size_t)(bufend
- buf
+ 1));
271 bufptr
= buf
+ strlen(buf
);
279 * '_cupsStrFree()' - Free/dereference a string.
283 _cupsStrFree(const char *s
) /* I - String to free */
285 _cups_sp_item_t
*item
, /* String pool item */
286 *key
; /* Search key */
290 * Range check input...
297 * Check the string pool...
299 * We don't need to lock the mutex yet, as we only want to know if
300 * the stringpool is initialized. The rest of the code will still
301 * work if it is initialized before we lock...
308 * See if the string is already in the pool...
311 _cupsMutexLock(&sp_mutex
);
313 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
315 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
&&
319 * Found it, dereference...
323 if (key
->guard
!= _CUPS_STR_GUARD
)
325 DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, ref_count=%d", key
, key
->str
, key
->guard
, key
->ref_count
));
328 #endif /* DEBUG_GUARDS */
332 if (!item
->ref_count
)
338 cupsArrayRemove(stringpool
, item
);
344 _cupsMutexUnlock(&sp_mutex
);
349 * '_cupsStrRetain()' - Increment the reference count of a string.
351 * Note: This function does not verify that the passed pointer is in the
352 * string pool, so any calls to it MUST know they are passing in a
356 char * /* O - Pointer to string */
357 _cupsStrRetain(const char *s
) /* I - String to retain */
359 _cups_sp_item_t
*item
; /* Pointer to string pool item */
364 item
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
367 if (item
->guard
!= _CUPS_STR_GUARD
)
369 DEBUG_printf(("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, "
370 "ref_count=%d", item
, s
, item
->guard
, item
->ref_count
));
373 #endif /* DEBUG_GUARDS */
375 _cupsMutexLock(&sp_mutex
);
379 _cupsMutexUnlock(&sp_mutex
);
387 * '_cupsStrScand()' - Scan a string for a floating-point number.
389 * This function handles the locale-specific BS so that a decimal
390 * point is always the period (".")...
393 double /* O - Number */
394 _cupsStrScand(const char *buf
, /* I - Pointer to number */
395 char **bufptr
, /* O - New pointer or NULL on error */
396 struct lconv
*loc
) /* I - Locale data */
398 char temp
[1024], /* Temporary buffer */
399 *tempptr
; /* Pointer into temporary buffer */
403 * Range check input...
410 * Skip leading whitespace...
413 while (_cups_isspace(*buf
))
417 * Copy leading sign, numbers, period, and then numbers...
421 if (*buf
== '-' || *buf
== '+')
424 while (isdigit(*buf
& 255))
425 if (tempptr
< (temp
+ sizeof(temp
) - 1))
438 * Read fractional portion of number...
443 if (loc
&& loc
->decimal_point
)
445 strlcpy(tempptr
, loc
->decimal_point
, sizeof(temp
) - (size_t)(tempptr
- temp
));
446 tempptr
+= strlen(tempptr
);
448 else if (tempptr
< (temp
+ sizeof(temp
) - 1))
458 while (isdigit(*buf
& 255))
459 if (tempptr
< (temp
+ sizeof(temp
) - 1))
470 if (*buf
== 'e' || *buf
== 'E')
476 if (tempptr
< (temp
+ sizeof(temp
) - 1))
486 if (*buf
== '+' || *buf
== '-')
488 if (tempptr
< (temp
+ sizeof(temp
) - 1))
499 while (isdigit(*buf
& 255))
500 if (tempptr
< (temp
+ sizeof(temp
) - 1))
512 * Nul-terminate the temporary string and return the value...
516 *bufptr
= (char *)buf
;
520 return (strtod(temp
, NULL
));
525 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
528 size_t /* O - Number of strings */
529 _cupsStrStatistics(size_t *alloc_bytes
, /* O - Allocated bytes */
530 size_t *total_bytes
) /* O - Total string bytes */
532 size_t count
, /* Number of strings */
533 abytes
, /* Allocated string bytes */
534 tbytes
, /* Total string bytes */
535 len
; /* Length of string */
536 _cups_sp_item_t
*item
; /* Current item */
540 * Loop through strings in pool, counting everything up...
543 _cupsMutexLock(&sp_mutex
);
545 for (count
= 0, abytes
= 0, tbytes
= 0,
546 item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
548 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
551 * Count allocated memory, using a 64-bit aligned buffer as a basis.
554 count
+= item
->ref_count
;
555 len
= (strlen(item
->str
) + 8) & (size_t)~7;
556 abytes
+= sizeof(_cups_sp_item_t
) + len
;
557 tbytes
+= item
->ref_count
* len
;
560 _cupsMutexUnlock(&sp_mutex
);
567 *alloc_bytes
= abytes
;
570 *total_bytes
= tbytes
;
577 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
581 _cups_strcpy(char *dst
, /* I - Destination string */
582 const char *src
) /* I - Source string */
592 * '_cups_strdup()' - Duplicate a string.
596 char * /* O - New string pointer */
597 _cups_strdup(const char *s
) /* I - String to duplicate */
599 char *t
; /* New string pointer */
600 size_t slen
; /* Length of string */
607 if ((t
= malloc(slen
+ 1)) == NULL
)
610 return (memcpy(t
, s
, slen
+ 1));
612 #endif /* !HAVE_STRDUP */
616 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
619 int /* O - Result of comparison (-1, 0, or 1) */
620 _cups_strcasecmp(const char *s
, /* I - First string */
621 const char *t
) /* I - Second string */
623 while (*s
!= '\0' && *t
!= '\0')
625 if (_cups_tolower(*s
) < _cups_tolower(*t
))
627 else if (_cups_tolower(*s
) > _cups_tolower(*t
))
634 if (*s
== '\0' && *t
== '\0')
643 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
646 int /* O - Result of comparison (-1, 0, or 1) */
647 _cups_strncasecmp(const char *s
, /* I - First string */
648 const char *t
, /* I - Second string */
649 size_t n
) /* I - Maximum number of characters to compare */
651 while (*s
!= '\0' && *t
!= '\0' && n
> 0)
653 if (_cups_tolower(*s
) < _cups_tolower(*t
))
655 else if (_cups_tolower(*s
) > _cups_tolower(*t
))
665 else if (*s
== '\0' && *t
== '\0')
676 * '_cups_strlcat()' - Safely concatenate two strings.
679 size_t /* O - Length of string */
680 _cups_strlcat(char *dst
, /* O - Destination string */
681 const char *src
, /* I - Source string */
682 size_t size
) /* I - Size of destination string buffer */
684 size_t srclen
; /* Length of source string */
685 size_t dstlen
; /* Length of destination string */
689 * Figure out how much room is left...
692 dstlen
= strlen(dst
);
694 if (size
< (dstlen
+ 1))
695 return (dstlen
); /* No room, return immediately... */
700 * Figure out how much room is needed...
703 srclen
= strlen(src
);
706 * Copy the appropriate amount...
712 memmove(dst
+ dstlen
, src
, srclen
);
713 dst
[dstlen
+ srclen
] = '\0';
715 return (dstlen
+ srclen
);
717 #endif /* !HAVE_STRLCAT */
722 * '_cups_strlcpy()' - Safely copy two strings.
725 size_t /* O - Length of string */
726 _cups_strlcpy(char *dst
, /* O - Destination string */
727 const char *src
, /* I - Source string */
728 size_t size
) /* I - Size of destination string buffer */
730 size_t srclen
; /* Length of source string */
734 * Figure out how much room is needed...
739 srclen
= strlen(src
);
742 * Copy the appropriate amount...
748 memmove(dst
, src
, srclen
);
753 #endif /* !HAVE_STRLCPY */
757 * 'compare_sp_items()' - Compare two string pool items...
760 static int /* O - Result of comparison */
761 compare_sp_items(_cups_sp_item_t
*a
, /* I - First item */
762 _cups_sp_item_t
*b
) /* I - Second item */
764 return (strcmp(a
->str
, b
->str
));