TPCCLIB
Loading...
Searching...
No Matches
iftren.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 "Renames the specified key in Interfile-type file.",
21 "Only the first occurrence of key name is renamed.",
22 "If new key name is not given, then previous key name is deleted.",
23 "Empty string can be entered as current key name to add key name for",
24 "the first line that currently has no key name.",
25 " ",
26 "Usage: @P [Options] filename key [newkey]",
27 " ",
28 "Options:",
29 " -stdoptions", // List standard options like --help, -v, etc
30 " ",
31 "Example:",
32 " @P parameters.ift p1 start_time",
33 " ",
34 "See also: iftlist, iftedit, iftadd, iftdel, iftisval, iftvalc, iftmatch",
35 " ",
36 "Keywords: header, 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], keyname1[FILENAME_MAX], keyname2[FILENAME_MAX];
60
61
62 /*
63 * Get arguments
64 */
65 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
66 iftfile[0]=keyname1[0]=keyname2[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(keyname1, argv[ai++], FILENAME_MAX);
87 if(ai<argc) strlcpy(keyname2, argv[ai++], FILENAME_MAX);
88 if(ai<argc) {fprintf(stderr, "Error: too many arguments.\n"); return(1);}
89
90 /* Is something missing? */
91 if(!keyname1[0] && !keyname2[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
92 /* Note that:
93 - if keyname1 is empty, then user wants to add new key name
94 - if keyname2 is empty or missing, then user wants to remove key name
95 - both cannot be empty or missing
96 */
97
98 /* In verbose mode print arguments and options */
99 if(verbose>1) {
100 for(ai=0; ai<argc; ai++) printf("%s ", argv[ai]);
101 printf("\n"); fflush(stdout);
102 printf("iftfile := %s\n", iftfile);
103 printf("keyname1 := %s\n", keyname1);
104 printf("keyname2 := %s\n", keyname2);
105 }
106
107
108 /*
109 * Read IFT file
110 */
111 IFT ift; iftInit(&ift);
112
113 if(verbose>1) printf("reading %s\n", iftfile);
114 FILE *fp=fopen(iftfile, "r"); if(fp==NULL) {
115 fprintf(stderr, "Error: cannot open file %s\n", iftfile);
116 return(2);
117 }
118 ret=iftRead(&ift, fp, 0, 1, &status); fclose(fp);
119 if(ret) {
120 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
121 iftFree(&ift); return(2);
122 }
123 if(verbose>2) printf("list size: %d item(s)\n", ift.keyNr);
124
125 /*
126 * Find the key from IFT
127 */
128 int li;
129 if(strlen(keyname1)==0) {
130 /* Search for the first empty key name */
131 li=-1;
132 for(int i=0; i<ift.keyNr; i++) {
133 if(ift.item[i].key==NULL || strlen(ift.item[i].key)==0) {li=i; break;}
134 }
135 } else {
136 /* Search for the first occurrence of keyname */
137 li=iftFindKey(&ift, keyname1, 0);
138 }
139 if(li<0) {
140 fprintf(stderr, "Error: file contains no key '%s'\n", keyname1);
141 iftFree(&ift); return(3);
142 }
143 if(verbose>3) printf("key_index := %d\n", li);
144
145 /*
146 * Replace the key name
147 */
148 if(verbose>1) printf("renaming key '%s' -> '%s'\n", ift.item[li].key, keyname2);
149 ret=iftReplaceKey(&ift, li, keyname2, &status);
150 if(ret) {
151 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
152 iftFree(&ift); return(4);
153 }
154
155
156 /*
157 * Write the modified IFT contents.
158 */
159 if(verbose>1) printf("writing modified IFT in %s\n", iftfile);
160 fp=fopen(iftfile, "w"); if(fp==NULL) {
161 fprintf(stderr, "Error: cannot open file %s\n", iftfile);
162 iftFree(&ift); return(11);
163 }
164 ret=iftWrite(&ift, fp, &status); fclose(fp);
165 if(ret!=TPCERROR_OK) {
166 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
167 iftFree(&ift); return(12);
168 }
169 iftFree(&ift);
170
171 return(0);
172}
173/*****************************************************************************/
174
175/*****************************************************************************/
void iftFree(IFT *ift)
Definition ift.c:37
void iftInit(IFT *ift)
Definition ift.c:21
int iftReplaceKey(IFT *ift, int i, const char *key, TPCSTATUS *status)
Definition ift.c:307
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 * 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.