Institut de Robòtica i Informàtica Industrial
KRD Group

The CuikSuite Project

filename.c

Go to the documentation of this file.
00001 #include "filename.h"
00002 
00003 #include "error.h"
00004 #include "defines.h"
00005 
00006 #include <string.h>
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <unistd.h>
00010 
00021 void CreateFileName(char *path,char *name,char *suffix,char *ext,Tfilename *fn)
00022 {
00023   char *p,*e;
00024   signed int len,lp,lPath,lName,lSuffix,lExt,slashPos,dotPos;
00025 
00026   /* The actual file name is the string between the last '/' in the input name 
00027      and the first '.' after the '/'
00028      The string before the '/' is part of the path and the string after the '.'
00029      is the extension. 
00030      The part is added to the global path and the extension is discarted
00031      if the user provides an extension as a parameter or used, otherwise.
00032   */
00033   len=strlen(name);
00034   
00035   /*look for the last '/' in the given name*/
00036   slashPos=len-1;
00037   while((slashPos>=0)&&(name[slashPos]!='/'))
00038     slashPos--;
00039   
00040   dotPos=slashPos;
00041   while((dotPos<len)&&(name[dotPos]!='.'))
00042     dotPos++;
00043 
00044   /*Set up the path = 'p' + part of the name till the last '/' */ 
00045   /*The path is either the given one or the one from the system*/
00046   if (name[0]=='/')
00047     {
00048       /*The given name is a full path name*/
00049       lPath=slashPos+1;
00050       NEW(fn->path,lPath+1,char);
00051       memcpy(fn->path,name,slashPos*sizeof(char));
00052     }
00053   else
00054     {
00055       if (path==NULL)
00056         p=getcwd(NULL,0);
00057       else 
00058         p=path;
00059       lp=strlen(p);
00060     
00061       lPath=lp+slashPos+2;
00062       if (lPath==0)
00063         Error("Empty file path");
00064       NEW(fn->path,lPath+1,char);
00065       memcpy(fn->path,p,lp*sizeof(char));
00066       fn->path[lp]='/';
00067       if (slashPos>=0)
00068         memcpy(&(fn->path[lp+1]),name,slashPos*sizeof(char));
00069       if (path==NULL)
00070         free(p);
00071     }
00072   if (fn->path[lPath-2]!='/')
00073     {
00074       fn->path[lPath-1]='/';
00075       fn->path[lPath]=0;
00076     }
00077   else
00078     fn->path[lPath-1]=0;
00079 
00080   /*Set up the name */
00081   if (suffix==NULL)
00082     lSuffix=0;
00083   else
00084     lSuffix=strlen(suffix);
00085   lName=dotPos-slashPos-1+lSuffix;
00086   if (lName==0)
00087     Error("Empty file name");
00088   NEW(fn->name,lName+1,char);
00089   memcpy(fn->name,&(name[slashPos+1]),lName*sizeof(char));
00090   if (lSuffix>0)
00091     memcpy(&(fn->name[dotPos-slashPos-1]),suffix,lSuffix*sizeof(char));
00092   fn->name[lName]=0;
00093 
00094   /*Set up the extension*/
00095   if (ext!=NULL)
00096     e=ext;
00097   else
00098     {
00099       if (dotPos==len)
00100         e=NULL;
00101       else
00102         e=&(name[dotPos+1]);
00103     }
00104 
00105   if (e==NULL) lExt=0;
00106   else lExt=strlen(e);
00107 
00108   if (lExt>0)
00109     {
00110       NEW(fn->ext,lExt+1,char);
00111       memcpy(fn->ext,e,lExt*sizeof(char));
00112       fn->ext[lExt]=0;
00113     }
00114   else
00115     fn->ext=NULL;
00116 
00117   /* concat everything but the extension in the basename */
00118 
00119   NEW(fn->baseName,lPath+lName+2,char);
00120   sprintf(fn->baseName,"%s%s",fn->path,fn->name);
00121 
00122   /* concat everything in the full name */
00123   NEW(fn->fullName,lPath+lName+lExt+2,char);
00124   if (lExt>0)
00125     sprintf(fn->fullName,"%s%s.%s",fn->path,fn->name,fn->ext);
00126   else
00127     sprintf(fn->fullName,"%s%s",fn->path,fn->name);
00128 }
00129 
00130 char *GetFileFullName(Tfilename *fn)
00131 {
00132   return(fn->fullName);
00133 }
00134 
00135 char *GetFilePath(Tfilename *fn)
00136 {
00137   return(fn->path);
00138 }
00139 
00140 char *GetFileName(Tfilename *fn)
00141 {
00142   return(fn->name);
00143 }
00144 
00145 char *GetFileBaseName(Tfilename *fn)
00146 {
00147   return(fn->baseName);
00148 }
00149 
00150 char *GetFileExtension(Tfilename *fn)
00151 {
00152   return(fn->ext);
00153 }
00154 
00155 void DeleteFileName(Tfilename *fn)
00156 {
00157   free(fn->path);
00158   free(fn->name);
00159   if (fn->ext!=NULL) free(fn->ext);
00160   free(fn->fullName);
00161 }