2 * "$Id: string.c 6345 2007-03-17 18:00:04Z mike $"
4 * String functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 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
= (int)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 (*tempptr
&& 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))
442 if (*buf
== 'e' || *buf
== 'E')
448 if (tempptr
< (temp
+ sizeof(temp
) - 1))
458 if (*buf
== '+' || *buf
== '-')
460 if (tempptr
< (temp
+ sizeof(temp
) - 1))
471 while (isdigit(*buf
& 255))
472 if (tempptr
< (temp
+ sizeof(temp
) - 1))
484 * Nul-terminate the temporary string and return the value...
488 *bufptr
= (char *)buf
;
492 return (strtod(temp
, NULL
));
497 * '_cupsStrStatistics()' - Return allocation statistics for string pool.
500 size_t /* O - Number of strings */
501 _cupsStrStatistics(size_t *alloc_bytes
, /* O - Allocated bytes */
502 size_t *total_bytes
) /* O - Total string bytes */
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 */
512 * Loop through strings in pool, counting everything up...
515 #ifdef HAVE_PTHREAD_H
516 pthread_mutex_lock(&sp_mutex
);
517 #endif /* HAVE_PTHREAD_H */
519 for (count
= 0, abytes
= 0, tbytes
= 0,
520 item
= (_cups_sp_item_t
*)cupsArrayFirst(stringpool
);
522 item
= (_cups_sp_item_t
*)cupsArrayNext(stringpool
))
525 * Count allocated memory, using a 64-bit aligned buffer as a basis.
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
;
534 #ifdef HAVE_PTHREAD_H
535 pthread_mutex_unlock(&sp_mutex
);
536 #endif /* HAVE_PTHREAD_H */
543 *alloc_bytes
= abytes
;
546 *total_bytes
= tbytes
;
553 * '_cups_strcpy()' - Copy a string allowing for overlapping strings.
557 _cups_strcpy(char *dst
, /* I - Destination string */
558 const char *src
) /* I - Source string */
568 * '_cups_strdup()' - Duplicate a string.
572 char * /* O - New string pointer */
573 _cups_strdup(const char *s
) /* I - String to duplicate */
575 char *t
; /* New string pointer */
581 if ((t
= malloc(strlen(s
) + 1)) == NULL
)
584 return (strcpy(t
, s
));
586 #endif /* !HAVE_STRDUP */
590 * '_cups_strcasecmp()' - Do a case-insensitive comparison.
593 #ifndef HAVE_STRCASECMP
594 int /* O - Result of comparison (-1, 0, or 1) */
595 _cups_strcasecmp(const char *s
, /* I - First string */
596 const char *t
) /* I - Second string */
598 while (*s
!= '\0' && *t
!= '\0')
600 if (tolower(*s
& 255) < tolower(*t
& 255))
602 else if (tolower(*s
& 255) > tolower(*t
& 255))
609 if (*s
== '\0' && *t
== '\0')
616 #endif /* !HAVE_STRCASECMP */
619 * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars.
622 #ifndef HAVE_STRNCASECMP
623 int /* 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 */
628 while (*s
!= '\0' && *t
!= '\0' && n
> 0)
630 if (tolower(*s
& 255) < tolower(*t
& 255))
632 else if (tolower(*s
& 255) > tolower(*t
& 255))
642 else if (*s
== '\0' && *t
== '\0')
649 #endif /* !HAVE_STRNCASECMP */
654 * '_cups_strlcat()' - Safely concatenate two strings.
657 size_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 */
662 size_t srclen
; /* Length of source string */
663 size_t dstlen
; /* Length of destination string */
667 * Figure out how much room is left...
670 dstlen
= strlen(dst
);
674 return (dstlen
); /* No room, return immediately... */
677 * Figure out how much room is needed...
680 srclen
= strlen(src
);
683 * Copy the appropriate amount...
689 memcpy(dst
+ dstlen
, src
, srclen
);
690 dst
[dstlen
+ srclen
] = '\0';
692 return (dstlen
+ srclen
);
694 #endif /* !HAVE_STRLCAT */
699 * '_cups_strlcpy()' - Safely copy two strings.
702 size_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 */
707 size_t srclen
; /* Length of source string */
711 * Figure out how much room is needed...
716 srclen
= strlen(src
);
719 * Copy the appropriate amount...
725 memcpy(dst
, src
, srclen
);
730 #endif /* !HAVE_STRLCPY */
734 * 'compare_sp_items()' - Compare two string pool items...
737 static int /* O - Result of comparison */
738 compare_sp_items(_cups_sp_item_t
*a
, /* I - First item */
739 _cups_sp_item_t
*b
) /* I - Second item */
741 return (strcmp(a
->str
, b
->str
));
746 * End of "$Id: string.c 6345 2007-03-17 18:00:04Z mike $".