]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/string.c
4 * String functions for CUPS.
6 * Copyright 2007-2012 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 size_t slen
; /* Length of string */
73 _cups_sp_item_t
*item
, /* String pool item */
74 *key
; /* Search key */
78 * Range check input...
85 * Get the string pool...
88 _cupsMutexLock(&sp_mutex
);
91 stringpool
= cupsArrayNew((cups_array_func_t
)compare_sp_items
, NULL
);
95 _cupsMutexUnlock(&sp_mutex
);
101 * See if the string is already in the pool...
104 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
106 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
)
109 * Found it, return the cached string...
115 DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, "
116 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
119 if (item
->guard
!= _CUPS_STR_GUARD
)
121 #endif /* DEBUG_GUARDS */
123 _cupsMutexUnlock(&sp_mutex
);
129 * Not found, so allocate a new one...
133 item
= (_cups_sp_item_t
*)calloc(1, sizeof(_cups_sp_item_t
) + slen
);
136 _cupsMutexUnlock(&sp_mutex
);
142 memcpy(item
->str
, s
, slen
+ 1);
145 item
->guard
= _CUPS_STR_GUARD
;
147 DEBUG_printf(("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, "
148 "ref_count=%d", item
, item
->str
, s
, item
->guard
,
150 #endif /* DEBUG_GUARDS */
153 * Add the string to the pool and return it...
156 cupsArrayAdd(stringpool
, item
);
158 _cupsMutexUnlock(&sp_mutex
);
165 * '_cupsStrFlush()' - Flush the string pool.
171 _cups_sp_item_t
*item
; /* Current item */
174 DEBUG_printf(("4_cupsStrFlush: %d strings in array",
175 cupsArrayCount(stringpool
)));
177 _cupsMutexLock(&sp_mutex
);
179 for (item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
181 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
184 cupsArrayDelete(stringpool
);
187 _cupsMutexUnlock(&sp_mutex
);
192 * '_cupsStrFormatd()' - Format a floating-point number.
195 char * /* O - Pointer to end of string */
196 _cupsStrFormatd(char *buf
, /* I - String */
197 char *bufend
, /* I - End of string buffer */
198 double number
, /* I - Number to format */
199 struct lconv
*loc
) /* I - Locale data */
201 char *bufptr
, /* Pointer into buffer */
202 temp
[1024], /* Temporary string */
203 *tempdec
, /* Pointer to decimal point */
204 *tempptr
; /* Pointer into temporary string */
205 const char *dec
; /* Decimal point */
206 int declen
; /* Length of decimal point */
210 * Format the number using the "%.12f" format and then eliminate
211 * unnecessary trailing 0's.
214 snprintf(temp
, sizeof(temp
), "%.12f", number
);
215 for (tempptr
= temp
+ strlen(temp
) - 1;
216 tempptr
> temp
&& *tempptr
== '0';
220 * Next, find the decimal point...
223 if (loc
&& loc
->decimal_point
)
225 dec
= loc
->decimal_point
;
226 declen
= (int)strlen(dec
);
235 tempdec
= strchr(temp
, *dec
);
237 tempdec
= strstr(temp
, dec
);
240 * Copy everything up to the decimal point...
245 for (tempptr
= temp
, bufptr
= buf
;
246 tempptr
< tempdec
&& bufptr
< bufend
;
247 *bufptr
++ = *tempptr
++);
251 if (*tempptr
&& bufptr
< bufend
)
255 while (*tempptr
&& bufptr
< bufend
)
256 *bufptr
++ = *tempptr
++;
263 strlcpy(buf
, temp
, bufend
- buf
+ 1);
264 bufptr
= buf
+ strlen(buf
);
272 * '_cupsStrFree()' - Free/dereference a string.
276 _cupsStrFree(const char *s
) /* I - String to free */
278 _cups_sp_item_t
*item
, /* String pool item */
279 *key
; /* Search key */
283 * Range check input...
290 * Check the string pool...
292 * We don't need to lock the mutex yet, as we only want to know if
293 * the stringpool is initialized. The rest of the code will still
294 * work if it is initialized before we lock...
301 * See if the string is already in the pool...
304 _cupsMutexLock(&sp_mutex
);
306 key
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
309 if (key
->guard
!= _CUPS_STR_GUARD
)
311 DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, "
312 "ref_count=%d", key
, key
->str
, key
->guard
, key
->ref_count
));
315 #endif /* DEBUG_GUARDS */
317 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, key
)) != NULL
&&
321 * Found it, dereference...
326 if (!item
->ref_count
)
332 cupsArrayRemove(stringpool
, item
);
338 _cupsMutexUnlock(&sp_mutex
);
343 * '_cupsStrRetain()' - Increment the reference count of a string.
345 * Note: This function does not verify that the passed pointer is in the
346 * string pool, so any calls to it MUST know they are passing in a
350 char * /* O - Pointer to string */
351 _cupsStrRetain(const char *s
) /* I - String to retain */
353 _cups_sp_item_t
*item
; /* Pointer to string pool item */
358 item
= (_cups_sp_item_t
*)(s
- offsetof(_cups_sp_item_t
, str
));
361 if (item
->guard
!= _CUPS_STR_GUARD
)
363 DEBUG_printf(("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, "
364 "ref_count=%d", item
, s
, item
->guard
, item
->ref_count
));
367 #endif /* DEBUG_GUARDS */
369 _cupsMutexLock(&sp_mutex
);
373 _cupsMutexUnlock(&sp_mutex
);
381 * '_cupsStrScand()' - Scan a string for a floating-point number.
383 * This function handles the locale-specific BS so that a decimal
384 * point is always the period (".")...
387 double /* O - Number */
388 _cupsStrScand(const char *buf
, /* I - Pointer to number */
389 char **bufptr
, /* O - New pointer or NULL on error */
390 struct lconv
*loc
) /* I - Locale data */
392 char temp
[1024], /* Temporary buffer */
393 *tempptr
; /* Pointer into temporary buffer */
397 * Range check input...
404 * Skip leading whitespace...
407 while (_cups_isspace(*buf
))
411 * Copy leading sign, numbers, period, and then numbers...
415 if (*buf
== '-' || *buf
== '+')
418 while (isdigit(*buf
& 255))
419 if (tempptr
< (temp
+ sizeof(temp
) - 1))
432 * Read fractional portion of number...
437 if (loc
&& loc
->decimal_point
)
439 strlcpy(tempptr
, loc
->decimal_point
, sizeof(temp
) - (tempptr
- temp
));
440 tempptr
+= strlen(tempptr
);
442 else if (tempptr
< (temp
+ sizeof(temp
) - 1))
452 while (isdigit(*buf
& 255))
453 if (tempptr
< (temp
+ sizeof(temp
) - 1))
464 if (*buf
== 'e' || *buf
== 'E')
470 if (tempptr
< (temp
+ sizeof(temp
) - 1))
480 if (*buf
== '+' || *buf
== '-')
482 if (tempptr
< (temp
+ sizeof(temp
) - 1))
493 while (isdigit(*buf
& 255))
494 if (tempptr
< (temp
+ sizeof(temp
) - 1))
506 * Nul-terminate the temporary string and return the value...
510 *bufptr
= (char *)buf
;
514 return (strtod(temp
, NULL
));
519 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
522 size_t /* O - Number of strings */
523 _cupsStrStatistics(size_t *alloc_bytes
, /* O - Allocated bytes */
524 size_t *total_bytes
) /* O - Total string bytes */
526 size_t count
, /* Number of strings */
527 abytes
, /* Allocated string bytes */
528 tbytes
, /* Total string bytes */
529 len
; /* Length of string */
530 _cups_sp_item_t
*item
; /* Current item */
534 * Loop through strings in pool, counting everything up...
537 _cupsMutexLock(&sp_mutex
);
539 for (count
= 0, abytes
= 0, tbytes
= 0,
540 item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
542 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
545 * Count allocated memory, using a 64-bit aligned buffer as a basis.
548 count
+= item
->ref_count
;
549 len
= (strlen(item
->str
) + 8) & ~7;
550 abytes
+= sizeof(_cups_sp_item_t
) + len
;
551 tbytes
+= item
->ref_count
* len
;
554 _cupsMutexUnlock(&sp_mutex
);
561 *alloc_bytes
= abytes
;
564 *total_bytes
= tbytes
;
571 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
575 _cups_strcpy(char *dst
, /* I - Destination string */
576 const char *src
) /* I - Source string */
586 * '_cups_strdup()' - Duplicate a string.
590 char * /* O - New string pointer */
591 _cups_strdup(const char *s
) /* I - String to duplicate */
593 char *t
; /* New string pointer */
594 size_t slen
; /* Length of string */
601 if ((t
= malloc(slen
+ 1)) == NULL
)
604 return (memcpy(t
, s
, slen
+ 1));
606 #endif /* !HAVE_STRDUP */
610 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
613 int /* O - Result of comparison (-1, 0, or 1) */
614 _cups_strcasecmp(const char *s
, /* I - First string */
615 const char *t
) /* I - Second string */
617 while (*s
!= '\0' && *t
!= '\0')
619 if (_cups_tolower(*s
) < _cups_tolower(*t
))
621 else if (_cups_tolower(*s
) > _cups_tolower(*t
))
628 if (*s
== '\0' && *t
== '\0')
637 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
640 int /* O - Result of comparison (-1, 0, or 1) */
641 _cups_strncasecmp(const char *s
, /* I - First string */
642 const char *t
, /* I - Second string */
643 size_t n
) /* I - Maximum number of characters to compare */
645 while (*s
!= '\0' && *t
!= '\0' && n
> 0)
647 if (_cups_tolower(*s
) < _cups_tolower(*t
))
649 else if (_cups_tolower(*s
) > _cups_tolower(*t
))
659 else if (*s
== '\0' && *t
== '\0')
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
));