]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/filter.c
2 * "$Id: filter.c,v 1.1 2000/01/25 03:50:48 mike Exp $"
4 * File type conversion routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2000 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-3111 USA
20 * Voice: (301) 373-9603
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 another.
28 * compare() - Compare two filter types...
29 * lookup() - Lookup a filter...
33 * Include necessary headers...
40 #include <cups/string.h>
48 static int compare(mime_filter_t
*, mime_filter_t
*);
49 static mime_filter_t
*lookup(mime_t
*, mime_type_t
*, mime_type_t
*);
53 * 'mimeAddFilter()' - Add a filter to the current MIME database.
56 mime_filter_t
* /* O - New filter */
57 mimeAddFilter(mime_t
*mime
, /* I - MIME database */
58 mime_type_t
*src
, /* I - Source type */
59 mime_type_t
*dst
, /* I - Destination type */
60 int cost
, /* I - Relative time/resource cost */
61 const char *filter
) /* I - Filter program to run */
63 mime_filter_t
*temp
; /* New filter */
67 * Range-check the input...
70 if (mime
== NULL
|| src
== NULL
|| dst
== NULL
|| filter
== NULL
)
73 if (strlen(filter
) > (MIME_MAX_FILTER
- 1))
77 * See if we already have an existing filter for the given source and
81 if ((temp
= lookup(mime
, src
, dst
)) != NULL
)
84 * Yup, does the existing filter have a higher cost? If so, copy the
85 * filter and cost to the existing filter entry and return it...
88 if (temp
->cost
> cost
)
91 strncpy(temp
->filter
, filter
, sizeof(temp
->filter
) - 1);
92 temp
->filter
[sizeof(temp
->filter
) - 1] = '\0';
98 * Nope, add a new one...
101 if (mime
->num_filters
== 0)
102 temp
= malloc(sizeof(mime_filter_t
));
104 temp
= realloc(mime
->filters
, sizeof(mime_filter_t
) * (mime
->num_filters
+ 1));
109 mime
->filters
= temp
;
110 temp
+= mime
->num_filters
;
111 mime
->num_filters
++;
114 * Copy the information over and sort if necessary...
120 strncpy(temp
->filter
, filter
, sizeof(temp
->filter
) - 1);
121 temp
->filter
[sizeof(temp
->filter
) - 1] = '\0';
123 if (mime
->num_filters
> 1)
124 qsort(mime
->filters
, mime
->num_filters
, sizeof(mime_filter_t
),
125 (int (*)(const void *, const void *))compare
);
129 * Return the new/updated filter...
137 * 'mimeFilter()' - Find the fastest way to convert from one type to another.
140 mime_filter_t
* /* O - Array of filters to run */
141 mimeFilter(mime_t
*mime
, /* I - MIME database */
142 mime_type_t
*src
, /* I - Source file type */
143 mime_type_t
*dst
, /* I - Destination file type */
144 int *num_filters
) /* O - Number of filters to run */
146 int i
, j
, /* Looping vars */
147 num_temp
, /* Number of temporary filters */
148 num_mintemp
, /* Number of filters in the minimum */
149 cost
, /* Current cost */
150 mincost
; /* Current minimum */
151 mime_filter_t
*temp
, /* Temporary filter */
152 *mintemp
, /* Current minimum */
153 *mincurrent
, /* Current filter for minimum */
154 *current
, /* Current filter */
155 *filters
; /* Filters to use */
159 * Range-check the input...
162 if (mime
== NULL
|| src
== NULL
|| dst
== NULL
|| num_filters
== NULL
)
168 * See if there is a filter that can convert the files directly...
171 if ((temp
= lookup(mime
, src
, dst
)) != NULL
)
174 * Got a direct filter!
177 if ((filters
= (mime_filter_t
*)malloc(sizeof(mime_filter_t
))) == NULL
)
180 memcpy(filters
, temp
, sizeof(mime_filter_t
));
186 * OK, now look for filters from the source type to any other type...
192 for (i
= mime
->num_filters
, current
= mime
->filters
; i
> 0; i
--, current
++)
193 if (current
->src
== src
)
196 * See if we have any filters that can convert from the destination type
197 * of this filter to the final type...
200 if ((temp
= mimeFilter(mime
, current
->dst
, dst
, &num_temp
)) == NULL
)
204 * Found a match; see if this one is less costly than the last (if
208 for (j
= 0, cost
= 0; j
< num_temp
; j
++)
218 num_mintemp
= num_temp
;
219 mincurrent
= current
;
228 * Hey, we got a match! Add the current filter to the beginning of the
232 filters
= (mime_filter_t
*)realloc(mintemp
, sizeof(mime_filter_t
) *
241 memmove(filters
+ 1, filters
, num_mintemp
* sizeof(mime_filter_t
));
242 memcpy(filters
, mincurrent
, sizeof(mime_filter_t
));
244 *num_filters
= num_mintemp
+ 1;
254 * 'compare()' - Compare two filter types...
257 static int /* O - Comparison result */
258 compare(mime_filter_t
*f0
, /* I - First filter */
259 mime_filter_t
*f1
) /* I - Second filter */
261 int i
; /* Result of comparison */
264 if ((i
= strcmp(f0
->src
->super
, f1
->src
->super
)) == 0)
265 if ((i
= strcmp(f0
->src
->type
, f1
->src
->type
)) == 0)
266 if ((i
= strcmp(f0
->dst
->super
, f1
->dst
->super
)) == 0)
267 i
= strcmp(f0
->dst
->type
, f1
->dst
->type
);
274 * 'lookup()' - Lookup a filter...
277 static mime_filter_t
* /* O - Filter for src->dst */
278 lookup(mime_t
*mime
, /* I - MIME database */
279 mime_type_t
*src
, /* I - Source type */
280 mime_type_t
*dst
) /* I - Destination type */
282 mime_filter_t key
; /* Key record for filter search */
285 if (mime
->num_filters
== 0)
291 return ((mime_filter_t
*)bsearch(&key
, mime
->filters
, mime
->num_filters
,
292 sizeof(mime_filter_t
),
293 (int (*)(const void *, const void *))compare
));
298 * End of "$Id: filter.c,v 1.1 2000/01/25 03:50:48 mike Exp $".