TPCCLIB
Loading...
Searching...
No Matches
rmcmnts.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/*****************************************************************************/
16
17/*****************************************************************************/
18static char *info[] = {
19 "Remove comment lines from data files.",
20 " ",
21 "Usage: @P [Options] datafile(s)",
22 " ",
23 "Options:",
24 " -semicolon=<y|N>",
25 " Semicolon is used to mark comment line (y) or not (n, default).",
26 " -sharp=<Y|n>",
27 " Sharp ('#') is used to mark comment line (y, default) or not (n).",
28 " -stdoptions", // List standard options like --help, -v, etc
29 " ",
30 "See also: iftlist, taclist, iftdel, csv2ift, parformat",
31 " ",
32 "Keywords: TAC, CSV, IFT, software testing, reporting",
33 0};
34/*****************************************************************************/
35
36/*****************************************************************************/
37/* Turn on the globbing of the command line, since it is disabled by default in
38 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
39 In Unix&Linux wildcard command line processing is enabled by default. */
40/*
41#undef _CRT_glob
42#define _CRT_glob -1
43*/
44int _dowildcard = -1;
45/*****************************************************************************/
46
47/*****************************************************************************/
51int main(int argc, char **argv)
52{
53 int ai, help=0, version=0, verbose=1;
54 char *cptr, datfile[FILENAME_MAX];
55 int ffi=0, fileNr=0;
56 int cmt_sharp=1;
57 int cmt_semicolon=0;
58
59 /*
60 * Get arguments
61 */
62 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
63 datfile[0]=(char)0;
64 /* Options */
65 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
66 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
67 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
68 if(strncasecmp(cptr, "SEMICOLON=", 10)==0) {
69 cptr+=10;
70 if(strncasecmp(cptr, "YES", 1)==0) {cmt_semicolon=1; continue;}
71 if(strncasecmp(cptr, "NO", 1)==0) {cmt_semicolon=0; continue;}
72 } else if(strncasecmp(cptr, "SHARP=", 6)==0) {
73 cptr+=6;
74 if(strncasecmp(cptr, "YES", 1)==0) {cmt_sharp=1; continue;}
75 if(strncasecmp(cptr, "NO", 1)==0) {cmt_sharp=0; continue;}
76 }
77 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
78 return(1);
79 } else break; // later arguments may start with '-'
80
81 /* Print help or version? */
82 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
83 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
84 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
85
86 TPCSTATUS status; statusInit(&status);
87 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
88 status.verbose=verbose-1;
89
90 /* Process other arguments, starting from the first non-option */
91 for(; ai<argc; ai++) {
92 if(ffi<1) ffi=ai;
93 fileNr++;
94 }
95
96 /* Check that we got the filenames */
97 if(fileNr<1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
98 /* Check that at least some comments are to be removed */
99 if(!cmt_semicolon && !cmt_sharp) {
100 fprintf(stderr, "Warning: no comments were selected to be removed.\n");
101 return(0);
102 }
103
104
105 /* In verbose mode print arguments and options */
106 if(verbose>1) {
107 for(ai=0; ai<argc; ai++)
108 printf("%s ", argv[ai]);
109 printf("\n");
110 printf("fileNr := %d\n", fileNr);
111 printf("cmt_semicolon := %d\n", cmt_semicolon);
112 printf("cmt_sharp := %d\n", cmt_sharp);
113 }
114
115
116 /*
117 * Process one file at a time
118 */
119 FILE *fp;
120 size_t fsize;
121 char *data, *data2, *line;
122 int i, j, n;
123 fileNr=0;
124 for(ai=ffi; ai<argc; ai++) {
125
126 strlcpy(datfile, argv[ai], FILENAME_MAX);
127 if(verbose>0) printf("%s\n", datfile);
128
129 /* Open the file */
130 fp=fopen(datfile, "r");
131 if(fp==NULL) {
132 fprintf(stderr, "Error: cannot open file %s\n", datfile);
133 return(2);
134 }
135
136 /* Get the size of the ASCII part of the file */
137 fsize=asciiFileSize(fp, NULL);
138 if(verbose>2) printf(" filesize := %u\n", (unsigned int)fsize);
139 /* If ASCII part is too small, then lets consider that an error */
140 if(fsize<1) {
141 fprintf(stderr, "Warning: not processed %s\n", datfile);
142 fclose(fp);
143 continue;
144 }
145 /* If ASCII part is too large, then lets consider that an error */
146 if(fsize>5000000) {
147 fprintf(stderr, "Warning: not processed %s\n", datfile);
148 fclose(fp);
149 continue;
150 }
151
152 /* Read that to a string */
153 data=asciiFileRead(fp, NULL, fsize+1); rewind(fp);
154 if(data==NULL) {
155 fprintf(stderr, "Error: cannot read file %s\n", datfile);
156 fclose(fp); return(3);
157 }
158 if(verbose>10) printf(" ASCII file read\n");
159 fclose(fp);
160
161 /* Allocate memory for new data */
162 data2=calloc(fsize+1, sizeof(char));
163 if(data2==NULL) {
164 fprintf(stderr, "Error: cannot allocate memory.\n");
165 free(data); return(4);
166 }
167
168 /* Read one line at a time from the string */
169 cptr=data; i=0; n=0;
170 while((line=strTokenDup(cptr, "\n\r", &j))!=NULL) {
171 /* If line starts with comment then jump over it */
172 if(cmt_sharp && line[0]=='#') {free(line); cptr+=j; n++; continue;}
173 if(cmt_semicolon && line[0]==';') {free(line); cptr+=j; n++; continue;}
174 strcat(data2, line); strcat(data2, "\n");
175 free(line); cptr+=j; i++;
176 }
177 free(data);
178 if(verbose>0) printf(" %d line(s) removed.\n", n);
179 if(verbose>1) printf(" %d line(s) retained.\n", i);
180 /* If no changes done, then continue with next file */
181 if(n==0) {
182 free(data2); continue;
183 }
184 /* Otherwise overwrite the original file */
185 fp=fopen(datfile, "w");
186 if(fp==NULL) {
187 fprintf(stderr, "Error: cannot write file %s\n", datfile);
188 free(data2); return(11);
189 }
190 n=fprintf(fp, "%s", data2);
191 free(data2); fclose(fp);
192 if(n<2) {
193 fprintf(stderr, "Error: cannot write file %s\n", datfile);
194 return(12);
195 }
196 fileNr++;
197
198 } /* next file */
199
200 if(verbose>0) {
201 printf("comments removed from %d file(s).\n", fileNr);
202 }
203
204 return(0);
205}
206/*****************************************************************************/
207
208/*****************************************************************************/
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
char * asciiFileRead(FILE *fp, char *data, size_t maxlen)
size_t asciiFileSize(FILE *fp, int *nonprintable)
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
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 * strTokenDup(const char *s1, const char *s2, int *next)
Definition stringext.c:413
int verbose
Verbose level, used by statusPrint() etc.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.