/*********************************************************************
 *
 *            EZFM---EZWGL based file manager for X11
 *
 *             Copyright (C) 1998  Maorong Zou
 *
 *
 *  This file is part of EZFM.
 *
 *  EZFM is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * EZFM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with EZFM; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 *************************************************************************/

#define _EXTENSION_C_
/*********************************************************************
 *
 *       Process extension-action association file
 */
#include "ezfm.h"
/*********************************************************************/
char  *GetSpecFromExtension    MY_ANSIARGS((char *pathname));
void  ProcessSuffixFile           MY_ANSIARGS((char *fname));
/*********************************************************************/
typedef struct {
  char    *suffix;
  int     slen;
  char    *spec;
} suffixActionPair;

static  suffixActionPair *SApairs = NULL;
static  int              nSApairs = 0;
static  int              SApairIdx = 0;
/*********************************************************************/
char *GetSpecFromExtension(pathname) char *pathname;
{
  if(SApairs == NULL || pathname == NULL) return(NULL);
  {
    int len = strlen(pathname);
    char *save = pathname + len -1;
    int i, j;
    for(i = 0; i < SApairIdx; i++)
      {
	char *p, *q, *r;
	r = SApairs[i].suffix;	p = save;
	q = r + SApairs[i].slen - 1;
	while(q >= r && p > pathname  && *p == *q) { p--; q--;}
	if(q == r-1) /* extension matches */
	  {
	    /* verify if pathname is a not a directory */
	    struct stat stat_buf;	    
	    if(stat(pathname, &stat_buf) < 0 || S_ISDIR(stat_buf.st_mode)) return(NULL);
	    return(SApairs[i].spec);
	  }
      }
    return(NULL);
  }
}
/*********************************************************************/
void ProcessSuffixFile(file) char *file;
{
  char pathname[1024], tmpname[256], *fname;
  FILE *fp = NULL;
  char *rcf;

  if(file != NULL)
    {
      char *r, *p, *q = strcpy(pathname, file);
      while(q)
	{
	  p = strchr(q, ':');
	  if(p) *p++ = '\0';
	  if( (r = EZ_TildeExpand(q)) != NULL)
	    {
	      strcpy(tmpname, r); fname = tmpname;
	      free(r);
	      if((fp = fopen(fname, "r")) != NULL)
		break;
	    }
	  q = p;
	}    
    }
  if(fp)
    {
      char *buf = pathname;
      int cnt = 0;
      while(nextLine(buf, &cnt, fp) > 0)
	{
	  char *ptr, *sfx, *spec;
	  sfx = skipSpaces(buf);
	  if(sfx)
	    {
	      ptr =sfx;
	      while(*ptr && *ptr != ' ' && *ptr != '\t')
		*ptr++;
	      *ptr++ = '\0';
	      spec = skipSpaces(ptr);
	      if(spec)
		{
		  if(SApairIdx + 1 >= nSApairs)
		    {
		      if(nSApairs == 0)
			{
			  nSApairs = 64;
			  SApairs = (suffixActionPair *)EZ_Malloc(64 * sizeof(suffixActionPair));
			  memset(SApairs, 0, 64 * sizeof(suffixActionPair));
			}
		      else
			{
			  int itmp =  nSApairs +   nSApairs;
			  SApairs = (suffixActionPair *)EZ_Realloc(SApairs,
								   itmp * sizeof(suffixActionPair));
			  memset( &(SApairs[SApairIdx]), 0, (itmp - SApairIdx)*sizeof(suffixActionPair));
			  nSApairs = itmp;
			}
		    }
		  {
		    int slen =strlen(sfx);
		    int plen = strlen(spec);
		    int tlen = slen + plen + 2;
		    char *ctmp = (char *)malloc(tlen * sizeof(char));
		    SApairs[SApairIdx].suffix = ctmp;
		    SApairs[SApairIdx].slen = slen;
		    SApairs[SApairIdx].spec = ctmp + slen + 1;
		    strcpy(ctmp, sfx);
		    strcpy(ctmp+slen+1, spec);
		    SApairIdx++;
		  }
		}
	    }
	}
      (void)fclose(fp);
    }
}
/*********************************************************************/
#undef _EXTENSION_C_
