/******************************************************************************
 *
 * RCS ID
 * $Id: ftypes,v 1.4 2000/04/17 16:48:32 david Exp $
 *
 * HISTORY
 * $Log: ftypes,v $
 * Revision 1.4  2000/04/17 16:48:32  david
 * modifications for running in RISCOS module
 *
 * Revision 1.3  1999/11/07 15:14:54  david
 * Updates to support StripExtension, set filetype
 * utils:new stuff!
 *
 * Revision 1.2  1999/07/20 21:07:56  david
 * Manage file type database.
 *
 * Revision 1.1  1999/05/16 12:00:08  david
 * Initial revision
 *
 *
 *****************************************************************************/
                                
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "includes.h"

#include "kernel.h"
#include "swis.h"
#include "my_os.h"
#include "mimemap.h"

typedef struct sftypes {
   char ext[15];
   int  ftype;
} tftypes, *tpftypes;

tftypes ftypes[4096];

extern int StrnCaseCmp(const char *s, const char *t, size_t n);

int ftypesShutdown = 0;

void ftypes_init(void)
{
   FILE *fp;
   char line[100];
   char ext[15];
   int  aftype;
   int  i;

   ftypesShutdown = 0;

   for (i=0;i<4096;i++)
      ftypes[i].ftype=0;

   fp=fopen("<samba$dir>.resources.FileTypes","r");

   if (fp!=NULL)
   {

      while (fgets(line,100,fp)!=NULL)
      {
       
         if (line[0]!='#')
         {
         
            sscanf(line,"%x : %s",&aftype, ext);
            ftypes[aftype].ftype=aftype;
            strcpy(ftypes[aftype].ext,ext);
         }
      }
   }

   fclose(fp);
}                                                

void ftypes_exit(void)
{
   FILE *fp;
   char s[100];
   int  i;
    
   if (ftypesShutdown==0)
   {
      ftypesShutdown=1;

      fp=fopen("<samba$dir>.resources.FileTypes","w");

      if (fp!=NULL)
      {
         for (i=0;i<4096;i++)
         {      
            sprintf(s,"%.3x",ftypes[i].ftype);
            if (ftypes[i].ftype!=0 &&
                strcmp(s,ftypes[i].ext)!=0)
            {
               fprintf(fp,"0x%.3x : %s\n",i, ftypes[i].ext);           
            }
      
         }
         fclose(fp);
      }
   }
}       

/* convert aftype to file extension string */

void get_ftype(int aftype,char *ext)
{             
  char            buffer[20];   
  int             r[10];
  _kernel_oserror *e; 

  if (aftype==0)
    strcpy(ext,"");
  else
  {
    /* ask MimeMap for the type first */

    r[0] = MMM_TYPE_RISCOS;
    r[1] = aftype;
    r[2] = MMM_TYPE_DOS_EXTN;
    r[3] = (int)buffer;

    e = myos_swi(MimeMap_Translate,r);

    if (e==NULL)
    {  
       int i;
   
       /* mimemap found it */
       DEBUG(4,("MimeMap input 0x%x, output %s\n",aftype,buffer));
       strcpy(ext,buffer);      
       if (strlen(ext)>3)
       {
          ext[3]='\0';
       }                              
    }
    else
    {   
       /* mimemap does't know it - look up in local database */

       DEBUG(4,("get_ftype:Mimemap: error 0x%x,%s\n", e->errnum, e->errmess)); 
    
       if (ftypes[aftype].ftype==0)
       {
         sprintf(ext,"%.3x",aftype);
         ftypes[aftype].ftype=aftype;  
         strcpy(ftypes[aftype].ext,ext);
       }
       else
         strcpy(ext,ftypes[aftype].ext); 
    }
  }

  DEBUG(4,("get_ftype %x, %s\n", aftype, ext));

}

/* convert extension to RISCOS ftype */

void get_ext(int *aftype,char *ext)
{   
  int             r[10];
  _kernel_oserror *e; 
  int i;      

  *aftype=0;

#ifdef FTEST
  printf("get_ext %s\n",ext);
#else
  DEBUG(5,("get_ext %s\n",ext));
#endif
   
  /* first ask MimeMap if it knows what it is */
  r[0] = MMM_TYPE_DOS_EXTN;
  r[1] = (int)ext;
  r[2] = MMM_TYPE_RISCOS;

  e = myos_swi(MimeMap_Translate,r);

  if (e==NULL)
  {                                      
    /* Mimemap knows what it is */
    *aftype = r[3];
    DEBUG(1,("MimeMap input %s, output 0x%x\n",ext,*aftype));    
  }
  else
  {                             
    /* Mimemap doesn't know what it is, so add it to out database */  

    DEBUG(1,("get_ftype:Mimemap: error 0x%x, %s\n", e->errnum, e->errmess));
  
    for (i=0;i<4096;i++)
    {
      if (StrCaseCmp((const char *)ftypes[i].ext,
                     (const char *)ext)==0) 
      {
        *aftype=i;
        break;
      }
    }         

    if (*aftype==0)    
    {
            
      /* start from 1 */
      for (i=1;i<4096;i++)
      {
        if (ftypes[i].ftype==0)
        {
          ftypes[i].ftype=i;
          strcpy(ftypes[i].ext,ext);
          *aftype=i;
          break;  
        }
      }
    }
  }    

  DEBUG(1,("get_ext %x, %s\n", *aftype, ext));

 
}

             
                                 
