/**********************************************************************
 * ti library's routine for fixing node type by filename extension
 *
 * $Author: brlewis $
 * $Source: /mit/techinfodev/src/libti/RCS/fixtype.c,v $
 * $Header: /mit/techinfodev/src/libti/RCS/fixtype.c,v 4.2 1995/03/28 19:24:22 brlewis Exp $
 *
 * Copyright 1994 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/

#include <mit-copyright.h>

#ifndef lint
static char rcsid[] = "$Header: /mit/techinfodev/src/libti/RCS/fixtype.c,v 4.2 1995/03/28 19:24:22 brlewis Exp $";
#endif /* lint */

#include <string.h>
#include <ti/ti.h>
#include <ti/node.h>

typedef struct ti_FileFormatStruct
{
  int typenum;
  char **extensions;
} ti_FileFormat;

static char *NT_ASCII_EXTENSIONS[] = { "TXT", "txt", (char *)0 };
static char *NT_NROFF_EXTENSIONS[] = { "NROFF", "nroff", (char *)0 };
static char *NT_TROFF_EXTENSIONS[] = { "TROFF", "troff", (char *)0 };
static char *NT_EZ_EXTENSIONS[] = { "EZ", "ez", (char *)0 };
static char *NT_RTF_EXTENSIONS[] = { "RTF", "rtf", (char *)0 };
static char *NT_SETEXT_EXTENSIONS[] = { "SETEXT", "setext", (char *)0 };
static char *NT_SMGL_EXTENSIONS[] = { "SMGL", "smgl", (char *)0 };
static char *NT_WRI_EXTENSIONS[] = { "WRI", "wri", (char *)0 };
static char *NT_MSWORD_EXTENSIONS[] = { "DOC", "doc", (char *)0 };
static char *NT_MIME_EXTENSIONS[] = { "MIME", "mime", (char *)0 };
static char *NT_PDF_EXTENSIONS[] = { "PDF", "pdf", (char *)0 };
static char *NT_HTML_EXTENSIONS[] = { "HTML", "html", "HTM", "htm", (char *)0 };
static char *NT_DVI_EXTENSIONS[] = { "DVI", "dvi", (char *)0 };
static char *NT_GIF_EXTENSIONS[] = { "GIF", "gif", (char *)0 };
static char *NT_JPEG_EXTENSIONS[] = { "JPEG", "jpeg", (char *)0 };
static char *NT_JFIF_EXTENSIONS[] = { "JFIF", "jfif", (char *)0 };
static char *NT_TIFF_EXTENSIONS[] = { "TIFF", "tiff", (char *)0 };
static char *NT_PS_EXTENSIONS[] = { "PS", "ps", (char *)0 };
static char *NT_EPS_EXTENSIONS[] = { "EPS", "eps", (char *)0 };
static char *NT_RGB_EXTENSIONS[] = { "RGB", "rgb", (char *)0 };
static char *NT_MPEG_EXTENSIONS[] = { "MPEG", "mpeg", (char *)0 };
static char *NT_QUICKTIME_EXTENSIONS[] = { "QTW", "qtw", (char *)0 };
static char *NT_ULAW_EXTENSIONS[] = { "ULAW", "ulaw", (char *)0 };
static char *NT_WAVE_EXTENSIONS[] = { "WAVE", "wave", (char *)0 };
static char *NT_VOC_EXTENSIONS[] = { "VOC", "voc", (char *)0 };
static char *NT_AIFF_EXTENSIONS[] = { "AIFF", "aiff", (char *)0 };
static char *NT_AIFF_C_EXTENSIONS[] = { "AIFF_C", "aiff_C", (char *)0 };
static char *NT_MOD_EXTENSIONS[] = { "MOD", "mod", (char *)0 };
static char *NT_MACBINARY_EXTENSIONS[] = { "MAC", "mac", (char *)0 };
static char *NT_SIT_EXTENSIONS[] = { "SIT", "sit", (char *)0 };
static char *NT_CPT_EXTENSIONS[] = { "CPT", "cpt", (char *)0 };
static char *NT_DOS_BIN_EXTENSIONS[] = { "EXE", "exe", (char *)0 };
static char *NT_ZIP_EXTENSIONS[] = { "ZIP", "zip", (char *)0 };
static char *NT_LZH_EXTENSIONS[] = { "LZH", "lzh", (char *)0 };
static char *NT_ARC_EXTENSIONS[] = { "ARC", "arc", (char *)0 };
static char *NT_ZOO_EXTENSIONS[] = { "ZOO", "zoo", (char *)0 };
static char *NT_MS_HELP_EXTENSIONS[] = { "HLP", "hlp", (char *)0 };
static char *NT_BINARY_EXTENSIONS[] = { "BIN", "bin", (char *)0 };
static char *NT_BINHEX_EXTENSIONS[] = { "HQX", "hqx", "Hqx", (char *)0 };
static char *NT_UUENCODE_EXTENSIONS[] = { "UU", "uu", (char *)0 };
static char *NT_BASE64_EXTENSIONS[] = { "B64", "b64", (char *)0 };
static char *NT_FRAME_EXTENSIONS[] = { "FRAME", "frame", (char *)0 };
static char *NT_MS_EXCEL_EXTENSIONS[] = { "XLS", "xls", (char *)0 };

static ti_FileFormat FileFormats[] =
{
  { NT_ASCII, NT_ASCII_EXTENSIONS },
  { NT_NROFF, NT_NROFF_EXTENSIONS },
  { NT_TROFF, NT_TROFF_EXTENSIONS },
  { NT_EZ, NT_EZ_EXTENSIONS },
  { NT_RTF, NT_RTF_EXTENSIONS },
  { NT_SETEXT, NT_SETEXT_EXTENSIONS },
  { NT_SMGL, NT_SMGL_EXTENSIONS },
  { NT_WRI, NT_WRI_EXTENSIONS },
  { NT_MSWORD, NT_MSWORD_EXTENSIONS },
  { NT_MIME, NT_MIME_EXTENSIONS },
  { NT_PDF, NT_PDF_EXTENSIONS },
  { NT_HTML, NT_HTML_EXTENSIONS },
  { NT_DVI, NT_DVI_EXTENSIONS },
  { NT_GIF, NT_GIF_EXTENSIONS },
  { NT_JPEG, NT_JPEG_EXTENSIONS },
  { NT_JFIF, NT_JFIF_EXTENSIONS },
  { NT_TIFF, NT_TIFF_EXTENSIONS },
  { NT_PS, NT_PS_EXTENSIONS },
  { NT_EPS, NT_EPS_EXTENSIONS },
  { NT_RGB, NT_RGB_EXTENSIONS },
  { NT_MPEG, NT_MPEG_EXTENSIONS },
  { NT_QUICKTIME, NT_QUICKTIME_EXTENSIONS },
  { NT_ULAW, NT_ULAW_EXTENSIONS },
  { NT_WAVE, NT_WAVE_EXTENSIONS },
  { NT_VOC, NT_VOC_EXTENSIONS },
  { NT_AIFF, NT_AIFF_EXTENSIONS },
  { NT_AIFF_C, NT_AIFF_C_EXTENSIONS },
  { NT_MOD, NT_MOD_EXTENSIONS },
  { NT_MACBINARY, NT_MACBINARY_EXTENSIONS },
  { NT_SIT, NT_SIT_EXTENSIONS },
  { NT_CPT, NT_CPT_EXTENSIONS },
  { NT_DOS_BIN, NT_DOS_BIN_EXTENSIONS },
  { NT_ZIP, NT_ZIP_EXTENSIONS },
  { NT_LZH, NT_LZH_EXTENSIONS },
  { NT_ARC, NT_ARC_EXTENSIONS },
  { NT_ZOO, NT_ZOO_EXTENSIONS },
  { NT_MS_HELP, NT_MS_HELP_EXTENSIONS },
  { NT_BINARY, NT_BINARY_EXTENSIONS },
  { NT_BINHEX, NT_BINHEX_EXTENSIONS },
  { NT_UUENCODE, NT_UUENCODE_EXTENSIONS },
  { NT_BASE64, NT_BASE64_EXTENSIONS },
  { NT_FRAME, NT_FRAME_EXTENSIONS },
  { NT_MS_EXCEL, NT_MS_EXCEL_EXTENSIONS },
  { 0, (char **)0 }
};

/**********************************************************************
 * ti_filetypeof(char *filename, u_long guess)
 *
 * - returns appropriate file type for filename
 * - if type for filename extension isn't found, use "guess"
 **********************************************************************/

unsigned long
ti_filetypeof(filename, guess)
     char *filename;
     unsigned long guess;
{
  register char **ext_maybe;	/* extension we're testing against */
  char *ext;			/* extension of filename */
  ti_FileFormat *ffp;		/* file format we're testing against */

  /* if there's no filename, it's a menu */
  if (!*filename) return(NT_MENU);

  /* get extension */
  ext = strrchr(filename, '.');

  /* no extension?  guess filetype (usually ASCII) */
  if (!ext)
    {
      if (strncasecmp(filename, "READ", 4)==0) return(NT_ASCII);
      return(guess);
    }

  ext++;			/* skip period */

  /* step through node types we have extensions for */
  for (ffp=FileFormats; ffp->typenum; ffp++)
    {
      /* step through possible extensions 'til we find one */
      for(ext_maybe=(ffp->extensions); *ext_maybe; ext_maybe++)
	  if (!strcmp(ext, *ext_maybe)) return(ffp->typenum);
    }

  /* guess filetype (usually ASCII) */
  return(guess);
}

/**********************************************************************
 * ti_flagsfortype(unsigned long nodetype)
 *
 * - returns flag bits to set for appropriate nodetype
 **********************************************************************/

unsigned long
ti_filenodetypeof(nodetype)
     unsigned long nodetype;
{
  /* menu */
  if (nodetype==NT_MENU)
    return(HI_NODE_TYPE(nodetype)|TI_NMENU);

  /* text document */
  if (NR_DOCS_BEG <= nodetype && nodetype <= NR_DOCS_END)
    return(HI_NODE_TYPE(nodetype)|TI_NTEXT);

  /* image */
  if (NR_GRF_BEG <= nodetype && nodetype <= NR_GRF_END)
    return(HI_NODE_TYPE(nodetype)|TI_NIMAGE|TI_NDONT_INDEX);

  /* other type */
  return(HI_NODE_TYPE(nodetype)|TI_NBINARY|TI_NDONT_INDEX);
}

void
ti_fixtype(np, guess)
     ti_node *np;
     unsigned long guess;
{
  ti_clrflag(np, TI_ALLTYPEBITS);
  ti_setflag(np, ti_filenodetypeof(ti_filetypeof(ti_file(np), guess)));
  return;
}
