]>
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 "string-private.h"
24 #include "debug-private.h"
25 #include "thread-private.h"
35 static _cups_mutex_t sp_mutex
= _CUPS_MUTEX_INITIALIZER
;
36 /* Mutex to control access to pool */
37 static cups_array_t
*stringpool
= NULL
;
38 /* Global string pool */
45 static int compare_sp_items(_cups_sp_item_t
*a
, _cups_sp_item_t
*b
);
49 * '_cupsStrAlloc()' - Allocate/reference a string.
52 char * /* O - String pointer */
53 _cupsStrAlloc(const char *s
) /* I - String */
55 size_t slen
; /* Length of string */
56 _cups_sp_item_t
*item
, /* String pool item */
57 *key
; /* Search key */
61 * Range check input...
68 * Get the string pool...
71 _cupsMutexLock(&sp_mutex
);
74 stringpool
= cupsArrayNew((cups_array_func_t
)compare_sp_items
, NULL
);
78 _cupsMutexUnlock(&sp_mutex
);
84 * See if the string is already in the pool...
87 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
89 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
)
92 * Found it, return the cached string...
98 DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, "
99 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
102 if (item
->guard
!= _CUPS_STR_GUARD
)
104 #endif /* DEBUG_GUARDS */
106 _cupsMutexUnlock(&sp_mutex
);
112 * Not found, so allocate a new one...
116 item
= (_cups_sp_item_t
*)calloc(1, sizeof(_cups_sp_item_t
) + slen
);
119 _cupsMutexUnlock(&sp_mutex
);
125 memcpy(item
->str
, s
, slen
+ 1);
128 item
->guard
= _CUPS_STR_GUARD
;
130 DEBUG_printf(("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, "
131 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
133 #endif /* DEBUG_GUARDS */
136 * Add the string to the pool and return it...
139 cupsArrayAdd(stringpool
, item
);
141 _cupsMutexUnlock(&sp_mutex
);
148 * '_cupsStrFlush()' - Flush the string pool.
154 _cups_sp_item_t
*item
; /* Current item */
157 DEBUG_printf(("4_cupsStrFlush: %d strings in array",
158 cupsArrayCount(stringpool
)));
160 _cupsMutexLock(&sp_mutex
);
162 for (item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
164 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
167 cupsArrayDelete(stringpool
);
170 _cupsMutexUnlock(&sp_mutex
);
175 * '_cupsStrFormatd()' - Format a floating-point number.
178 char * /* O - Pointer to end of string */
179 _cupsStrFormatd(char *buf
, /* I - String */
180 char *bufend
, /* I - End of string buffer */
181 double number
, /* I - Number to format */
182 struct lconv
*loc
) /* I - Locale data */
184 char *bufptr
, /* Pointer into buffer */
185 temp
[1024], /* Temporary string */
186 *tempdec
, /* Pointer to decimal point */
187 *tempptr
; /* Pointer into temporary string */
188 const char *dec
; /* Decimal point */
189 int declen
; /* Length of decimal point */
193 * Format the number using the "%.12f" format and then eliminate
194 * unnecessary trailing 0's.
197 snprintf(temp
, sizeof(temp
), "%.12f", number
);
198 for (tempptr
= temp
+ strlen(temp
) - 1;
199 tempptr
> temp
&& *tempptr
== '0';
203 * Next, find the decimal point...
206 if (loc
&& loc
->decimal_point
)
208 dec
= loc
->decimal_point
;
209 declen
= (int)strlen(dec
);
218 tempdec
= strchr(temp
, *dec
);
220 tempdec
= strstr(temp
, dec
);
223 * Copy everything up to the decimal point...
228 for (tempptr
= temp
, bufptr
= buf
;
229 tempptr
< tempdec
&& bufptr
< bufend
;
230 *bufptr
++ = *tempptr
++);
234 if (*tempptr
&& bufptr
< bufend
)
238 while (*tempptr
&& bufptr
< bufend
)
239 *bufptr
++ = *tempptr
++;
246 strlcpy(buf
, temp
, (size_t)(bufend
- buf
+ 1));
247 bufptr
= buf
+ strlen(buf
);
255 * '_cupsStrFree()' - Free/dereference a string.
259 _cupsStrFree(const char *s
) /* I - String to free */
261 _cups_sp_item_t
*item
, /* String pool item */
262 *key
; /* Search key */
266 * Range check input...
273 * Check the string pool...
275 * We don't need to lock the mutex yet, as we only want to know if
276 * the stringpool is initialized. The rest of the code will still
277 * work if it is initialized before we lock...
284 * See if the string is already in the pool...
287 _cupsMutexLock(&sp_mutex
);
289 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
292 if (key
->guard
!= _CUPS_STR_GUARD
)
294 DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, "
295 "ref_count=%d", key
, key
->str
, key
->guard
, key
->ref_count
));
298 #endif /* DEBUG_GUARDS */
300 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
&&
304 * Found it, dereference...
309 if (!item
->ref_count
)
315 cupsArrayRemove(stringpool
, item
);
321 _cupsMutexUnlock(&sp_mutex
);
326 * '_cupsStrRetain()' - Increment the reference count of a string.
328 * Note: This function does not verify that the passed pointer is in the
329 * string pool, so any calls to it MUST know they are passing in a
333 char * /* O - Pointer to string */
334 _cupsStrRetain(const char *s
) /* I - String to retain */
336 _cups_sp_item_t
*item
; /* Pointer to string pool item */
341 item
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
344 if (item
->guard
!= _CUPS_STR_GUARD
)
346 DEBUG_printf(("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, "
347 "ref_count=%d", item
, s
, item
->guard
, item
->ref_count
));
350 #endif /* DEBUG_GUARDS */
352 _cupsMutexLock(&sp_mutex
);
356 _cupsMutexUnlock(&sp_mutex
);
364 * '_cupsStrScand()' - Scan a string for a floating-point number.
366 * This function handles the locale-specific BS so that a decimal
367 * point is always the period (".")...
370 double /* O - Number */
371 _cupsStrScand(const char *buf
, /* I - Pointer to number */
372 char **bufptr
, /* O - New pointer or NULL on error */
373 struct lconv
*loc
) /* I - Locale data */
375 char temp
[1024], /* Temporary buffer */
376 *tempptr
; /* Pointer into temporary buffer */
380 * Range check input...
387 * Skip leading whitespace...
390 while (_cups_isspace(*buf
))
394 * Copy leading sign, numbers, period, and then numbers...
398 if (*buf
== '-' || *buf
== '+')
401 while (isdigit(*buf
& 255))
402 if (tempptr
< (temp
+ sizeof(temp
) - 1))
415 * Read fractional portion of number...
420 if (loc
&& loc
->decimal_point
)
422 strlcpy(tempptr
, loc
->decimal_point
, sizeof(temp
) - (size_t)(tempptr
- temp
));
423 tempptr
+= strlen(tempptr
);
425 else if (tempptr
< (temp
+ sizeof(temp
) - 1))
435 while (isdigit(*buf
& 255))
436 if (tempptr
< (temp
+ sizeof(temp
) - 1))
447 if (*buf
== 'e' || *buf
== 'E')
453 if (tempptr
< (temp
+ sizeof(temp
) - 1))
463 if (*buf
== '+' || *buf
== '-')
465 if (tempptr
< (temp
+ sizeof(temp
) - 1))
476 while (isdigit(*buf
& 255))
477 if (tempptr
< (temp
+ sizeof(temp
) - 1))
489 * Nul-terminate the temporary string and return the value...
493 *bufptr
= (char *)buf
;
497 return (strtod(temp
, NULL
));
502 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
505 size_t /* O - Number of strings */
506 _cupsStrStatistics(size_t *alloc_bytes
, /* O - Allocated bytes */
507 size_t *total_bytes
) /* O - Total string bytes */
509 size_t count
, /* Number of strings */
510 abytes
, /* Allocated string bytes */
511 tbytes
, /* Total string bytes */
512 len
; /* Length of string */
513 _cups_sp_item_t
*item
; /* Current item */
517 * Loop through strings in pool, counting everything up...
520 _cupsMutexLock(&sp_mutex
);
522 for (count
= 0, abytes
= 0, tbytes
= 0,
523 item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
525 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
528 * Count allocated memory, using a 64-bit aligned buffer as a basis.
531 count
+= item
->ref_count
;
532 len
= (strlen(item
->str
) + 8) & (size_t)~7;
533 abytes
+= sizeof(_cups_sp_item_t
) + len
;
534 tbytes
+= item
->ref_count
* len
;
537 _cupsMutexUnlock(&sp_mutex
);
544 *alloc_bytes
= abytes
;
547 *total_bytes
= tbytes
;
554 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
558 _cups_strcpy(char *dst
, /* I - Destination string */
559 const char *src
) /* I - Source string */
569 * '_cups_strdup()' - Duplicate a string.
573 char * /* O - New string pointer */
574 _cups_strdup(const char *s
) /* I - String to duplicate */
576 char *t
; /* New string pointer */
577 size_t slen
; /* Length of string */
584 if ((t
= malloc(slen
+ 1)) == NULL
)
587 return (memcpy(t
, s
, slen
+ 1));
589 #endif /* !HAVE_STRDUP */
593 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
596 int /* O - Result of comparison (-1, 0, or 1) */
597 _cups_strcasecmp(const char *s
, /* I - First string */
598 const char *t
) /* I - Second string */
600 while (*s
!= '\0' && *t
!= '\0')
602 if (_cups_tolower(*s
) < _cups_tolower(*t
))
604 else if (_cups_tolower(*s
) > _cups_tolower(*t
))
611 if (*s
== '\0' && *t
== '\0')
620 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
623 int /* O - Result of comparison (-1, 0, or 1) */
624 _cups_strncasecmp(const char *s
, /* I - First string */
625 const char *t
, /* I - Second string */
626 size_t n
) /* I - Maximum number of characters to compare */
628 while (*s
!= '\0' && *t
!= '\0' && n
> 0)
630 if (_cups_tolower(*s
) < _cups_tolower(*t
))
632 else if (_cups_tolower(*s
) > _cups_tolower(*t
))
642 else if (*s
== '\0' && *t
== '\0')
653 * '_cups_strlcat()' - Safely concatenate two strings.
656 size_t /* O - Length of string */
657 _cups_strlcat(char *dst
, /* O - Destination string */
658 const char *src
, /* I - Source string */
659 size_t size
) /* I - Size of destination string buffer */
661 size_t srclen
; /* Length of source string */
662 size_t dstlen
; /* Length of destination string */
666 * Figure out how much room is left...
669 dstlen
= strlen(dst
);
673 return (dstlen
); /* No room, return immediately... */
676 * Figure out how much room is needed...
679 srclen
= strlen(src
);
682 * Copy the appropriate amount...
688 memmove(dst
+ dstlen
, src
, srclen
);
689 dst
[dstlen
+ srclen
] = '\0';
691 return (dstlen
+ srclen
);
693 #endif /* !HAVE_STRLCAT */
698 * '_cups_strlcpy()' - Safely copy two strings.
701 size_t /* O - Length of string */
702 _cups_strlcpy(char *dst
, /* O - Destination string */
703 const char *src
, /* I - Source string */
704 size_t size
) /* I - Size of destination string buffer */
706 size_t srclen
; /* Length of source string */
710 * Figure out how much room is needed...
715 srclen
= strlen(src
);
718 * Copy the appropriate amount...
724 memmove(dst
, src
, srclen
);
729 #endif /* !HAVE_STRLCPY */
733 * 'compare_sp_items()' - Compare two string pool items...
736 static int /* O - Result of comparison */
737 compare_sp_items(_cups_sp_item_t
*a
, /* I - First item */
738 _cups_sp_item_t
*b
) /* I - Second item */
740 return (strcmp(a
->str
, b
->str
));