]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7faf1f5 | 2 | * "$Id: filter.c 5606 2006-05-30 19:40:34Z mike $" |
ef416fc2 | 3 | * |
4 | * File type conversion routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
fa73b229 | 6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. |
ef416fc2 | 7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
fa73b229 | 26 | * mimeAddFilter() - Add a filter to the current MIME database. |
bd7854cb | 27 | * mimeFilter() - Find the fastest way to convert from one type to |
28 | * another. | |
fa73b229 | 29 | * compare_filters() - Compare two filters... |
bd7854cb | 30 | * find_filters() - Find the filters to convert from one type to another. |
fa73b229 | 31 | * lookup() - Lookup a filter... |
ef416fc2 | 32 | */ |
33 | ||
34 | /* | |
35 | * Include necessary headers... | |
36 | */ | |
37 | ||
38 | #include <stdio.h> | |
39 | #include <stdlib.h> | |
40 | #include <ctype.h> | |
41 | ||
42 | #include <cups/debug.h> | |
43 | #include <cups/string.h> | |
44 | #include "mime.h" | |
45 | ||
46 | ||
bd7854cb | 47 | /* |
48 | * Local types... | |
49 | */ | |
50 | ||
51 | typedef struct _mime_typelist_s /**** List of source types ****/ | |
52 | { | |
53 | struct _mime_typelist_s *next; /* Next source type */ | |
54 | mime_type_t *src; /* Source type */ | |
55 | } _mime_typelist_t; | |
56 | ||
57 | ||
ef416fc2 | 58 | /* |
59 | * Local functions... | |
60 | */ | |
61 | ||
fa73b229 | 62 | static int compare_filters(mime_filter_t *, mime_filter_t *); |
a74454a7 | 63 | static int compare_srcs(mime_filter_t *, mime_filter_t *); |
bd7854cb | 64 | static cups_array_t *find_filters(mime_t *mime, mime_type_t *src, |
65 | mime_type_t *dst, int *cost, | |
66 | _mime_typelist_t *visited); | |
ef416fc2 | 67 | static mime_filter_t *lookup(mime_t *, mime_type_t *, mime_type_t *); |
68 | ||
69 | ||
70 | /* | |
71 | * 'mimeAddFilter()' - Add a filter to the current MIME database. | |
72 | */ | |
73 | ||
74 | mime_filter_t * /* O - New filter */ | |
75 | mimeAddFilter(mime_t *mime, /* I - MIME database */ | |
76 | mime_type_t *src, /* I - Source type */ | |
77 | mime_type_t *dst, /* I - Destination type */ | |
78 | int cost, /* I - Relative time/resource cost */ | |
79 | const char *filter) /* I - Filter program to run */ | |
80 | { | |
81 | mime_filter_t *temp; /* New filter */ | |
82 | ||
83 | ||
84 | /* | |
85 | * Range-check the input... | |
86 | */ | |
87 | ||
fa73b229 | 88 | if (!mime || !src || !dst || !filter) |
ef416fc2 | 89 | return (NULL); |
90 | ||
91 | /* | |
92 | * See if we already have an existing filter for the given source and | |
93 | * destination... | |
94 | */ | |
95 | ||
96 | if ((temp = lookup(mime, src, dst)) != NULL) | |
97 | { | |
98 | /* | |
99 | * Yup, does the existing filter have a higher cost? If so, copy the | |
100 | * filter and cost to the existing filter entry and return it... | |
101 | */ | |
102 | ||
103 | if (temp->cost > cost) | |
104 | { | |
105 | temp->cost = cost; | |
106 | strlcpy(temp->filter, filter, sizeof(temp->filter)); | |
107 | } | |
108 | } | |
109 | else | |
110 | { | |
111 | /* | |
112 | * Nope, add a new one... | |
113 | */ | |
114 | ||
fa73b229 | 115 | if (!mime->filters) |
116 | mime->filters = cupsArrayNew((cups_array_func_t)compare_filters, NULL); | |
ef416fc2 | 117 | |
fa73b229 | 118 | if (!mime->filters) |
ef416fc2 | 119 | return (NULL); |
120 | ||
fa73b229 | 121 | if ((temp = calloc(1, sizeof(mime_filter_t))) == NULL) |
122 | return (NULL); | |
ef416fc2 | 123 | |
124 | /* | |
125 | * Copy the information over and sort if necessary... | |
126 | */ | |
127 | ||
128 | temp->src = src; | |
129 | temp->dst = dst; | |
130 | temp->cost = cost; | |
131 | strlcpy(temp->filter, filter, sizeof(temp->filter)); | |
132 | ||
fa73b229 | 133 | cupsArrayAdd(mime->filters, temp); |
ef416fc2 | 134 | } |
135 | ||
136 | /* | |
137 | * Return the new/updated filter... | |
138 | */ | |
139 | ||
140 | return (temp); | |
141 | } | |
142 | ||
143 | ||
144 | /* | |
145 | * 'mimeFilter()' - Find the fastest way to convert from one type to another. | |
146 | */ | |
147 | ||
fa73b229 | 148 | cups_array_t * /* O - Array of filters to run */ |
ef416fc2 | 149 | mimeFilter(mime_t *mime, /* I - MIME database */ |
150 | mime_type_t *src, /* I - Source file type */ | |
151 | mime_type_t *dst, /* I - Destination file type */ | |
bd7854cb | 152 | int *cost) /* O - Cost of filters */ |
ef416fc2 | 153 | { |
ef416fc2 | 154 | /* |
155 | * Range-check the input... | |
156 | */ | |
157 | ||
fa73b229 | 158 | DEBUG_printf(("mimeFilter(mime=%p, src=%p(%s/%s), dst=%p(%s/%s), " |
bd7854cb | 159 | "cost=%p(%d))\n", |
ef416fc2 | 160 | mime, src, src ? src->super : "?", src ? src->type : "?", |
161 | dst, dst ? dst->super : "?", dst ? dst->type : "?", | |
bd7854cb | 162 | cost, cost ? *cost : 0)); |
163 | ||
ef416fc2 | 164 | |
fa73b229 | 165 | if (cost) |
166 | *cost = 0; | |
ef416fc2 | 167 | |
bd7854cb | 168 | if (!mime || !src || !dst) |
fa73b229 | 169 | return (NULL); |
ef416fc2 | 170 | |
a74454a7 | 171 | /* |
172 | * (Re)build the source lookup array as needed... | |
173 | */ | |
174 | ||
175 | if (!mime->srcs) | |
176 | { | |
177 | mime_filter_t *current; /* Current filter */ | |
178 | ||
179 | ||
180 | mime->srcs = cupsArrayNew((cups_array_func_t)compare_srcs, NULL); | |
181 | ||
182 | for (current = mimeFirstFilter(mime); | |
183 | current; | |
184 | current = mimeNextFilter(mime)) | |
185 | cupsArrayAdd(mime->srcs, current); | |
186 | } | |
187 | ||
bd7854cb | 188 | /* |
189 | * Find the filters... | |
190 | */ | |
191 | ||
192 | return (find_filters(mime, src, dst, cost, NULL)); | |
193 | } | |
194 | ||
195 | ||
196 | /* | |
197 | * 'compare_filters()' - Compare two filters... | |
198 | */ | |
199 | ||
200 | static int /* O - Comparison result */ | |
201 | compare_filters(mime_filter_t *f0, /* I - First filter */ | |
202 | mime_filter_t *f1) /* I - Second filter */ | |
203 | { | |
204 | int i; /* Result of comparison */ | |
205 | ||
206 | ||
207 | if ((i = strcmp(f0->src->super, f1->src->super)) == 0) | |
208 | if ((i = strcmp(f0->src->type, f1->src->type)) == 0) | |
209 | if ((i = strcmp(f0->dst->super, f1->dst->super)) == 0) | |
210 | i = strcmp(f0->dst->type, f1->dst->type); | |
211 | ||
212 | return (i); | |
213 | } | |
214 | ||
215 | ||
a74454a7 | 216 | /* |
217 | * 'compare_srcs()' - Compare two srcs... | |
218 | */ | |
219 | ||
220 | static int /* O - Comparison result */ | |
221 | compare_srcs(mime_filter_t *f0, /* I - First filter */ | |
222 | mime_filter_t *f1) /* I - Second filter */ | |
223 | { | |
224 | int i; /* Result of comparison */ | |
225 | ||
226 | ||
227 | if ((i = strcmp(f0->src->super, f1->src->super)) == 0) | |
228 | i = strcmp(f0->src->type, f1->src->type); | |
229 | ||
230 | return (i); | |
231 | } | |
232 | ||
233 | ||
bd7854cb | 234 | /* |
235 | * 'find_filters()' - Find the filters to convert from one type to another. | |
236 | */ | |
237 | ||
b423cd4c | 238 | static cups_array_t * /* O - Array of filters to run */ |
bd7854cb | 239 | find_filters(mime_t *mime, /* I - MIME database */ |
240 | mime_type_t *src, /* I - Source file type */ | |
241 | mime_type_t *dst, /* I - Destination file type */ | |
242 | int *cost, /* O - Cost of filters */ | |
243 | _mime_typelist_t *list) /* I - Source types we've used */ | |
244 | { | |
245 | int tempcost, /* Temporary cost */ | |
246 | mincost; /* Current minimum */ | |
247 | cups_array_t *temp, /* Temporary filter */ | |
248 | *mintemp; /* Current minimum */ | |
a74454a7 | 249 | mime_filter_t *current, /* Current filter */ |
250 | srckey; /* Source type key */ | |
bd7854cb | 251 | _mime_typelist_t listnode, /* New list node */ |
252 | *listptr; /* Pointer in list */ | |
253 | ||
254 | ||
255 | DEBUG_printf(("find_filters(mime=%p, src=%p(%s/%s), dst=%p(%s/%s), " | |
256 | "cost=%p, list=%p)\n", mime, src, src->super, src->type, | |
257 | dst, dst->super, dst->type, cost, list)); | |
258 | ||
ef416fc2 | 259 | /* |
260 | * See if there is a filter that can convert the files directly... | |
261 | */ | |
262 | ||
fa73b229 | 263 | if ((current = lookup(mime, src, dst)) != NULL) |
ef416fc2 | 264 | { |
265 | /* | |
266 | * Got a direct filter! | |
267 | */ | |
268 | ||
bd7854cb | 269 | DEBUG_puts("Direct filter found!"); |
270 | ||
fa73b229 | 271 | if ((mintemp = cupsArrayNew(NULL, NULL)) == NULL) |
ef416fc2 | 272 | return (NULL); |
273 | ||
fa73b229 | 274 | cupsArrayAdd(mintemp, current); |
275 | ||
276 | mincost = current->cost; | |
ef416fc2 | 277 | |
a74454a7 | 278 | if (!cost) |
279 | return (mintemp); | |
280 | ||
ef416fc2 | 281 | DEBUG_puts(" Found direct filter:"); |
bd7854cb | 282 | DEBUG_printf((" %s (cost=%d)\n", current->filter, mincost)); |
ef416fc2 | 283 | } |
284 | else | |
285 | { | |
286 | /* | |
287 | * No direct filter... | |
288 | */ | |
289 | ||
fa73b229 | 290 | mintemp = NULL; |
291 | mincost = 9999999; | |
ef416fc2 | 292 | } |
293 | ||
bd7854cb | 294 | /* |
295 | * Initialize this node in the type list... | |
296 | */ | |
297 | ||
298 | listnode.next = list; | |
299 | ||
ef416fc2 | 300 | /* |
301 | * OK, now look for filters from the source type to any other type... | |
302 | */ | |
303 | ||
a74454a7 | 304 | srckey.src = src; |
bd7854cb | 305 | |
a74454a7 | 306 | for (current = (mime_filter_t *)cupsArrayFind(mime->srcs, &srckey); |
307 | current && current->src == src; | |
308 | current = (mime_filter_t *)cupsArrayNext(mime->srcs)) | |
309 | { | |
310 | /* | |
311 | * See if we have already tried the destination type as a source | |
312 | * type (this avoids extra filter looping...) | |
313 | */ | |
bd7854cb | 314 | |
a74454a7 | 315 | mime_type_t *current_dst; /* Current destination type */ |
bd7854cb | 316 | |
ef416fc2 | 317 | |
a74454a7 | 318 | for (listptr = list, current_dst = current->dst; |
319 | listptr; | |
320 | listptr = listptr->next) | |
321 | if (current_dst == listptr->src) | |
322 | break; | |
323 | ||
324 | if (listptr) | |
325 | continue; | |
bd7854cb | 326 | |
a74454a7 | 327 | /* |
328 | * See if we have any filters that can convert from the destination type | |
329 | * of this filter to the final type... | |
330 | */ | |
331 | ||
332 | listnode.src = current->src; | |
fa73b229 | 333 | |
a74454a7 | 334 | cupsArraySave(mime->srcs); |
335 | temp = find_filters(mime, current->dst, dst, &tempcost, &listnode); | |
336 | cupsArrayRestore(mime->srcs); | |
337 | ||
338 | if (!temp) | |
339 | continue; | |
340 | ||
341 | if (!cost) | |
342 | return (temp); | |
343 | ||
344 | /* | |
345 | * Found a match; see if this one is less costly than the last (if | |
346 | * any...) | |
347 | */ | |
348 | ||
ed486911 | 349 | tempcost += current->cost; |
350 | ||
a74454a7 | 351 | if (tempcost < mincost) |
352 | { | |
353 | cupsArrayDelete(mintemp); | |
ef416fc2 | 354 | |
355 | /* | |
a74454a7 | 356 | * Hey, we got a match! Add the current filter to the beginning of the |
357 | * filter list... | |
ef416fc2 | 358 | */ |
359 | ||
a74454a7 | 360 | mintemp = temp; |
ed486911 | 361 | mincost = tempcost; |
a74454a7 | 362 | cupsArrayInsert(mintemp, current); |
ef416fc2 | 363 | } |
a74454a7 | 364 | else |
365 | cupsArrayDelete(temp); | |
366 | } | |
ef416fc2 | 367 | |
fa73b229 | 368 | if (mintemp) |
ef416fc2 | 369 | { |
370 | /* | |
371 | * Hey, we got a match! | |
372 | */ | |
373 | ||
ef416fc2 | 374 | #ifdef DEBUG |
fa73b229 | 375 | printf(" Returning %d filters:\n", cupsArrayCount(mintemp)); |
376 | for (current = (mime_filter_t *)cupsArrayFirst(mintemp); | |
377 | current; | |
378 | current = (mime_filter_t *)cupsArrayNext(mintemp)) | |
379 | printf(" %s\n", current->filter); | |
ef416fc2 | 380 | #endif /* DEBUG */ |
381 | ||
bd7854cb | 382 | if (cost) |
383 | *cost = mincost; | |
fa73b229 | 384 | |
ef416fc2 | 385 | return (mintemp); |
386 | } | |
387 | ||
388 | DEBUG_puts(" Returning zippo..."); | |
389 | ||
390 | return (NULL); | |
391 | } | |
392 | ||
393 | ||
ef416fc2 | 394 | /* |
395 | * 'lookup()' - Lookup a filter... | |
396 | */ | |
397 | ||
fa73b229 | 398 | static mime_filter_t * /* O - Filter for src->dst */ |
399 | lookup(mime_t *mime, /* I - MIME database */ | |
400 | mime_type_t *src, /* I - Source type */ | |
401 | mime_type_t *dst) /* I - Destination type */ | |
ef416fc2 | 402 | { |
fa73b229 | 403 | mime_filter_t key; /* Key record for filter search */ |
ef416fc2 | 404 | |
405 | ||
ef416fc2 | 406 | key.src = src; |
407 | key.dst = dst; | |
408 | ||
fa73b229 | 409 | return ((mime_filter_t *)cupsArrayFind(mime->filters, &key)); |
ef416fc2 | 410 | } |
411 | ||
412 | ||
413 | /* | |
f7faf1f5 | 414 | * End of "$Id: filter.c 5606 2006-05-30 19:40:34Z mike $". |
ef416fc2 | 415 | */ |