home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / plugin / npassoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.4 KB  |  240 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. /*
  20.  *  npassoc.c $Revision: 3.1 $
  21.  *  some xp mime-type file extension associations
  22.  */
  23.  
  24. #include "npassoc.h"
  25. #include "xp_mem.h"
  26. #include "xp_mcom.h"
  27. #include "net.h"
  28.  
  29. static NPFileTypeAssoc *np_fassoc_list=0; /* should use xp hash */
  30.  
  31. extern void NET_cdataAdd(NET_cdataStruct *cd);
  32.  
  33. #define NP_MAX_EXTS 64
  34. static char *np_array[NP_MAX_EXTS];
  35.  
  36. /* allocate and fill a null-terminated array of extensions from
  37.    a comma and space delimited list */
  38. static char **
  39. np_parse_exts(const char *exts)
  40. {
  41.     char *p, *e, **res;
  42.     int n=0;
  43.  
  44.     if(!exts)
  45.         return 0;
  46.  
  47.     /* p is the current position, e is the start of the nth extension */
  48.     for(e=p=(char *)exts; *p; p++)
  49.     {
  50.         if( (*p==' ') || (*p==',') )
  51.         {
  52.             if(*e == '.') e++;
  53.             if(p>e) 
  54.             {                
  55.                 if(!(np_array[n] = (char *)XP_ALLOC(p-e+1)))
  56.                     return 0;
  57.                 XP_MEMCPY(np_array[n], e, (p-e));
  58.                 *(np_array[n]+(p-e)) = 0;
  59.                 n++;
  60.                 e = p+1;
  61.             }
  62.         }
  63.     }
  64.     if(*e == '.') e++;
  65.     if(p>e) 
  66.     {                
  67.         if(!(np_array[n] = (char *)XP_ALLOC(p-e+1)))
  68.             return 0;
  69.         XP_MEMCPY(np_array[n], e, (p-e));
  70.         *(np_array[n]+(p-e)) = 0;
  71.         n++;
  72.     }
  73.  
  74.     if(!(res = (char **)XP_ALLOC((n+1)*sizeof(char *))))
  75.         return 0;
  76.     XP_MEMCPY(res, np_array, n*sizeof(char *));
  77.     res[n] = 0;
  78.  
  79.     return res;
  80. }
  81.  
  82.  
  83. /* construct a file association from a mime type.  
  84.  *   - extensions is a list of comma/space separated file extensions
  85.  *     with or without leading .s
  86.  *   - filetype is platform specific data for this list of extensions,
  87.  *     currently creator on mac and open file dialog descriptions on win.
  88.  *     filetype is callee owned data and must remain valid
  89.  */
  90. NPFileTypeAssoc *
  91. NPL_NewFileAssociation(const char *MIMEType, const char *extensions, const char *description, void *fileType)
  92. {
  93.     NPFileTypeAssoc *fassoc = 0;
  94.     
  95.     /* make a file association struct */
  96.     if(!(fassoc=XP_NEW_ZAP(NPFileTypeAssoc)))
  97.         return 0;
  98.  
  99.     StrAllocCopy((fassoc->type), MIMEType ? MIMEType : "");
  100.     StrAllocCopy((fassoc->description), description ? description : "");
  101.     StrAllocCopy((fassoc->extentstring), extensions ? extensions : "");
  102.  
  103.     fassoc->fileType = fileType; /* caller owns this data */
  104.     fassoc->extentlist = np_parse_exts(extensions);
  105.     return fassoc;
  106. }
  107.  
  108.  
  109. /* deletes a file association.  Returns the platform specific fileType
  110.    data that we dont know how to dispose of.
  111. */
  112. void *
  113. NPL_DeleteFileAssociation(NPFileTypeAssoc *fassoc)
  114. {
  115.     void* fileType;
  116.     
  117.     if (!fassoc)
  118.         return NULL;
  119.  
  120.     fileType = fassoc->fileType;
  121.     
  122.     NPL_RemoveFileAssociation(fassoc);
  123.  
  124.     if (fassoc->type) 
  125.     { 
  126.         XP_FREE(fassoc->type);
  127.         fassoc->type = NULL;
  128.     }
  129.  
  130.     if (fassoc->description) 
  131.     { 
  132.         XP_FREE(fassoc->description);
  133.         fassoc->description = NULL;
  134.     }
  135.  
  136.     if (fassoc->extentstring) 
  137.     { 
  138.         XP_FREE(fassoc->extentstring);
  139.         fassoc->extentstring = NULL;
  140.     }
  141.  
  142.     {
  143.         char** charPtrPtr;
  144.         for (charPtrPtr = &fassoc->extentlist[0]; *charPtrPtr; *charPtrPtr=0, charPtrPtr++)
  145.             XP_FREE(*charPtrPtr);
  146.  
  147.         fassoc->extentlist = NULL;
  148.     }
  149.  
  150.     XP_FREE(fassoc);
  151.     
  152.     return fileType;
  153. }
  154.  
  155.  
  156. /* Register a file association with us and netlib.
  157.  */
  158. void
  159. NPL_RegisterFileAssociation(NPFileTypeAssoc *fassoc)
  160. {
  161.     if (fassoc)
  162.     {
  163.         fassoc->pNext = np_fassoc_list;
  164.         np_fassoc_list = fassoc;
  165.  
  166.         NET_cdataCommit(fassoc->type, fassoc->extentstring);
  167.     
  168.         /*
  169.          * We need to add the description, too, which unfortunately requires
  170.          * looking the cinfo up AGAIN and setting the desc field...
  171.          */
  172.         if (fassoc->description)
  173.         {
  174.             NET_cdataStruct temp;
  175.             NET_cdataStruct* cdata;
  176.             
  177.             XP_BZERO(&temp, sizeof(temp));
  178.             temp.ci.type = fassoc->type;
  179.             cdata = NET_cdataExist(&temp);
  180.             XP_ASSERT(cdata);
  181.             if (cdata)
  182.                 StrAllocCopy(cdata->ci.desc, fassoc->description);
  183.         }
  184.     }
  185. }
  186.  
  187.  
  188. /* Unregister a file association.
  189.  */
  190. NPFileTypeAssoc *
  191. NPL_RemoveFileAssociation(NPFileTypeAssoc *fassoc)
  192. {
  193.     NPFileTypeAssoc *f = np_fassoc_list;
  194.  
  195.     if(!fassoc)
  196.         return 0;
  197.  
  198.     /* unregister with netlib */
  199.     if(fassoc == np_fassoc_list)
  200.         np_fassoc_list = np_fassoc_list->pNext;
  201.     else
  202.     {
  203.         for(; f; f=f->pNext)
  204.             if(f->pNext == fassoc)
  205.             {
  206.                 NPFileTypeAssoc *ft;
  207.                 ft = f->pNext;
  208.                 f->pNext = f->pNext->pNext;
  209.                 f = ft;
  210.                 break;
  211.             }
  212.     }
  213.     return f;
  214. }
  215.  
  216.  
  217. /* returns a linked list of registered associations.  
  218.  * if type is NULL you get the entire list else the association matching
  219.  * that MIME type
  220. */
  221. NPFileTypeAssoc *
  222. NPL_GetFileAssociation(const char *type)
  223. {
  224.     NPFileTypeAssoc *f=NULL;
  225.  
  226.     if(!np_fassoc_list)
  227.         return NULL;
  228.  
  229.     if(type==NULL)
  230.         return np_fassoc_list;
  231.  
  232.     for(f=np_fassoc_list; f; f=f->pNext)
  233.         if(!(XP_STRCMP(type, f->type)))
  234.             return f;
  235.  
  236.     return NULL;
  237. }
  238.  
  239.  
  240.