]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/attr.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / attr.c
1 /*
2 * "$Id: attr.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * PPD model-specific attribute routines for the Common UNIX Printing System
5 * (CUPS).
6 *
7 * Copyright 2007 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products.
9 *
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
15 *
16 * Contents:
17 *
18 * ppdFindAttr() - Find the first matching attribute...
19 * ppdFindNextAttr() - Find the next matching attribute...
20 */
21
22 /*
23 * Include necessary headers...
24 */
25
26 #include "ppd.h"
27 #include "debug.h"
28 #include "string.h"
29 #include <stdlib.h>
30
31
32 /*
33 * 'ppdFindAttr()' - Find the first matching attribute...
34 *
35 * @since CUPS 1.1.19@
36 */
37
38 ppd_attr_t * /* O - Attribute or NULL if not found */
39 ppdFindAttr(ppd_file_t *ppd, /* I - PPD file data */
40 const char *name, /* I - Attribute name */
41 const char *spec) /* I - Specifier string or NULL */
42 {
43 ppd_attr_t key, /* Search key */
44 *attr; /* Current attribute */
45 int diff; /* Current difference */
46
47
48 DEBUG_printf(("ppdFindAttr(ppd=%p, name=\"%s\", spec=\"%s\")\n", ppd,
49 name ? name : "(null)", spec ? spec : "(null)"));
50
51 /*
52 * Range check input...
53 */
54
55 if (!ppd || !name || ppd->num_attrs == 0)
56 return (NULL);
57
58 /*
59 * Search for a matching attribute...
60 */
61
62 memset(&key, 0, sizeof(key));
63 strlcpy(key.name, name, sizeof(key.name));
64 if (spec)
65 strlcpy(key.spec, spec, sizeof(key.spec));
66
67 /*
68 * Return the first matching attribute, if any...
69 */
70
71 if ((attr = (ppd_attr_t *)cupsArrayFind(ppd->sorted_attrs, &key)) != NULL)
72 return (attr);
73 else if (spec)
74 return (NULL);
75
76 /*
77 * No match found, loop through the sorted attributes to see if we can
78 * find a "wildcard" match for the attribute...
79 */
80
81 for (attr = (ppd_attr_t *)cupsArrayFirst(ppd->sorted_attrs);
82 attr;
83 attr = (ppd_attr_t *)cupsArrayNext(ppd->sorted_attrs))
84 {
85 if ((diff = strcasecmp(attr->name, name)) == 0)
86 break;
87
88 if (diff > 0)
89 {
90 /*
91 * All remaining attributes are > than the one we are trying to find...
92 */
93
94 cupsArrayIndex(ppd->sorted_attrs, cupsArrayCount(ppd->sorted_attrs));
95
96 return (NULL);
97 }
98 }
99
100 return (attr);
101 }
102
103
104 /*
105 * 'ppdFindNextAttr()' - Find the next matching attribute...
106 *
107 * @since CUPS 1.1.19@
108 */
109
110 ppd_attr_t * /* O - Attribute or NULL if not found */
111 ppdFindNextAttr(ppd_file_t *ppd, /* I - PPD file data */
112 const char *name, /* I - Attribute name */
113 const char *spec) /* I - Specifier string or NULL */
114 {
115 ppd_attr_t *attr; /* Current attribute */
116
117
118 /*
119 * Range check input...
120 */
121
122 if (!ppd || !name || ppd->num_attrs == 0 ||
123 !cupsArrayCurrent(ppd->sorted_attrs))
124 return (NULL);
125
126 /*
127 * See if there are more attributes to return...
128 */
129
130 if ((attr = (ppd_attr_t *)cupsArrayNext(ppd->sorted_attrs)) == NULL)
131 return (NULL);
132
133 /*
134 * Check the next attribute to see if it is a match...
135 */
136
137 if (strcasecmp(attr->name, name) || (spec && strcasecmp(attr->spec, spec)))
138 {
139 /*
140 * Nope, reset the current pointer to the end of the array...
141 */
142
143 cupsArrayIndex(ppd->sorted_attrs, cupsArrayCount(ppd->sorted_attrs));
144
145 return (NULL);
146 }
147
148 /*
149 * Return the next attribute's value...
150 */
151
152 return (attr);
153 }
154
155
156 /*
157 * End of "$Id: attr.c 6649 2007-07-11 21:46:42Z mike $".
158 */