]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2 | * "$Id: attr.c 4785 2005-10-13 19:39:05Z mike $" | |
3 | * | |
4 | * PPD model-specific attribute routines for the Common UNIX Printing System | |
5 | * (CUPS). | |
6 | * | |
7 | * Copyright 1997-2005 by Easy Software Products. | |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
10 | * property of Easy Software Products and are protected by Federal | |
11 | * copyright law. Distribution and use rights are outlined in the file | |
12 | * "LICENSE.txt" which should have been included with this file. If this | |
13 | * file is missing or damaged please contact Easy Software Products | |
14 | * at: | |
15 | * | |
16 | * Attn: CUPS Licensing Information | |
17 | * Easy Software Products | |
18 | * 44141 Airport View Drive, Suite 204 | |
19 | * Hollywood, Maryland 20636 USA | |
20 | * | |
21 | * Voice: (301) 373-9600 | |
22 | * EMail: cups-info@cups.org | |
23 | * WWW: http://www.cups.org | |
24 | * | |
25 | * Contents: | |
26 | * | |
27 | * ppdFindAttr() - Find the first matching attribute... | |
28 | * ppdFindNextAttr() - Find the next matching attribute... | |
29 | */ | |
30 | ||
31 | /* | |
32 | * Include necessary headers... | |
33 | */ | |
34 | ||
35 | #include "ppd.h" | |
36 | #include "debug.h" | |
37 | #include "string.h" | |
38 | #include <stdlib.h> | |
39 | ||
40 | ||
41 | /* | |
42 | * Private function... | |
43 | */ | |
44 | ||
45 | extern int _ppd_attr_compare(ppd_attr_t **a, ppd_attr_t **b); | |
46 | ||
47 | ||
48 | /* | |
49 | * 'ppdFindAttr()' - Find the first matching attribute... | |
50 | * | |
51 | * @since CUPS 1.1.19@ | |
52 | */ | |
53 | ||
54 | ppd_attr_t * /* O - Attribute or NULL if not found */ | |
55 | ppdFindAttr(ppd_file_t *ppd, /* I - PPD file data */ | |
56 | const char *name, /* I - Attribute name */ | |
57 | const char *spec) /* I - Specifier string or NULL */ | |
58 | { | |
59 | ppd_attr_t key, /* Search key */ | |
60 | *keyptr, /* Pointer to key */ | |
61 | **match; /* Matching attribute */ | |
62 | ||
63 | ||
64 | /* | |
65 | * Range check input... | |
66 | */ | |
67 | ||
68 | if (ppd == NULL || name == NULL || ppd->num_attrs == 0) | |
69 | return (NULL); | |
70 | ||
71 | /* | |
72 | * Do a binary search for a matching attribute... | |
73 | */ | |
74 | ||
75 | memset(&key, 0, sizeof(key)); | |
76 | strncpy(key.name, name, sizeof(key.name) - 1); | |
77 | if (spec) | |
78 | strncpy(key.spec, spec, sizeof(key.spec) - 1); | |
79 | ||
80 | keyptr = &key; | |
81 | ||
82 | match = bsearch(&keyptr, ppd->attrs, ppd->num_attrs, sizeof(ppd_attr_t *), | |
83 | (int (*)(const void *, const void *))_ppd_attr_compare); | |
84 | ||
85 | if (match == NULL) | |
86 | { | |
87 | /* | |
88 | * No match! | |
89 | */ | |
90 | ||
91 | ppd->cur_attr = -1; | |
92 | return (NULL); | |
93 | } | |
94 | ||
95 | if (match > ppd->attrs && spec == NULL) | |
96 | { | |
97 | /* | |
98 | * Find the first attribute with the same name... | |
99 | */ | |
100 | ||
101 | while (match > ppd->attrs) | |
102 | { | |
103 | if (strcmp(match[-1]->name, name) != 0) | |
104 | break; | |
105 | ||
106 | match --; | |
107 | } | |
108 | } | |
109 | ||
110 | /* | |
111 | * Save the current attribute and return its value... | |
112 | */ | |
113 | ||
114 | ppd->cur_attr = match - ppd->attrs; | |
115 | ||
116 | return (*match); | |
117 | } | |
118 | ||
119 | ||
120 | /* | |
121 | * 'ppdFindNextAttr()' - Find the next matching attribute... | |
122 | * | |
123 | * @since CUPS 1.1.19@ | |
124 | */ | |
125 | ||
126 | ppd_attr_t * /* O - Attribute or NULL if not found */ | |
127 | ppdFindNextAttr(ppd_file_t *ppd, /* I - PPD file data */ | |
128 | const char *name, /* I - Attribute name */ | |
129 | const char *spec) /* I - Specifier string or NULL */ | |
130 | { | |
131 | ppd_attr_t **match; /* Matching attribute */ | |
132 | ||
133 | ||
134 | /* | |
135 | * Range check input... | |
136 | */ | |
137 | ||
138 | if (ppd == NULL || name == NULL || ppd->num_attrs == 0 || ppd->cur_attr < 0) | |
139 | return (NULL); | |
140 | ||
141 | /* | |
142 | * See if there are more attributes to return... | |
143 | */ | |
144 | ||
145 | ppd->cur_attr ++; | |
146 | ||
147 | if (ppd->cur_attr >= ppd->num_attrs) | |
148 | { | |
149 | /* | |
150 | * Nope... | |
151 | */ | |
152 | ||
153 | ppd->cur_attr = -1; | |
154 | return (NULL); | |
155 | } | |
156 | ||
157 | /* | |
158 | * Check the next attribute to see if it is a match... | |
159 | */ | |
160 | ||
161 | match = ppd->attrs + ppd->cur_attr; | |
162 | ||
163 | if (strcmp((*match)->name, name) != 0 || | |
164 | (spec != NULL && strcmp((*match)->spec, spec) != 0)) | |
165 | { | |
166 | /* | |
167 | * Nope... | |
168 | */ | |
169 | ||
170 | ppd->cur_attr = -1; | |
171 | return (NULL); | |
172 | } | |
173 | ||
174 | /* | |
175 | * Return the next attribute's value... | |
176 | */ | |
177 | ||
178 | return (*match); | |
179 | } | |
180 | ||
181 | ||
182 | /* | |
183 | * End of "$Id: attr.c 4785 2005-10-13 19:39:05Z mike $". | |
184 | */ |