]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/filter.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / filter.c
index c783b5db74e4bc9876876b234482381c044f669e..92d68f8626f0e67a0aee4be5788eead424b0b871 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * "$Id: filter.c 4970 2006-01-24 14:05:45Z mike $"
+ * "$Id: filter.c 5196 2006-02-27 21:23:00Z mike $"
  *
  *   File type conversion routines for the Common UNIX Printing System (CUPS).
  *
  * Contents:
  *
  *   mimeAddFilter()   - Add a filter to the current MIME database.
- *   mimeFilter()      - Find the fastest way to convert from one type to another.
+ *   mimeFilter()      - Find the fastest way to convert from one type to
+ *                       another.
  *   compare_filters() - Compare two filters...
+ *   find_filters()    - Find the filters to convert from one type to another.
  *   lookup()          - Lookup a filter...
  */
 
 #include "mime.h"
 
 
+/*
+ * Local types...
+ */
+
+typedef struct _mime_typelist_s                /**** List of source types ****/
+{
+  struct _mime_typelist_s *next;       /* Next source type */
+  mime_type_t          *src;           /* Source type */
+} _mime_typelist_t;
+
+
 /*
  * Local functions...
  */
 
 static int             compare_filters(mime_filter_t *, mime_filter_t *);
+static cups_array_t    *find_filters(mime_t *mime, mime_type_t *src,
+                                     mime_type_t *dst, int *cost,
+                                     _mime_typelist_t *visited);
 static mime_filter_t   *lookup(mime_t *, mime_type_t *, mime_type_t *);
 
 
@@ -132,32 +148,77 @@ cups_array_t *                            /* O - Array of filters to run */
 mimeFilter(mime_t      *mime,          /* I - MIME database */
            mime_type_t *src,           /* I - Source file type */
           mime_type_t *dst,            /* I - Destination file type */
-          int         *cost,           /* O - Cost of filters */
-          int         max_depth)       /* I - Maximum depth of search */
+          int         *cost)           /* O - Cost of filters */
 {
-  int          tempcost,               /* Temporary cost */
-               mincost;                /* Current minimum */
-  cups_array_t *temp,                  /* Temporary filter */
-               *mintemp;               /* Current minimum */
-  mime_filter_t        *current;               /* Current filter */
-
-
  /*
   * Range-check the input...
   */
 
   DEBUG_printf(("mimeFilter(mime=%p, src=%p(%s/%s), dst=%p(%s/%s), "
-                "cost=%p(%d), max_depth=%d)\n",
+                "cost=%p(%d))\n",
                mime, src, src ? src->super : "?", src ? src->type : "?",
                dst, dst ? dst->super : "?", dst ? dst->type : "?",
-               cost, cost ? *cost : 0, max_depth));
+               cost, cost ? *cost : 0));
+
 
   if (cost)
     *cost = 0;
 
-  if (!mime || !src || !dst || !cost || max_depth <= 0)
+  if (!mime || !src || !dst)
     return (NULL);
 
+ /*
+  * Find the filters...
+  */
+
+  return (find_filters(mime, src, dst, cost, NULL));
+}
+
+
+/*
+ * 'compare_filters()' - Compare two filters...
+ */
+
+static int                             /* O - Comparison result */
+compare_filters(mime_filter_t *f0,     /* I - First filter */
+                mime_filter_t *f1)     /* I - Second filter */
+{
+  int  i;                              /* Result of comparison */
+
+
+  if ((i = strcmp(f0->src->super, f1->src->super)) == 0)
+    if ((i = strcmp(f0->src->type, f1->src->type)) == 0)
+      if ((i = strcmp(f0->dst->super, f1->dst->super)) == 0)
+        i = strcmp(f0->dst->type, f1->dst->type);
+
+  return (i);
+}
+
+
+/*
+ * 'find_filters()' - Find the filters to convert from one type to another.
+ */
+
+static cups_array_t *                  /* O - Array of filters to run */
+find_filters(mime_t           *mime,   /* I - MIME database */
+             mime_type_t      *src,    /* I - Source file type */
+            mime_type_t      *dst,     /* I - Destination file type */
+            int              *cost,    /* O - Cost of filters */
+            _mime_typelist_t *list)    /* I - Source types we've used */
+{
+  int                  tempcost,       /* Temporary cost */
+                       mincost;        /* Current minimum */
+  cups_array_t         *temp,          /* Temporary filter */
+                       *mintemp;       /* Current minimum */
+  mime_filter_t                *current;       /* Current filter */
+  _mime_typelist_t     listnode,       /* New list node */
+                       *listptr;       /* Pointer in list */
+
+
+  DEBUG_printf(("find_filters(mime=%p, src=%p(%s/%s), dst=%p(%s/%s), "
+                "cost=%p, list=%p)\n", mime, src, src->super, src->type,
+               dst, dst->super, dst->type, cost, list));
+
  /*
   * See if there is a filter that can convert the files directly...
   */
@@ -168,6 +229,8 @@ mimeFilter(mime_t      *mime,               /* I - MIME database */
     * Got a direct filter!
     */
 
+    DEBUG_puts("Direct filter found!");
+
     if ((mintemp = cupsArrayNew(NULL, NULL)) == NULL)
       return (NULL);
 
@@ -176,7 +239,7 @@ mimeFilter(mime_t      *mime,               /* I - MIME database */
     mincost = current->cost;
 
     DEBUG_puts("    Found direct filter:");
-    DEBUG_printf(("    %s (cost=%d)\n", mintemp->filter, mincost));
+    DEBUG_printf(("    %s (cost=%d)\n", current->filter, mincost));
   }
   else
   {
@@ -188,6 +251,12 @@ mimeFilter(mime_t      *mime,              /* I - MIME database */
     mincost = 9999999;
   }
 
+ /*
+  * Initialize this node in the type list...
+  */
+
+  listnode.next = list;
+
  /*
   * OK, now look for filters from the source type to any other type...
   */
@@ -197,13 +266,27 @@ mimeFilter(mime_t      *mime,             /* I - MIME database */
        current = (mime_filter_t *)cupsArrayNext(mime->filters))
     if (current->src == src)
     {
+     /*
+      * See if we have already tried the destination type as a source
+      * type (this avoids extra filter looping...)
+      */
+
+      for (listptr = list; listptr; listptr = listptr->next)
+        if (current->dst == listptr->src)
+         break;
+
+      if (listptr)
+        continue;
+
      /*
       * See if we have any filters that can convert from the destination type
       * of this filter to the final type...
       */
 
+      listnode.src = current->src;
+
       cupsArraySave(mime->filters);
-      temp = mimeFilter(mime, current->dst, dst, &tempcost, max_depth - 1);
+      temp = find_filters(mime, current->dst, dst, &tempcost, &listnode);
       cupsArrayRestore(mime->filters);
 
       if (!temp)
@@ -245,7 +328,8 @@ mimeFilter(mime_t      *mime,               /* I - MIME database */
       printf("    %s\n", current->filter);
 #endif /* DEBUG */
 
-    *cost = mincost;
+    if (cost)
+      *cost = mincost;
 
     return (mintemp);
   }
@@ -256,26 +340,6 @@ mimeFilter(mime_t      *mime,              /* I - MIME database */
 }
 
 
-/*
- * 'compare_filters()' - Compare two filters...
- */
-
-static int                             /* O - Comparison result */
-compare_filters(mime_filter_t *f0,     /* I - First filter */
-                mime_filter_t *f1)     /* I - Second filter */
-{
-  int  i;                              /* Result of comparison */
-
-
-  if ((i = strcmp(f0->src->super, f1->src->super)) == 0)
-    if ((i = strcmp(f0->src->type, f1->src->type)) == 0)
-      if ((i = strcmp(f0->dst->super, f1->dst->super)) == 0)
-        i = strcmp(f0->dst->type, f1->dst->type);
-
-  return (i);
-}
-
-
 /*
  * 'lookup()' - Lookup a filter...
  */
@@ -296,5 +360,5 @@ lookup(mime_t      *mime,           /* I - MIME database */
 
 
 /*
- * End of "$Id: filter.c 4970 2006-01-24 14:05:45Z mike $".
+ * End of "$Id: filter.c 5196 2006-02-27 21:23:00Z mike $".
  */