]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/util.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / util.c
1 /*
2 * "$Id: util.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * Mini-daemon utility functions for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2005 by Easy Software Products.
8 *
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/".
14 *
15 * Contents:
16 *
17 * cupsdCompareNames() - Compare two names.
18 * cupsdSendIPPGroup() - Send a group tag.
19 * cupsdSendIPPHeader() - Send the IPP response header.
20 * cupsdSendIPPInteger() - Send an integer attribute.
21 * cupsdSendIPPString() - Send a string attribute.
22 * cupsdSendIPPTrailer() - Send the end-of-message tag.
23 */
24
25 /*
26 * Include necessary headers...
27 */
28
29 #include "util.h"
30
31
32 /*
33 * 'cupsdCompareNames()' - Compare two names.
34 *
35 * This function basically does a strcasecmp() of the two strings,
36 * but is also aware of numbers so that "a2" < "a100".
37 */
38
39 int /* O - Result of comparison */
40 cupsdCompareNames(const char *s, /* I - First string */
41 const char *t) /* I - Second string */
42 {
43 int diff, /* Difference between digits */
44 digits; /* Number of digits */
45
46
47 /*
48 * Loop through both names, returning only when a difference is
49 * seen. Also, compare whole numbers rather than just characters, too!
50 */
51
52 while (*s && *t)
53 {
54 if (isdigit(*s & 255) && isdigit(*t & 255))
55 {
56 /*
57 * Got a number; start by skipping leading 0's...
58 */
59
60 while (*s == '0')
61 s ++;
62 while (*t == '0')
63 t ++;
64
65 /*
66 * Skip equal digits...
67 */
68
69 while (isdigit(*s & 255) && *s == *t)
70 {
71 s ++;
72 t ++;
73 }
74
75 /*
76 * Bounce out if *s and *t aren't both digits...
77 */
78
79 if (isdigit(*s & 255) && !isdigit(*t & 255))
80 return (1);
81 else if (!isdigit(*s & 255) && isdigit(*t & 255))
82 return (-1);
83 else if (!isdigit(*s & 255) || !isdigit(*t & 255))
84 continue;
85
86 if (*s < *t)
87 diff = -1;
88 else
89 diff = 1;
90
91 /*
92 * Figure out how many more digits there are...
93 */
94
95 digits = 0;
96 s ++;
97 t ++;
98
99 while (isdigit(*s & 255))
100 {
101 digits ++;
102 s ++;
103 }
104
105 while (isdigit(*t & 255))
106 {
107 digits --;
108 t ++;
109 }
110
111 /*
112 * Return if the number or value of the digits is different...
113 */
114
115 if (digits < 0)
116 return (-1);
117 else if (digits > 0)
118 return (1);
119 else if (diff)
120 return (diff);
121 }
122 else if (tolower(*s) < tolower(*t))
123 return (-1);
124 else if (tolower(*s) > tolower(*t))
125 return (1);
126 else
127 {
128 s ++;
129 t ++;
130 }
131 }
132
133 /*
134 * Return the results of the final comparison...
135 */
136
137 if (*s)
138 return (1);
139 else if (*t)
140 return (-1);
141 else
142 return (0);
143 }
144
145
146 /*
147 * 'cupsdSendIPPGroup()' - Send a group tag.
148 */
149
150 void
151 cupsdSendIPPGroup(ipp_tag_t group_tag) /* I - Group tag */
152 {
153 /*
154 * Send IPP group tag (1 byte)...
155 */
156
157 putchar(group_tag);
158 }
159
160
161 /*
162 * 'cupsdSendIPPHeader()' - Send the IPP response header.
163 */
164
165 void
166 cupsdSendIPPHeader(
167 ipp_status_t status_code, /* I - Status code */
168 int request_id) /* I - Request ID */
169 {
170 /*
171 * Send IPP/1.1 response header: version number (2 bytes), status code
172 * (2 bytes), and request ID (4 bytes)...
173 */
174
175 putchar(1);
176 putchar(1);
177
178 putchar(status_code >> 8);
179 putchar(status_code);
180
181 putchar(request_id >> 24);
182 putchar(request_id >> 16);
183 putchar(request_id >> 8);
184 putchar(request_id);
185 }
186
187
188 /*
189 * 'cupsdSendIPPInteger()' - Send an integer attribute.
190 */
191
192 void
193 cupsdSendIPPInteger(
194 ipp_tag_t value_tag, /* I - Value tag */
195 const char *name, /* I - Attribute name */
196 int value) /* I - Attribute value */
197 {
198 size_t len; /* Length of attribute name */
199
200
201 /*
202 * Send IPP integer value: value tag (1 byte), name length (2 bytes),
203 * name string (without nul), value length (2 bytes), and value (4 bytes)...
204 */
205
206 putchar(value_tag);
207
208 len = strlen(name);
209 putchar(len >> 8);
210 putchar(len);
211
212 fputs(name, stdout);
213
214 putchar(0);
215 putchar(4);
216
217 putchar(value >> 24);
218 putchar(value >> 16);
219 putchar(value >> 8);
220 putchar(value);
221 }
222
223
224 /*
225 * 'cupsdSendIPPString()' - Send a string attribute.
226 */
227
228 void
229 cupsdSendIPPString(
230 ipp_tag_t value_tag, /* I - Value tag */
231 const char *name, /* I - Attribute name */
232 const char *value) /* I - Attribute value */
233 {
234 size_t len; /* Length of attribute name */
235
236
237 /*
238 * Send IPP string value: value tag (1 byte), name length (2 bytes),
239 * name string (without nul), value length (2 bytes), and value string
240 * (without nul)...
241 */
242
243 putchar(value_tag);
244
245 len = strlen(name);
246 putchar(len >> 8);
247 putchar(len);
248
249 fputs(name, stdout);
250
251 len = strlen(value);
252 putchar(len >> 8);
253 putchar(len);
254
255 fputs(value, stdout);
256 }
257
258
259 /*
260 * 'cupsdSendIPPTrailer()' - Send the end-of-message tag.
261 */
262
263 void
264 cupsdSendIPPTrailer(void)
265 {
266 putchar(IPP_TAG_END);
267 fflush(stdout);
268 }
269
270
271 /*
272 * End of "$Id: util.c 6649 2007-07-11 21:46:42Z mike $".
273 */