TPCCLIB
Loading...
Searching...
No Matches
csvrmcol.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 <unistd.h>
15/*****************************************************************************/
16#include "tpcextensions.h"
17#include "tpccsv.h"
18#include "tpcift.h"
19/*****************************************************************************/
20
21/*****************************************************************************/
22static char *info[] = {
23 "Remove column from a CSV or TSV file.",
24 " ",
25 "Usage: @P [options] csvfile column [outputfile]",
26 " ",
27 "Options:",
28 " -stdoptions", // List standard options like --help, -v, etc
29 " ",
30 "Example:",
31 " @P data.csv 1 data_wo_1st_col.csv",
32 " ",
33 "See also: tacformat, taclist",
34 " ",
35 "Keywords: CSV, TAC",
36 0};
37/*****************************************************************************/
38
39/*****************************************************************************/
40/* Turn on the globbing of the command line, since it is disabled by default in
41 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
42 In Unix&Linux wildcard command line processing is enabled by default. */
43/*
44#undef _CRT_glob
45#define _CRT_glob -1
46*/
47int _dowildcard = -1;
48/*****************************************************************************/
49
50/*****************************************************************************/
51/*
52 * Main
53 */
54int main(int argc, char *argv[])
55{
56 int ai, help=0, version=0, verbose=1;
57 char csvfile[FILENAME_MAX], outfile[FILENAME_MAX];
58 int column=0;
59
60
61
62 /*
63 * Get arguments
64 */
65 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
66 csvfile[0]=outfile[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 //cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
71 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
72 return(1);
73 } else break; // tac name argument 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(csvfile, argv[ai++], FILENAME_MAX);}
86 if(ai<argc) {column=atoi(argv[ai++]);}
87 if(ai<argc) {strlcpy(outfile, argv[ai++], FILENAME_MAX);}
88 if(ai<argc) {fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]); return(1);}
89
90 if(column<1) {fprintf(stderr, "Error: invalid column.\n"); return(1);}
91 if(!csvfile[0]) {
92 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
93 return(1);
94 }
95 if(!outfile[0]) strcpy(outfile, csvfile);
96
97 /* In verbose mode print arguments and options */
98 if(verbose>1) {
99 printf("csvfile := %s\n", csvfile);
100 printf("column := %d\n", column);
101 printf("outfile := %s\n", outfile);
102 fflush(stdout);
103 }
104
105
106 /*
107 * Read the CSV (or TSV) file
108 */
109 CSV csv; csvInit(&csv);
110 if(verbose>1) fprintf(stdout, "reading %s\n", csvfile);
111 {
112 FILE *fp;
113 fp=fopen(csvfile, "r");
114 if(fp==NULL) {
115 fprintf(stderr, "Error: cannot open file %s\n", csvfile);
116 return(2);
117 }
118 if(csvRead(&csv, fp, &status)!=TPCERROR_OK) {
119 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
120 fclose(fp); return(2);
121 }
122 fclose(fp);
123 }
124 if(verbose>2) {
125 printf("colNr := %d\n", csv.col_nr);
126 printf("rowNr := %d\n", csv.row_nr);
127 printf("separator := '%c'\n", csv.separator);
128 }
129 if(csv.col_nr<2) {
130 fprintf(stderr, "Error: file has only 1 column\n");
131 csvFree(&csv); return(2);
132 }
133 if(column>csv.col_nr) {
134 fprintf(stderr, "Error: file has only %d column(s)\n", csv.col_nr);
135 csvFree(&csv); return(2);
136 }
137
138
139 /*
140 * Remove column
141 */
142 {
143 int i=csv.nr-1;
144 while(i>=0) {
145 if(csv.c[i].col==column-1) {
146 //printf("deleting item %d, '%s'\n", i, csv.c[i].content);
147 if(csvRemoveItem(&csv, i)!=TPCERROR_OK) {
148 fprintf(stderr, "Error: cannot delete item.\n");
149 csvFree(&csv); return(4);
150 }
151 }
152 i--;
153 }
154 /* Fix column indices and number */
155 for(int j=0; j<csv.nr; j++) if(csv.c[j].col>=column) csv.c[j].col--;
156 csv.col_nr--;
157 }
158
159
160 /*
161 * Save the edited file
162 */
163 if(verbose>1) printf("writing %s\n", outfile);
164 {
165 FILE *fp; fp=fopen(outfile, "w");
166 if(fp==NULL) {
167 fprintf(stderr, "Error: cannot open file for writing.\n");
168 csvFree(&csv); return(11);
169 }
170 int ret=csvWrite(&csv, 0, fp, &status);
171 fclose(fp); csvFree(&csv);
172 if(ret!=TPCERROR_OK) {
173 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
174 return(12);
175 }
176 if(verbose>0) printf("edited data saved in %s\n", outfile);
177 }
178
179 return(0);
180}
181/*****************************************************************************/
182
183/*****************************************************************************/
void csvInit(CSV *csv)
Definition csv.c:22
int csvRemoveItem(CSV *csv, int i)
Definition csv.c:409
void csvFree(CSV *csv)
Definition csv.c:38
int csvRead(CSV *csv, FILE *fp, TPCSTATUS *status)
Definition csvio.c:124
int csvWrite(CSV *csv, int regular, FILE *fp, TPCSTATUS *status)
Definition csvio.c:52
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
int col
Definition tpccsv.h:28
Definition tpccsv.h:36
int row_nr
Definition tpccsv.h:44
int col_nr
Definition tpccsv.h:46
char separator
Definition tpccsv.h:49
CSV_item * c
Definition tpccsv.h:38
int nr
Definition tpccsv.h:42
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
Header file for library libtpccsv.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpcift.