TPCCLIB
Loading...
Searching...
No Matches
iftadd.c
Go to the documentation of this file.
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 "Inserts the items with specified key name from Interfile-type file2 to",
22 "file1. If no key names are specified, then all items are copied.",
23 "If file1 does not exist, it will be created.",
24 "Notice that lines not containing key and value are deleted in file1.",
25 " ",
26 "Usage: @P [Options] filename1 filename2 key(s)",
27 " ",
28 "Options:",
29 " -ovr",
30 " When key name exists, the key and and its value are replaced.",
31 " -nodupl",
32 " When key name exists, the new key and its value are not inserted.",
33 " -stdoptions", // List standard options like --help, -v, etc
34 " ",
35 "Example:",
36 " @P patname.hdr iea345.hdr patient_name patient_id",
37 " ",
38 "See also: iftlist, iftdel, iftisval, iftvalc, iftren, nii_ehdr, e7emhdr",
39 " ",
40 "Keywords: header, IFT, tool",
41 0};
42/*****************************************************************************/
43
44/*****************************************************************************/
45/* Turn on the globbing of the command line, since it is disabled by default in
46 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
47 In Unix&Linux wildcard command line processing is enabled by default. */
48/*
49#undef _CRT_glob
50#define _CRT_glob -1
51*/
52int _dowildcard = -1;
53/*****************************************************************************/
54
55/*****************************************************************************/
59int main(int argc, char **argv)
60{
61 int ai, help=0, version=0, verbose=1;
62 int ret, n=0, fki=0, keyNr;
63 int copy_mode=0; /* 0=always insert, 1=overwrite, 2=cancel if exists */
64 char *cptr, iftfile1[FILENAME_MAX], iftfile2[FILENAME_MAX];
65 IFT ift1, ift2;
66 FILE *fp;
67
68
69 /*
70 * Get arguments
71 */
72 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
73 iftInit(&ift1); iftInit(&ift2);
74 iftfile1[0]=iftfile2[0]=(char)0;
75 /* Options */
76 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
77 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
78 cptr=argv[ai]+1;
79 if(strncasecmp(cptr, "OVR", 2)==0) {
80 copy_mode=1; continue;
81 } else if(strncasecmp(cptr, "NODUPL", 3)==0) {
82 copy_mode=2; continue;
83 }
84 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
85 return(1);
86 } else break; // later arguments may start with '-'
87
88 /* Print help or version? */
89 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
90 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
91 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
92
93 TPCSTATUS status; statusInit(&status);
94 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
95 status.verbose=verbose-1;
96
97 /* Process other arguments, starting from the first non-option */
98 keyNr=0;
99 for(; ai<argc; ai++) {
100 if(!iftfile1[0]) {strlcpy(iftfile1, argv[ai], FILENAME_MAX); continue;}
101 if(!iftfile2[0]) {strlcpy(iftfile2, argv[ai], FILENAME_MAX); continue;}
102 if(keyNr==0) fki=ai;
103 keyNr++;
104 }
105
106 /* Check that we got both filenames */
107 if(!iftfile2[0]) {tpcPrintUsage(argv[0], info, stderr); return(1);}
108
109 /* In verbose mode print arguments and options */
110 if(verbose>1) {
111 for(ai=0; ai<argc; ai++) printf("%s ", argv[ai]);
112 printf("\n");
113 printf("iftfile1 := %s\n", iftfile1);
114 printf("iftfile2 := %s\n", iftfile2);
115 printf("keyNr := %d\n", keyNr);
116 printf("copy_mode := %d\n", copy_mode);
117 }
118
119
120 /*
121 * Read the IFT files
122 */
123
124 if(verbose>1) printf("reading %s\n", iftfile1);
125 fp=fopen(iftfile1, "r"); if(fp==NULL) {
126 fprintf(stdout, "File %s did not exist and was created.\n", iftfile1);
127 } else {
128 ret=iftRead(&ift1, fp, 1, 1, &status); fclose(fp);
129 if(ret) {
130 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
131 iftFree(&ift1); return(2);
132 }
133 if(verbose>2) printf("list1 size: %d item(s)\n", ift1.keyNr);
134 }
135
136
137 if(verbose>1) printf("reading %s\n", iftfile2);
138 fp=fopen(iftfile2, "r"); if(fp==NULL) {
139 fprintf(stderr, "Error: cannot open file %s\n", iftfile2);
140 iftFree(&ift1); return(2);
141 }
142 ret=iftRead(&ift2, fp, 1, 1, &status); fclose(fp);
143 if(ret) {
144 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
145 iftFree(&ift1); iftFree(&ift2); return(2);
146 }
147 if(verbose>2) printf("list2 size: %d item(s)\n", ift2.keyNr);
148
149 /* If no key names were specified by user, then select all */
150 if(keyNr==0) for(int li=0; li<ift2.keyNr; li++) ift2.item[li].sw=1;
151
152 /* If key name(s) were given, then select them */
153 if(keyNr>0) {
154 int n=0, i, si;
155 for(ai=fki; ai<argc; ai++) {
156 si=0;
157 do {
158 i=iftFindKey(&ift2, argv[ai], si);
159 if(i>=0) {ift2.item[i].sw=1; n++; si=i+1;}
160 else if(si==0 && keyNr>1)
161 fprintf(stderr, "Warning: key '%s' was not found.\n", argv[ai]);
162 } while(i>=0);
163 }
164 if(n==0) {
165 if(keyNr==1) fprintf(stderr, "Error: key not found.\n");
166 else fprintf(stderr, "Error: none of keys found.\n");
167 iftFree(&ift1); iftFree(&ift2); return(3);
168 }
169 if(verbose>1) printf("%d key(s) found.\n", n);
170 }
171
172
173 /*
174 * Insert selected items
175 */
176 if(verbose>1) {
177 if(keyNr==0) printf("inserting the contents\n");
178 else printf("inserting selected contents\n");
179 }
180 n=0;
181 for(int i=0; i<ift2.keyNr; i++) {
182 if(verbose>4) printf("key := '%s'\n", ift2.item[i].key);
183 if(ift2.item[i].sw==0) {
184 if(verbose>4) printf(" not selected\n");
185 continue;
186 }
187 if(verbose>4) printf(" selected\n");
188 /* If required, check if key already exists */
189 if(copy_mode>0) {
190 if(iftFindNrOfKeys(&ift1, ift2.item[i].key)>0) {
191 if(copy_mode==1) {
192 if(verbose>0) printf("deleting existing key '%s'\n", ift2.item[i].key);
193 iftDeleteKey(&ift1, ift2.item[i].key);
194 } else {
195 if(verbose>0)
196 fprintf(stderr, "Warning: existing key '%s' not added.\n",
197 ift2.item[i].key);
198 continue;
199 }
200 }
201 }
202 if(verbose>3) printf("adding item with key '%s'\n", ift2.item[i].key);
203 ret=iftPut(&ift1, ift2.item[i].key, ift2.item[i].value,
204 ift2.item[i].comment, &status);
205 if(ret!=TPCERROR_OK)
206 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
207 else n++;
208 }
209 if(n==0) {
210 fprintf(stderr, "Error: no items could be inserted.\n");
211 iftFree(&ift1); iftFree(&ift2);
212 return(9);
213 } else {
214 if(verbose>0)
215 fprintf(stdout, " %d item(s) are inserted in %s\n", n, iftfile1);
216 }
217 iftFree(&ift2);
218
219
220 /*
221 * Write the modified IFT contents.
222 */
223 if(verbose>1) printf("writing modified IFT in %s\n", iftfile1);
224 fp=fopen(iftfile1, "w"); if(fp==NULL) {
225 fprintf(stderr, "Error: cannot open file %s\n", iftfile1);
226 iftFree(&ift1); return(11);
227 }
228 ret=iftWrite(&ift1, fp, &status); fclose(fp);
229 if(ret!=TPCERROR_OK) {
230 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
231 iftFree(&ift1); return(12);
232 }
233 iftFree(&ift1);
234
235 return(0);
236}
237/*****************************************************************************/
238
239/*****************************************************************************/
void iftFree(IFT *ift)
Definition ift.c:37
int iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
Definition ift.c:63
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 iftFindKey(IFT *ift, const char *key, int start_index)
Definition iftfind.c:30
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
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
short int sw
Definition tpcift.h:29
char comment
Definition tpcift.h:27
char * value
Definition tpcift.h:37
char * key
Definition tpcift.h:32
Definition tpcift.h:43
IFT_ITEM * item
Definition tpcift.h:57
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.