]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/string.c
f40f5f5fb5c9ae6af8398756b026366005e06749
2 * "$Id: string.c 7460 2008-04-16 02:19:54Z mike $"
4 * String functions for CUPS.
6 * Copyright 2007-2010 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 * _cupsStrAlloc() - Allocate/reference a string.
20 * _cupsStrFlush() - Flush the string pool.
21 * _cupsStrFormatd() - Format a floating-point number.
22 * _cupsStrFree() - Free/dereference a string.
23 * _cupsStrRetain() - Increment the reference count of a string.
24 * _cupsStrScand() - Scan a string for a floating-point number.
25 * _cupsStrStatistics() - Return allocation statistics for string pool.
26 * _cups_strcpy() - Copy a string allowing for overlapping strings.
27 * _cups_strdup() - Duplicate a string.
28 * _cups_strcasecmp() - Do a case-insensitive comparison.
29 * _cups_strncasecmp() - Do a case-insensitive comparison on up to N chars.
30 * _cups_strlcat() - Safely concatenate two strings.
31 * _cups_strlcpy() - Safely copy two strings.
32 * compare_sp_items() - Compare two string pool items...
36 * Include necessary headers...
39 #define _CUPS_STRING_C_
40 #include "string-private.h"
41 #include "debug-private.h"
42 #include "thread-private.h"
52 static _cups_mutex_t sp_mutex
= _CUPS_MUTEX_INITIALIZER
;
53 /* Mutex to control access to pool */
54 static cups_array_t
*stringpool
= NULL
;
55 /* Global string pool */
62 static int compare_sp_items(_cups_sp_item_t
*a
, _cups_sp_item_t
*b
);
66 * '_cupsStrAlloc()' - Allocate/reference a string.
69 char * /* O - String pointer */
70 _cupsStrAlloc(const char *s
) /* I - String */
72 _cups_sp_item_t
*item
, /* String pool item */
73 *key
; /* Search key */
77 * Range check input...
84 * Get the string pool...
87 _cupsMutexLock(&sp_mutex
);
90 stringpool
= cupsArrayNew((cups_array_func_t
)compare_sp_items
, NULL
);
94 _cupsMutexUnlock(&sp_mutex
);
100 * See if the string is already in the pool...
103 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
105 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
)
108 * Found it, return the cached string...
114 DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, "
115 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
118 if (item
->guard
!= _CUPS_STR_GUARD
)
120 #endif /* DEBUG_GUARDS */
122 _cupsMutexUnlock(&sp_mutex
);
128 * Not found, so allocate a new one...
131 item
= (_cups_sp_item_t
*)calloc(1, sizeof(_cups_sp_item_t
) + strlen(s
));
134 _cupsMutexUnlock(&sp_mutex
);
140 strcpy(item
->str
, s
);
143 item
->guard
= _CUPS_STR_GUARD
;
145 DEBUG_printf(("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, "
146 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
148 #endif /* DEBUG_GUARDS */
151 * Add the string to the pool and return it...
154 cupsArrayAdd(stringpool
, item
);
156 _cupsMutexUnlock(&sp_mutex
);
163 * '_cupsStrFlush()' - Flush the string pool.
169 _cups_sp_item_t
*item
; /* Current item */
172 DEBUG_printf(("4_cupsStrFlush: %d strings in array",
173 cupsArrayCount(stringpool
)));
175 _cupsMutexLock(&sp_mutex
);
177 for (item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
179 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
182 cupsArrayDelete(stringpool
);
185 _cupsMutexUnlock(&sp_mutex
);
190 * '_cupsStrFormatd()' - Format a floating-point number.
193 char * /* O - Pointer to end of string */
194 _cupsStrFormatd(char *buf
, /* I - String */
195 char *bufend
, /* I - End of string buffer */
196 double number
, /* I - Number to format */
197 struct lconv
*loc
) /* I - Locale data */
199 char *bufptr
, /* Pointer into buffer */
200 temp
[1024], /* Temporary string */
201 *tempdec
, /* Pointer to decimal point */
202 *tempptr
; /* Pointer into temporary string */
203 const char *dec
; /* Decimal point */
204 int declen
; /* Length of decimal point */
208 * Format the number using the "%.12f" format and then eliminate
209 * unnecessary trailing 0's.
212 snprintf(temp
, sizeof(temp
), "%.12f", number
);
213 for (tempptr
= temp
+ strlen(temp
) - 1;
214 tempptr
> temp
&& *tempptr
== '0';
218 * Next, find the decimal point...
221 if (loc
&& loc
->decimal_point
)
223 dec
= loc
->decimal_point
;
224 declen
= (int)strlen(dec
);
233 tempdec
= strchr(temp
, *dec
);
235 tempdec
= strstr(temp
, dec
);
238 * Copy everything up to the decimal point...
243 for (tempptr
= temp
, bufptr
= buf
;
244 tempptr
< tempdec
&& bufptr
< bufend
;
245 *bufptr
++ = *tempptr
++);
249 if (*tempptr
&& bufptr
< bufend
)
253 while (*tempptr
&& bufptr
< bufend
)
254 *bufptr
++ = *tempptr
++;
261 strlcpy(buf
, temp
, bufend
- buf
+ 1);
262 bufptr
= buf
+ strlen(buf
);
270 * '_cupsStrFree()' - Free/dereference a string.
274 _cupsStrFree(const char *s
) /* I - String to free */
276 _cups_sp_item_t
*item
, /* String pool item */
277 *key
; /* Search key */
281 * Range check input...
288 * Check the string pool...
290 * We don't need to lock the mutex yet, as we only want to know if
291 * the stringpool is initialized. The rest of the code will still
292 * work if it is initialized before we lock...
299 * See if the string is already in the pool...
302 _cupsMutexLock(&sp_mutex
);
304 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
307 if (key
->guard
!= _CUPS_STR_GUARD
)
309 DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, "
310 "ref_count=%d", key
, key
->str
, key
->guard
, key
->ref_count
));
313 #endif /* DEBUG_GUARDS */
315 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
&&
319 * Found it, dereference...
324 if (!item
->ref_count
)
330 cupsArrayRemove(stringpool
, item
);
336 _cupsMutexUnlock(&sp_mutex
);
341 * '_cupsStrRetain()' - Increment the reference count of a string.
343 * Note: This function does not verify that the passed pointer is in the
344 * string pool, so any calls to it MUST know they are passing in a
348 char * /* O - Pointer to string */
349 _cupsStrRetain(const char *s
) /* I - String to retain */
351 _cups_sp_item_t
*item
; /* Pointer to string pool item */
356 item
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
359 if (item
->guard
!= _CUPS_STR_GUARD
)
361 DEBUG_printf(("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, "
362 "ref_count=%d", item
, s
, item
->guard
, item
->ref_count
));
365 #endif /* DEBUG_GUARDS */
367 _cupsMutexLock(&sp_mutex
);
371 _cupsMutexUnlock(&sp_mutex
);
379 * '_cupsStrScand()' - Scan a string for a floating-point number.
381 * This function handles the locale-specific BS so that a decimal
382 * point is always the period (".")...
385 double /* O - Number */
386 _cupsStrScand(const char *buf
, /* I - Pointer to number */
387 char **bufptr
, /* O - New pointer or NULL on error */
388 struct lconv
*loc
) /* I - Locale data */
390 char temp
[1024], /* Temporary buffer */
391 *tempptr
; /* Pointer into temporary buffer */
395 * Range check input...
402 * Skip leading whitespace...
405 while (_cups_isspace(*buf
))
409 * Copy leading sign, numbers, period, and then numbers...
413 if (*buf
== '-' || *buf
== '+')
416 while (isdigit(*buf
& 255))
417 if (tempptr
< (temp
+ sizeof(temp
) - 1))
430 * Read fractional portion of number...
435 if (loc
&& loc
->decimal_point
)
437 strlcpy(tempptr
, loc
->decimal_point
, sizeof(temp
) - (tempptr
- temp
));
438 tempptr
+= strlen(tempptr
);
440 else if (tempptr
< (temp
+ sizeof(temp
) - 1))
450 while (isdigit(*buf
& 255))
451 if (tempptr
< (temp
+ sizeof(temp
) - 1))
462 if (*buf
== 'e' || *buf
== 'E')
468 if (tempptr
< (temp
+ sizeof(temp
) - 1))
478 if (*buf
== '+' || *buf
== '-')
480 if (tempptr
< (temp
+ sizeof(temp
) - 1))
491 while (isdigit(*buf
& 255))
492 if (tempptr
< (temp
+ sizeof(temp
) - 1))
504 * Nul-terminate the temporary string and return the value...
508 *bufptr
= (char *)buf
;
512 return (strtod(temp
, NULL
));
517 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
520 size_t /* O - Number of strings */
521 _cupsStrStatistics(size_t *alloc_bytes
, /* O - Allocated bytes */
522 size_t *total_bytes
) /* O - Total string bytes */
524 size_t count
, /* Number of strings */
525 abytes
, /* Allocated string bytes */
526 tbytes
, /* Total string bytes */
527 len
; /* Length of string */
528 _cups_sp_item_t
*item
; /* Current item */
532 * Loop through strings in pool, counting everything up...
535 _cupsMutexLock(&sp_mutex
);
537 for (count
= 0, abytes
= 0, tbytes
= 0,
538 item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
540 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
543 * Count allocated memory, using a 64-bit aligned buffer as a basis.
546 count
+= item
->ref_count
;
547 len
= (strlen(item
->str
) + 8) & ~7;
548 abytes
+= sizeof(_cups_sp_item_t
) + len
;
549 tbytes
+= item
->ref_count
* len
;
552 _cupsMutexUnlock(&sp_mutex
);
559 *alloc_bytes
= abytes
;
562 *total_bytes
= tbytes
;
569 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
573 _cups_strcpy(char *dst
, /* I - Destination string */
574 const char *src
) /* I - Source string */
584 * '_cups_strdup()' - Duplicate a string.
588 char * /* O - New string pointer */
589 _cups_strdup(const char *s
) /* I - String to duplicate */
591 char *t
; /* New string pointer */
597 if ((t
= malloc(strlen(s
) + 1)) == NULL
)
600 return (strcpy(t
, s
));
602 #endif /* !HAVE_STRDUP */
606 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
609 #ifndef HAVE_STRCASECMP
610 int /* O - Result of comparison (-1, 0, or 1) */
611 _cups_strcasecmp(const char *s
, /* I - First string */
612 const char *t
) /* I - Second string */
614 while (*s
!= '\0' && *t
!= '\0')
616 if (tolower(*s
& 255) < tolower(*t
& 255))
618 else if (tolower(*s
& 255) > tolower(*t
& 255))
625 if (*s
== '\0' && *t
== '\0')
632 #endif /* !HAVE_STRCASECMP */
635 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
638 #ifndef HAVE_STRNCASECMP
639 int /* O - Result of comparison (-1, 0, or 1) */
640 _cups_strncasecmp(const char *s
, /* I - First string */
641 const char *t
, /* I - Second string */
642 size_t n
) /* I - Maximum number of characters to compare */
644 while (*s
!= '\0' && *t
!= '\0' && n
> 0)
646 if (tolower(*s
& 255) < tolower(*t
& 255))
648 else if (tolower(*s
& 255) > tolower(*t
& 255))
658 else if (*s
== '\0' && *t
== '\0')
665 #endif /* !HAVE_STRNCASECMP */
670 * '_cups_strlcat()' - Safely concatenate two strings.
673 size_t /* O - Length of string */
674 _cups_strlcat(char *dst
, /* O - Destination string */
675 const char *src
, /* I - Source string */
676 size_t size
) /* I - Size of destination string buffer */
678 size_t srclen
; /* Length of source string */
679 size_t dstlen
; /* Length of destination string */
683 * Figure out how much room is left...
686 dstlen
= strlen(dst
);
690 return (dstlen
); /* No room, return immediately... */
693 * Figure out how much room is needed...
696 srclen
= strlen(src
);
699 * Copy the appropriate amount...
705 memcpy(dst
+ dstlen
, src
, srclen
);
706 dst
[dstlen
+ srclen
] = '\0';
708 return (dstlen
+ srclen
);
710 #endif /* !HAVE_STRLCAT */
715 * '_cups_strlcpy()' - Safely copy two strings.
718 size_t /* O - Length of string */
719 _cups_strlcpy(char *dst
, /* O - Destination string */
720 const char *src
, /* I - Source string */
721 size_t size
) /* I - Size of destination string buffer */
723 size_t srclen
; /* Length of source string */
727 * Figure out how much room is needed...
732 srclen
= strlen(src
);
735 * Copy the appropriate amount...
741 memcpy(dst
, src
, srclen
);
746 #endif /* !HAVE_STRLCPY */
750 * 'compare_sp_items()' - Compare two string pool items...
753 static int /* O - Result of comparison */
754 compare_sp_items(_cups_sp_item_t
*a
, /* I - First item */
755 _cups_sp_item_t
*b
) /* I - Second item */
757 return (strcmp(a
->str
, b
->str
));
762 * End of "$Id: string.c 7460 2008-04-16 02:19:54Z mike $".