home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libfont / src / wfMime.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.0 KB  |  308 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* 
  19.  * wfMime.cpp (wfMimeList.cpp)
  20.  *
  21.  * Implements a list of mime types
  22.  *
  23.  * dp Suresh <dp@netscape.com>
  24.  */
  25.  
  26.  
  27. #include "wfMime.h"
  28. #include "stdio.h"
  29. #include "stdlib.h"
  30. #include "string.h"
  31. #include "ctype.h"
  32.  
  33. static void free_mime_store(wfList *object, void *item);
  34.  
  35. wfMimeList::wfMimeList(const char *str) : wfList(free_mime_store)
  36. {
  37.     // Parse the mimeString and create mime_store(s)
  38.     // The format of the mimeString is
  39.     //        mime/type:suffix[,suffix]...:Description
  40.     //        [;mime/type:suffix[,suffix]...:Description]...
  41.     if (str && *str)
  42.     {
  43.         reconstruct(str);
  44.     }
  45. }
  46.  
  47.  
  48. char *
  49. wfMimeList::describe()
  50. {
  51.     char buf[1024];    // XXX this should be dynamic
  52.     char *s = buf;
  53.  
  54.     struct wfListElement *tmp = head;
  55.  
  56.     buf[0] = '\0';
  57.     for (; tmp; tmp = tmp->next)
  58.     {
  59.         struct mime_store *ele = (struct mime_store *) tmp->item;
  60.         // XXX Need to check for memory overflow
  61.         // NO I18N REQUIRED FOR THESE STRINGS
  62.         sprintf(s, "%s:%s:%s:%s;", ele->mimetype,
  63.             (ele->extensions?ele->extensions:" "),
  64.             (ele->description?ele->description:" "),
  65.             (ele->isEnabled?"enable":"disable"));
  66.         s += strlen(s);
  67.     }
  68.     return (*buf ? strdup(buf) : 0);
  69. }
  70.  
  71. int
  72. wfMimeList::reconstruct(const char *str)
  73. {
  74.     char buf[512];
  75.     char *mimetype, *extensions, *description;
  76.     int enabled;
  77.     int err = 0;
  78.     struct mime_store *ele;
  79.  
  80.     if (!str || !*str)
  81.     {
  82.         // Error. No string to reconstruct from.
  83.         err--;
  84.     }
  85.  
  86.     while (err == 0 && *str )
  87.     {
  88.         mimetype = extensions = description = NULL;
  89.         enabled = 1;
  90.         str = wf_scanToken(str, buf, sizeof(buf), ":", 0);
  91.         if (buf[0] == '\0' || *str != ':')
  92.         {
  93.             break;
  94.         }
  95.         str++;
  96.         mimetype = CopyString(buf);
  97.     
  98.         str = wf_scanToken(str, buf, sizeof(buf), ";:", 1);
  99.         extensions = CopyString(buf);
  100.         
  101.         if (*str == ':')
  102.         {
  103.             str++;
  104.             str = wf_scanToken(str, buf, sizeof(buf), ";:", 0);
  105.             description = CopyString(buf);
  106.             if (*str == ':')
  107.             {
  108.                 str++;
  109.                 str = wf_scanToken(str, buf, sizeof(buf), ";", 0);
  110.                 // NO I18N REQUIRED FOR THESE STRINGS
  111.                 if (buf[0] != '\0' && !strncmp(buf, "disable", 7))
  112.                 {
  113.                     enabled = 0;
  114.                 }
  115.             }
  116.         }
  117.         ele = new mime_store;
  118.         if (!ele)
  119.         {
  120.             // No memory
  121.             err--;
  122.             continue;
  123.         }
  124.         ele->mimetype = mimetype;
  125.         ele->description = description;
  126.         ele->extensions = extensions;
  127.         ele->isEnabled = enabled;
  128.         add(ele);
  129.         if (*str != ';')
  130.         {
  131.             break;
  132.         }
  133.         str++;
  134.  
  135.     }
  136.     return err;
  137. }
  138.  
  139.  
  140. wfMimeList::~wfMimeList()
  141. {
  142.     finalize();
  143. }
  144.  
  145. int wfMimeList::finalize(void)
  146. {
  147.     wfList::removeAll();
  148.     return (0);
  149. }
  150.  
  151. void
  152. /*ARGSUSED*/
  153. free_mime_store(wfList *object, void *item)
  154. {
  155.     struct mime_store *ele = (struct mime_store *) item;
  156.     if (ele->mimetype)
  157.     {
  158.         delete ele->mimetype;
  159.         ele->mimetype = NULL;
  160.     }
  161.     if (ele->extensions)
  162.     {
  163.         delete ele->extensions;
  164.         ele->extensions = NULL;
  165.     }
  166.     if (ele->description)
  167.     {
  168.         delete ele->description;
  169.         ele->description = NULL;
  170.     }
  171.     delete ele;
  172. }
  173.  
  174.  
  175. //
  176. // Implementation of public methods
  177. //
  178.  
  179. int wfMimeList::setEnabledStatus(const char *mimetype, int enabledStatus)
  180. {
  181.     struct mime_store *ele = find(mimetype);
  182.     if (!ele)
  183.     {
  184.         // mimetype not found
  185.         return (-1);
  186.     }
  187.  
  188.     ele->isEnabled = (enabledStatus?1:0);
  189.     return (0);
  190. }
  191.  
  192.  
  193. int wfMimeList::isEnabled(const char *mimetype)
  194. {
  195.     struct mime_store *ele = find(mimetype);
  196.     if (!ele)
  197.     {
  198.         // mimetype not found
  199.         return (-1);
  200.     }
  201.  
  202.     return (ele->isEnabled);
  203. }
  204.  
  205.  
  206. const char * wfMimeList::getMimetypeFromExtension(const char *ext)
  207. {
  208.     const char *mimetype = NULL;
  209.     struct wfListElement *tmp = head;
  210.     struct mime_store *ele = NULL;
  211.  
  212.     if (!ext || !*ext)
  213.     {
  214.         return (NULL);
  215.     }
  216.  
  217.     for (; tmp; tmp = tmp->next)
  218.     {
  219.         ele = (struct mime_store *) tmp->item;
  220.  
  221.         if (!ele->extensions || !*(ele->extensions))
  222.         {
  223.             continue;
  224.         }
  225.  
  226.         //
  227.         // Check if ele->extensions contains ext
  228.         // Extension is a comma separated list. We have normalized it
  229.         // before storing it into ele so that it contains no leading '.'
  230.         // or spaces.
  231.         //
  232.         const char *p = ele->extensions;
  233.         const char *q = ext;
  234.         int found = 0;
  235.         while (*p)
  236.         {
  237.             if (*p == ',')
  238.             {
  239.                 // Move to the next extension
  240.                 p++;
  241.                 q = ext;
  242.             }
  243.             else if (!*q)
  244.             {
  245.                 // maybe Success if *p is either , or NULL
  246.                 if ((*p == '\0') || (*p == ','))
  247.                 {
  248.                     found = 1;
  249.                     break;
  250.                 }
  251.                 else
  252.                 {
  253.                     q = ext;
  254.                     // Nope. Skip to next extension.
  255.                     while (*p && *p != ',') p++;
  256.                 }
  257.             }
  258.             else if ((*p == *q) || (tolower(*p) == tolower(*q)))
  259.             {
  260.                 // keep going.
  261.                 p++; q++;
  262.             }
  263.             else
  264.             {
  265.                 // *p != *q
  266.                 q = ext;
  267.                 // Nope. Skip to next extension.
  268.                 while (*p && *p != ',') p++;
  269.             }
  270.         }
  271.         if (!*q)
  272.         {
  273.             // We came out becasue p is null. If q is also null, then
  274.             // we did find a match.
  275.             found = 1;
  276.         }
  277.         if (found)
  278.         {
  279.             mimetype = ele->mimetype;
  280.             break;
  281.         }
  282.     }
  283.  
  284.     return (mimetype);
  285. }
  286.  
  287. //
  288. // Implementation of private methods
  289. //
  290.  
  291. struct mime_store *
  292. wfMimeList::find(const char *mimetype)
  293. {
  294.     struct mime_store *ret = NULL;
  295.     struct wfListElement *tmp = head;
  296.  
  297.     for (; tmp; tmp = tmp->next)
  298.     {
  299.         struct mime_store *ele = (struct mime_store *) tmp->item;
  300.         if (!wf_strcasecmp(ele->mimetype, mimetype))
  301.         {
  302.             ret = ele;
  303.         }
  304.     }
  305.  
  306.     return (ret);
  307. }
  308.