TPCCLIB
Loading...
Searching...
No Matches
csvtrps.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 "Transpose data in a CSV or TSV file.",
24 "Table cells or lines starting with '#' are removed.",
25 " ",
26 "Usage: @P [options] csvfile [outputfile]",
27 " ",
28 "Options:",
29 " -stdoptions", // List standard options like --help, -v, etc
30 " ",
31 "Example:",
32 " @P data.csv transposed.csv",
33 " ",
34 "See also: rmcmnts, csvrmcol, tacformat, parformat",
35 " ",
36 "Keywords: CSV, TAC",
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/*****************************************************************************/
52/*
53 * Main
54 */
55int main(int argc, char *argv[])
56{
57 int ai, help=0, version=0, verbose=1;
58 char csvfile[FILENAME_MAX], outfile[FILENAME_MAX];
59
60
61 /*
62 * Get arguments
63 */
64 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
65 csvfile[0]=outfile[0]=(char)0;
66 /* Options */
67 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
68 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
69 //cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
70 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
71 return(1);
72 } else break; // tac name argument may start with '-'
73
74 /* Print help or version? */
75 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
76 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
77 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
78
79 TPCSTATUS status; statusInit(&status);
80 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
81 status.verbose=verbose-1;
82
83 /* Process other arguments, starting from the first non-option */
84 if(ai<argc) {strlcpy(csvfile, argv[ai++], FILENAME_MAX);}
85 if(ai<argc) {strlcpy(outfile, argv[ai++], FILENAME_MAX);}
86 if(ai<argc) {fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]); return(1);}
87
88 if(!csvfile[0]) {
89 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
90 return(1);
91 }
92 if(!outfile[0]) strcpy(outfile, csvfile);
93
94 /* In verbose mode print arguments and options */
95 if(verbose>1) {
96 printf("csvfile := %s\n", csvfile);
97 printf("outfile := %s\n", outfile);
98 fflush(stdout);
99 }
100
101
102 /*
103 * Read the CSV (or TSV) file
104 */
105 CSV csv; csvInit(&csv);
106 if(verbose>1) fprintf(stdout, "reading %s\n", csvfile);
107 {
108 FILE *fp;
109 fp=fopen(csvfile, "r");
110 if(fp==NULL) {
111 fprintf(stderr, "Error: cannot open file %s\n", csvfile);
112 return(2);
113 }
114 if(csvRead(&csv, fp, &status)!=TPCERROR_OK) {
115 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
116 fclose(fp); return(2);
117 }
118 fclose(fp);
119 }
120 if(verbose>2) {
121 printf("colNr := %d\n", csv.col_nr);
122 printf("rowNr := %d\n", csv.row_nr);
123 printf("separator := '%c'\n", csv.separator);
124 }
125 if(csv.col_nr<1 || csv.row_nr<1) {
126 fprintf(stderr, "Error: invalid data.\n");
127 csvFree(&csv); return(2);
128 }
129 if(csv.col_nr==1 && csv.row_nr==1) {
130 if(verbose>0) fprintf(stderr, "Notice: file has only one cell.\n");
131 }
132
133
134 /*
135 * Remove comments and Transpose
136 */
137 if(csvRemoveComments(&csv)!=TPCERROR_OK) {
138 fprintf(stderr, "Error: invalid data.\n");
139 csvFree(&csv); return(3);
140 }
142 fprintf(stderr, "Error: invalid data.\n");
143 csvFree(&csv); return(3);
144 }
145 if(csvTranspose(&csv)!=TPCERROR_OK) {
146 fprintf(stderr, "Error: cannot transpose data.\n");
147 csvFree(&csv); return(4);
148 }
149
150
151 /*
152 * Save the edited file
153 */
154 if(verbose>1) printf("writing %s\n", outfile);
155 {
156 FILE *fp; fp=fopen(outfile, "w");
157 if(fp==NULL) {
158 fprintf(stderr, "Error: cannot open file for writing.\n");
159 csvFree(&csv); return(11);
160 }
161 int ret=csvWrite(&csv, 0, fp, &status);
162 fclose(fp); csvFree(&csv);
163 if(ret!=TPCERROR_OK) {
164 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
165 return(12);
166 }
167 if(verbose>0) printf("edited data saved in %s\n", outfile);
168 }
169
170 return(0);
171}
172/*****************************************************************************/
173
174/*****************************************************************************/
void csvInit(CSV *csv)
Definition csv.c:22
int csvRemoveComments(CSV *csv)
Definition csv.c:470
int csvTranspose(CSV *csv)
Definition csv.c:530
int csvRemoveEmptyRows(CSV *csv)
Definition csv.c:434
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
Definition tpccsv.h:36
int row_nr
Definition tpccsv.h:44
int col_nr
Definition tpccsv.h:46
char separator
Definition tpccsv.h:49
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.