4 * String functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
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.
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...
44 * Include necessary headers...
54 #endif /* HAVE_PTHREAD_H */
62 static pthread_mutex_t sp_mutex
= PTHREAD_MUTEX_INITIALIZER
;
63 /* Mutex to control access to pool */
64 #endif /* HAVE_PTHREAD_H */
65 static cups_array_t
*stringpool
= NULL
;
66 /* Global string pool */
73 static int compare_sp_items(_cups_sp_item_t
*a
, _cups_sp_item_t
*b
);
77 * '_cupsStrAlloc()' - Allocate/reference a string.
80 char * /* O - String pointer */
81 _cupsStrAlloc(const char *s
) /* I - String */
83 _cups_sp_item_t
*item
, /* String pool item */
88 * Range check input...
95 * Get the string pool...
99 pthread_mutex_lock(&sp_mutex
);
100 #endif /* HAVE_PTHREAD_H */
103 stringpool
= cupsArrayNew((cups_array_func_t
)compare_sp_items
, NULL
);
107 #ifdef HAVE_PTHREAD_H
108 pthread_mutex_unlock(&sp_mutex
);
109 #endif /* HAVE_PTHREAD_H */
115 * See if the string is already in the pool...
120 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, &key
)) != NULL
)
123 * Found it, return the cached string...
128 #ifdef HAVE_PTHREAD_H
129 pthread_mutex_unlock(&sp_mutex
);
130 #endif /* HAVE_PTHREAD_H */
136 * Not found, so allocate a new one...
139 item
= (_cups_sp_item_t
*)calloc(1, sizeof(_cups_sp_item_t
));
142 #ifdef HAVE_PTHREAD_H
143 pthread_mutex_unlock(&sp_mutex
);
144 #endif /* HAVE_PTHREAD_H */
150 item
->str
= strdup(s
);
156 #ifdef HAVE_PTHREAD_H
157 pthread_mutex_unlock(&sp_mutex
);
158 #endif /* HAVE_PTHREAD_H */
164 * Add the string to the pool and return it...
167 cupsArrayAdd(stringpool
, item
);
169 #ifdef HAVE_PTHREAD_H
170 pthread_mutex_unlock(&sp_mutex
);
171 #endif /* HAVE_PTHREAD_H */
178 * '_cupsStrFlush()' - Flush the string pool...
184 _cups_sp_item_t
*item
; /* Current item */
187 DEBUG_printf(("_cupsStrFlush(cg=%p)\n", cg
));
188 DEBUG_printf((" %d strings in array\n", cupsArrayCount(stringpool
)));
190 #ifdef HAVE_PTHREAD_H
191 pthread_mutex_lock(&sp_mutex
);
192 #endif /* HAVE_PTHREAD_H */
194 for (item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
196 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
202 cupsArrayDelete(stringpool
);
205 #ifdef HAVE_PTHREAD_H
206 pthread_mutex_unlock(&sp_mutex
);
207 #endif /* HAVE_PTHREAD_H */
212 * '_cupsStrFormatd()' - Format a floating-point number.
215 char * /* 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 */
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 */
230 * Format the number using the "%.12f" format and then eliminate
231 * unnecessary trailing 0's.
234 snprintf(temp
, sizeof(temp
), "%.12f", number
);
235 for (tempptr
= temp
+ strlen(temp
) - 1;
236 tempptr
> temp
&& *tempptr
== '0';
240 * Next, find the decimal point...
243 if (loc
&& loc
->decimal_point
)
245 dec
= loc
->decimal_point
;
246 declen
= strlen(dec
);
255 tempdec
= strchr(temp
, *dec
);
257 tempdec
= strstr(temp
, dec
);
260 * Copy everything up to the decimal point...
265 for (tempptr
= temp
, bufptr
= buf
;
266 tempptr
< tempdec
&& bufptr
< bufend
;
267 *bufptr
++ = *tempptr
++);
271 if (*tempdec
&& bufptr
< bufend
)
275 while (*tempptr
&& bufptr
< bufend
)
276 *bufptr
++ = *tempptr
++;
283 strlcpy(buf
, temp
, bufend
- buf
+ 1);
284 bufptr
= buf
+ strlen(buf
);
292 * '_cupsStrFree()' - Free/dereference a string.
296 _cupsStrFree(const char *s
) /* I - String to free */
298 _cups_sp_item_t
*item
, /* String pool item */
299 key
; /* Search key */
303 * Range check input...
310 * Check the string pool...
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...
321 * See if the string is already in the pool...
324 #ifdef HAVE_PTHREAD_H
325 pthread_mutex_lock(&sp_mutex
);
326 #endif /* HAVE_PTHREAD_H */
330 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, &key
)) != NULL
&&
334 * Found it, dereference...
339 if (!item
->ref_count
)
345 cupsArrayRemove(stringpool
, item
);
352 #ifdef HAVE_PTHREAD_H
353 pthread_mutex_unlock(&sp_mutex
);
354 #endif /* HAVE_PTHREAD_H */
359 * '_cupsStrScand()' - Scan a string for a floating-point number.
361 * This function handles the locale-specific BS so that a decimal
362 * point is always the period (".")...
365 double /* 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 */
370 char temp
[1024], /* Temporary buffer */
371 *tempptr
; /* Pointer into temporary buffer */
375 * Range check input...
382 * Skip leading whitespace...
385 while (isspace(*buf
& 255))
389 * Copy leading sign, numbers, period, and then numbers...
393 if (*buf
== '-' || *buf
== '+')
396 while (isdigit(*buf
& 255))
397 if (tempptr
< (temp
+ sizeof(temp
) - 1))
410 * Read fractional portion of number...
415 if (loc
&& loc
->decimal_point
)
417 strlcpy(tempptr
, loc
->decimal_point
, sizeof(temp
) - (tempptr
- temp
));
418 tempptr
+= strlen(tempptr
);
420 else if (tempptr
< (temp
+ sizeof(temp
) - 1))
430 while (isdigit(*buf
& 255))
431 if (tempptr
< (temp
+ sizeof(temp
) - 1))
443 * Nul-terminate the temporary string and return the value...
447 *bufptr
= (char *)buf
;
451 return (strtod(temp
, NULL
));
456 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
459 size_t /* O - Number of strings */
460 _cupsStrStatistics(size_t *alloc_bytes
, /* O - Allocated bytes */
461 size_t *total_bytes
) /* O - Total string bytes */
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 */
471 * Loop through strings in pool, counting everything up...
474 #ifdef HAVE_PTHREAD_H
475 pthread_mutex_lock(&sp_mutex
);
476 #endif /* HAVE_PTHREAD_H */
478 for (count
= 0, abytes
= 0, tbytes
= 0,
479 item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
481 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
484 * Count allocated memory, using a 64-bit aligned buffer as a basis.
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
;
493 #ifdef HAVE_PTHREAD_H
494 pthread_mutex_unlock(&sp_mutex
);
495 #endif /* HAVE_PTHREAD_H */
502 *alloc_bytes
= abytes
;
505 *total_bytes
= tbytes
;
512 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
516 _cups_strcpy(char *dst
, /* I - Destination string */
517 const char *src
) /* I - Source string */
527 * '_cups_strdup()' - Duplicate a string.
531 char * /* O - New string pointer */
532 _cups_strdup(const char *s
) /* I - String to duplicate */
534 char *t
; /* New string pointer */
540 if ((t
= malloc(strlen(s
) + 1)) == NULL
)
543 return (strcpy(t
, s
));
545 #endif /* !HAVE_STRDUP */
549 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
552 #ifndef HAVE_STRCASECMP
553 int /* O - Result of comparison (-1, 0, or 1) */
554 _cups_strcasecmp(const char *s
, /* I - First string */
555 const char *t
) /* I - Second string */
557 while (*s
!= '\0' && *t
!= '\0')
559 if (tolower(*s
& 255) < tolower(*t
& 255))
561 else if (tolower(*s
& 255) > tolower(*t
& 255))
568 if (*s
== '\0' && *t
== '\0')
575 #endif /* !HAVE_STRCASECMP */
578 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
581 #ifndef HAVE_STRNCASECMP
582 int /* 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 */
587 while (*s
!= '\0' && *t
!= '\0' && n
> 0)
589 if (tolower(*s
& 255) < tolower(*t
& 255))
591 else if (tolower(*s
& 255) > tolower(*t
& 255))
601 else if (*s
== '\0' && *t
== '\0')
608 #endif /* !HAVE_STRNCASECMP */
613 * '_cups_strlcat()' - Safely concatenate two strings.
616 size_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 */
621 size_t srclen
; /* Length of source string */
622 size_t dstlen
; /* Length of destination string */
626 * Figure out how much room is left...
629 dstlen
= strlen(dst
);
633 return (dstlen
); /* No room, return immediately... */
636 * Figure out how much room is needed...
639 srclen
= strlen(src
);
642 * Copy the appropriate amount...
648 memcpy(dst
+ dstlen
, src
, srclen
);
649 dst
[dstlen
+ srclen
] = '\0';
651 return (dstlen
+ srclen
);
653 #endif /* !HAVE_STRLCAT */
658 * '_cups_strlcpy()' - Safely copy two strings.
661 size_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 */
666 size_t srclen
; /* Length of source string */
670 * Figure out how much room is needed...
675 srclen
= strlen(src
);
678 * Copy the appropriate amount...
684 memcpy(dst
, src
, srclen
);
689 #endif /* !HAVE_STRLCPY */
693 * 'compare_sp_items()' - Compare two string pool items...
696 static int /* O - Result of comparison */
697 compare_sp_items(_cups_sp_item_t
*a
, /* I - First item */
698 _cups_sp_item_t
*b
) /* I - Second item */
700 return (strcmp(a
->str
, b
->str
));