/*********************************************************************
 *
 *            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 _FINFO_C_
#include "ezfm.h"

/*************************************************************
 *
 * public functions
 */
char *GetFileTypeInfo  MY_ANSIARGS((char *name));
void getSpecField  MY_ANSIARGS ((char *spec, int field, char *filed_ret));
char *gotoSpecField MY_ANSIARGS ((char *spec, int field1));
void getSpecColor MY_ANSIARGS ((char *spec, char *ret));
void getSpecPixmap MY_ANSIARGS ((char *spec, char *ret, char *ret1));
int getSpecFontId  MY_ANSIARGS ((char *spec));

/*************************************************************
 *
 *  return the action specification for a file. Consult
 *  extension file first, if not found, consult the magic file.
 */
char *GetFileTypeInfo(name) char *name;
{
  char *spec = GetSpecFromExtension(name);
  if(spec) return(spec);
  return(GetSpecFromMagic(name));
}
/*************************************************************
 *
 * Given a file type specification, extract the given
 * fields.
 */
void getSpecField(spec, field, field_ret)
     char *spec; int field; char *field_ret;
{
  char *dest = field_ret;
  if(spec && field >= 0 && field <= SPEC_NDIELDS)
    {
      int  i;
      char *src = spec;
      for(i = 0; i < field && *src; )
	if(*src++ == ':')i++;
      while(*src && *src != ':') *dest++ = *src++; 
    }
  *dest = '\0';
}

char *gotoSpecField(spec, field)
     char *spec; int field; 
{
  if(spec && field >= 0 && field <= SPEC_NDIELDS)
    {
      int  i;
      char *src = spec;
      for(i = 0; i < field; )
	if(*src++ == ':')i++;
      return(src);
    }
  return(NULL);
}
/**********************************************************/
void getSpecColor(spec, clr_ret)
     char *spec; char *clr_ret;
{
  char buf[256];
  char *dest = clr_ret;
  getSpecField(spec, SPEC_COLOR,  buf);
  if(buf[0] != '\0')  
    { 
      char *ptr = buf;
      while(*ptr && *ptr == ' ') ptr++;
      while(*ptr && *ptr != ' ') *dest++ = *ptr++;      
    }
  *dest = '\0';
}
/**********************************************************/
void getSpecPixmap(spec, lpm_ret, spm_ret)
     char *spec, *lpm_ret, *spm_ret;
{
  char buf[256];
  getSpecField(spec, SPEC_PIXMAP,  buf);
  if(buf[0] != '\0')
    {
      char *ptr = buf;
      char *dest = lpm_ret;
      while(*ptr  && *ptr == ' ') ptr++;
      while(*ptr  && *ptr != ',' && *ptr != ' ') *dest++ = *ptr++;
      *dest = '\0';
      while(*ptr  && *ptr != ',') ptr++;
      if(*ptr == ',')
	{
	  ptr++;
	  while(*ptr && *ptr == ' ') ptr++;
	  dest = spm_ret;
	  while(*ptr && *ptr != ' ') *dest++ = *ptr++;
	  *dest = '\0';
	}
      else spm_ret[0] = '\0';
    }
  else
    {
      lpm_ret[0] = '\0';
      spm_ret[0] = '\0';
    }
}
/**********************************************************/
int getSpecFontId(spec)
     char *spec;
{
  char buf[256];
  getSpecField(spec, SPEC_FONT,  buf);
  if(buf[0] != '\0')
    {
      char *ptr = buf;
      while(*ptr  && *ptr == ' ') ptr++;
      switch(*ptr)
	{
	case 'b':
	  if(ptr[1] == 'i') return(ITEM_BOLD_SLANT_FONT);
	  else return(ITEM_BOLD_FONT);
	  break;
	case 'i':
	  return(ITEM_SLANT_FONT);
	  break;
	default:
	  return(ITEM_MEDIUM_FONT);
	  break;
	}
    }
  return(ITEM_MEDIUM_FONT);
}
/**********************************************************/

