TPCCLIB
Loading...
Searching...
No Matches
filename.c File Reference

Functions for editing file names. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "tpcextensions.h"

Go to the source code of this file.

Functions

void filenameRmPath (char *s)
 
void filenameRmFile (char *s)
 
int filenameRmExtension (char *s)
 
void filenameRmExtensions (char *s)
 
int fnmatch (const char *fname, const char *key)
 
int fncasematch (const char *fname, const char *key)
 
char * filenameGetExtension (const char *s)
 Get the last extension of a file name.
 
char * filenameGetExtensions (const char *s)
 Get all extensions of a file name.
 

Detailed Description

Functions for editing file names.

Definition in file filename.c.

Function Documentation

◆ filenameGetExtension()

char * filenameGetExtension ( const char * s)

Get the last extension of a file name.

Extension(s) in pathname are not searched for. Note that pointer points to the original string.

See also
filenameGetExtensions, filenameRmExtension, strTrimLeft
Returns
Pointer to the file name extension starting with '.', or NULL if no extension is found.
Parameters
sPointer to string; string is not edited here.

Definition at line 178 of file filename.c.

181 {
182 if(s==NULL || strnlen(s, 1)<1) return((char*)NULL);
183 /* Identify path */
184 char *pptr=strrchr(s, '/'); if(pptr==NULL) pptr=strrchr(s, '\\');
185 if(pptr==NULL) pptr=(char*)s; else pptr++;
186 /* Search for the last '.' after the path */
187 char *cptr=strrchr(pptr, '.'); if(cptr==NULL) return(cptr);
188 /* If file name starts with '.', that is not counted as extension */
189 if(strlen(pptr)==strlen(cptr)) return((char*)NULL);
190 return(cptr);
191}
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566

Referenced by abssIdFromFName(), imgWriteNifti(), micropetExists(), niftiBasename(), and tacRead().

◆ filenameGetExtensions()

char * filenameGetExtensions ( const char * s)

Get all extensions of a file name.

Extension(s) in pathname are not searched for. Note that pointer points to the original string.

See also
filenameGetExtension, filenameRmExtensions, strTrimLeft
Returns
Pointer to the file name extension starting with '.', or NULL if no extension is found.
Parameters
sPointer to string; string is not edited here.

Definition at line 203 of file filename.c.

206 {
207 if(s==NULL || strnlen(s, 1)<1) return((char*)NULL);
208 /* Identify path */
209 char *pptr=strrchr(s, '/'); if(pptr==NULL) pptr=strrchr(s, '\\');
210 if(pptr==NULL) pptr=(char*)s; else pptr++;
211 /* Search for the last '.' after the path, ignoring first character in case file name starts with '.' */
212 char *cptr=strchr(pptr+1, '.');
213 return(cptr);
214}

Referenced by imgFormatFromFName(), and parFormatFromExtension().

◆ filenameRmExtension()

int filenameRmExtension ( char * s)

Remove the last extension from file name.

See also
filenameGetExtension, filenameRmExtensions, strTrimLeft
Author
Vesa Oikonen
Returns
1 if extension was found (and removed), 0 if not.
Parameters
sPointer to string.

Definition at line 71 of file filename.c.

74 {
75 if(s==NULL || strnlen(s, 1)<1) return(0);
76 char *cptr;
77 cptr=strrchr(s, '.'); if(cptr==NULL) return(0);
78 if(cptr[1]=='/' || cptr[1]=='\\') return(0);
79 *cptr=(char)0;
80 return(1);
81}

Referenced by filenameRmExtensions(), micropetExists(), tpcHtmlUsage(), tpcPrintBuild(), tpcPrintUsage(), and tpcProgramName().

◆ filenameRmExtensions()

void filenameRmExtensions ( char * s)

Remove all extensions from file name.

See also
filenameGetExtensions, filenameRmExtension, strdelstr, strTrimLeft
Author
Vesa Oikonen
Parameters
sPointer to string.

Definition at line 89 of file filename.c.

92 {
93 if(s==NULL || strnlen(s, 1)<1) return;
94 while(filenameRmExtension(s)) {}
95 return;
96}
int filenameRmExtension(char *s)
Definition filename.c:71

Referenced by studynrFromFilename().

◆ filenameRmFile()

void filenameRmFile ( char * s)

Remove file name from file name string including path, leaving only path without trailing '/'. If there is no path, then result is ".". For root path the result string is empty. See also POSIX function dirname() in libgen.h.

See also
filenameRmPath, filenameRmExtensions, pathExist
Author
Vesa Oikonen
Parameters
sPointer to string.

Definition at line 44 of file filename.c.

47 {
48 if(s==NULL) return;
49 unsigned int len=strnlen(s, FILENAME_MAX);
50 if(len==0) return;
51 //if(len==1) {s[0]='.'; return;}
52 char *cptr;
53 cptr=strrchr(s, '/'); if(cptr==NULL) cptr=strrchr(s, '\\');
54 if(cptr!=NULL) {
55 *cptr=(char)0;
56 if(strcmp(s, "/")==0 || strcmp(s, "\\")==0) s[0]=(char)0;
57 return;
58 }
59 if(strcmp(s, "..")==0) return;
60 s[0]='.'; s[1]=(char)0;
61 return;
62}

Referenced by dcmFileList(), and imgWriteNifti().

◆ filenameRmPath()

void filenameRmPath ( char * s)

Remove path from file name. See also POSIX function basename() in libgen.h.

See also
filenameRmFile, filenameRmExtensions, strTrimleft
Author
Vesa Oikonen
Parameters
sPointer to string.

Definition at line 20 of file filename.c.

23 {
24 if(s==NULL || strnlen(s, 1)<1) return;
25 char *cptr;
26 cptr=strrchr(s, '/'); if(cptr==NULL) cptr=strrchr(s, '\\');
27 if(cptr==NULL) return;
28 cptr++;
29 int i, n=strlen(cptr);
30 for(i=0; i<n; i++, cptr++) s[i]=*cptr;
31 s[i]=(char)0;
32 return;
33}

Referenced by imgWriteNifti(), niftiCreateFNames(), studynrFromFilename(), tpcHtmlUsage(), tpcPrintBuild(), tpcPrintUsage(), and tpcProgramName().

◆ fncasematch()

int fncasematch ( const char * fname,
const char * key )

Case-independent check whether string fname matches string key, which may contain wild-cards ? and *.

See also
fnmatch, fileExist, pathExist
Returns
1 if strings do match and 0 if not.
Parameters
fnamefile name that is evaluated.
keykey string which may contain wild-cards '?' and '*'.

Definition at line 139 of file filename.c.

144 {
145 if(fname==NULL || key==NULL) return(0);
146 char *key_ptr=NULL, *fname_ptr=NULL;
147
148 while((*key)&&(*fname)) {
149 if((*key=='?')||(toupper((int)*key)==toupper((int)*fname))) {
150 key++; fname++;
151 } else if(*key=='*') {
152 if(toupper((int)*(key+1))==toupper((int)*fname)) {
153 key_ptr=(char*)key++; fname_ptr=(char*)fname+1;
154 } else {
155 fname++;
156 if(*(key+1)=='?') {key_ptr=(char*)key++; fname_ptr=(char*)fname;}
157 }
158 } else if((key_ptr!=NULL) && (*fname_ptr)) {
159 return(fnmatch(key_ptr, fname_ptr));
160 } else {
161 return(0);
162 }
163 }
164 if((*fname)&&(key_ptr!=NULL)) {return(fnmatch(key_ptr, fname_ptr));}
165 else {if(*key=='*') key++; return(toupper((int)*key)==toupper((int)*fname));}
166}
int fnmatch(const char *fname, const char *key)
Definition filename.c:104

Referenced by roinameMatch().

◆ fnmatch()

int fnmatch ( const char * fname,
const char * key )

Check if string fname matches string key, which may contain wild cards * ? and *.

See also
fncasematch, fileExist, pathExist, roinameMatch, strncpyClean
Returns
1 if strings do match and 0 if not.
Parameters
fnamefile name that is evaluated.
keykey string which may contain wild cards '?' and '*'.

Definition at line 104 of file filename.c.

109 {
110 if(fname==NULL || key==NULL) return(0);
111 char *key_ptr=NULL, *fname_ptr=NULL;
112
113 while((*key)&&(*fname)) {
114 if((*key=='?')||(*key==*fname)) {
115 key++; fname++;
116 } else if(*key=='*') {
117 if(*(key+1)==*fname) {key_ptr=(char*)key++; fname_ptr=(char*)fname+1;
118 } else {
119 fname++;
120 if(*(key+1)=='?') {key_ptr=(char*)key++; fname_ptr=(char*)fname;}
121 }
122 } else if((key_ptr!=NULL) && (*fname_ptr)) {
123 return(fnmatch(key_ptr, fname_ptr));
124 } else {
125 return(0);
126 }
127 }
128 if((*fname)&&(key_ptr!=NULL)) {return(fnmatch(key_ptr, fname_ptr));}
129 else {if(*key=='*') key++; return(*key==*fname);}
130}

Referenced by fncasematch(), and fnmatch().