]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
7e86f2f6 | 4 | * Option routines for CUPS. |
ef416fc2 | 5 | * |
7e86f2f6 MS |
6 | * Copyright 2007-2014 by Apple Inc. |
7 | * Copyright 1997-2007 by Easy Software Products. | |
ef416fc2 | 8 | * |
7e86f2f6 MS |
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/". | |
ef416fc2 | 14 | * |
7e86f2f6 | 15 | * This file is subject to the Apple OS-Developed Software exception. |
ef416fc2 | 16 | */ |
17 | ||
18 | /* | |
19 | * Include necessary headers... | |
20 | */ | |
21 | ||
7cf5915e | 22 | #include "cups-private.h" |
ef416fc2 | 23 | |
24 | ||
426c6a59 MS |
25 | /* |
26 | * Local functions... | |
27 | */ | |
28 | ||
29 | static int cups_compare_options(cups_option_t *a, cups_option_t *b); | |
30 | static int cups_find_option(const char *name, int num_options, | |
31 | cups_option_t *option, int prev, int *rdiff); | |
32 | ||
33 | ||
ef416fc2 | 34 | /* |
35 | * 'cupsAddOption()' - Add an option to an option array. | |
5a738aea MS |
36 | * |
37 | * New option arrays can be initialized simply by passing 0 for the | |
38 | * "num_options" parameter. | |
ef416fc2 | 39 | */ |
40 | ||
5a738aea MS |
41 | int /* O - Number of options */ |
42 | cupsAddOption(const char *name, /* I - Name of option */ | |
43 | const char *value, /* I - Value of option */ | |
44 | int num_options,/* I - Number of options */ | |
ef416fc2 | 45 | cups_option_t **options) /* IO - Pointer to options */ |
46 | { | |
ef416fc2 | 47 | cups_option_t *temp; /* Pointer to new option */ |
426c6a59 MS |
48 | int insert, /* Insertion point */ |
49 | diff; /* Result of search */ | |
ef416fc2 | 50 | |
51 | ||
807315e6 | 52 | DEBUG_printf(("2cupsAddOption(name=\"%s\", value=\"%s\", num_options=%d, options=%p)", name, value, num_options, (void *)options)); |
88f9aafc | 53 | |
20fbc903 MS |
54 | if (!name || !name[0] || !value || !options || num_options < 0) |
55 | { | |
f11a948a | 56 | DEBUG_printf(("3cupsAddOption: Returning %d", num_options)); |
ef416fc2 | 57 | return (num_options); |
20fbc903 | 58 | } |
ef416fc2 | 59 | |
60 | /* | |
61 | * Look for an existing option with the same name... | |
62 | */ | |
63 | ||
426c6a59 MS |
64 | if (num_options == 0) |
65 | { | |
66 | insert = 0; | |
67 | diff = 1; | |
68 | } | |
69 | else | |
70 | { | |
71 | insert = cups_find_option(name, num_options, *options, num_options - 1, | |
72 | &diff); | |
ef416fc2 | 73 | |
426c6a59 MS |
74 | if (diff > 0) |
75 | insert ++; | |
76 | } | |
77 | ||
78 | if (diff) | |
ef416fc2 | 79 | { |
80 | /* | |
81 | * No matching option name... | |
82 | */ | |
83 | ||
f11a948a | 84 | DEBUG_printf(("4cupsAddOption: New option inserted at index %d...", |
426c6a59 | 85 | insert)); |
20fbc903 | 86 | |
ef416fc2 | 87 | if (num_options == 0) |
88 | temp = (cups_option_t *)malloc(sizeof(cups_option_t)); | |
89 | else | |
7e86f2f6 | 90 | temp = (cups_option_t *)realloc(*options, sizeof(cups_option_t) * (size_t)(num_options + 1)); |
ef416fc2 | 91 | |
7e86f2f6 | 92 | if (!temp) |
20fbc903 | 93 | { |
f11a948a | 94 | DEBUG_puts("3cupsAddOption: Unable to expand option array, returning 0"); |
ef416fc2 | 95 | return (0); |
20fbc903 | 96 | } |
ef416fc2 | 97 | |
426c6a59 MS |
98 | *options = temp; |
99 | ||
100 | if (insert < num_options) | |
101 | { | |
f11a948a | 102 | DEBUG_printf(("4cupsAddOption: Shifting %d options...", |
426c6a59 | 103 | (int)(num_options - insert))); |
7e86f2f6 | 104 | memmove(temp + insert + 1, temp + insert, (size_t)(num_options - insert) * sizeof(cups_option_t)); |
426c6a59 MS |
105 | } |
106 | ||
107 | temp += insert; | |
2e4ff8af | 108 | temp->name = _cupsStrAlloc(name); |
ef416fc2 | 109 | num_options ++; |
110 | } | |
111 | else | |
112 | { | |
113 | /* | |
114 | * Match found; free the old value... | |
115 | */ | |
116 | ||
f11a948a | 117 | DEBUG_printf(("4cupsAddOption: Option already exists at index %d...", |
426c6a59 MS |
118 | insert)); |
119 | ||
120 | temp = *options + insert; | |
2e4ff8af | 121 | _cupsStrFree(temp->value); |
ef416fc2 | 122 | } |
123 | ||
2e4ff8af | 124 | temp->value = _cupsStrAlloc(value); |
ef416fc2 | 125 | |
f11a948a | 126 | DEBUG_printf(("3cupsAddOption: Returning %d", num_options)); |
20fbc903 | 127 | |
ef416fc2 | 128 | return (num_options); |
129 | } | |
130 | ||
131 | ||
132 | /* | |
133 | * 'cupsFreeOptions()' - Free all memory used by options. | |
134 | */ | |
135 | ||
136 | void | |
137 | cupsFreeOptions( | |
138 | int num_options, /* I - Number of options */ | |
139 | cups_option_t *options) /* I - Pointer to options */ | |
140 | { | |
141 | int i; /* Looping var */ | |
142 | ||
143 | ||
807315e6 | 144 | DEBUG_printf(("cupsFreeOptions(num_options=%d, options=%p)", num_options, (void *)options)); |
20fbc903 MS |
145 | |
146 | if (num_options <= 0 || !options) | |
ef416fc2 | 147 | return; |
148 | ||
149 | for (i = 0; i < num_options; i ++) | |
150 | { | |
2e4ff8af MS |
151 | _cupsStrFree(options[i].name); |
152 | _cupsStrFree(options[i].value); | |
ef416fc2 | 153 | } |
154 | ||
155 | free(options); | |
156 | } | |
157 | ||
158 | ||
159 | /* | |
160 | * 'cupsGetOption()' - Get an option value. | |
161 | */ | |
162 | ||
5a738aea | 163 | const char * /* O - Option value or @code NULL@ */ |
ef416fc2 | 164 | cupsGetOption(const char *name, /* I - Name of option */ |
165 | int num_options,/* I - Number of options */ | |
166 | cups_option_t *options) /* I - Options */ | |
167 | { | |
426c6a59 MS |
168 | int diff, /* Result of comparison */ |
169 | match; /* Matching index */ | |
ef416fc2 | 170 | |
171 | ||
807315e6 | 172 | DEBUG_printf(("2cupsGetOption(name=\"%s\", num_options=%d, options=%p)", name, num_options, (void *)options)); |
20fbc903 MS |
173 | |
174 | if (!name || num_options <= 0 || !options) | |
175 | { | |
e07d4801 | 176 | DEBUG_puts("3cupsGetOption: Returning NULL"); |
ef416fc2 | 177 | return (NULL); |
20fbc903 | 178 | } |
ef416fc2 | 179 | |
426c6a59 MS |
180 | match = cups_find_option(name, num_options, options, -1, &diff); |
181 | ||
182 | if (!diff) | |
183 | { | |
e07d4801 | 184 | DEBUG_printf(("3cupsGetOption: Returning \"%s\"", options[match].value)); |
426c6a59 MS |
185 | return (options[match].value); |
186 | } | |
ef416fc2 | 187 | |
e07d4801 | 188 | DEBUG_puts("3cupsGetOption: Returning NULL"); |
ef416fc2 | 189 | return (NULL); |
190 | } | |
191 | ||
192 | ||
ef416fc2 | 193 | /* |
b423cd4c | 194 | * 'cupsParseOptions()' - Parse options from a command-line argument. |
195 | * | |
196 | * This function converts space-delimited name/value pairs according | |
197 | * to the PAPI text option ABNF specification. Collection values | |
198 | * ("name={a=... b=... c=...}") are stored with the curley brackets | |
5a738aea MS |
199 | * intact - use @code cupsParseOptions@ on the value to extract the |
200 | * collection attributes. | |
b423cd4c | 201 | */ |
202 | ||
203 | int /* O - Number of options found */ | |
204 | cupsParseOptions( | |
205 | const char *arg, /* I - Argument to parse */ | |
206 | int num_options, /* I - Number of options */ | |
207 | cups_option_t **options) /* O - Options found */ | |
208 | { | |
209 | char *copyarg, /* Copy of input string */ | |
210 | *ptr, /* Pointer into string */ | |
211 | *name, /* Pointer to name */ | |
5a738aea | 212 | *value, /* Pointer to value */ |
426c6a59 | 213 | sep, /* Separator character */ |
5a738aea | 214 | quote; /* Quote character */ |
b423cd4c | 215 | |
216 | ||
807315e6 | 217 | DEBUG_printf(("cupsParseOptions(arg=\"%s\", num_options=%d, options=%p)", arg, num_options, (void *)options)); |
20fbc903 | 218 | |
91c84a35 MS |
219 | /* |
220 | * Range check input... | |
221 | */ | |
222 | ||
223 | if (!arg) | |
20fbc903 | 224 | { |
e07d4801 | 225 | DEBUG_printf(("1cupsParseOptions: Returning %d", num_options)); |
91c84a35 | 226 | return (num_options); |
20fbc903 | 227 | } |
91c84a35 MS |
228 | |
229 | if (!options || num_options < 0) | |
20fbc903 | 230 | { |
e07d4801 | 231 | DEBUG_puts("1cupsParseOptions: Returning 0"); |
b423cd4c | 232 | return (0); |
20fbc903 | 233 | } |
b423cd4c | 234 | |
235 | /* | |
236 | * Make a copy of the argument string and then divide it up... | |
237 | */ | |
238 | ||
91c84a35 | 239 | if ((copyarg = strdup(arg)) == NULL) |
20fbc903 | 240 | { |
e07d4801 MS |
241 | DEBUG_puts("1cupsParseOptions: Unable to copy arg string"); |
242 | DEBUG_printf(("1cupsParseOptions: Returning %d", num_options)); | |
91c84a35 | 243 | return (num_options); |
20fbc903 | 244 | } |
91c84a35 | 245 | |
ee571f26 MS |
246 | if (*copyarg == '{') |
247 | { | |
248 | /* | |
249 | * Remove surrounding {} so we can parse "{name=value ... name=value}"... | |
250 | */ | |
251 | ||
252 | if ((ptr = copyarg + strlen(copyarg) - 1) > copyarg && *ptr == '}') | |
253 | { | |
254 | *ptr = '\0'; | |
255 | ptr = copyarg + 1; | |
256 | } | |
257 | else | |
258 | ptr = copyarg; | |
259 | } | |
260 | else | |
261 | ptr = copyarg; | |
b423cd4c | 262 | |
263 | /* | |
264 | * Skip leading spaces... | |
265 | */ | |
266 | ||
7cf5915e | 267 | while (_cups_isspace(*ptr)) |
b423cd4c | 268 | ptr ++; |
269 | ||
270 | /* | |
271 | * Loop through the string... | |
272 | */ | |
273 | ||
274 | while (*ptr != '\0') | |
275 | { | |
276 | /* | |
277 | * Get the name up to a SPACE, =, or end-of-string... | |
278 | */ | |
279 | ||
280 | name = ptr; | |
7cf5915e | 281 | while (!strchr("\f\n\r\t\v =", *ptr) && *ptr) |
b423cd4c | 282 | ptr ++; |
283 | ||
284 | /* | |
285 | * Avoid an empty name... | |
286 | */ | |
287 | ||
288 | if (ptr == name) | |
289 | break; | |
290 | ||
291 | /* | |
292 | * Skip trailing spaces... | |
293 | */ | |
294 | ||
7cf5915e | 295 | while (_cups_isspace(*ptr)) |
b423cd4c | 296 | *ptr++ = '\0'; |
297 | ||
426c6a59 MS |
298 | if ((sep = *ptr) == '=') |
299 | *ptr++ = '\0'; | |
300 | ||
e07d4801 | 301 | DEBUG_printf(("2cupsParseOptions: name=\"%s\"", name)); |
20fbc903 | 302 | |
426c6a59 | 303 | if (sep != '=') |
b423cd4c | 304 | { |
305 | /* | |
5a738aea | 306 | * Boolean option... |
b423cd4c | 307 | */ |
308 | ||
88f9aafc | 309 | if (!_cups_strncasecmp(name, "no", 2)) |
b423cd4c | 310 | num_options = cupsAddOption(name + 2, "false", num_options, |
311 | options); | |
312 | else | |
313 | num_options = cupsAddOption(name, "true", num_options, options); | |
314 | ||
315 | continue; | |
316 | } | |
317 | ||
318 | /* | |
319 | * Remove = and parse the value... | |
320 | */ | |
321 | ||
426c6a59 | 322 | value = ptr; |
b423cd4c | 323 | |
7cf5915e | 324 | while (*ptr && !_cups_isspace(*ptr)) |
b423cd4c | 325 | { |
20fbc903 MS |
326 | if (*ptr == ',') |
327 | ptr ++; | |
328 | else if (*ptr == '\'' || *ptr == '\"') | |
b423cd4c | 329 | { |
20fbc903 MS |
330 | /* |
331 | * Quoted string constant... | |
332 | */ | |
b423cd4c | 333 | |
20fbc903 MS |
334 | quote = *ptr; |
335 | _cups_strcpy(ptr, ptr + 1); | |
b423cd4c | 336 | |
20fbc903 MS |
337 | while (*ptr != quote && *ptr) |
338 | { | |
339 | if (*ptr == '\\' && ptr[1]) | |
340 | _cups_strcpy(ptr, ptr + 1); | |
341 | ||
342 | ptr ++; | |
343 | } | |
b423cd4c | 344 | |
20fbc903 MS |
345 | if (*ptr) |
346 | _cups_strcpy(ptr, ptr + 1); | |
347 | } | |
348 | else if (*ptr == '{') | |
349 | { | |
350 | /* | |
351 | * Collection value... | |
352 | */ | |
b423cd4c | 353 | |
20fbc903 | 354 | int depth; |
b423cd4c | 355 | |
20fbc903 | 356 | for (depth = 0; *ptr; ptr ++) |
b423cd4c | 357 | { |
20fbc903 MS |
358 | if (*ptr == '{') |
359 | depth ++; | |
360 | else if (*ptr == '}') | |
b423cd4c | 361 | { |
20fbc903 MS |
362 | depth --; |
363 | if (!depth) | |
364 | { | |
365 | ptr ++; | |
b423cd4c | 366 | break; |
20fbc903 | 367 | } |
b423cd4c | 368 | } |
20fbc903 MS |
369 | else if (*ptr == '\\' && ptr[1]) |
370 | _cups_strcpy(ptr, ptr + 1); | |
371 | } | |
372 | } | |
373 | else | |
b423cd4c | 374 | { |
20fbc903 MS |
375 | /* |
376 | * Normal space-delimited string... | |
377 | */ | |
b423cd4c | 378 | |
7cf5915e | 379 | while (*ptr && !_cups_isspace(*ptr)) |
20fbc903 MS |
380 | { |
381 | if (*ptr == '\\' && ptr[1]) | |
382 | _cups_strcpy(ptr, ptr + 1); | |
383 | ||
384 | ptr ++; | |
385 | } | |
b423cd4c | 386 | } |
387 | } | |
388 | ||
20fbc903 MS |
389 | if (*ptr != '\0') |
390 | *ptr++ = '\0'; | |
391 | ||
e07d4801 | 392 | DEBUG_printf(("2cupsParseOptions: value=\"%s\"", value)); |
20fbc903 | 393 | |
b423cd4c | 394 | /* |
395 | * Skip trailing whitespace... | |
396 | */ | |
397 | ||
7cf5915e | 398 | while (_cups_isspace(*ptr)) |
20fbc903 | 399 | ptr ++; |
b423cd4c | 400 | |
401 | /* | |
402 | * Add the string value... | |
403 | */ | |
404 | ||
405 | num_options = cupsAddOption(name, value, num_options, options); | |
406 | } | |
407 | ||
408 | /* | |
409 | * Free the copy of the argument we made and return the number of options | |
410 | * found. | |
411 | */ | |
412 | ||
413 | free(copyarg); | |
414 | ||
e07d4801 | 415 | DEBUG_printf(("1cupsParseOptions: Returning %d", num_options)); |
20fbc903 | 416 | |
b423cd4c | 417 | return (num_options); |
418 | } | |
419 | ||
420 | ||
421 | /* | |
f7deaa1a | 422 | * 'cupsRemoveOption()' - Remove an option from an option array. |
b423cd4c | 423 | * |
f3c17241 | 424 | * @since CUPS 1.2/OS X 10.5@ |
b423cd4c | 425 | */ |
426 | ||
427 | int /* O - New number of options */ | |
428 | cupsRemoveOption( | |
429 | const char *name, /* I - Option name */ | |
430 | int num_options, /* I - Current number of options */ | |
431 | cups_option_t **options) /* IO - Options */ | |
432 | { | |
433 | int i; /* Looping var */ | |
434 | cups_option_t *option; /* Current option */ | |
435 | ||
436 | ||
807315e6 | 437 | DEBUG_printf(("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)", name, num_options, (void *)options)); |
20fbc903 | 438 | |
b423cd4c | 439 | /* |
440 | * Range check input... | |
441 | */ | |
442 | ||
443 | if (!name || num_options < 1 || !options) | |
20fbc903 | 444 | { |
f11a948a | 445 | DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options)); |
b423cd4c | 446 | return (num_options); |
20fbc903 | 447 | } |
b423cd4c | 448 | |
449 | /* | |
450 | * Loop for the option... | |
451 | */ | |
452 | ||
453 | for (i = num_options, option = *options; i > 0; i --, option ++) | |
88f9aafc | 454 | if (!_cups_strcasecmp(name, option->name)) |
b423cd4c | 455 | break; |
456 | ||
457 | if (i) | |
458 | { | |
459 | /* | |
460 | * Remove this option from the array... | |
461 | */ | |
462 | ||
f11a948a | 463 | DEBUG_puts("4cupsRemoveOption: Found option, removing it..."); |
20fbc903 | 464 | |
b423cd4c | 465 | num_options --; |
466 | i --; | |
467 | ||
2e4ff8af MS |
468 | _cupsStrFree(option->name); |
469 | _cupsStrFree(option->value); | |
b423cd4c | 470 | |
471 | if (i > 0) | |
7e86f2f6 | 472 | memmove(option, option + 1, (size_t)i * sizeof(cups_option_t)); |
b423cd4c | 473 | } |
474 | ||
475 | /* | |
476 | * Return the new number of options... | |
477 | */ | |
478 | ||
f11a948a | 479 | DEBUG_printf(("3cupsRemoveOption: Returning %d", num_options)); |
b423cd4c | 480 | return (num_options); |
481 | } | |
482 | ||
483 | ||
f8b3a85b MS |
484 | /* |
485 | * '_cupsGet1284Values()' - Get 1284 device ID keys and values. | |
486 | * | |
487 | * The returned dictionary is a CUPS option array that can be queried with | |
488 | * cupsGetOption and freed with cupsFreeOptions. | |
489 | */ | |
490 | ||
491 | int /* O - Number of key/value pairs */ | |
492 | _cupsGet1284Values( | |
493 | const char *device_id, /* I - IEEE-1284 device ID string */ | |
494 | cups_option_t **values) /* O - Array of key/value pairs */ | |
495 | { | |
496 | int num_values; /* Number of values */ | |
497 | char key[256], /* Key string */ | |
498 | value[256], /* Value string */ | |
499 | *ptr; /* Pointer into key/value */ | |
500 | ||
501 | ||
502 | /* | |
503 | * Range check input... | |
504 | */ | |
505 | ||
506 | if (values) | |
507 | *values = NULL; | |
508 | ||
509 | if (!device_id || !values) | |
510 | return (0); | |
511 | ||
512 | /* | |
513 | * Parse the 1284 device ID value into keys and values. The format is | |
514 | * repeating sequences of: | |
515 | * | |
516 | * [whitespace]key:value[whitespace]; | |
517 | */ | |
518 | ||
519 | num_values = 0; | |
520 | while (*device_id) | |
521 | { | |
7cf5915e | 522 | while (_cups_isspace(*device_id)) |
f8b3a85b MS |
523 | device_id ++; |
524 | ||
525 | if (!*device_id) | |
526 | break; | |
527 | ||
528 | for (ptr = key; *device_id && *device_id != ':'; device_id ++) | |
529 | if (ptr < (key + sizeof(key) - 1)) | |
530 | *ptr++ = *device_id; | |
531 | ||
532 | if (!*device_id) | |
533 | break; | |
534 | ||
7cf5915e | 535 | while (ptr > key && _cups_isspace(ptr[-1])) |
f8b3a85b MS |
536 | ptr --; |
537 | ||
538 | *ptr = '\0'; | |
539 | device_id ++; | |
540 | ||
7cf5915e | 541 | while (_cups_isspace(*device_id)) |
f8b3a85b MS |
542 | device_id ++; |
543 | ||
544 | if (!*device_id) | |
545 | break; | |
546 | ||
547 | for (ptr = value; *device_id && *device_id != ';'; device_id ++) | |
548 | if (ptr < (value + sizeof(value) - 1)) | |
549 | *ptr++ = *device_id; | |
550 | ||
551 | if (!*device_id) | |
552 | break; | |
553 | ||
7cf5915e | 554 | while (ptr > value && _cups_isspace(ptr[-1])) |
f8b3a85b MS |
555 | ptr --; |
556 | ||
557 | *ptr = '\0'; | |
558 | device_id ++; | |
559 | ||
560 | num_values = cupsAddOption(key, value, num_values, values); | |
561 | } | |
562 | ||
563 | return (num_values); | |
564 | } | |
565 | ||
566 | ||
09a101d6 | 567 | /* |
426c6a59 MS |
568 | * 'cups_compare_options()' - Compare two options. |
569 | */ | |
570 | ||
571 | static int /* O - Result of comparison */ | |
572 | cups_compare_options(cups_option_t *a, /* I - First option */ | |
573 | cups_option_t *b) /* I - Second option */ | |
574 | { | |
88f9aafc | 575 | return (_cups_strcasecmp(a->name, b->name)); |
426c6a59 MS |
576 | } |
577 | ||
578 | ||
579 | /* | |
580 | * 'cups_find_option()' - Find an option using a binary search. | |
581 | */ | |
582 | ||
583 | static int /* O - Index of match */ | |
584 | cups_find_option( | |
585 | const char *name, /* I - Option name */ | |
586 | int num_options, /* I - Number of options */ | |
587 | cups_option_t *options, /* I - Options */ | |
588 | int prev, /* I - Previous index */ | |
589 | int *rdiff) /* O - Difference of match */ | |
590 | { | |
591 | int left, /* Low mark for binary search */ | |
592 | right, /* High mark for binary search */ | |
593 | current, /* Current index */ | |
594 | diff; /* Result of comparison */ | |
595 | cups_option_t key; /* Search key */ | |
596 | ||
597 | ||
807315e6 | 598 | DEBUG_printf(("7cups_find_option(name=\"%s\", num_options=%d, options=%p, prev=%d, rdiff=%p)", name, num_options, (void *)options, prev, (void *)rdiff)); |
426c6a59 MS |
599 | |
600 | #ifdef DEBUG | |
601 | for (left = 0; left < num_options; left ++) | |
e07d4801 | 602 | DEBUG_printf(("9cups_find_option: options[%d].name=\"%s\", .value=\"%s\"", |
426c6a59 MS |
603 | left, options[left].name, options[left].value)); |
604 | #endif /* DEBUG */ | |
605 | ||
606 | key.name = (char *)name; | |
607 | ||
608 | if (prev >= 0) | |
609 | { | |
610 | /* | |
611 | * Start search on either side of previous... | |
612 | */ | |
613 | ||
614 | if ((diff = cups_compare_options(&key, options + prev)) == 0 || | |
615 | (diff < 0 && prev == 0) || | |
616 | (diff > 0 && prev == (num_options - 1))) | |
617 | { | |
618 | *rdiff = diff; | |
619 | return (prev); | |
620 | } | |
621 | else if (diff < 0) | |
622 | { | |
623 | /* | |
624 | * Start with previous on right side... | |
625 | */ | |
626 | ||
627 | left = 0; | |
628 | right = prev; | |
629 | } | |
630 | else | |
631 | { | |
632 | /* | |
633 | * Start wih previous on left side... | |
634 | */ | |
635 | ||
636 | left = prev; | |
637 | right = num_options - 1; | |
638 | } | |
639 | } | |
640 | else | |
641 | { | |
642 | /* | |
643 | * Start search in the middle... | |
644 | */ | |
645 | ||
646 | left = 0; | |
647 | right = num_options - 1; | |
648 | } | |
649 | ||
650 | do | |
651 | { | |
652 | current = (left + right) / 2; | |
653 | diff = cups_compare_options(&key, options + current); | |
654 | ||
655 | if (diff == 0) | |
656 | break; | |
657 | else if (diff < 0) | |
658 | right = current; | |
659 | else | |
660 | left = current; | |
661 | } | |
662 | while ((right - left) > 1); | |
663 | ||
664 | if (diff != 0) | |
665 | { | |
666 | /* | |
667 | * Check the last 1 or 2 elements... | |
668 | */ | |
669 | ||
670 | if ((diff = cups_compare_options(&key, options + left)) <= 0) | |
671 | current = left; | |
672 | else | |
673 | { | |
674 | diff = cups_compare_options(&key, options + right); | |
675 | current = right; | |
676 | } | |
677 | } | |
678 | ||
679 | /* | |
680 | * Return the closest destination and the difference... | |
681 | */ | |
682 | ||
683 | *rdiff = diff; | |
684 | ||
685 | return (current); | |
686 | } | |
687 | ||
688 | ||
689 | /* | |
f2d18633 | 690 | * End of "$Id$". |
ef416fc2 | 691 | */ |