]> 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/*
f7faf1f5 2 * "$Id: string.c 5368 2006-04-02 19:23:50Z mike $"
ef416fc2 3 *
4 * String functions for the Common UNIX Printing System (CUPS).
5 *
4400e98d 6 * Copyright 1997-2006 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;
246 declen = strlen(dec);
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
442 /*
443 * Nul-terminate the temporary string and return the value...
444 */
445
446 if (bufptr)
447 *bufptr = (char *)buf;
448
449 *tempptr = '\0';
450
451 return (strtod(temp, NULL));
452}
453
454
455/*
456 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
4400e98d 457 */
458
459size_t /* O - Number of strings */
757d2cad 460_cupsStrStatistics(size_t *alloc_bytes, /* O - Allocated bytes */
461 size_t *total_bytes) /* O - Total string bytes */
4400e98d 462{
463 size_t count, /* Number of strings */
464 abytes, /* Allocated string bytes */
465 tbytes, /* Total string bytes */
466 len; /* Length of string */
467 _cups_sp_item_t *item; /* Current item */
4400e98d 468
469
470 /*
471 * Loop through strings in pool, counting everything up...
472 */
473
d6ae789d 474#ifdef HAVE_PTHREAD_H
475 pthread_mutex_lock(&sp_mutex);
476#endif /* HAVE_PTHREAD_H */
4400e98d 477
478 for (count = 0, abytes = 0, tbytes = 0,
d6ae789d 479 item = (_cups_sp_item_t *)cupsArrayFirst(stringpool);
4400e98d 480 item;
d6ae789d 481 item = (_cups_sp_item_t *)cupsArrayNext(stringpool))
4400e98d 482 {
483 /*
484 * Count allocated memory, using a 64-bit aligned buffer as a basis.
485 */
486
487 count += item->ref_count;
488 len = (strlen(item->str) + 8) & ~7;
489 abytes += sizeof(_cups_sp_item_t) + len;
490 tbytes += item->ref_count * len;
491 }
492
d6ae789d 493#ifdef HAVE_PTHREAD_H
494 pthread_mutex_unlock(&sp_mutex);
495#endif /* HAVE_PTHREAD_H */
496
4400e98d 497 /*
498 * Return values...
499 */
500
501 if (alloc_bytes)
502 *alloc_bytes = abytes;
503
504 if (total_bytes)
505 *total_bytes = tbytes;
506
507 return (count);
508}
ef416fc2 509
510
511/*
512 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
513 */
514
515void
516_cups_strcpy(char *dst, /* I - Destination string */
517 const char *src) /* I - Source string */
518{
519 while (*src)
520 *dst++ = *src++;
521
522 *dst = '\0';
523}
524
525
526/*
527 * '_cups_strdup()' - Duplicate a string.
528 */
529
530#ifndef HAVE_STRDUP
531char * /* O - New string pointer */
532_cups_strdup(const char *s) /* I - String to duplicate */
533{
534 char *t; /* New string pointer */
535
536
537 if (s == NULL)
538 return (NULL);
539
540 if ((t = malloc(strlen(s) + 1)) == NULL)
541 return (NULL);
542
543 return (strcpy(t, s));
544}
545#endif /* !HAVE_STRDUP */
546
547
548/*
549 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
550 */
551
552#ifndef HAVE_STRCASECMP
553int /* O - Result of comparison (-1, 0, or 1) */
554_cups_strcasecmp(const char *s, /* I - First string */
555 const char *t) /* I - Second string */
556{
557 while (*s != '\0' && *t != '\0')
558 {
559 if (tolower(*s & 255) < tolower(*t & 255))
560 return (-1);
561 else if (tolower(*s & 255) > tolower(*t & 255))
562 return (1);
563
564 s ++;
565 t ++;
566 }
567
568 if (*s == '\0' && *t == '\0')
569 return (0);
570 else if (*s != '\0')
571 return (1);
572 else
573 return (-1);
574}
575#endif /* !HAVE_STRCASECMP */
576
577/*
578 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
579 */
580
581#ifndef HAVE_STRNCASECMP
582int /* O - Result of comparison (-1, 0, or 1) */
583_cups_strncasecmp(const char *s, /* I - First string */
584 vconst char *t, /* I - Second string */
585 size_t n) /* I - Maximum number of characters to compare */
586{
587 while (*s != '\0' && *t != '\0' && n > 0)
588 {
589 if (tolower(*s & 255) < tolower(*t & 255))
590 return (-1);
591 else if (tolower(*s & 255) > tolower(*t & 255))
592 return (1);
593
594 s ++;
595 t ++;
596 n --;
597 }
598
599 if (n == 0)
600 return (0);
601 else if (*s == '\0' && *t == '\0')
602 return (0);
603 else if (*s != '\0')
604 return (1);
605 else
606 return (-1);
607}
608#endif /* !HAVE_STRNCASECMP */
609
610
611#ifndef HAVE_STRLCAT
612/*
613 * '_cups_strlcat()' - Safely concatenate two strings.
614 */
615
616size_t /* O - Length of string */
617_cups_strlcat(char *dst, /* O - Destination string */
618 const char *src, /* I - Source string */
619 size_t size) /* I - Size of destination string buffer */
620{
621 size_t srclen; /* Length of source string */
622 size_t dstlen; /* Length of destination string */
623
624
625 /*
626 * Figure out how much room is left...
627 */
628
629 dstlen = strlen(dst);
630 size -= dstlen + 1;
631
632 if (!size)
633 return (dstlen); /* No room, return immediately... */
634
635 /*
636 * Figure out how much room is needed...
637 */
638
639 srclen = strlen(src);
640
641 /*
642 * Copy the appropriate amount...
643 */
644
645 if (srclen > size)
646 srclen = size;
647
648 memcpy(dst + dstlen, src, srclen);
649 dst[dstlen + srclen] = '\0';
650
651 return (dstlen + srclen);
652}
653#endif /* !HAVE_STRLCAT */
654
655
656#ifndef HAVE_STRLCPY
657/*
658 * '_cups_strlcpy()' - Safely copy two strings.
659 */
660
661size_t /* O - Length of string */
662_cups_strlcpy(char *dst, /* O - Destination string */
663 const char *src, /* I - Source string */
664 size_t size) /* I - Size of destination string buffer */
665{
666 size_t srclen; /* Length of source string */
667
668
669 /*
670 * Figure out how much room is needed...
671 */
672
673 size --;
674
675 srclen = strlen(src);
676
677 /*
678 * Copy the appropriate amount...
679 */
680
681 if (srclen > size)
682 srclen = size;
683
684 memcpy(dst, src, srclen);
685 dst[srclen] = '\0';
686
687 return (srclen);
688}
689#endif /* !HAVE_STRLCPY */
690
691
692/*
4400e98d 693 * 'compare_sp_items()' - Compare two string pool items...
694 */
695
696static int /* O - Result of comparison */
697compare_sp_items(_cups_sp_item_t *a, /* I - First item */
698 _cups_sp_item_t *b) /* I - Second item */
699{
700 return (strcmp(a->str, b->str));
701}
702
703
704/*
f7faf1f5 705 * End of "$Id: string.c 5368 2006-04-02 19:23:50Z mike $".
ef416fc2 706 */