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