filename.c
Go to the documentation of this file.
1 #include "filename.h"
2 
3 #include "error.h"
4 #include "defines.h"
5 #include "boolean.h"
6 
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 
22 void CreateFileName(char *path,char *name,char *suffix,char *ext,Tfilename *fn)
23 {
24  char *p,*e;
25  signed int len,lp,lPath,lNameOnly,lName,lSuffix,lExt,slashPos,dotPos,prot;
26  boolean found;
27 
28  /* The actual file name is the string between the last '/' in the input name
29  and the first '.' after the '/'
30  The string before the '/' is part of the path and the string after the '.'
31  is the extension.
32  The path is added to the global path and the extension is discarted
33  if the user provides an extension as a parameter or used, otherwise.
34  */
35  len=strlen(name);
36 
37  /*look for the last '/' in the given name*/
38  slashPos=len-1;
39  while((slashPos>=0)&&(name[slashPos]!='/'))
40  slashPos--;
41 
42  dotPos=(slashPos>0?slashPos:0);
43  while((dotPos<len)&&(name[dotPos]!='.'))
44  dotPos++;
45 
46  /* skip the protocal, if any. Some filenames start with "file://" or "http://"
47  We must skip this part if present */
48  found=FALSE;
49  prot=0;
50  while((!found)&&(prot<slashPos-1))
51  {
52  found=((name[prot]==':')&&(name[prot+1]=='/')&&(name[prot+2]=='/'));
53  if (!found)
54  prot++;
55  }
56  if (found)
57  prot+=3;
58  else
59  prot=0;
60 
61  /*Set up the path = 'p' + part of the name till the last '/' */
62  /*The path is either the given one or the one from the system*/
63  if (name[prot]=='/')
64  {
65  /*The given name is a full path name*/
66  lPath=slashPos+1;
67  NEW(fn->path,lPath+1,char);
68  memcpy(fn->path,&(name[prot]),(slashPos-prot)*sizeof(char));
69  }
70  else
71  {
72  if (path==NULL)
73  p=getcwd(NULL,0);
74  else
75  p=path;
76  lp=strlen(p);
77  /* if the path includes a '/' at the end remove it. It will
78  be added latter. */
79  if (p[lp-1]=='/')
80  lp--;
81 
82  lPath=lp+(slashPos-prot)+2;
83  if (lPath==0)
84  Error("Empty file path");
85  NEW(fn->path,lPath+1,char);
86  memcpy(fn->path,p,lp*sizeof(char));
87  fn->path[lp]='/';
88  if ((slashPos-prot)>=0)
89  memcpy(&(fn->path[lp+1]),&(name[prot]),(slashPos-prot)*sizeof(char));
90  if (path==NULL)
91  free(p);
92  }
93  if (fn->path[lPath-2]!='/')
94  {
95  fn->path[lPath-1]='/';
96  fn->path[lPath]=0;
97  }
98  else
99  fn->path[lPath-1]=0;
100 
101  /*Set up the name */
102  if (suffix==NULL)
103  lSuffix=0;
104  else
105  lSuffix=strlen(suffix);
106  lNameOnly=dotPos-slashPos-1;
107  lName=lNameOnly+lSuffix;
108  if (lName==0)
109  Error("Empty file name");
110  NEW(fn->name,lName+1,char);
111  memcpy(fn->name,&(name[slashPos+1]),lNameOnly*sizeof(char));
112  if (lSuffix>0)
113  memcpy(&(fn->name[lNameOnly]),suffix,lSuffix*sizeof(char));
114  fn->name[lName]=0;
115 
116  /*Set up the extension*/
117  if (ext!=NULL)
118  e=ext;
119  else
120  {
121  if (dotPos==len)
122  e=NULL;
123  else
124  e=&(name[dotPos+1]);
125  }
126 
127  if (e==NULL) lExt=0;
128  else lExt=strlen(e);
129 
130  if (lExt>0)
131  {
132  NEW(fn->ext,lExt+1,char);
133  memcpy(fn->ext,e,lExt*sizeof(char));
134  fn->ext[lExt]=0;
135  }
136  else
137  fn->ext=NULL;
138 
139  /* concat everything but the extension in the basename */
140  NEW(fn->baseName,lPath+lName+2,char);
141  sprintf(fn->baseName,"%s%s",fn->path,fn->name);
142 
143  /* concat everything in the full name */
144  NEW(fn->fullName,lPath+lName+lExt+2,char);
145  if (lExt>0)
146  sprintf(fn->fullName,"%s%s.%s",fn->path,fn->name,fn->ext);
147  else
148  sprintf(fn->fullName,"%s%s",fn->path,fn->name);
149 }
150 
152 {
153  return(fn->fullName);
154 }
155 
157 {
158  return(fn->path);
159 }
160 
162 {
163  return(fn->name);
164 }
165 
167 {
168  return(fn->baseName);
169 }
170 
172 {
173  return(fn->ext);
174 }
175 
176 
178 {
179  FILE *f;
180  Tfilename fin,fout;
181 
182  /* Create a symbolic link, if it original file exists
183  If a the new file exist, we remove it. */
184  CreateFileName(GetFilePath(fn1),GetFileName(fn1),NULL,ext,&fin);
185  f=fopen(GetFileFullName(&fin),"r");
186  if (f)
187  {
188  fclose(f);
189  CreateFileName(GetFilePath(fn2),GetFileName(fn2),NULL,ext,&fout);
190  f=fopen(GetFileFullName(&fout),"r");
191  if (f)
192  {
193  fclose(f);
194  remove(GetFileFullName(&fout));
195  }
196  symlink(GetFileFullName(&fin),GetFileFullName(&fout));
197  #if (_DEBUG>0)
198  printf("Linking file : %s (-> %s)\n",GetFileFullName(&fout),GetFileFullName(&fin));
199  #endif
200  DeleteFileName(&fout);
201  }
202  DeleteFileName(&fin);
203 }
204 
206 {
207  free(fn->path);
208  free(fn->name);
209  if (fn->ext!=NULL) free(fn->ext);
210  free(fn->baseName);
211  free(fn->fullName);
212 }
Definition of the boolean type.
char * GetFileName(Tfilename *fn)
Gets the file name.
Definition: filename.c:161
#define FALSE
FALSE.
Definition: boolean.h:30
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
char * GetFileBaseName(Tfilename *fn)
Gets the file base name (paht+name) .
Definition: filename.c:166
Data structure to hold the information about the name of a file.
Definition: filename.h:248
char * ext
Definition: filename.h:251
Definition of the Tfilename type and the associated functions.
char * GetFilePath(Tfilename *fn)
Gets the file path.
Definition: filename.c:156
void Error(const char *s)
General error function.
Definition: error.c:80
Error and warning functions.
void DeleteFileName(Tfilename *fn)
Destructor.
Definition: filename.c:205
Definitions of constants and macros used in several parts of the cuik library.
void CreateFileName(char *path, char *name, char *suffix, char *ext, Tfilename *fn)
Constructor.
Definition: filename.c:22
char * GetFileFullName(Tfilename *fn)
Gets the file full name (paht+name+extension).
Definition: filename.c:151
char * path
Definition: filename.h:249
char * name
Definition: filename.h:250
void LinkFileNmeWithExtension(Tfilename *fn1, char *ext, Tfilename *fn2)
Creates a link to a given filename.
Definition: filename.c:177
char * GetFileExtension(Tfilename *fn)
Gets the file extension.
Definition: filename.c:171
char * fullName
Definition: filename.h:253
char * baseName
Definition: filename.h:252