]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/string.c
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 #include "string-private.h"
40 #include "debug-private.h"
41 #include "thread-private.h"
51 static _cups_mutex_t sp_mutex
= _CUPS_MUTEX_INITIALIZER
;
52 /* Mutex to control access to pool */
53 static cups_array_t
*stringpool
= NULL
;
54 /* Global string pool */
61 static int compare_sp_items(_cups_sp_item_t
*a
, _cups_sp_item_t
*b
);
65 * '_cupsStrAlloc()' - Allocate/reference a string.
68 char * /* O - String pointer */
69 _cupsStrAlloc(const char *s
) /* I - String */
71 _cups_sp_item_t
*item
, /* String pool item */
72 *key
; /* Search key */
76 * Range check input...
83 * Get the string pool...
86 _cupsMutexLock(&sp_mutex
);
89 stringpool
= cupsArrayNew((cups_array_func_t
)compare_sp_items
, NULL
);
93 _cupsMutexUnlock(&sp_mutex
);
99 * See if the string is already in the pool...
102 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
104 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
)
107 * Found it, return the cached string...
113 DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, "
114 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
117 if (item
->guard
!= _CUPS_STR_GUARD
)
119 #endif /* DEBUG_GUARDS */
121 _cupsMutexUnlock(&sp_mutex
);
127 * Not found, so allocate a new one...
130 item
= (_cups_sp_item_t
*)calloc(1, sizeof(_cups_sp_item_t
) + strlen(s
));
133 _cupsMutexUnlock(&sp_mutex
);
139 strcpy(item
->str
, s
);
142 item
->guard
= _CUPS_STR_GUARD
;
144 DEBUG_printf(("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, "
145 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
147 #endif /* DEBUG_GUARDS */
150 * Add the string to the pool and return it...
153 cupsArrayAdd(stringpool
, item
);
155 _cupsMutexUnlock(&sp_mutex
);
162 * '_cupsStrFlush()' - Flush the string pool.
168 _cups_sp_item_t
*item
; /* Current item */
171 DEBUG_printf(("4_cupsStrFlush: %d strings in array",
172 cupsArrayCount(stringpool
)));
174 _cupsMutexLock(&sp_mutex
);
176 for (item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
178 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
181 cupsArrayDelete(stringpool
);
184 _cupsMutexUnlock(&sp_mutex
);
189 * '_cupsStrFormatd()' - Format a floating-point number.
192 char * /* O - Pointer to end of string */
193 _cupsStrFormatd(char *buf
, /* I - String */
194 char *bufend
, /* I - End of string buffer */
195 double number
, /* I - Number to format */
196 struct lconv
*loc
) /* I - Locale data */
198 char *bufptr
, /* Pointer into buffer */
199 temp
[1024], /* Temporary string */
200 *tempdec
, /* Pointer to decimal point */
201 *tempptr
; /* Pointer into temporary string */
202 const char *dec
; /* Decimal point */
203 int declen
; /* Length of decimal point */
207 * Format the number using the "%.12f" format and then eliminate
208 * unnecessary trailing 0's.
211 snprintf(temp
, sizeof(temp
), "%.12f", number
);
212 for (tempptr
= temp
+ strlen(temp
) - 1;
213 tempptr
> temp
&& *tempptr
== '0';
217 * Next, find the decimal point...
220 if (loc
&& loc
->decimal_point
)
222 dec
= loc
->decimal_point
;
223 declen
= (int)strlen(dec
);
232 tempdec
= strchr(temp
, *dec
);
234 tempdec
= strstr(temp
, dec
);
237 * Copy everything up to the decimal point...
242 for (tempptr
= temp
, bufptr
= buf
;
243 tempptr
< tempdec
&& bufptr
< bufend
;
244 *bufptr
++ = *tempptr
++);
248 if (*tempptr
&& bufptr
< bufend
)
252 while (*tempptr
&& bufptr
< bufend
)
253 *bufptr
++ = *tempptr
++;
260 strlcpy(buf
, temp
, bufend
- buf
+ 1);
261 bufptr
= buf
+ strlen(buf
);
269 * '_cupsStrFree()' - Free/dereference a string.
273 _cupsStrFree(const char *s
) /* I - String to free */
275 _cups_sp_item_t
*item
, /* String pool item */
276 *key
; /* Search key */
280 * Range check input...
287 * Check the string pool...
289 * We don't need to lock the mutex yet, as we only want to know if
290 * the stringpool is initialized. The rest of the code will still
291 * work if it is initialized before we lock...
298 * See if the string is already in the pool...
301 _cupsMutexLock(&sp_mutex
);
303 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
306 if (key
->guard
!= _CUPS_STR_GUARD
)
308 DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, "
309 "ref_count=%d", key
, key
->str
, key
->guard
, key
->ref_count
));
312 #endif /* DEBUG_GUARDS */
314 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
&&
318 * Found it, dereference...
323 if (!item
->ref_count
)
329 cupsArrayRemove(stringpool
, item
);
335 _cupsMutexUnlock(&sp_mutex
);
340 * '_cupsStrRetain()' - Increment the reference count of a string.
342 * Note: This function does not verify that the passed pointer is in the
343 * string pool, so any calls to it MUST know they are passing in a
347 char * /* O - Pointer to string */
348 _cupsStrRetain(const char *s
) /* I - String to retain */
350 _cups_sp_item_t
*item
; /* Pointer to string pool item */
355 item
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
358 if (item
->guard
!= _CUPS_STR_GUARD
)
360 DEBUG_printf(("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, "
361 "ref_count=%d", item
, s
, item
->guard
, item
->ref_count
));
364 #endif /* DEBUG_GUARDS */
366 _cupsMutexLock(&sp_mutex
);
370 _cupsMutexUnlock(&sp_mutex
);
378 * '_cupsStrScand()' - Scan a string for a floating-point number.
380 * This function handles the locale-specific BS so that a decimal
381 * point is always the period (".")...
384 double /* O - Number */
385 _cupsStrScand(const char *buf
, /* I - Pointer to number */
386 char **bufptr
, /* O - New pointer or NULL on error */
387 struct lconv
*loc
) /* I - Locale data */
389 char temp
[1024], /* Temporary buffer */
390 *tempptr
; /* Pointer into temporary buffer */
394 * Range check input...
401 * Skip leading whitespace...
404 while (isspace(*buf
& 255))
408 * Copy leading sign, numbers, period, and then numbers...
412 if (*buf
== '-' || *buf
== '+')
415 while (isdigit(*buf
& 255))
416 if (tempptr
< (temp
+ sizeof(temp
) - 1))
429 * Read fractional portion of number...
434 if (loc
&& loc
->decimal_point
)
436 strlcpy(tempptr
, loc
->decimal_point
, sizeof(temp
) - (tempptr
- temp
));
437 tempptr
+= strlen(tempptr
);
439 else if (tempptr
< (temp
+ sizeof(temp
) - 1))
449 while (isdigit(*buf
& 255))
450 if (tempptr
< (temp
+ sizeof(temp
) - 1))
461 if (*buf
== 'e' || *buf
== 'E')
467 if (tempptr
< (temp
+ sizeof(temp
) - 1))
477 if (*buf
== '+' || *buf
== '-')
479 if (tempptr
< (temp
+ sizeof(temp
) - 1))
490 while (isdigit(*buf
& 255))
491 if (tempptr
< (temp
+ sizeof(temp
) - 1))
503 * Nul-terminate the temporary string and return the value...
507 *bufptr
= (char *)buf
;
511 return (strtod(temp
, NULL
));
516 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
519 size_t /* O - Number of strings */
520 _cupsStrStatistics(size_t *alloc_bytes
, /* O - Allocated bytes */
521 size_t *total_bytes
) /* O - Total string bytes */
523 size_t count
, /* Number of strings */
524 abytes
, /* Allocated string bytes */
525 tbytes
, /* Total string bytes */
526 len
; /* Length of string */
527 _cups_sp_item_t
*item
; /* Current item */
531 * Loop through strings in pool, counting everything up...
534 _cupsMutexLock(&sp_mutex
);
536 for (count
= 0, abytes
= 0, tbytes
= 0,
537 item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
539 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
542 * Count allocated memory, using a 64-bit aligned buffer as a basis.
545 count
+= item
->ref_count
;
546 len
= (strlen(item
->str
) + 8) & ~7;
547 abytes
+= sizeof(_cups_sp_item_t
) + len
;
548 tbytes
+= item
->ref_count
* len
;
551 _cupsMutexUnlock(&sp_mutex
);
558 *alloc_bytes
= abytes
;
561 *total_bytes
= tbytes
;
568 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
572 _cups_strcpy(char *dst
, /* I - Destination string */
573 const char *src
) /* I - Source string */
583 * '_cups_strdup()' - Duplicate a string.
587 char * /* O - New string pointer */
588 _cups_strdup(const char *s
) /* I - String to duplicate */
590 char *t
; /* New string pointer */
596 if ((t
= malloc(strlen(s
) + 1)) == NULL
)
599 return (strcpy(t
, s
));
601 #endif /* !HAVE_STRDUP */
605 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
608 #ifndef HAVE_STRCASECMP
609 int /* O - Result of comparison (-1, 0, or 1) */
610 _cups_strcasecmp(const char *s
, /* I - First string */
611 const char *t
) /* I - Second string */
613 while (*s
!= '\0' && *t
!= '\0')
615 if (tolower(*s
& 255) < tolower(*t
& 255))
617 else if (tolower(*s
& 255) > tolower(*t
& 255))
624 if (*s
== '\0' && *t
== '\0')
631 #endif /* !HAVE_STRCASECMP */
634 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
637 #ifndef HAVE_STRNCASECMP
638 int /* O - Result of comparison (-1, 0, or 1) */
639 _cups_strncasecmp(const char *s
, /* I - First string */
640 const char *t
, /* I - Second string */
641 size_t n
) /* I - Maximum number of characters to compare */
643 while (*s
!= '\0' && *t
!= '\0' && n
> 0)
645 if (tolower(*s
& 255) < tolower(*t
& 255))
647 else if (tolower(*s
& 255) > tolower(*t
& 255))
657 else if (*s
== '\0' && *t
== '\0')
664 #endif /* !HAVE_STRNCASECMP */
669 * '_cups_strlcat()' - Safely concatenate two strings.
672 size_t /* O - Length of string */
673 _cups_strlcat(char *dst
, /* O - Destination string */
674 const char *src
, /* I - Source string */
675 size_t size
) /* I - Size of destination string buffer */
677 size_t srclen
; /* Length of source string */
678 size_t dstlen
; /* Length of destination string */
682 * Figure out how much room is left...
685 dstlen
= strlen(dst
);
689 return (dstlen
); /* No room, return immediately... */
692 * Figure out how much room is needed...
695 srclen
= strlen(src
);
698 * Copy the appropriate amount...
704 memcpy(dst
+ dstlen
, src
, srclen
);
705 dst
[dstlen
+ srclen
] = '\0';
707 return (dstlen
+ srclen
);
709 #endif /* !HAVE_STRLCAT */
714 * '_cups_strlcpy()' - Safely copy two strings.
717 size_t /* O - Length of string */
718 _cups_strlcpy(char *dst
, /* O - Destination string */
719 const char *src
, /* I - Source string */
720 size_t size
) /* I - Size of destination string buffer */
722 size_t srclen
; /* Length of source string */
726 * Figure out how much room is needed...
731 srclen
= strlen(src
);
734 * Copy the appropriate amount...
740 memcpy(dst
, src
, srclen
);
745 #endif /* !HAVE_STRLCPY */
749 * 'compare_sp_items()' - Compare two string pool items...
752 static int /* O - Result of comparison */
753 compare_sp_items(_cups_sp_item_t
*a
, /* I - First item */
754 _cups_sp_item_t
*b
) /* I - Second item */
756 return (strcmp(a
->str
, b
->str
));
761 * End of "$Id: string.c 7460 2008-04-16 02:19:54Z mike $".