2 * "$Id: string.c 6649 2007-07-11 21:46:42Z mike $"
4 * String functions for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
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.
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...
35 * Include necessary headers...
45 #endif /* HAVE_PTHREAD_H */
53 static pthread_mutex_t sp_mutex
= PTHREAD_MUTEX_INITIALIZER
;
54 /* Mutex to control access to pool */
55 #endif /* HAVE_PTHREAD_H */
56 static cups_array_t
*stringpool
= NULL
;
57 /* Global string pool */
64 static int compare_sp_items(_cups_sp_item_t
*a
, _cups_sp_item_t
*b
);
68 * '_cupsStrAlloc()' - Allocate/reference a string.
71 char * /* O - String pointer */
72 _cupsStrAlloc(const char *s
) /* I - String */
74 _cups_sp_item_t
*item
, /* String pool item */
79 * Range check input...
86 * Get the string pool...
90 pthread_mutex_lock(&sp_mutex
);
91 #endif /* HAVE_PTHREAD_H */
94 stringpool
= cupsArrayNew((cups_array_func_t
)compare_sp_items
, NULL
);
99 pthread_mutex_unlock(&sp_mutex
);
100 #endif /* HAVE_PTHREAD_H */
106 * See if the string is already in the pool...
111 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, &key
)) != NULL
)
114 * Found it, return the cached string...
119 #ifdef HAVE_PTHREAD_H
120 pthread_mutex_unlock(&sp_mutex
);
121 #endif /* HAVE_PTHREAD_H */
127 * Not found, so allocate a new one...
130 item
= (_cups_sp_item_t
*)calloc(1, sizeof(_cups_sp_item_t
));
133 #ifdef HAVE_PTHREAD_H
134 pthread_mutex_unlock(&sp_mutex
);
135 #endif /* HAVE_PTHREAD_H */
141 item
->str
= strdup(s
);
147 #ifdef HAVE_PTHREAD_H
148 pthread_mutex_unlock(&sp_mutex
);
149 #endif /* HAVE_PTHREAD_H */
155 * Add the string to the pool and return it...
158 cupsArrayAdd(stringpool
, item
);
160 #ifdef HAVE_PTHREAD_H
161 pthread_mutex_unlock(&sp_mutex
);
162 #endif /* HAVE_PTHREAD_H */
169 * '_cupsStrFlush()' - Flush the string pool...
175 _cups_sp_item_t
*item
; /* Current item */
178 DEBUG_printf(("_cupsStrFlush(cg=%p)\n", cg
));
179 DEBUG_printf((" %d strings in array\n", cupsArrayCount(stringpool
)));
181 #ifdef HAVE_PTHREAD_H
182 pthread_mutex_lock(&sp_mutex
);
183 #endif /* HAVE_PTHREAD_H */
185 for (item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
187 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
193 cupsArrayDelete(stringpool
);
196 #ifdef HAVE_PTHREAD_H
197 pthread_mutex_unlock(&sp_mutex
);
198 #endif /* HAVE_PTHREAD_H */
203 * '_cupsStrFormatd()' - Format a floating-point number.
206 char * /* 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 */
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 */
221 * Format the number using the "%.12f" format and then eliminate
222 * unnecessary trailing 0's.
225 snprintf(temp
, sizeof(temp
), "%.12f", number
);
226 for (tempptr
= temp
+ strlen(temp
) - 1;
227 tempptr
> temp
&& *tempptr
== '0';
231 * Next, find the decimal point...
234 if (loc
&& loc
->decimal_point
)
236 dec
= loc
->decimal_point
;
237 declen
= (int)strlen(dec
);
246 tempdec
= strchr(temp
, *dec
);
248 tempdec
= strstr(temp
, dec
);
251 * Copy everything up to the decimal point...
256 for (tempptr
= temp
, bufptr
= buf
;
257 tempptr
< tempdec
&& bufptr
< bufend
;
258 *bufptr
++ = *tempptr
++);
262 if (*tempptr
&& bufptr
< bufend
)
266 while (*tempptr
&& bufptr
< bufend
)
267 *bufptr
++ = *tempptr
++;
274 strlcpy(buf
, temp
, bufend
- buf
+ 1);
275 bufptr
= buf
+ strlen(buf
);
283 * '_cupsStrFree()' - Free/dereference a string.
287 _cupsStrFree(const char *s
) /* I - String to free */
289 _cups_sp_item_t
*item
, /* String pool item */
290 key
; /* Search key */
294 * Range check input...
301 * Check the string pool...
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...
312 * See if the string is already in the pool...
315 #ifdef HAVE_PTHREAD_H
316 pthread_mutex_lock(&sp_mutex
);
317 #endif /* HAVE_PTHREAD_H */
321 if ((item
= (_cups_sp_item_t
*)cupsArrayFind(stringpool
, &key
)) != NULL
&&
325 * Found it, dereference...
330 if (!item
->ref_count
)
336 cupsArrayRemove(stringpool
, item
);
343 #ifdef HAVE_PTHREAD_H
344 pthread_mutex_unlock(&sp_mutex
);
345 #endif /* HAVE_PTHREAD_H */
350 * '_cupsStrScand()' - Scan a string for a floating-point number.
352 * This function handles the locale-specific BS so that a decimal
353 * point is always the period (".")...
356 double /* 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 */
361 char temp
[1024], /* Temporary buffer */
362 *tempptr
; /* Pointer into temporary buffer */
366 * Range check input...
373 * Skip leading whitespace...
376 while (isspace(*buf
& 255))
380 * Copy leading sign, numbers, period, and then numbers...
384 if (*buf
== '-' || *buf
== '+')
387 while (isdigit(*buf
& 255))
388 if (tempptr
< (temp
+ sizeof(temp
) - 1))
401 * Read fractional portion of number...
406 if (loc
&& loc
->decimal_point
)
408 strlcpy(tempptr
, loc
->decimal_point
, sizeof(temp
) - (tempptr
- temp
));
409 tempptr
+= strlen(tempptr
);
411 else if (tempptr
< (temp
+ sizeof(temp
) - 1))
421 while (isdigit(*buf
& 255))
422 if (tempptr
< (temp
+ sizeof(temp
) - 1))
433 if (*buf
== 'e' || *buf
== 'E')
439 if (tempptr
< (temp
+ sizeof(temp
) - 1))
449 if (*buf
== '+' || *buf
== '-')
451 if (tempptr
< (temp
+ sizeof(temp
) - 1))
462 while (isdigit(*buf
& 255))
463 if (tempptr
< (temp
+ sizeof(temp
) - 1))
475 * Nul-terminate the temporary string and return the value...
479 *bufptr
= (char *)buf
;
483 return (strtod(temp
, NULL
));
488 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
491 size_t /* O - Number of strings */
492 _cupsStrStatistics(size_t *alloc_bytes
, /* O - Allocated bytes */
493 size_t *total_bytes
) /* O - Total string bytes */
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 */
503 * Loop through strings in pool, counting everything up...
506 #ifdef HAVE_PTHREAD_H
507 pthread_mutex_lock(&sp_mutex
);
508 #endif /* HAVE_PTHREAD_H */
510 for (count
= 0, abytes
= 0, tbytes
= 0,
511 item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
513 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
516 * Count allocated memory, using a 64-bit aligned buffer as a basis.
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
;
525 #ifdef HAVE_PTHREAD_H
526 pthread_mutex_unlock(&sp_mutex
);
527 #endif /* HAVE_PTHREAD_H */
534 *alloc_bytes
= abytes
;
537 *total_bytes
= tbytes
;
544 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
548 _cups_strcpy(char *dst
, /* I - Destination string */
549 const char *src
) /* I - Source string */
559 * '_cups_strdup()' - Duplicate a string.
563 char * /* O - New string pointer */
564 _cups_strdup(const char *s
) /* I - String to duplicate */
566 char *t
; /* New string pointer */
572 if ((t
= malloc(strlen(s
) + 1)) == NULL
)
575 return (strcpy(t
, s
));
577 #endif /* !HAVE_STRDUP */
581 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
584 #ifndef HAVE_STRCASECMP
585 int /* O - Result of comparison (-1, 0, or 1) */
586 _cups_strcasecmp(const char *s
, /* I - First string */
587 const char *t
) /* I - Second string */
589 while (*s
!= '\0' && *t
!= '\0')
591 if (tolower(*s
& 255) < tolower(*t
& 255))
593 else if (tolower(*s
& 255) > tolower(*t
& 255))
600 if (*s
== '\0' && *t
== '\0')
607 #endif /* !HAVE_STRCASECMP */
610 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
613 #ifndef HAVE_STRNCASECMP
614 int /* O - Result of comparison (-1, 0, or 1) */
615 _cups_strncasecmp(const char *s
, /* I - First string */
616 vconst
char *t
, /* I - Second string */
617 size_t n
) /* I - Maximum number of characters to compare */
619 while (*s
!= '\0' && *t
!= '\0' && n
> 0)
621 if (tolower(*s
& 255) < tolower(*t
& 255))
623 else if (tolower(*s
& 255) > tolower(*t
& 255))
633 else if (*s
== '\0' && *t
== '\0')
640 #endif /* !HAVE_STRNCASECMP */
645 * '_cups_strlcat()' - Safely concatenate two strings.
648 size_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 */
653 size_t srclen
; /* Length of source string */
654 size_t dstlen
; /* Length of destination string */
658 * Figure out how much room is left...
661 dstlen
= strlen(dst
);
665 return (dstlen
); /* No room, return immediately... */
668 * Figure out how much room is needed...
671 srclen
= strlen(src
);
674 * Copy the appropriate amount...
680 memcpy(dst
+ dstlen
, src
, srclen
);
681 dst
[dstlen
+ srclen
] = '\0';
683 return (dstlen
+ srclen
);
685 #endif /* !HAVE_STRLCAT */
690 * '_cups_strlcpy()' - Safely copy two strings.
693 size_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 */
698 size_t srclen
; /* Length of source string */
702 * Figure out how much room is needed...
707 srclen
= strlen(src
);
710 * Copy the appropriate amount...
716 memcpy(dst
, src
, srclen
);
721 #endif /* !HAVE_STRLCPY */
725 * 'compare_sp_items()' - Compare two string pool items...
728 static int /* O - Result of comparison */
729 compare_sp_items(_cups_sp_item_t
*a
, /* I - First item */
730 _cups_sp_item_t
*b
) /* I - Second item */
732 return (strcmp(a
->str
, b
->str
));
737 * End of "$Id: string.c 6649 2007-07-11 21:46:42Z mike $".