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

Functions for working with paths. More...

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

Go to the source code of this file.

Functions

int pathExist (const char *pathname)
 
int pathRemove (const char *pathname)
 
int pathRemoveFiles (const char *pathname)
 
int pathCreate (const char *pathname)
 

Detailed Description

Functions for working with paths.

Definition in file pathexist.c.

Function Documentation

◆ pathCreate()

int pathCreate ( const char * pathname)

Create path.

See also
pathExist, pathRemove, pathFileNr, filenameRmFile
Returns
Returns 0, if path could be created or exists already, 1, if it does not exist and cannot be created.
Parameters
pathnameName of path to create; can contain subdirectories.

Definition at line 81 of file pathexist.c.

84 {
85 //printf("pathname='%s'\n", pathname);
86 if(pathname==NULL || strnlen(pathname, 2)<1) return(1);
87 if(fileExist(pathname)) return(1);
88 if(pathExist(pathname)) return(0); // exists already
89 // check if this is a subfolder
90 char *lpath=strdup(pathname);
91 char *cptr=strrchr(lpath, '/'); if(cptr==NULL) cptr=strrchr(lpath, '\\');
92 if(cptr!=NULL) { // it is a subfolder, so call recursively with the parent folder name
93 *cptr=(char)0;
94 int ret=pathCreate(lpath);
95 if(ret!=0) {free(lpath); return(1);}
96 }
97 free(lpath);
98 // now try to create the folder
99#ifndef HAVE__MKDIR
100 // other than Windows
101 if(mkdir(pathname, 0777)!=0 && errno!=EEXIST) return(1); else return(0);
102#else
103 // Windows (MinGW)
104 if(_mkdir(pathname)!=0 && errno!=EEXIST) return(1); else return(0);
105#endif
106}
int fileExist(const char *filename)
Definition filexist.c:17
int pathExist(const char *pathname)
Definition pathexist.c:17
int pathCreate(const char *pathname)
Definition pathexist.c:81
char * strdup(const char *s)
Definition stringext.c:185
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566

Referenced by imgWriteDICOM(), imgWriteNifti(), and pathCreate().

◆ pathExist()

int pathExist ( const char * pathname)

Check if specified directory (path) exists.

See also
fileExist, pathFileNr, filenameRmFile
Returns
Returns 1, if path exists, and 0 if it does not or is not a directory.
Parameters
pathnameName of path to check.

Definition at line 17 of file pathexist.c.

20 {
21 if(pathname==NULL) return(0);
22 int len=strlen(pathname);
23 if(len<1) return(0);
24 char *str=strdup(pathname);
25 if(len>1 && str[len-1]=='/') {
26 if(len<2) {free(str); return(0);}
27 str[len-1]='\0';
28 }
29 struct stat fst;
30 if(stat(str, &fst)!=0) {free(str); return(0);}
31 free(str);
32 if(S_ISDIR(fst.st_mode)) return(1);
33 return(0);
34}

Referenced by dcmFileList(), imgFormatFromFName(), imgRead(), imgWriteDICOM(), pathCreate(), pathFileList(), pathFileNr(), pathRemove(), and pathRemoveFiles().

◆ pathRemove()

int pathRemove ( const char * pathname)

Remove path.

See also
fileExist, pathFileNr, filenameRmFile
Returns
Returns 0, if path could be removed or it did not exist, 1, if it exists and cannot be removed.
Parameters
pathnameName of path to remove.

Definition at line 43 of file pathexist.c.

46 {
47 if(fileExist(pathname)) return(1);
48 if(!pathExist(pathname)) return(0);
49 if(rmdir(pathname)==0) return(0); else return(1);
50}

Referenced by imgWriteDICOM().

◆ pathRemoveFiles()

int pathRemoveFiles ( const char * pathname)

Remove regular files in specified path. Subfolders are not affected.

See also
fileExist, pathFileNr, pathFileList, pathRemove
Returns
Returns 0, if files could be removed or did not exist, 1, in case of an error.
Parameters
pathnameName of path from where to remove files.

Definition at line 58 of file pathexist.c.

61 {
62 if(fileExist(pathname)) return(1); // Error if filename was given
63 if(!pathExist(pathname)) return(0); // No error if path does not exist
64 IFT ift; iftInit(&ift);
65 unsigned short int n=pathFileList(pathname, &ift);
66 if(n==0) {iftFree(&ift); return(0);}
67 for(int i=0; i<ift.keyNr; i++) {
68 if(remove(ift.item[i].value)) {iftFree(&ift); return(1);}
69 }
70 iftFree(&ift);
71 return(0);
72}
unsigned short int pathFileList(const char *pathname, IFT *ift)
Definition filexist.c:83
void iftFree(IFT *ift)
Definition ift.c:37
void iftInit(IFT *ift)
Definition ift.c:21
char * value
Definition tpcift.h:37
Definition tpcift.h:43
IFT_ITEM * item
Definition tpcift.h:57
int keyNr
Definition tpcift.h:47