]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/banners.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / banners.c
index 98c3364eacf1caf1dfbe088b71dd008e69731774..47b56d3b11da2a791790193a1366884983a2a0c6 100644 (file)
@@ -1,32 +1,24 @@
 /*
- * "$Id: banners.c 4719 2005-09-28 21:12:44Z mike $"
+ * "$Id: banners.c 6649 2007-07-11 21:46:42Z mike $"
  *
  *   Banner routines for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 1997-2005 by Easy Software Products.
+ *   Copyright 2007 by Apple Inc.
+ *   Copyright 1997-2006 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
- *   "LICENSE.txt" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  * Contents:
  *
- *   cupsdAddBanner()   - Add a banner to the array.
  *   cupsdFindBanner()  - Find a named banner.
  *   cupsdLoadBanners() - Load all available banner files...
- *   compare()          - Compare two banners.
+ *   add_banner()       - Add a banner to the array.
+ *   compare_banners()  - Compare two banners.
+ *   free_banners()     - Free all banners.
  */
 
 /*
  * Local functions...
  */
 
-static int     compare(const cupsd_banner_t *b0, const cupsd_banner_t *b1);
-
-
-/*
- * 'cupsdAddBanner()' - Add a banner to the array.
- */
-
-void
-cupsdAddBanner(const char *name,       /* I - Name of banner */
-               const char *filename)   /* I - Filename for banner */
-{
-  mime_type_t          *filetype;      /* Filetype */
-  cupsd_banner_t       *temp;          /* New banner data */
-
-
- /*
-  * See what the filetype is...
-  */
-
-  if ((filetype = mimeFileType(MimeDatabase, filename, NULL)) == NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_WARN,
-                    "cupsdAddBanner: Banner \"%s\" (\"%s\") is of an unknown file type - skipping!",
-                    name, filename);
-    return;
-  }
-
- /*
-  * Allocate memory...
-  */
-
-  if (NumBanners == 0)
-    temp = malloc(sizeof(cupsd_banner_t));
-  else
-    temp = realloc(Banners, sizeof(cupsd_banner_t) * (NumBanners + 1));
-
-  if (temp == NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "cupsdAddBanner: Ran out of memory adding a banner!");
-    return;
-  }
-
- /*
-  * Copy the new banner data over...
-  */
-
-  Banners = temp;
-  temp    += NumBanners;
-  NumBanners ++;
-
-  memset(temp, 0, sizeof(cupsd_banner_t));
-  strlcpy(temp->name, name, sizeof(temp->name));
-  temp->filetype = filetype;
-}
+static void    add_banner(const char *name, const char *filename);
+static int     compare_banners(const cupsd_banner_t *b0,
+                               const cupsd_banner_t *b1);
+static void    free_banners(void);
 
 
 /*
@@ -108,10 +49,9 @@ cupsdFindBanner(const char *name)   /* I - Name of banner */
   cupsd_banner_t       key;            /* Search key */
 
 
-  strlcpy(key.name, name, sizeof(key.name));
+  key.name = (char *)name;
 
-  return ((cupsd_banner_t *)bsearch(&key, Banners, NumBanners, sizeof(cupsd_banner_t),
-                              (int (*)(const void *, const void *))compare));
+  return ((cupsd_banner_t *)cupsArrayFind(Banners, &key));
 }
 
 
@@ -132,11 +72,7 @@ cupsdLoadBanners(const char *d)             /* I - Directory to search */
   * Free old banner info...
   */
 
-  if (NumBanners)
-  {
-    free(Banners);
-    NumBanners = 0;
-  }
+  free_banners();
 
  /*
   * Try opening the banner directory...
@@ -153,6 +89,8 @@ cupsdLoadBanners(const char *d)              /* I - Directory to search */
   * Read entries, skipping directories and backup files.
   */
 
+  Banners = cupsArrayNew((cups_array_func_t)compare_banners, NULL);
+
   while ((dent = cupsDirRead(dir)) != NULL)
   {
    /*
@@ -165,7 +103,8 @@ cupsdLoadBanners(const char *d)             /* I - Directory to search */
     if (S_ISDIR(dent->fileinfo.st_mode))
       continue;
 
-    if (dent->filename[0] == '~')
+    if (dent->filename[0] == '~' ||
+        dent->filename[strlen(dent->filename) - 1] == '~')
       continue;
 
     if ((ext = strrchr(dent->filename, '.')) != NULL)
@@ -178,33 +117,94 @@ cupsdLoadBanners(const char *d)           /* I - Directory to search */
     * Must be a valid file; add it!
     */
 
-    cupsdAddBanner(dent->filename, filename);
+    add_banner(dent->filename, filename);
   }
 
  /*
-  * Close the directory and sort as needed...
+  * Close the directory...
   */
 
   cupsDirClose(dir);
+}
 
-  if (NumBanners > 1)
-    qsort(Banners, NumBanners, sizeof(cupsd_banner_t),
-          (int (*)(const void *, const void *))compare);
+
+/*
+ * 'add_banner()' - Add a banner to the array.
+ */
+
+static void
+add_banner(const char *name,           /* I - Name of banner */
+           const char *filename)       /* I - Filename for banner */
+{
+  mime_type_t          *filetype;      /* Filetype */
+  cupsd_banner_t       *temp;          /* New banner data */
+
+
+ /*
+  * See what the filetype is...
+  */
+
+  if ((filetype = mimeFileType(MimeDatabase, filename, NULL, NULL)) == NULL)
+  {
+    cupsdLogMessage(CUPSD_LOG_WARN,
+                    "add_banner: Banner \"%s\" (\"%s\") is of an unknown file type - skipping!",
+                    name, filename);
+    return;
+  }
+
+ /*
+  * Allocate memory...
+  */
+
+  temp = calloc(1, sizeof(cupsd_banner_t));
+
+ /*
+  * Copy the new banner data over...
+  */
+
+  temp->name     = strdup(name);
+  temp->filetype = filetype;
+
+  cupsArrayAdd(Banners, temp);
 }
 
 
 /*
- * 'compare()' - Compare two banners.
+ * 'compare_banners()' - Compare two banners.
  */
 
 static int                             /* O - -1 if name0 < name1, etc. */
-compare(const cupsd_banner_t *b0,      /* I - First banner */
-        const cupsd_banner_t *b1)      /* I - Second banner */
+compare_banners(
+    const cupsd_banner_t *b0,          /* I - First banner */
+    const cupsd_banner_t *b1)          /* I - Second banner */
 {
   return (strcasecmp(b0->name, b1->name));
 }
 
 
 /*
- * End of "$Id: banners.c 4719 2005-09-28 21:12:44Z mike $".
+ * 'free_banners()' - Free all banners.
+ */
+
+static void
+free_banners(void)
+{
+  cupsd_banner_t       *temp;          /* Current banner */
+
+
+  for (temp = (cupsd_banner_t *)cupsArrayFirst(Banners);
+       temp;
+       temp = (cupsd_banner_t *)cupsArrayNext(Banners))
+  {
+    free(temp->name);
+    free(temp);
+  }
+
+  cupsArrayDelete(Banners);
+  Banners = NULL;
+}
+
+
+/*
+ * End of "$Id: banners.c 6649 2007-07-11 21:46:42Z mike $".
  */