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

Functions for checking if file exists or how many files do exist. More...

#include "tpcclibConfig.h"
#include "tpcextensions.h"
#include "tpcfileutil.h"

Go to the source code of this file.

Functions

int fileExist (const char *filename)
 
unsigned short int pathFileNr (const char *pathname)
 
unsigned short int pathFileList (const char *pathname, IFT *ift)
 

Detailed Description

Functions for checking if file exists or how many files do exist.

Definition in file filexist.c.

Function Documentation

◆ fileExist()

int fileExist ( const char * filename)

Check if specified file (not path) exists.

See also
pathExist, pathFileNr, pathFileList, filenameRmPath, fncasematch, asciiFileRead
Returns
Returns 1, if file exists, and 0 if it does not or is not a regular file.
Parameters
filenameName of file to check.

Definition at line 17 of file filexist.c.

20 {
21 if(filename==NULL || strnlen(filename, 2)<1) return(0);
22 struct stat fst;
23 if(stat(filename, &fst)!=0) return(0);
24 if(S_ISREG(fst.st_mode)) return(1);
25 return(0);
26}
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566

Referenced by anaExists(), dcmFileList(), imgRead(), imgWriteNifti(), micropetExists(), niftiExists(), niftiWriteHeader(), pathCreate(), pathFileList(), pathFileNr(), pathRemove(), and pathRemoveFiles().

◆ pathFileList()

unsigned short int pathFileList ( const char * pathname,
IFT * ift )

List the names regular files in specified path into IFT structure. File names will contain the directory path.

Returns
Returns the number of stored regular file names.
See also
pathFileNr, iftInit, iftFree
Parameters
pathnameName of path.
iftPointer to initiated IFT structure; any previous contents are kept.

Definition at line 83 of file filexist.c.

88 {
89 if(pathname==NULL || strnlen(pathname, 2)<1) return(0);
90 if(ift==NULL) return(0);
91 /* Check that path exists */
92 if(!pathExist(pathname)) return(0);
93 /* Open the directory */
94 DIR *dp;
95 dp=opendir(pathname); if(dp==NULL) return(0);
96#if(1)
97 char tempname[2*FILENAME_MAX];
98 unsigned short int n=0;
99 struct dirent *de;
100 /* Go throught the directory */
101 while((de=readdir(dp))!=NULL) {
102 if(de->d_name[0]=='.') continue; /* Ignore hidden and 'system' dirs */
103 /* Combine path and name */
104 sprintf(tempname, "%s/%s", pathname, de->d_name);
105 /* Check that it is regular file */
106 if(!fileExist(tempname)) continue;
107 /* Add to IFT */
108 if(iftPut(ift, NULL, tempname, 0, NULL)) break;
109 n++;
110 }
111#else // readdir_r deprecated
112 struct dirent de, *deptr;
113 char tempname[2*FILENAME_MAX];
114 unsigned short int n=0;
115 /* Go throught the directory */
116 while(readdir_r(dp, &de, &deptr)==0 && deptr!=NULL) {
117 if(de.d_name[0]=='.') continue; /* Ignore hidden and 'system' dirs */
118 /* Combine path and name */
119 sprintf(tempname, "%s/%s", pathname, de.d_name);
120 /* Check that it is regular file */
121 if(!fileExist(tempname)) continue;
122 /* Add to IFT */
123 if(iftPut(ift, NULL, tempname, 0, NULL)) break;
124 n++;
125 }
126#endif
127 closedir(dp);
128 return(n);
129}
int fileExist(const char *filename)
Definition filexist.c:17
int iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
Definition ift.c:63
int pathExist(const char *pathname)
Definition pathexist.c:17

Referenced by dcmFileList(), imgRead(), and pathRemoveFiles().

◆ pathFileNr()

unsigned short int pathFileNr ( const char * pathname)
Returns
Returns the number of regular files in specified path.
See also
pathExist, fileExist, pathFileList
Parameters
pathnameName of path.

Definition at line 33 of file filexist.c.

36 {
37 if(pathname==NULL || strnlen(pathname, 2)<1) return(0);
38 /* Check that path exists */
39 if(!pathExist(pathname)) return(0);
40 /* Open the directory */
41 DIR *dp;
42 dp=opendir(pathname); if(dp==NULL) return(0);
43#if(1)
44 char tempname[2*FILENAME_MAX];
45 unsigned short int n=0;
46 struct dirent *de;
47 /* Go through the directory */
48 while((de=readdir(dp))!=NULL) {
49 //printf("d_name='%s'\n", de.d_name);
50 if(de->d_name[0]=='.') continue; /* Ignore hidden and 'system' dirs */
51 /* Combine path and name */
52 sprintf(tempname, "%s/%s", pathname, de->d_name);
53 //printf("name='%s'\n", tempname);
54 /* Check that it is regular file */
55 if(fileExist(tempname)) n++;
56 }
57#else // readdir_r deprecated
58 struct dirent de, *deptr;
59 char tempname[2*FILENAME_MAX];
60 unsigned short int n=0;
61 /* Go through the directory */
62 while(readdir_r(dp, &de, &deptr)==0 && deptr!=NULL) {
63 //printf("d_name='%s'\n", de.d_name);
64 if(de.d_name[0]=='.') continue; /* Ignore hidden and 'system' dirs */
65 /* Combine path and name */
66 sprintf(tempname, "%s/%s", pathname, de.d_name);
67 //printf("name='%s'\n", tempname);
68 /* Check that it is regular file */
69 if(fileExist(tempname)) n++;
70 }
71#endif
72 closedir(dp);
73 return(n);
74}

Referenced by imgRead(), and imgWriteDICOM().