TPCCLIB
Loading...
Searching...
No Matches
resdel.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#include <math.h>
14#include <time.h>
15/*****************************************************************************/
16#include "libtpcmisc.h"
17#include "libtpccurveio.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
21static char *info[] = {
22 "Removes region(s) from result file.",
23 "Optionally, modified results are written in a new file.",
24 " ",
25 "Usage: @P filename tacid",
26 " ",
27 "Options:",
28 " -o=<filename>",
29 " Remaining results are written in specified file;",
30 " by default, input file is modified.",
31 " --dry",
32 " Dry run: nothing is actually written on disk.",
33 " -stdoptions", // List standard options like --help, -v, etc
34 " ",
35 "Example:",
36 " @P ug2345ki.res cereb",
37 " ",
38 "See also: reslist, resrmdpl, rescoll, parcoll, pardiff, paradd, tacdel",
39 " ",
40 "Keywords: results, 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 ri, n=0, dry_run=0;
63 RES res;
64 char *cptr, resfile1[FILENAME_MAX], resfile2[FILENAME_MAX], tmp[128];
65
66
67 /*
68 * Get arguments
69 */
70 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
71 resfile1[0]=resfile2[0]=(char)0;
72 resInit(&res);
73 /* Options */
74 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
75 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
76 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!cptr[0]) continue;
77 if(strncasecmp(cptr, "O=", 2)==0) {
78 strlcpy(resfile2, cptr+2, FILENAME_MAX); if(strlen(resfile2)>0) continue;
79 } else if(strcasecmp(cptr, "DRY")==0) {
80 dry_run=1; continue;
81 }
82 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
83 return(1);
84 } else break;
85
86 /* Print help or version? */
87 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
88 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
89 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
90
91 /* Process other arguments, starting from the first non-option */
92 for(; ai<argc; ai++) {
93 if(!resfile1[0]) {strlcpy(resfile1, argv[ai], FILENAME_MAX); continue;}
94 else break;
95 }
96
97 /* Is something missing? */
98 if(!resfile1[0] || ai>=argc) {
99 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
100 return(1);
101 }
102 if(!resfile2[0]) strcpy(resfile2, resfile1);
103
104
105 /* In verbose mode print arguments and options */
106 if(verbose>1) {
107 printf("dry_run := %d\n", dry_run);
108 printf("resfile1 := %s\n", resfile1);
109 printf("resfile2 := %s\n", resfile2);
110 }
111 if(verbose>4) RESULT_TEST=verbose-4; else RESULT_TEST=0;
112
113
114 /*
115 * Read result file
116 */
117 if(verbose>1) printf("reading %s\n", resfile1);
118 if(resRead(resfile1, &res, verbose-2)) {
119 fprintf(stderr, "Error in reading '%s': %s\n", resfile1, reserrmsg);
120 resEmpty(&res);
121 return(2);
122 }
123
124
125 /*
126 * Select the regions with user-defined IDs
127 */
128 for(; ai<argc; ai++) {
129 if(verbose>1) printf("searching for region %s\n", argv[ai]);
130 n=0; ri=atoi(argv[ai])-1; /* maybe its the region number? */
131 if(ri<res.voiNr && ri>=0) { /* select that region if it exists */
132 res.voi[ri].sw2=1; n=1;
133 } else { /* if not then it is the region name */
134 n=resSelectRegions(&res, argv[ai], 0);
135 for(ri=0; ri<res.voiNr; ri++) if(res.voi[ri].sw) res.voi[ri].sw2=1;
136 }
137 if(n==0) {
138 fprintf(stderr, "Error: %s did not match with any of the regions.\n",
139 argv[ai]);
140 resEmpty(&res); return(1);
141 }
142 }
143
144
145 /*
146 * Delete the selected regions
147 */
148 if(verbose>0 || dry_run==1) {
149 if(dry_run==0) strcpy(tmp, "deleted"); else strcpy(tmp, "would be deleted");
150 for(ri=n=0; ri<res.voiNr; ri++) {
151 printf(" %3d: %s ", ri+1, res.voi[ri].name);
152 if(res.voi[ri].sw2) {n++; printf("%s\n", tmp);} else printf("\n");
153 }
154 }
155 /* Give warning if no regions were deleted, and quit */
156 if(n<1) {
157 fprintf(stderr, "Warning: datafile does not contain specified regions.\n");
158 resEmpty(&res); return(0);
159 }
160 /* and if all were deleted, tell that too, and quit */
161 if(res.voiNr==n) {
162 fprintf(stderr, "Warning: all regions were selected for removal; aborted.\n");
163 resEmpty(&res); return(0);
164 }
165 /* now do the deleting */
166 ri=res.voiNr-1;
167 while(ri>=0) {
168 if(res.voi[ri].sw2) resDelete(&res, ri);
169 ri--;
170 }
171
172
173 if(dry_run==0) {
174 /*
175 * Write the remaining results
176 */
177 if(verbose>2) printf("writing %s\n", resfile2);
178 if(resWrite(&res, resfile2, verbose-3)) {
179 fprintf(stderr, "Error in writing '%s': %s\n", resfile2, reserrmsg);
180 resEmpty(&res); return(11);
181 }
182 if(verbose>0) printf(" %s written.\n", resfile2);
183 } else {
184 printf(" would write %s\n", resfile2);
185 }
186
187 /* Free memory */
188 resEmpty(&res);
189
190 return(0);
191}
192/*****************************************************************************/
193
194/*****************************************************************************/
Header file for libtpccurveio.
char reserrmsg[64]
Definition result.c:6
void resInit(RES *res)
Definition result.c:52
int resSelectRegions(RES *res, char *region_name, int reset)
Definition result.c:1417
int resWrite(RES *res, char *filename, int verbose)
Definition result.c:565
int resDelete(RES *res, int voi)
Definition result.c:1342
int resRead(char *filename, RES *res, int verbose)
Definition result.c:199
void resEmpty(RES *res)
Definition result.c:22
int RESULT_TEST
Definition result.c:5
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:40
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:213
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:383
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:158
int voiNr
ResVOI * voi
char name[MAX_REGIONNAME_LEN+1]