4 * File type conversion routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * mimeAddFilter() - Add a filter to the current MIME database.
27 * mimeFilter() - Find the fastest way to convert from one type to
29 * compare_filters() - Compare two filters...
30 * find_filters() - Find the filters to convert from one type to another.
31 * lookup() - Lookup a filter...
35 * Include necessary headers...
42 #include <cups/debug.h>
43 #include <cups/string.h>
51 typedef struct _mime_typelist_s
/**** List of source types ****/
53 struct _mime_typelist_s
*next
; /* Next source type */
54 mime_type_t
*src
; /* Source type */
62 static int compare_filters(mime_filter_t
*, mime_filter_t
*);
63 static int compare_srcs(mime_filter_t
*, mime_filter_t
*);
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
);
67 static mime_filter_t
*lookup(mime_t
*, mime_type_t
*, mime_type_t
*);
71 * 'mimeAddFilter()' - Add a filter to the current MIME database.
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 */
81 mime_filter_t
*temp
; /* New filter */
85 * Range-check the input...
88 if (!mime
|| !src
|| !dst
|| !filter
)
92 * See if we already have an existing filter for the given source and
96 if ((temp
= lookup(mime
, src
, dst
)) != NULL
)
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...
103 if (temp
->cost
> cost
)
106 strlcpy(temp
->filter
, filter
, sizeof(temp
->filter
));
112 * Nope, add a new one...
116 mime
->filters
= cupsArrayNew((cups_array_func_t
)compare_filters
, NULL
);
121 if ((temp
= calloc(1, sizeof(mime_filter_t
))) == NULL
)
125 * Copy the information over and sort if necessary...
131 strlcpy(temp
->filter
, filter
, sizeof(temp
->filter
));
133 cupsArrayAdd(mime
->filters
, temp
);
137 * Return the new/updated filter...
145 * 'mimeFilter()' - Find the fastest way to convert from one type to another.
148 cups_array_t
* /* O - Array of filters to run */
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 */
152 int *cost
) /* O - Cost of filters */
155 * Range-check the input...
158 DEBUG_printf(("mimeFilter(mime=%p, src=%p(%s/%s), dst=%p(%s/%s), "
160 mime
, src
, src
? src
->super
: "?", src
? src
->type
: "?",
161 dst
, dst
? dst
->super
: "?", dst
? dst
->type
: "?",
162 cost
, cost
? *cost
: 0));
168 if (!mime
|| !src
|| !dst
)
172 * (Re)build the source lookup array as needed...
177 mime_filter_t
*current
; /* Current filter */
180 mime
->srcs
= cupsArrayNew((cups_array_func_t
)compare_srcs
, NULL
);
182 for (current
= mimeFirstFilter(mime
);
184 current
= mimeNextFilter(mime
))
185 cupsArrayAdd(mime
->srcs
, current
);
189 * Find the filters...
192 return (find_filters(mime
, src
, dst
, cost
, NULL
));
197 * 'compare_filters()' - Compare two filters...
200 static int /* O - Comparison result */
201 compare_filters(mime_filter_t
*f0
, /* I - First filter */
202 mime_filter_t
*f1
) /* I - Second filter */
204 int i
; /* Result of comparison */
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
);
217 * 'compare_srcs()' - Compare two srcs...
220 static int /* O - Comparison result */
221 compare_srcs(mime_filter_t
*f0
, /* I - First filter */
222 mime_filter_t
*f1
) /* I - Second filter */
224 int i
; /* Result of comparison */
227 if ((i
= strcmp(f0
->src
->super
, f1
->src
->super
)) == 0)
228 i
= strcmp(f0
->src
->type
, f1
->src
->type
);
235 * 'find_filters()' - Find the filters to convert from one type to another.
238 static cups_array_t
* /* O - Array of filters to run */
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 */
245 int tempcost
, /* Temporary cost */
246 mincost
; /* Current minimum */
247 cups_array_t
*temp
, /* Temporary filter */
248 *mintemp
; /* Current minimum */
249 mime_filter_t
*current
, /* Current filter */
250 srckey
; /* Source type key */
251 _mime_typelist_t listnode
, /* New list node */
252 *listptr
; /* Pointer in list */
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
));
260 * See if there is a filter that can convert the files directly...
263 if ((current
= lookup(mime
, src
, dst
)) != NULL
)
266 * Got a direct filter!
269 DEBUG_puts("Direct filter found!");
271 if ((mintemp
= cupsArrayNew(NULL
, NULL
)) == NULL
)
274 cupsArrayAdd(mintemp
, current
);
276 mincost
= current
->cost
;
281 DEBUG_puts(" Found direct filter:");
282 DEBUG_printf((" %s (cost=%d)\n", current
->filter
, mincost
));
287 * No direct filter...
295 * Initialize this node in the type list...
298 listnode
.next
= list
;
301 * OK, now look for filters from the source type to any other type...
306 for (current
= (mime_filter_t
*)cupsArrayFind(mime
->srcs
, &srckey
);
307 current
&& current
->src
== src
;
308 current
= (mime_filter_t
*)cupsArrayNext(mime
->srcs
))
311 * See if we have already tried the destination type as a source
312 * type (this avoids extra filter looping...)
315 mime_type_t
*current_dst
; /* Current destination type */
318 for (listptr
= list
, current_dst
= current
->dst
;
320 listptr
= listptr
->next
)
321 if (current_dst
== listptr
->src
)
328 * See if we have any filters that can convert from the destination type
329 * of this filter to the final type...
332 listnode
.src
= current
->src
;
334 cupsArraySave(mime
->srcs
);
335 temp
= find_filters(mime
, current
->dst
, dst
, &tempcost
, &listnode
);
336 cupsArrayRestore(mime
->srcs
);
345 * Found a match; see if this one is less costly than the last (if
349 tempcost
+= current
->cost
;
351 if (tempcost
< mincost
)
353 cupsArrayDelete(mintemp
);
356 * Hey, we got a match! Add the current filter to the beginning of the
362 cupsArrayInsert(mintemp
, current
);
365 cupsArrayDelete(temp
);
371 * Hey, we got a match!
375 printf(" Returning %d filters:\n", cupsArrayCount(mintemp
));
376 for (current
= (mime_filter_t
*)cupsArrayFirst(mintemp
);
378 current
= (mime_filter_t
*)cupsArrayNext(mintemp
))
379 printf(" %s\n", current
->filter
);
388 DEBUG_puts(" Returning zippo...");
395 * 'lookup()' - Lookup a filter...
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 */
403 mime_filter_t key
; /* Key record for filter search */
409 return ((mime_filter_t
*)cupsArrayFind(mime
->filters
, &key
));