]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/attr.c
Copyright update.
[thirdparty/cups.git] / cups / attr.c
1 /*
2 * "$Id: attr.c,v 1.1.2.2 2003/01/07 18:26:23 mike Exp $"
3 *
4 * PPD model-specific attribute routines for the Common UNIX Printing System
5 * (CUPS).
6 *
7 * Copyright 1997-2003 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-3111 USA
20 *
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
24 *
25 * Contents:
26 *
27 */
28
29 /*
30 * Include necessary headers...
31 */
32
33 #include "ppd.h"
34 #include "debug.h"
35 #include "string.h"
36 #include <stdlib.h>
37
38
39 /*
40 * Private function...
41 */
42
43 extern int _ppd_attr_compare(ppd_attr_t **a, ppd_attr_t **b);
44
45
46 /*
47 * 'ppdFindAttr()' - Find the first matching attribute...
48 */
49
50 const char * /* O - Value or NULL if not found */
51 ppdFindAttr(ppd_file_t *ppd, /* I - PPD file data */
52 const char *name, /* I - Attribute name */
53 const char *spec) /* I - Specifier string or NULL */
54 {
55 ppd_attr_t key, /* Search key */
56 *keyptr, /* Pointer to key */
57 **match; /* Matching attribute */
58
59
60 /*
61 * Range check input...
62 */
63
64 if (ppd == NULL || name == NULL || ppd->num_attrs == 0)
65 return (NULL);
66
67 /*
68 * Do a binary search for a matching attribute...
69 */
70
71 memset(&key, 0, sizeof(key));
72 strncpy(key.name, name, sizeof(key.name) - 1);
73 if (spec)
74 strncpy(key.spec, spec, sizeof(key.spec) - 1);
75
76 keyptr = &key;
77
78 match = bsearch(&keyptr, ppd->attrs, ppd->num_attrs, sizeof(ppd_attr_t *),
79 (int (*)(const void *, const void *))_ppd_attr_compare);
80
81 if (match == NULL)
82 {
83 /*
84 * No match!
85 */
86
87 ppd->cur_attr = -1;
88 return (NULL);
89 }
90
91 if (match > ppd->attrs && spec == NULL)
92 {
93 /*
94 * Find the first attribute with the same name...
95 */
96
97 while (match > ppd->attrs)
98 {
99 if (strcmp(match[-1]->name, name) != 0)
100 break;
101
102 match --;
103 }
104 }
105
106 /*
107 * Save the current attribute and return its value...
108 */
109
110 ppd->cur_attr = match - ppd->attrs;
111
112 if ((*match)->value)
113 return ((*match)->value);
114 else
115 return ("");
116 }
117
118
119 /*
120 * 'ppdFindNextAttr()' - Find the next matching attribute...
121 */
122
123 const char * /* O - Value or NULL if not found */
124 ppdFindNextAttr(ppd_file_t *ppd, /* I - PPD file data */
125 const char *name, /* I - Attribute name */
126 const char *spec) /* I - Specifier string or NULL */
127 {
128 ppd_attr_t **match; /* Matching attribute */
129
130
131 /*
132 * Range check input...
133 */
134
135 if (ppd == NULL || name == NULL || ppd->num_attrs == 0 || ppd->cur_attr < 0)
136 return (NULL);
137
138 /*
139 * See if there are more attributes to return...
140 */
141
142 ppd->cur_attr ++;
143
144 if (ppd->cur_attr >= ppd->num_attrs)
145 {
146 /*
147 * Nope...
148 */
149
150 ppd->cur_attr = -1;
151 return (NULL);
152 }
153
154 /*
155 * Check the next attribute to see if it is a match...
156 */
157
158 match = ppd->attrs + ppd->cur_attr;
159
160 if (strcmp((*match)->name, name) != 0 ||
161 (spec != NULL && strcmp((*match)->spec, spec) != 0))
162 {
163 /*
164 * Nope...
165 */
166
167 ppd->cur_attr = -1;
168 return (NULL);
169 }
170
171 /*
172 * Return the next attribute's value...
173 */
174
175 if ((*match)->value)
176 return ((*match)->value);
177 else
178 return ("");
179 }
180
181
182 /*
183 * End of "$Id: attr.c,v 1.1.2.2 2003/01/07 18:26:23 mike Exp $".
184 */