TPCCLIB
Loading...
Searching...
No Matches
tacformat.c
Go to the documentation of this file.
1
8/*****************************************************************************/
9#include "tpcclibConfig.h"
10/*****************************************************************************/
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <math.h>
15/*****************************************************************************/
16#include "tpcextensions.h"
17#include "tpcift.h"
18#include "tpctac.h"
19/*****************************************************************************/
20
21/*****************************************************************************/
22static char *info[] = {
23 "Convert regional or input TAC data in filename1 into filename2",
24 "in specified file format.",
25 "Data can be written only in a few formats (listed below), but more",
26 "file formats can be read.",
27 " ",
28 "Usage: @P [options] filename1 [filename2]",
29 " ",
30 "Options:",
31 " -f=<format-id>",
32 " Accepted format-id's:",
33 " CSV-INT - CSV format with semicolons and decimal commas.",
34 " CSV-UK - CSV format with commas and decimal points.",
35 " TSV-INT - TSV format with tabs and decimal commas.",
36 " TSV-UK - TSV format with tabs and decimal points.",
37 " PMOD - PMOD tac and bld format.",
38 " DFT - TPC TAC format.",
39 " SIMPLE - txt file with tabs and decimal points.",
40 " XML - MS Excel compatible XML format.",
41 " Without this option, only the format of the given TAC file is shown.",
42 " -hdr=<Yes|no>",
43 " Extra information is (y, default) or is not stored (n) in lines",
44 " starting with '#' character; not effective with all formats.",
45 " -mid",
46 " Frame mid times are used instead of frame start and end times.",
47 " -stdoptions", // List standard options like --help, -v, etc
48 " ",
49 "Example 1: convert DFT file into PMOD TAC format",
50 " @P -f=pmod -hdr=no iea446.dft iea446.tac",
51 " ",
52 "Example 2a: convert all TAC files into DFT format on Windows",
53 " for %g in (*.tac) do @P -f=DFT -hdr=no %g",
54 " ",
55 "Example 2b: the same on linux and macOS",
56 " ",
57 " for file in ./*.tac; do tacformat -f=dft -hdr=no $file; done",
58 " ",
59 "See also: taclist, tacunit, tacframe, tacnames, tac2xml",
60 " ",
61 "Keywords: TAC, tool, format, PMOD, CSV, DFT",
62 0};
63/*****************************************************************************/
64
65/*****************************************************************************/
66/* Turn on the globbing of the command line, since it is disabled by default in
67 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
68 In Unix&Linux wildcard command line processing is enabled by default. */
69/*
70#undef _CRT_glob
71#define _CRT_glob -1
72*/
73int _dowildcard = -1;
74/*****************************************************************************/
75
76/*****************************************************************************/
80int main(int argc, char **argv)
81{
82 int ai, help=0, version=0, verbose=1;
83 int ret;
84 int new_format=TAC_FORMAT_UNKNOWN;
85 int save_header=1;
86 int mid_time=0;
87 char *cptr, tacfile1[FILENAME_MAX], tacfile2[FILENAME_MAX];
88 TAC tac;
89
90
91 /*
92 * Get arguments
93 */
94 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
95 tacInit(&tac);
96 tacfile1[0]=tacfile2[0]=(char)0;
97 /* Options */
98 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
99 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
100 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
101 if(strncasecmp(cptr, "F=", 2)==0) {
102 new_format=tacFormatIdentify(cptr+2);
103 if(new_format!=TAC_FORMAT_UNKNOWN) continue;
104 } else if(strncasecmp(cptr, "FORMAT=", 7)==0) {
105 new_format=tacFormatIdentify(cptr+7);
106 if(new_format!=TAC_FORMAT_UNKNOWN) continue;
107 } else if(strncasecmp(cptr, "HDR=", 4)==0) {
108 cptr+=4;
109 if(strncasecmp(cptr, "YES", 1)==0) {save_header=1; continue;}
110 else if(strncasecmp(cptr, "NO", 1)==0) {save_header=0; continue;}
111 } else if(strncasecmp(cptr, "HEADER=", 7)==0) {
112 cptr+=7;
113 if(strncasecmp(cptr, "YES", 1)==0) {save_header=1; continue;}
114 else if(strncasecmp(cptr, "NO", 1)==0) {save_header=0; continue;}
115 } else if(strncasecmp(cptr, "MIDDLE", 3)==0) {
116 mid_time=1; continue;
117 }
118 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
119 return(1);
120 } else break; // tac name argument may start with '-'
121
122 TPCSTATUS status; statusInit(&status);
123 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
124 status.verbose=verbose-1;
125
126 /* Print help or version? */
127 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
128 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
129 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
130
131 /* Process other arguments, starting from the first non-option */
132 if(ai<argc) {strlcpy(tacfile1, argv[ai++], FILENAME_MAX);}
133 if(ai<argc) {strlcpy(tacfile2, argv[ai++], FILENAME_MAX);}
134 if(ai<argc) {fprintf(stderr, "Error: too many arguments: '%s'.\n", argv[ai]); return(1);}
135
136 /* Is something missing? */
137 if(!tacfile1[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
138
139 /* In verbose mode print arguments and options */
140 if(verbose>1) {
141 printf("new_format := %s\n", tacFormattxt(new_format));
142 printf("save_header := %d\n", save_header);
143 printf("tacfile1 := %s\n", tacfile1);
144 if(tacfile2[0]) printf("tacfile2 := %s\n", tacfile2);
145 fflush(stdout);
146 }
147
148
149 /*
150 * Read TAC data
151 */
152 if(verbose>1) printf("reading %s\n", tacfile1);
153 ret=tacRead(&tac, tacfile1, &status);
154 if(ret) { // error
155 tacFree(&tac);
156 /* If format conversion was requested, then this certainly is bad */
157 if(new_format!=TAC_FORMAT_UNKNOWN) {
158 fprintf(stderr, "Error: %s (%s)\n", errorMsg(status.error), tacfile1);
159 return(2);
160 }
161 /* User just wanted to know the current format */
162 ret=tacFormatDetermine(tacfile1, &status);
163 if(ret==TAC_FORMAT_UNKNOWN) {
164 fprintf(stderr, "Error: %s (%s)\n", errorMsg(status.error), tacfile1);
165 return(2);
166 }
167 fprintf(stdout, "%s\n", tacFormattxt(ret));
168 return(0);
169 }
170
171 /* If new format was not specified, then just print the current format */
172 if(new_format==TAC_FORMAT_UNKNOWN) {
173 fprintf(stdout, "%s\n", tacFormattxt(tac.format));
174 tacFree(&tac);
175 return 0;
176 }
177
178 /* If output file name was not given by user, then make it here */
179 if(!tacfile2[0]) {
180 strcpy(tacfile2, tacfile1);
181 filenameRmPath(tacfile2); filenameRmExtension(tacfile2);
182 strcat(tacfile2, tacDefaultExtension(new_format));
183 }
184 if(verbose>1) {
185 printf("tacfile2 := %s\n", tacfile2);
186 }
187
188 /* Use mid times if requested */
189 if(mid_time) tac.isframe=0;
190 /* Set mid times or guess frame start and end times */
191 tacSetX(&tac, NULL);
192
193
194 /* Print current extra headers for testing */
195 if(verbose>4) {
196 printf("\n---- tac.h ----\n");
197 iftWrite(&tac.h, stdout, NULL);
198 printf("---------------\n\n");
199 }
200
201 /* Try to save the data in the new format */
202 if(verbose>1) printf("writing %s\n", tacfile2);
203 FILE *fp; fp=fopen(tacfile2, "w");
204 if(fp==NULL) {
205 fprintf(stderr, "Error: cannot open file for writing (%s)\n", tacfile2);
206 tacFree(&tac); return(5);
207 }
208 ret=tacWrite(&tac, fp, new_format, save_header, &status);
209 fclose(fp);
210 tacFree(&tac);
211 if(ret==TPCERROR_UNSUPPORTED) {
212 fprintf(stderr, "Error: writing format %s is not supported.\n", tacFormattxt(new_format));
213 return(6);
214 }
215 if(ret!=TPCERROR_OK) {
216 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
217 return(7);
218 }
219 if(verbose>0) printf(" %s written.\n", tacfile2);
220
221 return(0);
222}
223/*****************************************************************************/
224
225/*****************************************************************************/
void filenameRmPath(char *s)
Definition filename.c:20
int filenameRmExtension(char *s)
Definition filename.c:71
int iftWrite(IFT *ift, FILE *fp, TPCSTATUS *status)
Definition iftio.c:98
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 tpctac.h:87
tacformat format
Definition tpctac.h:93
IFT h
Optional (but often useful) header information.
Definition tpctac.h:141
int isframe
Definition tpctac.h:95
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
void tacFree(TAC *tac)
Definition tac.c:106
void tacInit(TAC *tac)
Definition tac.c:24
int tacFormatDetermine(const char *fname, TPCSTATUS *status)
Definition tacio.c:161
char * tacDefaultExtension(tacformat c)
Definition tacio.c:144
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
Definition tacio.c:413
int tacFormatIdentify(const char *s)
Definition tacio.c:113
char * tacFormattxt(tacformat c)
Definition tacio.c:98
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
Definition tacio.c:332
int tacSetX(TAC *d, TPCSTATUS *status)
Set TAC x values based on x1 and x2 values, or guess x1 and x2 values based on x values.
Definition tacx.c:653
Header file for library libtpcextensions.
@ TPCERROR_UNSUPPORTED
Unsupported file type.
@ TPCERROR_OK
No error.
Header file for library libtpcift.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28