/*
**  $RCSfile: strfn.c,v $
**  $Release$
**  $Revision: 1.4 $
**  $Date: 92/08/31 15:43:20 $
**  $Author: tf $
**  $State: Exp $
**
**  File name support functions
**
**  (c) Copyright 1991,92 Tobias Ferber.  All Rights Reserved.
*/

#include "strfn.h"

/* check if a file exists */

BOOL fexist(char *name)
{ FILE *fp;
  if(!(fp=fopen(name,"r")))
    return(FALSE);
  fclose(fp);
  return(TRUE);
}

extern struct DosLibrary *DOSBase;

/* get device list type */

long dltype(char *dname)
{ struct DeviceList *dlist;
  int n;
  char *t;
  for(n=0;dname[n]!=_COLON && dname[n]!='\0'; n++)
    ;
  dlist=(struct DeviceList *)
    BADDR(((struct DosInfo *)
      BADDR(((struct RootNode *)(DOSBase->dl_Root))->rn_Info))->di_DevInfo);
  while(dlist)
  { t=(char *)(BADDR(dlist->dl_Name))+1;
    if((strlen(t)==n) && (!strnicmp(t,dname,n)))
      return(dlist->dl_Type);
    else
      dlist=(struct DeviceList *) BADDR(dlist->dl_Next);
  }
  return(DLT_UNKNOWN);
}

/* make file name */

char *strmfn(name, drive, path, node, ext)
char *name,*drive,*path,*node,*ext;
{ if(name)
  { *name='\0';
    if(drive && *drive!='\0')
    { while((*name=*drive++)!='\0' && *name!=_COLON)
        name++;
      *name++=_COLON;
    }
    if(path && *path!='\0')
    { while((*name=*path++)!='\0')
        name++;
      --name;
      if(*name!=_SLASH)
        *++name=_SLASH;
      *++name='\0';
    }
    if(node && *node!='\0')
    { while((*name=*node++)!='\0')
        name++;
    }
    if(ext && *ext!='\0')
    { if(*name!=_PERIOD && *ext!=_PERIOD)
        *name++=_PERIOD;
      while((*name=*ext++)!='\0')
        name++;
    }
  }
  return(name);
}

/* split file name */

void strsfn(name, drive, path, node, ext)
char *name,*drive,*path,*node,*ext;
{ if(name)
  { char *f;
    int p,n,e,x;
    for(f=name,x=p=n=e=0; *f!='\0'; f++,x++)
    { switch(*f)
      { case _COLON:
          if(drive)
            strncpy(drive,name,x+1);
          p=n=x+1;
          break;
        case _SLASH:
          n=x+1;
          break;
        case _PERIOD:
          e=x+1;
          break;
      }
    }
    if(path)
    { if(p!=n)
        strncpy(path,&name[p],n-p);
      path[n-p]='\0';
    }
    if(node)
    { if(e>0 && n!=e)
      { strncpy(node,&name[n],e-1-n);
        node[e-1-n]='\0';
      }
      else
      { if(n!=x)
          strncpy(node,&name[n],x-n);
        node[x-n]='\0';
      }
    }
    if(ext)
    { if(e>0)
        strcpy(ext,&name[e]);
      else
        *ext='\0';
    }
  }
}

/* get file drive */

int stcgfd(drive,name)
char *drive,*name;
{ if(name && *name!='\0')
  { int x,n;
    for(x=n=0; name[x]!='\0'; x++)
    { if(name[x]==_COLON)
        n=x+1;
    }
    if(drive && n>0)
    { strncpy(drive,name,n);
      drive[n]='\0';
    }
    return(n);
  }
  else return(0);
}

/* get file path (incl. drive) */

int stcgfp(path,name)
char *path, *name;
{ if(name && *name!='\0')
  { int x,n;
    for(x=n=0; name[x]!='\0'; x++)
    { if(name[x]==_COLON || name[x]==_SLASH)
        n=x+1;
    }
    if(n>0 && path)
    { strncpy(path,name,n);
      path[n]='\0';
    }
    return(n);
  }
  else return(0);
}

/* get file name (incl. extension(s) ) */

int stcgfn(file,name)
char *file,*name;
{ char *t;
  if(name && *name!='\0')
  { for(t=name; *name!='\0'; name++)
    { if(*name==_COLON || *name==_SLASH)
        t=&name[1];
    }
    if(file)
      strcpy(file,t);
    return strlen(t);
  }
  else if(file)
    *file='\0';
  return(0L);
}

/* get file extension */

int stcgfe(ext,name)
char *ext, *name;
{ if(name)
  { char *t=NULL;
    while(*name!='\0')
    { if(*name++==_PERIOD)
        t=name;
    }
    if(ext)
    { if(t!=NULL)
      { strcpy(ext,t);
        return strlen(ext);
      }
      else *ext='\0';
    }
  }
  return(0L);
}

/* make file with (new) extension */

void strmfe(new,old,ext)
char *new,*old,*ext;
{ if(new)
  { char *t=NULL;
    if(old)
    { while((*new=*old++)!='\0')
      { if(*new==_PERIOD)
          t=new;
        else if(*new==_SLASH || *new==_COLON)
          t=NULL;
        new++;
      }
    }
    if(t!=NULL)
      new=t;
    if(ext)
    { *new++ = _PERIOD;
      strcpy(new,ext);
    }
    else *new='\0';
  }
}

/* make filename: path (+ _SLASH) + node */

void strmfp(name,path,node)
char *name,*path,*node;
{ if(name)
  { if(path)
    { while((*name=*path++)!='\0')
        name++;
      --name;
      if(*name!=_SLASH && *name!=_COLON)
        *++name=_SLASH;
      *++name='\0';
    }
    if(node)
    { while((*name++ = *node++)!='\0')
        ;
    }
  }
}
