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