2 * "$Id: filter.c 5196 2006-02-27 21:23:00Z mike $"
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 cups_array_t
*find_filters(mime_t
*mime
, mime_type_t
*src
,
64 mime_type_t
*dst
, int *cost
,
65 _mime_typelist_t
*visited
);
66 static mime_filter_t
*lookup(mime_t
*, mime_type_t
*, mime_type_t
*);
70 * 'mimeAddFilter()' - Add a filter to the current MIME database.
73 mime_filter_t
* /* O - New filter */
74 mimeAddFilter(mime_t
*mime
, /* I - MIME database */
75 mime_type_t
*src
, /* I - Source type */
76 mime_type_t
*dst
, /* I - Destination type */
77 int cost
, /* I - Relative time/resource cost */
78 const char *filter
) /* I - Filter program to run */
80 mime_filter_t
*temp
; /* New filter */
84 * Range-check the input...
87 if (!mime
|| !src
|| !dst
|| !filter
)
91 * See if we already have an existing filter for the given source and
95 if ((temp
= lookup(mime
, src
, dst
)) != NULL
)
98 * Yup, does the existing filter have a higher cost? If so, copy the
99 * filter and cost to the existing filter entry and return it...
102 if (temp
->cost
> cost
)
105 strlcpy(temp
->filter
, filter
, sizeof(temp
->filter
));
111 * Nope, add a new one...
115 mime
->filters
= cupsArrayNew((cups_array_func_t
)compare_filters
, NULL
);
120 if ((temp
= calloc(1, sizeof(mime_filter_t
))) == NULL
)
124 * Copy the information over and sort if necessary...
130 strlcpy(temp
->filter
, filter
, sizeof(temp
->filter
));
132 cupsArrayAdd(mime
->filters
, temp
);
136 * Return the new/updated filter...
144 * 'mimeFilter()' - Find the fastest way to convert from one type to another.
147 cups_array_t
* /* O - Array of filters to run */
148 mimeFilter(mime_t
*mime
, /* I - MIME database */
149 mime_type_t
*src
, /* I - Source file type */
150 mime_type_t
*dst
, /* I - Destination file type */
151 int *cost
) /* O - Cost of filters */
154 * Range-check the input...
157 DEBUG_printf(("mimeFilter(mime=%p, src=%p(%s/%s), dst=%p(%s/%s), "
159 mime
, src
, src
? src
->super
: "?", src
? src
->type
: "?",
160 dst
, dst
? dst
->super
: "?", dst
? dst
->type
: "?",
161 cost
, cost
? *cost
: 0));
167 if (!mime
|| !src
|| !dst
)
171 * Find the filters...
174 return (find_filters(mime
, src
, dst
, cost
, NULL
));
179 * 'compare_filters()' - Compare two filters...
182 static int /* O - Comparison result */
183 compare_filters(mime_filter_t
*f0
, /* I - First filter */
184 mime_filter_t
*f1
) /* I - Second filter */
186 int i
; /* Result of comparison */
189 if ((i
= strcmp(f0
->src
->super
, f1
->src
->super
)) == 0)
190 if ((i
= strcmp(f0
->src
->type
, f1
->src
->type
)) == 0)
191 if ((i
= strcmp(f0
->dst
->super
, f1
->dst
->super
)) == 0)
192 i
= strcmp(f0
->dst
->type
, f1
->dst
->type
);
199 * 'find_filters()' - Find the filters to convert from one type to another.
202 static cups_array_t
* /* O - Array of filters to run */
203 find_filters(mime_t
*mime
, /* I - MIME database */
204 mime_type_t
*src
, /* I - Source file type */
205 mime_type_t
*dst
, /* I - Destination file type */
206 int *cost
, /* O - Cost of filters */
207 _mime_typelist_t
*list
) /* I - Source types we've used */
209 int tempcost
, /* Temporary cost */
210 mincost
; /* Current minimum */
211 cups_array_t
*temp
, /* Temporary filter */
212 *mintemp
; /* Current minimum */
213 mime_filter_t
*current
; /* Current filter */
214 _mime_typelist_t listnode
, /* New list node */
215 *listptr
; /* Pointer in list */
218 DEBUG_printf(("find_filters(mime=%p, src=%p(%s/%s), dst=%p(%s/%s), "
219 "cost=%p, list=%p)\n", mime
, src
, src
->super
, src
->type
,
220 dst
, dst
->super
, dst
->type
, cost
, list
));
223 * See if there is a filter that can convert the files directly...
226 if ((current
= lookup(mime
, src
, dst
)) != NULL
)
229 * Got a direct filter!
232 DEBUG_puts("Direct filter found!");
234 if ((mintemp
= cupsArrayNew(NULL
, NULL
)) == NULL
)
237 cupsArrayAdd(mintemp
, current
);
239 mincost
= current
->cost
;
241 DEBUG_puts(" Found direct filter:");
242 DEBUG_printf((" %s (cost=%d)\n", current
->filter
, mincost
));
247 * No direct filter...
255 * Initialize this node in the type list...
258 listnode
.next
= list
;
261 * OK, now look for filters from the source type to any other type...
264 for (current
= (mime_filter_t
*)cupsArrayFirst(mime
->filters
);
266 current
= (mime_filter_t
*)cupsArrayNext(mime
->filters
))
267 if (current
->src
== src
)
270 * See if we have already tried the destination type as a source
271 * type (this avoids extra filter looping...)
274 for (listptr
= list
; listptr
; listptr
= listptr
->next
)
275 if (current
->dst
== listptr
->src
)
282 * See if we have any filters that can convert from the destination type
283 * of this filter to the final type...
286 listnode
.src
= current
->src
;
288 cupsArraySave(mime
->filters
);
289 temp
= find_filters(mime
, current
->dst
, dst
, &tempcost
, &listnode
);
290 cupsArrayRestore(mime
->filters
);
296 * Found a match; see if this one is less costly than the last (if
300 if (tempcost
< mincost
)
302 cupsArrayDelete(mintemp
);
305 * Hey, we got a match! Add the current filter to the beginning of the
310 mincost
= tempcost
+ current
->cost
;
311 cupsArrayInsert(mintemp
, current
);
314 cupsArrayDelete(temp
);
320 * Hey, we got a match!
324 printf(" Returning %d filters:\n", cupsArrayCount(mintemp
));
325 for (current
= (mime_filter_t
*)cupsArrayFirst(mintemp
);
327 current
= (mime_filter_t
*)cupsArrayNext(mintemp
))
328 printf(" %s\n", current
->filter
);
337 DEBUG_puts(" Returning zippo...");
344 * 'lookup()' - Lookup a filter...
347 static mime_filter_t
* /* O - Filter for src->dst */
348 lookup(mime_t
*mime
, /* I - MIME database */
349 mime_type_t
*src
, /* I - Source type */
350 mime_type_t
*dst
) /* I - Destination type */
352 mime_filter_t key
; /* Key record for filter search */
358 return ((mime_filter_t
*)cupsArrayFind(mime
->filters
, &key
));
363 * End of "$Id: filter.c 5196 2006-02-27 21:23:00Z mike $".