]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
a51f28ec | 2 | * "$Id: string.c 11890 2014-05-22 13:59:21Z msweet $" |
ef416fc2 | 3 | * |
71e16022 | 4 | * String functions for CUPS. |
ef416fc2 | 5 | * |
5a9febac | 6 | * Copyright 2007-2012 by Apple Inc. |
b86bc4cf | 7 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
426c6a59 MS |
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 | * _cupsStrRetain() - Increment the reference count of a string. | |
24 | * _cupsStrScand() - Scan a string for a floating-point number. | |
25 | * _cupsStrStatistics() - Return allocation statistics for string pool. | |
26 | * _cups_strcpy() - Copy a string allowing for overlapping strings. | |
27 | * _cups_strdup() - Duplicate a string. | |
28 | * _cups_strcasecmp() - Do a case-insensitive comparison. | |
29 | * _cups_strncasecmp() - Do a case-insensitive comparison on up to N chars. | |
30 | * _cups_strlcat() - Safely concatenate two strings. | |
31 | * _cups_strlcpy() - Safely copy two strings. | |
32 | * compare_sp_items() - Compare two string pool items... | |
ef416fc2 | 33 | */ |
34 | ||
35 | /* | |
36 | * Include necessary headers... | |
37 | */ | |
38 | ||
7cf5915e | 39 | #define _CUPS_STRING_C_ |
a51f28ec | 40 | #include "cups-private.h" |
745129be | 41 | #include <stddef.h> |
4400e98d | 42 | #include <limits.h> |
d6ae789d | 43 | |
44 | ||
45 | /* | |
46 | * Local globals... | |
47 | */ | |
48 | ||
6d2f911b | 49 | static _cups_mutex_t sp_mutex = _CUPS_MUTEX_INITIALIZER; |
d6ae789d | 50 | /* Mutex to control access to pool */ |
d6ae789d | 51 | static cups_array_t *stringpool = NULL; |
52 | /* Global string pool */ | |
4400e98d | 53 | |
54 | ||
55 | /* | |
56 | * Local functions... | |
57 | */ | |
58 | ||
59 | static int compare_sp_items(_cups_sp_item_t *a, _cups_sp_item_t *b); | |
60 | ||
61 | ||
62 | /* | |
757d2cad | 63 | * '_cupsStrAlloc()' - Allocate/reference a string. |
4400e98d | 64 | */ |
65 | ||
66 | char * /* O - String pointer */ | |
757d2cad | 67 | _cupsStrAlloc(const char *s) /* I - String */ |
4400e98d | 68 | { |
5a9febac | 69 | size_t slen; /* Length of string */ |
4400e98d | 70 | _cups_sp_item_t *item, /* String pool item */ |
426c6a59 | 71 | *key; /* Search key */ |
4400e98d | 72 | |
73 | ||
74 | /* | |
75 | * Range check input... | |
76 | */ | |
77 | ||
78 | if (!s) | |
79 | return (NULL); | |
80 | ||
81 | /* | |
82 | * Get the string pool... | |
83 | */ | |
84 | ||
6d2f911b | 85 | _cupsMutexLock(&sp_mutex); |
d6ae789d | 86 | |
87 | if (!stringpool) | |
88 | stringpool = cupsArrayNew((cups_array_func_t)compare_sp_items, NULL); | |
4400e98d | 89 | |
d6ae789d | 90 | if (!stringpool) |
91 | { | |
6d2f911b | 92 | _cupsMutexUnlock(&sp_mutex); |
4400e98d | 93 | |
4400e98d | 94 | return (NULL); |
d6ae789d | 95 | } |
4400e98d | 96 | |
97 | /* | |
98 | * See if the string is already in the pool... | |
99 | */ | |
100 | ||
745129be | 101 | key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); |
4400e98d | 102 | |
426c6a59 | 103 | if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL) |
4400e98d | 104 | { |
105 | /* | |
106 | * Found it, return the cached string... | |
107 | */ | |
108 | ||
109 | item->ref_count ++; | |
110 | ||
745129be | 111 | #ifdef DEBUG_GUARDS |
e07d4801 MS |
112 | DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, " |
113 | "ref_count=%d", item, item->str, s, item->guard, | |
745129be MS |
114 | item->ref_count)); |
115 | ||
116 | if (item->guard != _CUPS_STR_GUARD) | |
117 | abort(); | |
118 | #endif /* DEBUG_GUARDS */ | |
119 | ||
6d2f911b | 120 | _cupsMutexUnlock(&sp_mutex); |
d6ae789d | 121 | |
4400e98d | 122 | return (item->str); |
123 | } | |
124 | ||
125 | /* | |
126 | * Not found, so allocate a new one... | |
127 | */ | |
128 | ||
5a9febac MS |
129 | slen = strlen(s); |
130 | item = (_cups_sp_item_t *)calloc(1, sizeof(_cups_sp_item_t) + slen); | |
4400e98d | 131 | if (!item) |
d6ae789d | 132 | { |
6d2f911b | 133 | _cupsMutexUnlock(&sp_mutex); |
d6ae789d | 134 | |
4400e98d | 135 | return (NULL); |
d6ae789d | 136 | } |
4400e98d | 137 | |
138 | item->ref_count = 1; | |
5a9febac | 139 | memcpy(item->str, s, slen + 1); |
4400e98d | 140 | |
745129be MS |
141 | #ifdef DEBUG_GUARDS |
142 | item->guard = _CUPS_STR_GUARD; | |
143 | ||
e07d4801 MS |
144 | DEBUG_printf(("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, " |
145 | "ref_count=%d", item, item->str, s, item->guard, | |
745129be MS |
146 | item->ref_count)); |
147 | #endif /* DEBUG_GUARDS */ | |
148 | ||
4400e98d | 149 | /* |
150 | * Add the string to the pool and return it... | |
151 | */ | |
152 | ||
d6ae789d | 153 | cupsArrayAdd(stringpool, item); |
154 | ||
6d2f911b | 155 | _cupsMutexUnlock(&sp_mutex); |
4400e98d | 156 | |
157 | return (item->str); | |
158 | } | |
159 | ||
160 | ||
a51f28ec MS |
161 | /* |
162 | * '_cupsStrDate()' - Return a localized date for a given time value. | |
163 | * | |
164 | * This function works around the locale encoding issues of strftime... | |
165 | */ | |
166 | ||
167 | char * /* O - Buffer */ | |
168 | _cupsStrDate(char *buf, /* I - Buffer */ | |
169 | size_t bufsize, /* I - Size of buffer */ | |
170 | time_t timeval) /* I - Time value */ | |
171 | { | |
172 | struct tm *dateval; /* Local date/time */ | |
173 | char temp[1024]; /* Temporary buffer */ | |
174 | _cups_globals_t *cg = _cupsGlobals(); /* Per-thread globals */ | |
175 | ||
176 | ||
177 | if (!cg->lang_default) | |
178 | cg->lang_default = cupsLangDefault(); | |
179 | ||
180 | dateval = localtime(&timeval); | |
181 | ||
182 | if (cg->lang_default->encoding != CUPS_UTF8) | |
183 | { | |
184 | strftime(temp, sizeof(temp), "%c", dateval); | |
185 | cupsCharsetToUTF8((cups_utf8_t *)buf, temp, (int)bufsize, cg->lang_default->encoding); | |
186 | } | |
187 | else | |
188 | strftime(buf, bufsize, "%c", dateval); | |
189 | ||
190 | return (buf); | |
191 | } | |
192 | ||
193 | ||
4400e98d | 194 | /* |
426c6a59 | 195 | * '_cupsStrFlush()' - Flush the string pool. |
4400e98d | 196 | */ |
197 | ||
198 | void | |
d6ae789d | 199 | _cupsStrFlush(void) |
4400e98d | 200 | { |
201 | _cups_sp_item_t *item; /* Current item */ | |
202 | ||
203 | ||
e07d4801 | 204 | DEBUG_printf(("4_cupsStrFlush: %d strings in array", |
ae71f5de | 205 | cupsArrayCount(stringpool))); |
d6ae789d | 206 | |
6d2f911b | 207 | _cupsMutexLock(&sp_mutex); |
d6ae789d | 208 | |
209 | for (item = (_cups_sp_item_t *)cupsArrayFirst(stringpool); | |
4400e98d | 210 | item; |
d6ae789d | 211 | item = (_cups_sp_item_t *)cupsArrayNext(stringpool)) |
4400e98d | 212 | free(item); |
4400e98d | 213 | |
d6ae789d | 214 | cupsArrayDelete(stringpool); |
215 | stringpool = NULL; | |
216 | ||
6d2f911b | 217 | _cupsMutexUnlock(&sp_mutex); |
4400e98d | 218 | } |
219 | ||
220 | ||
221 | /* | |
757d2cad | 222 | * '_cupsStrFormatd()' - Format a floating-point number. |
223 | */ | |
224 | ||
225 | char * /* O - Pointer to end of string */ | |
226 | _cupsStrFormatd(char *buf, /* I - String */ | |
227 | char *bufend, /* I - End of string buffer */ | |
228 | double number, /* I - Number to format */ | |
229 | struct lconv *loc) /* I - Locale data */ | |
230 | { | |
231 | char *bufptr, /* Pointer into buffer */ | |
232 | temp[1024], /* Temporary string */ | |
233 | *tempdec, /* Pointer to decimal point */ | |
234 | *tempptr; /* Pointer into temporary string */ | |
235 | const char *dec; /* Decimal point */ | |
236 | int declen; /* Length of decimal point */ | |
237 | ||
238 | ||
239 | /* | |
240 | * Format the number using the "%.12f" format and then eliminate | |
241 | * unnecessary trailing 0's. | |
242 | */ | |
243 | ||
244 | snprintf(temp, sizeof(temp), "%.12f", number); | |
245 | for (tempptr = temp + strlen(temp) - 1; | |
246 | tempptr > temp && *tempptr == '0'; | |
247 | *tempptr-- = '\0'); | |
248 | ||
249 | /* | |
250 | * Next, find the decimal point... | |
251 | */ | |
252 | ||
253 | if (loc && loc->decimal_point) | |
254 | { | |
255 | dec = loc->decimal_point; | |
b86bc4cf | 256 | declen = (int)strlen(dec); |
757d2cad | 257 | } |
258 | else | |
259 | { | |
260 | dec = "."; | |
261 | declen = 1; | |
262 | } | |
263 | ||
264 | if (declen == 1) | |
265 | tempdec = strchr(temp, *dec); | |
266 | else | |
267 | tempdec = strstr(temp, dec); | |
268 | ||
269 | /* | |
270 | * Copy everything up to the decimal point... | |
271 | */ | |
272 | ||
273 | if (tempdec) | |
274 | { | |
275 | for (tempptr = temp, bufptr = buf; | |
276 | tempptr < tempdec && bufptr < bufend; | |
277 | *bufptr++ = *tempptr++); | |
278 | ||
7594b224 | 279 | tempptr += declen; |
757d2cad | 280 | |
7594b224 | 281 | if (*tempptr && bufptr < bufend) |
757d2cad | 282 | { |
283 | *bufptr++ = '.'; | |
284 | ||
285 | while (*tempptr && bufptr < bufend) | |
286 | *bufptr++ = *tempptr++; | |
287 | } | |
288 | ||
289 | *bufptr = '\0'; | |
290 | } | |
291 | else | |
292 | { | |
293 | strlcpy(buf, temp, bufend - buf + 1); | |
294 | bufptr = buf + strlen(buf); | |
295 | } | |
296 | ||
297 | return (bufptr); | |
298 | } | |
299 | ||
300 | ||
301 | /* | |
302 | * '_cupsStrFree()' - Free/dereference a string. | |
4400e98d | 303 | */ |
304 | ||
305 | void | |
757d2cad | 306 | _cupsStrFree(const char *s) /* I - String to free */ |
4400e98d | 307 | { |
4400e98d | 308 | _cups_sp_item_t *item, /* String pool item */ |
426c6a59 | 309 | *key; /* Search key */ |
4400e98d | 310 | |
311 | ||
312 | /* | |
313 | * Range check input... | |
314 | */ | |
315 | ||
316 | if (!s) | |
317 | return; | |
318 | ||
319 | /* | |
d6ae789d | 320 | * Check the string pool... |
321 | * | |
322 | * We don't need to lock the mutex yet, as we only want to know if | |
323 | * the stringpool is initialized. The rest of the code will still | |
324 | * work if it is initialized before we lock... | |
4400e98d | 325 | */ |
326 | ||
d6ae789d | 327 | if (!stringpool) |
4400e98d | 328 | return; |
329 | ||
330 | /* | |
331 | * See if the string is already in the pool... | |
332 | */ | |
333 | ||
6d2f911b | 334 | _cupsMutexLock(&sp_mutex); |
d6ae789d | 335 | |
745129be MS |
336 | key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); |
337 | ||
338 | #ifdef DEBUG_GUARDS | |
339 | if (key->guard != _CUPS_STR_GUARD) | |
340 | { | |
e07d4801 MS |
341 | DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, " |
342 | "ref_count=%d", key, key->str, key->guard, key->ref_count)); | |
745129be MS |
343 | abort(); |
344 | } | |
345 | #endif /* DEBUG_GUARDS */ | |
4400e98d | 346 | |
426c6a59 MS |
347 | if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL && |
348 | item == key) | |
4400e98d | 349 | { |
350 | /* | |
351 | * Found it, dereference... | |
352 | */ | |
353 | ||
354 | item->ref_count --; | |
355 | ||
356 | if (!item->ref_count) | |
357 | { | |
358 | /* | |
359 | * Remove and free... | |
360 | */ | |
361 | ||
d6ae789d | 362 | cupsArrayRemove(stringpool, item); |
4400e98d | 363 | |
4400e98d | 364 | free(item); |
365 | } | |
366 | } | |
d6ae789d | 367 | |
6d2f911b | 368 | _cupsMutexUnlock(&sp_mutex); |
4400e98d | 369 | } |
370 | ||
371 | ||
426c6a59 MS |
372 | /* |
373 | * '_cupsStrRetain()' - Increment the reference count of a string. | |
374 | * | |
375 | * Note: This function does not verify that the passed pointer is in the | |
376 | * string pool, so any calls to it MUST know they are passing in a | |
377 | * good pointer. | |
378 | */ | |
379 | ||
380 | char * /* O - Pointer to string */ | |
e07d4801 | 381 | _cupsStrRetain(const char *s) /* I - String to retain */ |
426c6a59 MS |
382 | { |
383 | _cups_sp_item_t *item; /* Pointer to string pool item */ | |
384 | ||
385 | ||
386 | if (s) | |
387 | { | |
745129be MS |
388 | item = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); |
389 | ||
390 | #ifdef DEBUG_GUARDS | |
391 | if (item->guard != _CUPS_STR_GUARD) | |
392 | { | |
e07d4801 MS |
393 | DEBUG_printf(("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, " |
394 | "ref_count=%d", item, s, item->guard, item->ref_count)); | |
745129be MS |
395 | abort(); |
396 | } | |
397 | #endif /* DEBUG_GUARDS */ | |
426c6a59 | 398 | |
6d2f911b | 399 | _cupsMutexLock(&sp_mutex); |
426c6a59 MS |
400 | |
401 | item->ref_count ++; | |
402 | ||
6d2f911b | 403 | _cupsMutexUnlock(&sp_mutex); |
426c6a59 MS |
404 | } |
405 | ||
e07d4801 | 406 | return ((char *)s); |
426c6a59 MS |
407 | } |
408 | ||
409 | ||
4400e98d | 410 | /* |
757d2cad | 411 | * '_cupsStrScand()' - Scan a string for a floating-point number. |
412 | * | |
413 | * This function handles the locale-specific BS so that a decimal | |
414 | * point is always the period (".")... | |
415 | */ | |
416 | ||
417 | double /* O - Number */ | |
418 | _cupsStrScand(const char *buf, /* I - Pointer to number */ | |
419 | char **bufptr, /* O - New pointer or NULL on error */ | |
420 | struct lconv *loc) /* I - Locale data */ | |
421 | { | |
422 | char temp[1024], /* Temporary buffer */ | |
423 | *tempptr; /* Pointer into temporary buffer */ | |
424 | ||
425 | ||
426 | /* | |
427 | * Range check input... | |
428 | */ | |
429 | ||
430 | if (!buf) | |
431 | return (0.0); | |
432 | ||
433 | /* | |
434 | * Skip leading whitespace... | |
435 | */ | |
436 | ||
7cf5915e | 437 | while (_cups_isspace(*buf)) |
757d2cad | 438 | buf ++; |
439 | ||
440 | /* | |
441 | * Copy leading sign, numbers, period, and then numbers... | |
442 | */ | |
443 | ||
444 | tempptr = temp; | |
445 | if (*buf == '-' || *buf == '+') | |
446 | *tempptr++ = *buf++; | |
447 | ||
448 | while (isdigit(*buf & 255)) | |
449 | if (tempptr < (temp + sizeof(temp) - 1)) | |
450 | *tempptr++ = *buf++; | |
451 | else | |
452 | { | |
453 | if (bufptr) | |
454 | *bufptr = NULL; | |
455 | ||
456 | return (0.0); | |
457 | } | |
458 | ||
459 | if (*buf == '.') | |
460 | { | |
e1d6a774 | 461 | /* |
462 | * Read fractional portion of number... | |
463 | */ | |
464 | ||
465 | buf ++; | |
466 | ||
757d2cad | 467 | if (loc && loc->decimal_point) |
468 | { | |
469 | strlcpy(tempptr, loc->decimal_point, sizeof(temp) - (tempptr - temp)); | |
470 | tempptr += strlen(tempptr); | |
471 | } | |
472 | else if (tempptr < (temp + sizeof(temp) - 1)) | |
473 | *tempptr++ = '.'; | |
474 | else | |
475 | { | |
476 | if (bufptr) | |
477 | *bufptr = NULL; | |
478 | ||
479 | return (0.0); | |
480 | } | |
481 | ||
482 | while (isdigit(*buf & 255)) | |
483 | if (tempptr < (temp + sizeof(temp) - 1)) | |
484 | *tempptr++ = *buf++; | |
485 | else | |
486 | { | |
487 | if (bufptr) | |
488 | *bufptr = NULL; | |
489 | ||
490 | return (0.0); | |
491 | } | |
492 | } | |
493 | ||
b86bc4cf | 494 | if (*buf == 'e' || *buf == 'E') |
495 | { | |
496 | /* | |
497 | * Read exponent... | |
498 | */ | |
499 | ||
500 | if (tempptr < (temp + sizeof(temp) - 1)) | |
501 | *tempptr++ = *buf++; | |
502 | else | |
503 | { | |
504 | if (bufptr) | |
505 | *bufptr = NULL; | |
506 | ||
507 | return (0.0); | |
508 | } | |
509 | ||
510 | if (*buf == '+' || *buf == '-') | |
511 | { | |
512 | if (tempptr < (temp + sizeof(temp) - 1)) | |
513 | *tempptr++ = *buf++; | |
514 | else | |
515 | { | |
516 | if (bufptr) | |
517 | *bufptr = NULL; | |
518 | ||
519 | return (0.0); | |
520 | } | |
521 | } | |
522 | ||
523 | while (isdigit(*buf & 255)) | |
524 | if (tempptr < (temp + sizeof(temp) - 1)) | |
525 | *tempptr++ = *buf++; | |
526 | else | |
527 | { | |
528 | if (bufptr) | |
529 | *bufptr = NULL; | |
530 | ||
531 | return (0.0); | |
532 | } | |
533 | } | |
534 | ||
757d2cad | 535 | /* |
536 | * Nul-terminate the temporary string and return the value... | |
537 | */ | |
538 | ||
539 | if (bufptr) | |
540 | *bufptr = (char *)buf; | |
541 | ||
542 | *tempptr = '\0'; | |
543 | ||
544 | return (strtod(temp, NULL)); | |
545 | } | |
546 | ||
547 | ||
548 | /* | |
549 | * '_cupsStrStatistics()' - Return allocation statistics for string pool. | |
4400e98d | 550 | */ |
551 | ||
552 | size_t /* O - Number of strings */ | |
757d2cad | 553 | _cupsStrStatistics(size_t *alloc_bytes, /* O - Allocated bytes */ |
554 | size_t *total_bytes) /* O - Total string bytes */ | |
4400e98d | 555 | { |
556 | size_t count, /* Number of strings */ | |
557 | abytes, /* Allocated string bytes */ | |
558 | tbytes, /* Total string bytes */ | |
559 | len; /* Length of string */ | |
560 | _cups_sp_item_t *item; /* Current item */ | |
4400e98d | 561 | |
562 | ||
563 | /* | |
564 | * Loop through strings in pool, counting everything up... | |
565 | */ | |
566 | ||
6d2f911b | 567 | _cupsMutexLock(&sp_mutex); |
4400e98d | 568 | |
569 | for (count = 0, abytes = 0, tbytes = 0, | |
d6ae789d | 570 | item = (_cups_sp_item_t *)cupsArrayFirst(stringpool); |
4400e98d | 571 | item; |
d6ae789d | 572 | item = (_cups_sp_item_t *)cupsArrayNext(stringpool)) |
4400e98d | 573 | { |
574 | /* | |
575 | * Count allocated memory, using a 64-bit aligned buffer as a basis. | |
576 | */ | |
577 | ||
578 | count += item->ref_count; | |
579 | len = (strlen(item->str) + 8) & ~7; | |
580 | abytes += sizeof(_cups_sp_item_t) + len; | |
581 | tbytes += item->ref_count * len; | |
582 | } | |
583 | ||
6d2f911b | 584 | _cupsMutexUnlock(&sp_mutex); |
d6ae789d | 585 | |
4400e98d | 586 | /* |
587 | * Return values... | |
588 | */ | |
589 | ||
590 | if (alloc_bytes) | |
591 | *alloc_bytes = abytes; | |
592 | ||
593 | if (total_bytes) | |
594 | *total_bytes = tbytes; | |
595 | ||
596 | return (count); | |
597 | } | |
ef416fc2 | 598 | |
599 | ||
600 | /* | |
601 | * '_cups_strcpy()' - Copy a string allowing for overlapping strings. | |
602 | */ | |
603 | ||
604 | void | |
605 | _cups_strcpy(char *dst, /* I - Destination string */ | |
88f9aafc | 606 | const char *src) /* I - Source string */ |
ef416fc2 | 607 | { |
608 | while (*src) | |
609 | *dst++ = *src++; | |
610 | ||
611 | *dst = '\0'; | |
612 | } | |
613 | ||
614 | ||
615 | /* | |
616 | * '_cups_strdup()' - Duplicate a string. | |
617 | */ | |
618 | ||
619 | #ifndef HAVE_STRDUP | |
620 | char * /* O - New string pointer */ | |
621 | _cups_strdup(const char *s) /* I - String to duplicate */ | |
622 | { | |
5a9febac MS |
623 | char *t; /* New string pointer */ |
624 | size_t slen; /* Length of string */ | |
ef416fc2 | 625 | |
626 | ||
5a9febac | 627 | if (!s) |
ef416fc2 | 628 | return (NULL); |
629 | ||
5a9febac MS |
630 | slen = strlen(s); |
631 | if ((t = malloc(slen + 1)) == NULL) | |
ef416fc2 | 632 | return (NULL); |
633 | ||
5a9febac | 634 | return (memcpy(t, s, slen + 1)); |
ef416fc2 | 635 | } |
636 | #endif /* !HAVE_STRDUP */ | |
637 | ||
638 | ||
639 | /* | |
640 | * '_cups_strcasecmp()' - Do a case-insensitive comparison. | |
641 | */ | |
642 | ||
ef416fc2 | 643 | int /* O - Result of comparison (-1, 0, or 1) */ |
644 | _cups_strcasecmp(const char *s, /* I - First string */ | |
88f9aafc | 645 | const char *t) /* I - Second string */ |
ef416fc2 | 646 | { |
647 | while (*s != '\0' && *t != '\0') | |
648 | { | |
88f9aafc | 649 | if (_cups_tolower(*s) < _cups_tolower(*t)) |
ef416fc2 | 650 | return (-1); |
88f9aafc | 651 | else if (_cups_tolower(*s) > _cups_tolower(*t)) |
ef416fc2 | 652 | return (1); |
653 | ||
654 | s ++; | |
655 | t ++; | |
656 | } | |
657 | ||
658 | if (*s == '\0' && *t == '\0') | |
659 | return (0); | |
660 | else if (*s != '\0') | |
661 | return (1); | |
662 | else | |
663 | return (-1); | |
664 | } | |
ef416fc2 | 665 | |
666 | /* | |
667 | * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars. | |
668 | */ | |
669 | ||
ef416fc2 | 670 | int /* O - Result of comparison (-1, 0, or 1) */ |
671 | _cups_strncasecmp(const char *s, /* I - First string */ | |
0a682745 | 672 | const char *t, /* I - Second string */ |
ef416fc2 | 673 | size_t n) /* I - Maximum number of characters to compare */ |
674 | { | |
675 | while (*s != '\0' && *t != '\0' && n > 0) | |
676 | { | |
88f9aafc | 677 | if (_cups_tolower(*s) < _cups_tolower(*t)) |
ef416fc2 | 678 | return (-1); |
88f9aafc | 679 | else if (_cups_tolower(*s) > _cups_tolower(*t)) |
ef416fc2 | 680 | return (1); |
681 | ||
682 | s ++; | |
683 | t ++; | |
684 | n --; | |
685 | } | |
686 | ||
687 | if (n == 0) | |
688 | return (0); | |
689 | else if (*s == '\0' && *t == '\0') | |
690 | return (0); | |
691 | else if (*s != '\0') | |
692 | return (1); | |
693 | else | |
694 | return (-1); | |
695 | } | |
ef416fc2 | 696 | |
697 | ||
698 | #ifndef HAVE_STRLCAT | |
699 | /* | |
700 | * '_cups_strlcat()' - Safely concatenate two strings. | |
701 | */ | |
702 | ||
703 | size_t /* O - Length of string */ | |
704 | _cups_strlcat(char *dst, /* O - Destination string */ | |
705 | const char *src, /* I - Source string */ | |
706 | size_t size) /* I - Size of destination string buffer */ | |
707 | { | |
708 | size_t srclen; /* Length of source string */ | |
709 | size_t dstlen; /* Length of destination string */ | |
710 | ||
711 | ||
712 | /* | |
713 | * Figure out how much room is left... | |
714 | */ | |
715 | ||
716 | dstlen = strlen(dst); | |
717 | size -= dstlen + 1; | |
718 | ||
719 | if (!size) | |
720 | return (dstlen); /* No room, return immediately... */ | |
721 | ||
722 | /* | |
723 | * Figure out how much room is needed... | |
724 | */ | |
725 | ||
726 | srclen = strlen(src); | |
727 | ||
728 | /* | |
729 | * Copy the appropriate amount... | |
730 | */ | |
731 | ||
732 | if (srclen > size) | |
733 | srclen = size; | |
734 | ||
735 | memcpy(dst + dstlen, src, srclen); | |
736 | dst[dstlen + srclen] = '\0'; | |
737 | ||
738 | return (dstlen + srclen); | |
739 | } | |
740 | #endif /* !HAVE_STRLCAT */ | |
741 | ||
742 | ||
743 | #ifndef HAVE_STRLCPY | |
744 | /* | |
745 | * '_cups_strlcpy()' - Safely copy two strings. | |
746 | */ | |
747 | ||
748 | size_t /* O - Length of string */ | |
749 | _cups_strlcpy(char *dst, /* O - Destination string */ | |
750 | const char *src, /* I - Source string */ | |
751 | size_t size) /* I - Size of destination string buffer */ | |
752 | { | |
753 | size_t srclen; /* Length of source string */ | |
754 | ||
755 | ||
756 | /* | |
757 | * Figure out how much room is needed... | |
758 | */ | |
759 | ||
760 | size --; | |
761 | ||
762 | srclen = strlen(src); | |
763 | ||
764 | /* | |
765 | * Copy the appropriate amount... | |
766 | */ | |
767 | ||
768 | if (srclen > size) | |
769 | srclen = size; | |
770 | ||
771 | memcpy(dst, src, srclen); | |
772 | dst[srclen] = '\0'; | |
773 | ||
774 | return (srclen); | |
775 | } | |
776 | #endif /* !HAVE_STRLCPY */ | |
777 | ||
778 | ||
779 | /* | |
4400e98d | 780 | * 'compare_sp_items()' - Compare two string pool items... |
781 | */ | |
782 | ||
783 | static int /* O - Result of comparison */ | |
784 | compare_sp_items(_cups_sp_item_t *a, /* I - First item */ | |
785 | _cups_sp_item_t *b) /* I - Second item */ | |
786 | { | |
787 | return (strcmp(a->str, b->str)); | |
788 | } | |
789 | ||
790 | ||
791 | /* | |
a51f28ec | 792 | * End of "$Id: string.c 11890 2014-05-22 13:59:21Z msweet $". |
ef416fc2 | 793 | */ |