]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/string.c
Merge changes from CUPS 1.5svn-r9105.
[thirdparty/cups.git] / cups / string.c
1 /*
2 * "$Id: string.c 7460 2008-04-16 02:19:54Z mike $"
3 *
4 * String functions for CUPS.
5 *
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
8 *
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/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
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...
33 */
34
35 /*
36 * Include necessary headers...
37 */
38
39 #include "string-private.h"
40 #include "debug-private.h"
41 #include "thread-private.h"
42 #include "array.h"
43 #include <stddef.h>
44 #include <limits.h>
45
46
47 /*
48 * Local globals...
49 */
50
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 */
55
56
57 /*
58 * Local functions...
59 */
60
61 static int compare_sp_items(_cups_sp_item_t *a, _cups_sp_item_t *b);
62
63
64 /*
65 * '_cupsStrAlloc()' - Allocate/reference a string.
66 */
67
68 char * /* O - String pointer */
69 _cupsStrAlloc(const char *s) /* I - String */
70 {
71 _cups_sp_item_t *item, /* String pool item */
72 *key; /* Search key */
73
74
75 /*
76 * Range check input...
77 */
78
79 if (!s)
80 return (NULL);
81
82 /*
83 * Get the string pool...
84 */
85
86 _cupsMutexLock(&sp_mutex);
87
88 if (!stringpool)
89 stringpool = cupsArrayNew((cups_array_func_t)compare_sp_items, NULL);
90
91 if (!stringpool)
92 {
93 _cupsMutexUnlock(&sp_mutex);
94
95 return (NULL);
96 }
97
98 /*
99 * See if the string is already in the pool...
100 */
101
102 key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str));
103
104 if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL)
105 {
106 /*
107 * Found it, return the cached string...
108 */
109
110 item->ref_count ++;
111
112 #ifdef DEBUG_GUARDS
113 DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, "
114 "ref_count=%d", item, item->str, s, item->guard,
115 item->ref_count));
116
117 if (item->guard != _CUPS_STR_GUARD)
118 abort();
119 #endif /* DEBUG_GUARDS */
120
121 _cupsMutexUnlock(&sp_mutex);
122
123 return (item->str);
124 }
125
126 /*
127 * Not found, so allocate a new one...
128 */
129
130 item = (_cups_sp_item_t *)calloc(1, sizeof(_cups_sp_item_t) + strlen(s));
131 if (!item)
132 {
133 _cupsMutexUnlock(&sp_mutex);
134
135 return (NULL);
136 }
137
138 item->ref_count = 1;
139 strcpy(item->str, s);
140
141 #ifdef DEBUG_GUARDS
142 item->guard = _CUPS_STR_GUARD;
143
144 DEBUG_printf(("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, "
145 "ref_count=%d", item, item->str, s, item->guard,
146 item->ref_count));
147 #endif /* DEBUG_GUARDS */
148
149 /*
150 * Add the string to the pool and return it...
151 */
152
153 cupsArrayAdd(stringpool, item);
154
155 _cupsMutexUnlock(&sp_mutex);
156
157 return (item->str);
158 }
159
160
161 /*
162 * '_cupsStrFlush()' - Flush the string pool.
163 */
164
165 void
166 _cupsStrFlush(void)
167 {
168 _cups_sp_item_t *item; /* Current item */
169
170
171 DEBUG_printf(("4_cupsStrFlush: %d strings in array",
172 cupsArrayCount(stringpool)));
173
174 _cupsMutexLock(&sp_mutex);
175
176 for (item = (_cups_sp_item_t *)cupsArrayFirst(stringpool);
177 item;
178 item = (_cups_sp_item_t *)cupsArrayNext(stringpool))
179 free(item);
180
181 cupsArrayDelete(stringpool);
182 stringpool = NULL;
183
184 _cupsMutexUnlock(&sp_mutex);
185 }
186
187
188 /*
189 * '_cupsStrFormatd()' - Format a floating-point number.
190 */
191
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 */
197 {
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 */
204
205
206 /*
207 * Format the number using the "%.12f" format and then eliminate
208 * unnecessary trailing 0's.
209 */
210
211 snprintf(temp, sizeof(temp), "%.12f", number);
212 for (tempptr = temp + strlen(temp) - 1;
213 tempptr > temp && *tempptr == '0';
214 *tempptr-- = '\0');
215
216 /*
217 * Next, find the decimal point...
218 */
219
220 if (loc && loc->decimal_point)
221 {
222 dec = loc->decimal_point;
223 declen = (int)strlen(dec);
224 }
225 else
226 {
227 dec = ".";
228 declen = 1;
229 }
230
231 if (declen == 1)
232 tempdec = strchr(temp, *dec);
233 else
234 tempdec = strstr(temp, dec);
235
236 /*
237 * Copy everything up to the decimal point...
238 */
239
240 if (tempdec)
241 {
242 for (tempptr = temp, bufptr = buf;
243 tempptr < tempdec && bufptr < bufend;
244 *bufptr++ = *tempptr++);
245
246 tempptr += declen;
247
248 if (*tempptr && bufptr < bufend)
249 {
250 *bufptr++ = '.';
251
252 while (*tempptr && bufptr < bufend)
253 *bufptr++ = *tempptr++;
254 }
255
256 *bufptr = '\0';
257 }
258 else
259 {
260 strlcpy(buf, temp, bufend - buf + 1);
261 bufptr = buf + strlen(buf);
262 }
263
264 return (bufptr);
265 }
266
267
268 /*
269 * '_cupsStrFree()' - Free/dereference a string.
270 */
271
272 void
273 _cupsStrFree(const char *s) /* I - String to free */
274 {
275 _cups_sp_item_t *item, /* String pool item */
276 *key; /* Search key */
277
278
279 /*
280 * Range check input...
281 */
282
283 if (!s)
284 return;
285
286 /*
287 * Check the string pool...
288 *
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...
292 */
293
294 if (!stringpool)
295 return;
296
297 /*
298 * See if the string is already in the pool...
299 */
300
301 _cupsMutexLock(&sp_mutex);
302
303 key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str));
304
305 #ifdef DEBUG_GUARDS
306 if (key->guard != _CUPS_STR_GUARD)
307 {
308 DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, "
309 "ref_count=%d", key, key->str, key->guard, key->ref_count));
310 abort();
311 }
312 #endif /* DEBUG_GUARDS */
313
314 if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL &&
315 item == key)
316 {
317 /*
318 * Found it, dereference...
319 */
320
321 item->ref_count --;
322
323 if (!item->ref_count)
324 {
325 /*
326 * Remove and free...
327 */
328
329 cupsArrayRemove(stringpool, item);
330
331 free(item);
332 }
333 }
334
335 _cupsMutexUnlock(&sp_mutex);
336 }
337
338
339 /*
340 * '_cupsStrRetain()' - Increment the reference count of a string.
341 *
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
344 * good pointer.
345 */
346
347 char * /* O - Pointer to string */
348 _cupsStrRetain(const char *s) /* I - String to retain */
349 {
350 _cups_sp_item_t *item; /* Pointer to string pool item */
351
352
353 if (s)
354 {
355 item = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str));
356
357 #ifdef DEBUG_GUARDS
358 if (item->guard != _CUPS_STR_GUARD)
359 {
360 DEBUG_printf(("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, "
361 "ref_count=%d", item, s, item->guard, item->ref_count));
362 abort();
363 }
364 #endif /* DEBUG_GUARDS */
365
366 _cupsMutexLock(&sp_mutex);
367
368 item->ref_count ++;
369
370 _cupsMutexUnlock(&sp_mutex);
371 }
372
373 return ((char *)s);
374 }
375
376
377 /*
378 * '_cupsStrScand()' - Scan a string for a floating-point number.
379 *
380 * This function handles the locale-specific BS so that a decimal
381 * point is always the period (".")...
382 */
383
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 */
388 {
389 char temp[1024], /* Temporary buffer */
390 *tempptr; /* Pointer into temporary buffer */
391
392
393 /*
394 * Range check input...
395 */
396
397 if (!buf)
398 return (0.0);
399
400 /*
401 * Skip leading whitespace...
402 */
403
404 while (isspace(*buf & 255))
405 buf ++;
406
407 /*
408 * Copy leading sign, numbers, period, and then numbers...
409 */
410
411 tempptr = temp;
412 if (*buf == '-' || *buf == '+')
413 *tempptr++ = *buf++;
414
415 while (isdigit(*buf & 255))
416 if (tempptr < (temp + sizeof(temp) - 1))
417 *tempptr++ = *buf++;
418 else
419 {
420 if (bufptr)
421 *bufptr = NULL;
422
423 return (0.0);
424 }
425
426 if (*buf == '.')
427 {
428 /*
429 * Read fractional portion of number...
430 */
431
432 buf ++;
433
434 if (loc && loc->decimal_point)
435 {
436 strlcpy(tempptr, loc->decimal_point, sizeof(temp) - (tempptr - temp));
437 tempptr += strlen(tempptr);
438 }
439 else if (tempptr < (temp + sizeof(temp) - 1))
440 *tempptr++ = '.';
441 else
442 {
443 if (bufptr)
444 *bufptr = NULL;
445
446 return (0.0);
447 }
448
449 while (isdigit(*buf & 255))
450 if (tempptr < (temp + sizeof(temp) - 1))
451 *tempptr++ = *buf++;
452 else
453 {
454 if (bufptr)
455 *bufptr = NULL;
456
457 return (0.0);
458 }
459 }
460
461 if (*buf == 'e' || *buf == 'E')
462 {
463 /*
464 * Read exponent...
465 */
466
467 if (tempptr < (temp + sizeof(temp) - 1))
468 *tempptr++ = *buf++;
469 else
470 {
471 if (bufptr)
472 *bufptr = NULL;
473
474 return (0.0);
475 }
476
477 if (*buf == '+' || *buf == '-')
478 {
479 if (tempptr < (temp + sizeof(temp) - 1))
480 *tempptr++ = *buf++;
481 else
482 {
483 if (bufptr)
484 *bufptr = NULL;
485
486 return (0.0);
487 }
488 }
489
490 while (isdigit(*buf & 255))
491 if (tempptr < (temp + sizeof(temp) - 1))
492 *tempptr++ = *buf++;
493 else
494 {
495 if (bufptr)
496 *bufptr = NULL;
497
498 return (0.0);
499 }
500 }
501
502 /*
503 * Nul-terminate the temporary string and return the value...
504 */
505
506 if (bufptr)
507 *bufptr = (char *)buf;
508
509 *tempptr = '\0';
510
511 return (strtod(temp, NULL));
512 }
513
514
515 /*
516 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
517 */
518
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 */
522 {
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 */
528
529
530 /*
531 * Loop through strings in pool, counting everything up...
532 */
533
534 _cupsMutexLock(&sp_mutex);
535
536 for (count = 0, abytes = 0, tbytes = 0,
537 item = (_cups_sp_item_t *)cupsArrayFirst(stringpool);
538 item;
539 item = (_cups_sp_item_t *)cupsArrayNext(stringpool))
540 {
541 /*
542 * Count allocated memory, using a 64-bit aligned buffer as a basis.
543 */
544
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;
549 }
550
551 _cupsMutexUnlock(&sp_mutex);
552
553 /*
554 * Return values...
555 */
556
557 if (alloc_bytes)
558 *alloc_bytes = abytes;
559
560 if (total_bytes)
561 *total_bytes = tbytes;
562
563 return (count);
564 }
565
566
567 /*
568 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
569 */
570
571 void
572 _cups_strcpy(char *dst, /* I - Destination string */
573 const char *src) /* I - Source string */
574 {
575 while (*src)
576 *dst++ = *src++;
577
578 *dst = '\0';
579 }
580
581
582 /*
583 * '_cups_strdup()' - Duplicate a string.
584 */
585
586 #ifndef HAVE_STRDUP
587 char * /* O - New string pointer */
588 _cups_strdup(const char *s) /* I - String to duplicate */
589 {
590 char *t; /* New string pointer */
591
592
593 if (s == NULL)
594 return (NULL);
595
596 if ((t = malloc(strlen(s) + 1)) == NULL)
597 return (NULL);
598
599 return (strcpy(t, s));
600 }
601 #endif /* !HAVE_STRDUP */
602
603
604 /*
605 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
606 */
607
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 */
612 {
613 while (*s != '\0' && *t != '\0')
614 {
615 if (tolower(*s & 255) < tolower(*t & 255))
616 return (-1);
617 else if (tolower(*s & 255) > tolower(*t & 255))
618 return (1);
619
620 s ++;
621 t ++;
622 }
623
624 if (*s == '\0' && *t == '\0')
625 return (0);
626 else if (*s != '\0')
627 return (1);
628 else
629 return (-1);
630 }
631 #endif /* !HAVE_STRCASECMP */
632
633 /*
634 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
635 */
636
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 */
642 {
643 while (*s != '\0' && *t != '\0' && n > 0)
644 {
645 if (tolower(*s & 255) < tolower(*t & 255))
646 return (-1);
647 else if (tolower(*s & 255) > tolower(*t & 255))
648 return (1);
649
650 s ++;
651 t ++;
652 n --;
653 }
654
655 if (n == 0)
656 return (0);
657 else if (*s == '\0' && *t == '\0')
658 return (0);
659 else if (*s != '\0')
660 return (1);
661 else
662 return (-1);
663 }
664 #endif /* !HAVE_STRNCASECMP */
665
666
667 #ifndef HAVE_STRLCAT
668 /*
669 * '_cups_strlcat()' - Safely concatenate two strings.
670 */
671
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 */
676 {
677 size_t srclen; /* Length of source string */
678 size_t dstlen; /* Length of destination string */
679
680
681 /*
682 * Figure out how much room is left...
683 */
684
685 dstlen = strlen(dst);
686 size -= dstlen + 1;
687
688 if (!size)
689 return (dstlen); /* No room, return immediately... */
690
691 /*
692 * Figure out how much room is needed...
693 */
694
695 srclen = strlen(src);
696
697 /*
698 * Copy the appropriate amount...
699 */
700
701 if (srclen > size)
702 srclen = size;
703
704 memcpy(dst + dstlen, src, srclen);
705 dst[dstlen + srclen] = '\0';
706
707 return (dstlen + srclen);
708 }
709 #endif /* !HAVE_STRLCAT */
710
711
712 #ifndef HAVE_STRLCPY
713 /*
714 * '_cups_strlcpy()' - Safely copy two strings.
715 */
716
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 */
721 {
722 size_t srclen; /* Length of source string */
723
724
725 /*
726 * Figure out how much room is needed...
727 */
728
729 size --;
730
731 srclen = strlen(src);
732
733 /*
734 * Copy the appropriate amount...
735 */
736
737 if (srclen > size)
738 srclen = size;
739
740 memcpy(dst, src, srclen);
741 dst[srclen] = '\0';
742
743 return (srclen);
744 }
745 #endif /* !HAVE_STRLCPY */
746
747
748 /*
749 * 'compare_sp_items()' - Compare two string pool items...
750 */
751
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 */
755 {
756 return (strcmp(a->str, b->str));
757 }
758
759
760 /*
761 * End of "$Id: string.c 7460 2008-04-16 02:19:54Z mike $".
762 */