TPCCLIB
Loading...
Searching...
No Matches
tpcfileutil.h File Reference

Header file for libtpcfileutil. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include "tpcextensions.h"
#include "tpcift.h"

Go to the source code of this file.

Functions

int fileBackup (const char *filename, const char *backup_ext, char *status)
int fileExist (const char *filename)
unsigned short int pathFileNr (const char *pathname)
unsigned short int pathFileList (const char *pathname, IFT *ift)
int pathExist (const char *pathname)
int pathRemove (const char *pathname)
int pathRemoveFiles (const char *pathname)
int pathCreate (const char *pathname)

Detailed Description

Header file for libtpcfileutil.

Requires POSIX compatible compiler. The idea is to isolate system dependent functions from most of the code base to allow easier code changes when necessary.

Author
Vesa Oikonen

Definition in file tpcfileutil.h.

Function Documentation

◆ fileBackup()

int fileBackup ( const char * filename,
const char * backup_ext,
char * status )
extern

Check if specified file exists; rename existing file to a backup file. If also backup file exists, then remove that.

Returns
Returns 0, if successful, and <>0 in case of an error.
Parameters
filenameName of file which, if it exists, is renamed to a backup file.
backup_extExtension for backup file; NULL will set the default ".bak" extension.
statusPointer to a string (allocated for at least 64 chars) where error message or other execution status will be written; enter NULL, if not needed.

Definition at line 17 of file backup.c.

25 {
26 char bakfile[FILENAME_MAX];
27 int ret;
28
29 // Check the input
30 if(filename==NULL || strlen(filename)<1) {
31 if(status!=NULL) sprintf(status, "invalid filename");
32 return 1;
33 }
34
35 // Check if file exists; if not then no need to make any backup
36 if(access(filename, 0) == -1) {
37 if(status!=NULL) sprintf(status, "file does not pre-exist");
38 return 0;
39 }
40 // Create filename for the backup file
41 strlcpy(bakfile, filename, FILENAME_MAX);
42 if(backup_ext==NULL) strlcat(bakfile, ".bak", FILENAME_MAX);
43 else strlcat(bakfile, backup_ext, FILENAME_MAX);
44 // If also backup file exists, then just delete it
45 if(access(bakfile, 0) != -1) {
46 ret=remove(bakfile);
47 if(ret!=0) {
48 if(status!=NULL) sprintf(status, "cannot delete previous backup file");
49 return 3;
50 }
51 }
52 // Rename file
53 ret=rename(filename, bakfile);
54 if(ret!=0) {
55 if(status!=NULL) sprintf(status, "cannot rename file as backup");
56 return 5;
57 }
58 if(status!=NULL) sprintf(status, "file renamed as backup");
59 return 0;
60}
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition stringext.c:592

◆ fileExist()

int fileExist ( const char * filename)
extern

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().

◆ pathCreate()

int pathCreate ( const char * pathname)
extern

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

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

◆ pathExist()

int pathExist ( const char * pathname)
extern

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().

◆ pathFileList()

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

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 iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
Definition ift.c:63

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

◆ pathFileNr()

unsigned short int pathFileNr ( const char * pathname)
extern
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().

◆ pathRemove()

int pathRemove ( const char * pathname)
extern

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)
extern

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