TPCCLIB
Loading...
Searching...
No Matches
iftdel.c
1
8/*****************************************************************************/
9#include "tpcclibConfig.h"
10/*****************************************************************************/
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14/*****************************************************************************/
15#include "tpcextensions.h"
16#include "tpcift.h"
17/*****************************************************************************/
18
19/*****************************************************************************/
20static char *info[] = {
21 "Deletes the items with specified key names from an Interfile-type file.",
22 "Notice that also all lines containing no key are deleted.",
23 " ",
24 "Usage: @P [Options] filename key(s)",
25 " ",
26 "Options:",
27 " -stdoptions", // List standard options like --help, -v, etc
28 " ",
29 "Example:",
30 " @P iea345.hdr patient_name patient_id",
31 " ",
32 "See also: iftlist, iftadd, iftren, iftedit, iftisval, taclist, nii_lhdr",
33 " ",
34 "Keywords: header, IFT, tool",
35 0};
36/*****************************************************************************/
37
38/*****************************************************************************/
39/* Turn on the globbing of the command line, since it is disabled by default in
40 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
41 In Unix&Linux wildcard command line processing is enabled by default. */
42/*
43#undef _CRT_glob
44#define _CRT_glob -1
45*/
46int _dowildcard = -1;
47/*****************************************************************************/
48
49/*****************************************************************************/
53int main(int argc, char **argv)
54{
55 int ai, help=0, version=0, verbose=1;
56 int ret, keyNr=0, fki=0;
57 char iftfile[FILENAME_MAX];
58 IFT ift;
59 FILE *fp;
60
61
62 /*
63 * Get arguments
64 */
65 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
66 iftInit(&ift);
67 iftfile[0]=(char)0;
68 /* Options */
69 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
70 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
71 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
72 return(1);
73 } else break; // later arguments may start with '-'
74
75 /* Print help or version? */
76 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
77 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
78 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
79
80 TPCSTATUS status; statusInit(&status);
81 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
82 status.verbose=verbose-1;
83
84 /* Process other arguments, starting from the first non-option */
85 keyNr=0;
86 for(; ai<argc; ai++) {
87 if(!iftfile[0]) {strcpy(iftfile, argv[ai]); continue;}
88 if(keyNr==0) fki=ai;
89 keyNr++;
90 }
91
92 /* Check that we got the filename and at least one key */
93 if(!iftfile[0] || keyNr<1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
94
95 /* In verbose mode print arguments and options */
96 if(verbose>1) {
97 printf("iftfile := %s\n", iftfile);
98 printf("keyNr := %d\n", keyNr);
99 }
100
101
102 /*
103 * Read the IFT file
104 */
105 if(verbose>1) printf("reading %s\n", iftfile);
106 fp=fopen(iftfile, "r"); if(fp==NULL) {
107 fprintf(stderr, "Error: cannot open file %s\n", iftfile);
108 return(2);
109 }
110 ret=iftRead(&ift, fp, 1, 1, &status); fclose(fp);
111 if(ret) {
112 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
113 iftFree(&ift); return(2);
114 }
115 if(verbose>2) printf("list_size: %d item(s)\n", ift.keyNr);
116
117
118 /*
119 * Delete the items with the given key(s)
120 */
121 int n=0;
122 for(ai=fki; ai<argc; ai++) {
123 if(verbose>3) printf("key := '%s'\n", argv[ai]);
124 if(iftFindNrOfKeys(&ift, argv[ai])<1) {
125 if(keyNr>1) fprintf(stderr, "Warning: key '%s' was not found.\n", argv[ai]);
126 continue;
127 }
128 iftDeleteKey(&ift, argv[ai]);
129 n++;
130 } // next key
131 if(n==0) {
132 if(keyNr==1) fprintf(stderr, "Error: key not found.\n");
133 else fprintf(stderr, "Error: none of keys found.\n");
134 iftFree(&ift); return(3);
135 }
136 if(ift.keyNr==0) {
137 fprintf(stderr, "Error: all items would be removed.\n");
138 iftFree(&ift); return(4);
139 }
140
141
142 /*
143 * Write the modified IFT contents.
144 */
145 if(verbose>1) printf("writing modified IFT in %s\n", iftfile);
146 fp=fopen(iftfile, "w"); if(fp==NULL) {
147 fprintf(stderr, "Error: cannot open file %s\n", iftfile);
148 iftFree(&ift); return(11);
149 }
150 ret=iftWrite(&ift, fp, &status); fclose(fp);
151 if(ret!=TPCERROR_OK) {
152 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
153 iftFree(&ift); return(12);
154 }
155 iftFree(&ift);
156
157 return(0);
158}
159/*****************************************************************************/
160
161/*****************************************************************************/
void iftFree(IFT *ift)
Definition ift.c:37
void iftInit(IFT *ift)
Definition ift.c:21
void iftDeleteKey(IFT *ift, const char *key)
Definition iftfind.c:169
int iftFindNrOfKeys(IFT *ift, const char *key)
Definition iftfind.c:142
int iftWrite(IFT *ift, FILE *fp, TPCSTATUS *status)
Definition iftio.c:98
int iftRead(IFT *ift, FILE *fp, int is_key_required, int is_comment_accepted, TPCSTATUS *status)
Definition iftio.c:130
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
char * errorMsg(tpcerror e)
Definition statusmsg.c:68
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
Definition tpcift.h:43
int keyNr
Definition tpcift.h:47
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpcift.