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