9#include "tpcclibConfig.h"
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.",
26 "Usage: @P [Options] filename1 filename2 key(s)",
30 " When key name exists, the key and and its value are replaced.",
32 " When key name exists, the new key and its value are not inserted.",
36 " @P patname.hdr iea345.hdr patient_name patient_id",
38 "See also: iftlist, iftdel, iftisval, iftvalc, iftren, nii_ehdr, e7emhdr",
40 "Keywords: header, IFT, tool",
59int main(
int argc,
char **argv)
61 int ai, help=0, version=0, verbose=1;
62 int ret, n=0, fki=0, keyNr;
64 char *cptr, iftfile1[FILENAME_MAX], iftfile2[FILENAME_MAX];
72 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
74 iftfile1[0]=iftfile2[0]=(char)0;
76 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
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;
84 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
89 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(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;}
107 if(!iftfile2[0]) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
111 for(ai=0; ai<argc; ai++) printf(
"%s ", argv[ai]);
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);
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);
128 ret=
iftRead(&ift1, fp, 1, 1, &status); fclose(fp);
130 fprintf(stderr,
"Error (%d): %s\n", ret,
errorMsg(status.
error));
133 if(verbose>2) printf(
"list1 size: %d item(s)\n", ift1.
keyNr);
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);
142 ret=
iftRead(&ift2, fp, 1, 1, &status); fclose(fp);
144 fprintf(stderr,
"Error (%d): %s\n", ret,
errorMsg(status.
error));
147 if(verbose>2) printf(
"list2 size: %d item(s)\n", ift2.
keyNr);
150 if(keyNr==0)
for(
int li=0; li<ift2.
keyNr; li++) ift2.
item[li].
sw=1;
155 for(ai=fki; ai<argc; ai++) {
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]);
165 if(keyNr==1) fprintf(stderr,
"Error: key not found.\n");
166 else fprintf(stderr,
"Error: none of keys found.\n");
169 if(verbose>1) printf(
"%d key(s) found.\n", n);
177 if(keyNr==0) printf(
"inserting the contents\n");
178 else printf(
"inserting selected contents\n");
181 for(
int i=0; i<ift2.
keyNr; i++) {
182 if(verbose>4) printf(
"key := '%s'\n", ift2.
item[i].
key);
184 if(verbose>4) printf(
" not selected\n");
187 if(verbose>4) printf(
" selected\n");
192 if(verbose>0) printf(
"deleting existing key '%s'\n", ift2.
item[i].
key);
196 fprintf(stderr,
"Warning: existing key '%s' not added.\n",
202 if(verbose>3) printf(
"adding item with key '%s'\n", ift2.
item[i].
key);
210 fprintf(stderr,
"Error: no items could be inserted.\n");
215 fprintf(stdout,
" %d item(s) are inserted in %s\n", n, iftfile1);
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);
228 ret=
iftWrite(&ift1, fp, &status); fclose(fp);
int iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
void iftDeleteKey(IFT *ift, const char *key)
int iftFindNrOfKeys(IFT *ift, const char *key)
int iftFindKey(IFT *ift, const char *key, int start_index)
int iftWrite(IFT *ift, FILE *fp, TPCSTATUS *status)
int iftRead(IFT *ift, FILE *fp, int is_key_required, int is_comment_accepted, TPCSTATUS *status)
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
void statusInit(TPCSTATUS *s)
char * errorMsg(tpcerror e)
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
Header file for library libtpcextensions.
Header file for library libtpcift.