]> 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/*
e1d6a774 2 * "$Id: string.c 5286 2006-03-13 16:32:28Z 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>
49#include "debug.h"
ef416fc2 50#include "string.h"
4400e98d 51#include "globals.h"
52
53
54/*
55 * Local functions...
56 */
57
58static int compare_sp_items(_cups_sp_item_t *a, _cups_sp_item_t *b);
59
60
61/*
757d2cad 62 * '_cupsStrAlloc()' - Allocate/reference a string.
4400e98d 63 */
64
65char * /* O - String pointer */
757d2cad 66_cupsStrAlloc(const char *s) /* I - String */
4400e98d 67{
68 _cups_globals_t *cg; /* Global data */
69 _cups_sp_item_t *item, /* String pool item */
70 key; /* Search key */
71
72
73 /*
74 * Range check input...
75 */
76
77 if (!s)
78 return (NULL);
79
80 /*
81 * Get the string pool...
82 */
83
84 cg = _cupsGlobals();
85
86 if (!cg->stringpool)
87 cg->stringpool = cupsArrayNew((cups_array_func_t)compare_sp_items, NULL);
88
89 if (!cg->stringpool)
90 return (NULL);
91
92 /*
93 * See if the string is already in the pool...
94 */
95
96 key.str = (char *)s;
97
98 if ((item = (_cups_sp_item_t *)cupsArrayFind(cg->stringpool, &key)) != NULL)
99 {
100 /*
101 * Found it, return the cached string...
102 */
103
104 item->ref_count ++;
105
106 return (item->str);
107 }
108
109 /*
110 * Not found, so allocate a new one...
111 */
112
113 item = (_cups_sp_item_t *)calloc(1, sizeof(_cups_sp_item_t));
114 if (!item)
115 return (NULL);
116
117 item->ref_count = 1;
118 item->str = strdup(s);
119
120 if (!item->str)
121 {
122 free(item);
123 return (NULL);
124 }
125
126 /*
127 * Add the string to the pool and return it...
128 */
129
130 cupsArrayAdd(cg->stringpool, item);
131
132 return (item->str);
133}
134
135
136/*
757d2cad 137 * '_cupsStrFlush()' - Flush the string pool...
4400e98d 138 */
139
140void
757d2cad 141_cupsStrFlush(_cups_globals_t *cg) /* I - Global data */
4400e98d 142{
143 _cups_sp_item_t *item; /* Current item */
144
145
146 for (item = (_cups_sp_item_t *)cupsArrayFirst(cg->stringpool);
147 item;
148 item = (_cups_sp_item_t *)cupsArrayNext(cg->stringpool))
149 {
150 free(item->str);
151 free(item);
152 }
153
154 cupsArrayDelete(cg->stringpool);
155}
156
157
158/*
757d2cad 159 * '_cupsStrFormatd()' - Format a floating-point number.
160 */
161
162char * /* O - Pointer to end of string */
163_cupsStrFormatd(char *buf, /* I - String */
164 char *bufend, /* I - End of string buffer */
165 double number, /* I - Number to format */
166 struct lconv *loc) /* I - Locale data */
167{
168 char *bufptr, /* Pointer into buffer */
169 temp[1024], /* Temporary string */
170 *tempdec, /* Pointer to decimal point */
171 *tempptr; /* Pointer into temporary string */
172 const char *dec; /* Decimal point */
173 int declen; /* Length of decimal point */
174
175
176 /*
177 * Format the number using the "%.12f" format and then eliminate
178 * unnecessary trailing 0's.
179 */
180
181 snprintf(temp, sizeof(temp), "%.12f", number);
182 for (tempptr = temp + strlen(temp) - 1;
183 tempptr > temp && *tempptr == '0';
184 *tempptr-- = '\0');
185
186 /*
187 * Next, find the decimal point...
188 */
189
190 if (loc && loc->decimal_point)
191 {
192 dec = loc->decimal_point;
193 declen = strlen(dec);
194 }
195 else
196 {
197 dec = ".";
198 declen = 1;
199 }
200
201 if (declen == 1)
202 tempdec = strchr(temp, *dec);
203 else
204 tempdec = strstr(temp, dec);
205
206 /*
207 * Copy everything up to the decimal point...
208 */
209
210 if (tempdec)
211 {
212 for (tempptr = temp, bufptr = buf;
213 tempptr < tempdec && bufptr < bufend;
214 *bufptr++ = *tempptr++);
215
216 tempdec += declen;
217
218 if (*tempdec && bufptr < bufend)
219 {
220 *bufptr++ = '.';
221
222 while (*tempptr && bufptr < bufend)
223 *bufptr++ = *tempptr++;
224 }
225
226 *bufptr = '\0';
227 }
228 else
229 {
230 strlcpy(buf, temp, bufend - buf + 1);
231 bufptr = buf + strlen(buf);
232 }
233
234 return (bufptr);
235}
236
237
238/*
239 * '_cupsStrFree()' - Free/dereference a string.
4400e98d 240 */
241
242void
757d2cad 243_cupsStrFree(const char *s) /* I - String to free */
4400e98d 244{
245 _cups_globals_t *cg; /* Global data */
246 _cups_sp_item_t *item, /* String pool item */
247 key; /* Search key */
248
249
250 /*
251 * Range check input...
252 */
253
254 if (!s)
255 return;
256
257 /*
258 * Get the string pool...
259 */
260
261 cg = _cupsGlobals();
262
263 if (!cg->stringpool)
264 return;
265
266 /*
267 * See if the string is already in the pool...
268 */
269
270 key.str = (char *)s;
271
272 if ((item = (_cups_sp_item_t *)cupsArrayFind(cg->stringpool, &key)) != NULL)
273 {
274 /*
275 * Found it, dereference...
276 */
277
278 item->ref_count --;
279
280 if (!item->ref_count)
281 {
282 /*
283 * Remove and free...
284 */
285
286 cupsArrayRemove(cg->stringpool, item);
287
288 free(item->str);
289 free(item);
290 }
291 }
292}
293
294
295/*
757d2cad 296 * '_cupsStrScand()' - Scan a string for a floating-point number.
297 *
298 * This function handles the locale-specific BS so that a decimal
299 * point is always the period (".")...
300 */
301
302double /* O - Number */
303_cupsStrScand(const char *buf, /* I - Pointer to number */
304 char **bufptr, /* O - New pointer or NULL on error */
305 struct lconv *loc) /* I - Locale data */
306{
307 char temp[1024], /* Temporary buffer */
308 *tempptr; /* Pointer into temporary buffer */
309
310
311 /*
312 * Range check input...
313 */
314
315 if (!buf)
316 return (0.0);
317
318 /*
319 * Skip leading whitespace...
320 */
321
322 while (isspace(*buf & 255))
323 buf ++;
324
325 /*
326 * Copy leading sign, numbers, period, and then numbers...
327 */
328
329 tempptr = temp;
330 if (*buf == '-' || *buf == '+')
331 *tempptr++ = *buf++;
332
333 while (isdigit(*buf & 255))
334 if (tempptr < (temp + sizeof(temp) - 1))
335 *tempptr++ = *buf++;
336 else
337 {
338 if (bufptr)
339 *bufptr = NULL;
340
341 return (0.0);
342 }
343
344 if (*buf == '.')
345 {
e1d6a774 346 /*
347 * Read fractional portion of number...
348 */
349
350 buf ++;
351
757d2cad 352 if (loc && loc->decimal_point)
353 {
354 strlcpy(tempptr, loc->decimal_point, sizeof(temp) - (tempptr - temp));
355 tempptr += strlen(tempptr);
356 }
357 else if (tempptr < (temp + sizeof(temp) - 1))
358 *tempptr++ = '.';
359 else
360 {
361 if (bufptr)
362 *bufptr = NULL;
363
364 return (0.0);
365 }
366
367 while (isdigit(*buf & 255))
368 if (tempptr < (temp + sizeof(temp) - 1))
369 *tempptr++ = *buf++;
370 else
371 {
372 if (bufptr)
373 *bufptr = NULL;
374
375 return (0.0);
376 }
377 }
378
379 /*
380 * Nul-terminate the temporary string and return the value...
381 */
382
383 if (bufptr)
384 *bufptr = (char *)buf;
385
386 *tempptr = '\0';
387
388 return (strtod(temp, NULL));
389}
390
391
392/*
393 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
4400e98d 394 */
395
396size_t /* O - Number of strings */
757d2cad 397_cupsStrStatistics(size_t *alloc_bytes, /* O - Allocated bytes */
398 size_t *total_bytes) /* O - Total string bytes */
4400e98d 399{
400 size_t count, /* Number of strings */
401 abytes, /* Allocated string bytes */
402 tbytes, /* Total string bytes */
403 len; /* Length of string */
404 _cups_sp_item_t *item; /* Current item */
405 _cups_globals_t *cg; /* Global data */
406
407
408 /*
409 * Loop through strings in pool, counting everything up...
410 */
411
412 cg = _cupsGlobals();
413
414 for (count = 0, abytes = 0, tbytes = 0,
415 item = (_cups_sp_item_t *)cupsArrayFirst(cg->stringpool);
416 item;
417 item = (_cups_sp_item_t *)cupsArrayNext(cg->stringpool))
418 {
419 /*
420 * Count allocated memory, using a 64-bit aligned buffer as a basis.
421 */
422
423 count += item->ref_count;
424 len = (strlen(item->str) + 8) & ~7;
425 abytes += sizeof(_cups_sp_item_t) + len;
426 tbytes += item->ref_count * len;
427 }
428
429 /*
430 * Return values...
431 */
432
433 if (alloc_bytes)
434 *alloc_bytes = abytes;
435
436 if (total_bytes)
437 *total_bytes = tbytes;
438
439 return (count);
440}
ef416fc2 441
442
443/*
444 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
445 */
446
447void
448_cups_strcpy(char *dst, /* I - Destination string */
449 const char *src) /* I - Source string */
450{
451 while (*src)
452 *dst++ = *src++;
453
454 *dst = '\0';
455}
456
457
458/*
459 * '_cups_strdup()' - Duplicate a string.
460 */
461
462#ifndef HAVE_STRDUP
463char * /* O - New string pointer */
464_cups_strdup(const char *s) /* I - String to duplicate */
465{
466 char *t; /* New string pointer */
467
468
469 if (s == NULL)
470 return (NULL);
471
472 if ((t = malloc(strlen(s) + 1)) == NULL)
473 return (NULL);
474
475 return (strcpy(t, s));
476}
477#endif /* !HAVE_STRDUP */
478
479
480/*
481 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
482 */
483
484#ifndef HAVE_STRCASECMP
485int /* O - Result of comparison (-1, 0, or 1) */
486_cups_strcasecmp(const char *s, /* I - First string */
487 const char *t) /* I - Second string */
488{
489 while (*s != '\0' && *t != '\0')
490 {
491 if (tolower(*s & 255) < tolower(*t & 255))
492 return (-1);
493 else if (tolower(*s & 255) > tolower(*t & 255))
494 return (1);
495
496 s ++;
497 t ++;
498 }
499
500 if (*s == '\0' && *t == '\0')
501 return (0);
502 else if (*s != '\0')
503 return (1);
504 else
505 return (-1);
506}
507#endif /* !HAVE_STRCASECMP */
508
509/*
510 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
511 */
512
513#ifndef HAVE_STRNCASECMP
514int /* O - Result of comparison (-1, 0, or 1) */
515_cups_strncasecmp(const char *s, /* I - First string */
516 vconst char *t, /* I - Second string */
517 size_t n) /* I - Maximum number of characters to compare */
518{
519 while (*s != '\0' && *t != '\0' && n > 0)
520 {
521 if (tolower(*s & 255) < tolower(*t & 255))
522 return (-1);
523 else if (tolower(*s & 255) > tolower(*t & 255))
524 return (1);
525
526 s ++;
527 t ++;
528 n --;
529 }
530
531 if (n == 0)
532 return (0);
533 else if (*s == '\0' && *t == '\0')
534 return (0);
535 else if (*s != '\0')
536 return (1);
537 else
538 return (-1);
539}
540#endif /* !HAVE_STRNCASECMP */
541
542
543#ifndef HAVE_STRLCAT
544/*
545 * '_cups_strlcat()' - Safely concatenate two strings.
546 */
547
548size_t /* O - Length of string */
549_cups_strlcat(char *dst, /* O - Destination string */
550 const char *src, /* I - Source string */
551 size_t size) /* I - Size of destination string buffer */
552{
553 size_t srclen; /* Length of source string */
554 size_t dstlen; /* Length of destination string */
555
556
557 /*
558 * Figure out how much room is left...
559 */
560
561 dstlen = strlen(dst);
562 size -= dstlen + 1;
563
564 if (!size)
565 return (dstlen); /* No room, return immediately... */
566
567 /*
568 * Figure out how much room is needed...
569 */
570
571 srclen = strlen(src);
572
573 /*
574 * Copy the appropriate amount...
575 */
576
577 if (srclen > size)
578 srclen = size;
579
580 memcpy(dst + dstlen, src, srclen);
581 dst[dstlen + srclen] = '\0';
582
583 return (dstlen + srclen);
584}
585#endif /* !HAVE_STRLCAT */
586
587
588#ifndef HAVE_STRLCPY
589/*
590 * '_cups_strlcpy()' - Safely copy two strings.
591 */
592
593size_t /* O - Length of string */
594_cups_strlcpy(char *dst, /* O - Destination string */
595 const char *src, /* I - Source string */
596 size_t size) /* I - Size of destination string buffer */
597{
598 size_t srclen; /* Length of source string */
599
600
601 /*
602 * Figure out how much room is needed...
603 */
604
605 size --;
606
607 srclen = strlen(src);
608
609 /*
610 * Copy the appropriate amount...
611 */
612
613 if (srclen > size)
614 srclen = size;
615
616 memcpy(dst, src, srclen);
617 dst[srclen] = '\0';
618
619 return (srclen);
620}
621#endif /* !HAVE_STRLCPY */
622
623
624/*
4400e98d 625 * 'compare_sp_items()' - Compare two string pool items...
626 */
627
628static int /* O - Result of comparison */
629compare_sp_items(_cups_sp_item_t *a, /* I - First item */
630 _cups_sp_item_t *b) /* I - Second item */
631{
632 return (strcmp(a->str, b->str));
633}
634
635
636/*
e1d6a774 637 * End of "$Id: string.c 5286 2006-03-13 16:32:28Z mike $".
ef416fc2 638 */