TPCCLIB
Loading...
Searching...
No Matches
iftedit.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13/*****************************************************************************/
14#include "tpcextensions.h"
15#include "tpcift.h"
16/*****************************************************************************/
17
18/*****************************************************************************/
19static char *info[] = {
20 "Change the value of specified key in Interfile-type file or files that can",
21 "contain similar header data (such as TAC and parameter files).",
22 "Any additional lines with the same key name are removed.",
23 "If new value is not given, then previous key and value are deleted.",
24 "If key does not exist in the file, then the key with value are added.",
25 " ",
26 "Usage: @P [Options] filename key [value]",
27 " ",
28 "Options:",
29 " -stdoptions", // List standard options like --help, -v, etc
30 " ",
31 "Example:",
32 " @P rabbit12.par studynr tg2r12",
33 " ",
34 "See also: iftlist, iftren, iftvalc, iftadd, iftdel, iftisval",
35 " ",
36 "Keywords: header, parameter, IFT, tool",
37 0};
38/*****************************************************************************/
39
40/*****************************************************************************/
41/* Turn on the globbing of the command line, since it is disabled by default in
42 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
43 In Unix&Linux wildcard command line processing is enabled by default. */
44/*
45#undef _CRT_glob
46#define _CRT_glob -1
47*/
48int _dowildcard = -1;
49/*****************************************************************************/
50
51/*****************************************************************************/
55int main(int argc, char **argv)
56{
57 int ai, help=0, version=0, verbose=1;
58 int ret;
59 char iftfile[FILENAME_MAX], keyname[FILENAME_MAX], keyvalue[FILENAME_MAX];
60
61
62 /*
63 * Get arguments
64 */
65 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
66 iftfile[0]=keyname[0]=keyvalue[0]=(char)0;
67 /* Options */
68 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
69 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
70 //char *cptr=argv[ai]+1;
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 if(ai<argc) strlcpy(iftfile, argv[ai++], FILENAME_MAX);
86 if(ai<argc) strlcpy(keyname, argv[ai++], FILENAME_MAX);
87 if(ai<argc) strlcpy(keyvalue, argv[ai++], FILENAME_MAX);
88 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
89
90 /* Is something missing? */
91 if(!keyname[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
92
93 /* In verbose mode print arguments and options */
94 if(verbose>1) {
95 for(ai=0; ai<argc; ai++) printf("%s ", argv[ai]);
96 printf("\n"); fflush(stdout);
97 printf("iftfile := %s\n", iftfile);
98 printf("keyname := %s\n", keyname);
99 printf("keyvalue := %s\n", keyvalue);
100 }
101
102
103 /*
104 * Read IFT file
105 */
106 IFT ift; iftInit(&ift);
107
108 if(verbose>1) {printf("reading %s\n", iftfile); fflush(stdout);}
109 FILE *fp=fopen(iftfile, "r"); if(fp==NULL) {
110 fprintf(stderr, "Error: cannot open file %s\n", iftfile);
111 return(2);
112 }
113 ret=iftRead(&ift, fp, 0, 1, &status); fclose(fp);
114 if(ret) {
115 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
116 iftFree(&ift); return(2);
117 }
118 if(verbose>2) {printf("list size: %d item(s)\n", ift.keyNr); fflush(stdout);}
119 int foundNr=iftFindNrOfKeys(&ift, keyname);
120 if(verbose>1) {printf("%d match(es) found.\n", foundNr); fflush(stdout);}
121 if(foundNr==0 && !keyvalue[0]) {
122 fprintf(stderr, "Note: file contains no key '%s'\n", keyname);
123 iftFree(&ift); return(0);
124 }
125 if(foundNr>1) {
126 if(verbose>0) {printf("deleting duplicate keys\n"); fflush(stdout);}
127 iftDeleteDuplicateKeys(&ift, &status);
128 }
129
130 /*
131 * Find the key from IFT
132 */
133 int li;
134 /* Search for the first occurrence of keyname */
135 li=iftFindKey(&ift, keyname, 0);
136 if(li<0) {
137 if(verbose>2) {printf("adding new key\n"); fflush(stdout);}
138 if(iftPut(&ift, keyname, keyvalue, 1, &status)!=TPCERROR_OK) {
139 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
140 iftFree(&ift); return(3);
141 }
142 if(verbose>0) {printf("added new key with given value\n"); fflush(stdout);}
143 /* saving later */
144 } else if(keyvalue[0]) {
145 if(verbose>3) printf("key_index := %d\n", li);
146 if(verbose>0) printf("resetting value '%s' -> '%s'\n", ift.item[li].value, keyvalue);
147 if(iftReplaceValue(&ift, li, keyvalue, &status)!=TPCERROR_OK) {
148 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
149 iftFree(&ift); return(3);
150 }
151 if(verbose>0) {printf("added new key with given value\n"); fflush(stdout);}
152 } else {
153 if(verbose>3) printf("key_index := %d\n", li);
154 if(verbose>0) printf("removing '%s','%s'\n", ift.item[li].key, ift.item[li].value);
155 ret=iftDelete(&ift, li);
156 if(ret!=TPCERROR_OK) {
157 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
158 iftFree(&ift); return(3);
159 }
160 }
161
162
163 /*
164 * Write the modified IFT contents.
165 */
166 if(verbose>1) printf("writing modified IFT in %s\n", iftfile);
167 fp=fopen(iftfile, "w"); if(fp==NULL) {
168 fprintf(stderr, "Error: cannot open file %s\n", iftfile);
169 iftFree(&ift); return(11);
170 }
171 ret=iftWrite(&ift, fp, &status); fclose(fp);
172 if(ret!=TPCERROR_OK) {
173 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
174 iftFree(&ift); return(12);
175 }
176 iftFree(&ift);
177
178 return(0);
179}
180/*****************************************************************************/
181
182/*****************************************************************************/
void iftFree(IFT *ift)
Definition ift.c:37
int iftReplaceValue(IFT *ift, int i, const char *value, TPCSTATUS *status)
Definition ift.c:268
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
int iftDeleteDuplicateKeys(IFT *ift, TPCSTATUS *status)
Definition ift.c:348
int iftDelete(IFT *ift, int index)
Definition ift.c:206
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
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.