TPCCLIB
Loading...
Searching...
No Matches
parformat.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#include "tpcpar.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "Convert model or function fit parameter data in filename1 into filename2",
25 "in specified file format.",
26 "Data can be written only in a few formats (listed below), but more",
27 "file formats can be read.",
28 " ",
29 "Usage: @P [options] filename1 [filename2]",
30 " ",
31 "Options:",
32 " -f=<format-id>",
33 " Accepted format-id's:",
34 " CSV-INT - CSV format with semicolons and decimal commas.",
35 " CSV-UK - CSV format with commas and decimal points.",
36 " TSV-INT - TSV format with tabs and decimal commas.",
37 " TSV-UK - TSV format with tabs and decimal points.",
38 " RES - TPC RES format (deprecated).",
39 " FIT - TPC FIT format (deprecated).",
40 " IFT - Interfile type format.",
41 " XML - MS Excel compatible XML format.",
42 " Without this option, only the format of the given file is shown.",
43 " -hdr=<Yes|no>",
44 " Extra information is (y, default) or is not stored (n) in lines",
45 " starting with '#' character; not effective with all formats.",
46 " -units=<Yes|no>",
47 " Keep (y, default) or remove (n) parameter units.",
48 " -stdoptions", // List standard options like --help, -v, etc
49 " ",
50 "Example: convert RES file into CSV format for exporting to Excel",
51 " @P -f=CSV-UK -hdr=no iea446ki.res iea446ki.csv",
52 " ",
53 "See also: parsort, paradd, parget, csvtrps, reslist, parmatch, rescoll",
54 " ",
55 "Keywords: parameter, tool, format, CSV, RES, FIT",
56 0};
57/*****************************************************************************/
58
59/*****************************************************************************/
60/* Turn on the globbing of the command line, since it is disabled by default in
61 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
62 In Unix&Linux wildcard command line processing is enabled by default. */
63/*
64#undef _CRT_glob
65#define _CRT_glob -1
66*/
67int _dowildcard = -1;
68/*****************************************************************************/
69
70/*****************************************************************************/
74int main(int argc, char **argv)
75{
76 int ai, help=0, version=0, verbose=1;
77 int ret;
78 int new_format=PAR_FORMAT_UNKNOWN;
79 int save_header=1;
80 int keep_units=1;
81 char *cptr, parfile1[FILENAME_MAX], parfile2[FILENAME_MAX];
82 PAR par;
83
84
85 /*
86 * Get arguments
87 */
88 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
89 parInit(&par);
90 parfile1[0]=parfile2[0]=(char)0;
91 /* Options */
92 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
93 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
94 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
95 if(strncasecmp(cptr, "F=", 2)==0) {
96 new_format=parFormatIdentify(cptr+2);
97 if(new_format!=PAR_FORMAT_UNKNOWN) continue;
98 } else if(strncasecmp(cptr, "FORMAT=", 7)==0) {
99 new_format=parFormatIdentify(cptr+7);
100 if(new_format!=PAR_FORMAT_UNKNOWN) continue;
101 } else if(strncasecmp(cptr, "HDR=", 4)==0) {
102 cptr+=4;
103 if(strncasecmp(cptr, "YES", 1)==0) {save_header=1; continue;}
104 else if(strncasecmp(cptr, "NO", 1)==0) {save_header=0; continue;}
105 } else if(strncasecmp(cptr, "HEADER=", 7)==0) {
106 cptr+=7;
107 if(strncasecmp(cptr, "YES", 1)==0) {save_header=1; continue;}
108 else if(strncasecmp(cptr, "NO", 1)==0) {save_header=0; continue;}
109 } else if(strncasecmp(cptr, "UNITS=", 6)==0) {
110 cptr+=6;
111 if(strncasecmp(cptr, "YES", 1)==0) {keep_units=1; continue;}
112 else if(strncasecmp(cptr, "NO", 1)==0) {keep_units=0; continue;}
113 }
114 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
115 return(1);
116 } else break; // tac name argument may start with '-'
117
118 /* Print help or version? */
119 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
120 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
121 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
122
123 /* Process other arguments, starting from the first non-option */
124 for(; ai<argc; ai++) {
125 /* It should be the PAR filename */
126 if(!parfile1[0]) {
127 strlcpy(parfile1, argv[ai], FILENAME_MAX);
128 } else if(!parfile2[0] && new_format!=PAR_FORMAT_UNKNOWN) {
129 strlcpy(parfile2, argv[ai], FILENAME_MAX);
130 } else {
131 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
132 return(1);
133 }
134 }
135 /* Is something missing? */
136 if(!parfile1[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
137
138 /* In verbose mode print arguments and options */
139 if(verbose>1) {
140 printf("new_format := %s\n", parFormattxt(new_format));
141 printf("save_header := %d\n", save_header);
142 printf("keep_units := %d\n", keep_units);
143 printf("parfile1 := %s\n", parfile1);
144 if(parfile2[0]) printf("parfile2 := %s\n", parfile2);
145 }
146
147 TPCSTATUS status; statusInit(&status);
148 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
149 status.verbose=verbose-1;
150
151 /*
152 * Read PAR data
153 */
154 if(verbose>1) printf("reading %s\n", parfile1);
155 ret=parRead(&par, parfile1, &status);
156 if(ret) {
157 fprintf(stderr, "Error: %s (%s)\n", errorMsg(status.error), parfile1);
158 parFree(&par);
159 return(2);
160 }
161
162 /* If new format was not specified, then just print the current format */
163 if(new_format==PAR_FORMAT_UNKNOWN) {
164 fprintf(stdout, "format := %s\n", parFormattxt(par.format));
165 parFree(&par);
166 return 0;
167 }
168
169 /* Remove units, if user does not want to keep those */
170 if(keep_units==0) {
171 for(int i=0; i<par.parNr; i++) par.n[i].unit=UNIT_UNKNOWN;
172 }
173
174 /* If output file name was not given by user, then make it here */
175 if(!parfile2[0]) {
176 strcpy(parfile2, parfile1);
177 filenameRmPath(parfile2); filenameRmExtension(parfile2);
178 strcat(parfile2, parDefaultExtension(new_format));
179 }
180 if(verbose>1) {
181 printf("parfile2 := %s\n", parfile2);
182 }
183
184 /* Print current extra headers for testing */
185 if(verbose>4) {
186 printf("\n---- par.h ----\n");
187 iftWrite(&par.h, stdout, NULL);
188 printf("---------------\n\n");
189 }
190
191 /* Try to save the data in the new format */
192 if(verbose>1) printf("writing %s\n", parfile2);
193 FILE *fp; fp=fopen(parfile2, "w");
194 if(fp==NULL) {
195 fprintf(stderr, "Error: cannot open file for writing (%s)\n", parfile2);
196 parFree(&par); return(5);
197 }
198 ret=parWrite(&par, fp, new_format, save_header, &status);
199 fclose(fp);
200 parFree(&par);
201 if(ret==TPCERROR_UNSUPPORTED) {
202 fprintf(stderr, "Error: writing format %s is not supported.\n", parFormattxt(new_format));
203 return(6);
204 }
205 if(ret!=TPCERROR_OK) {
206 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
207 return(7);
208 }
209 if(verbose>0) printf(" %s written.\n", parfile2);
210
211 return(0);
212}
213/*****************************************************************************/
214
215/*****************************************************************************/
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
void parFree(PAR *par)
Definition par.c:75
void parInit(PAR *par)
Definition par.c:25
char * parFormattxt(parformat c)
Definition pario.c:59
int parFormatIdentify(const char *s)
Definition pario.c:74
int parWrite(PAR *par, FILE *fp, parformat format, int extra, TPCSTATUS *status)
Definition pario.c:148
int parRead(PAR *par, const char *fname, TPCSTATUS *status)
Definition pario.c:232
char * parDefaultExtension(parformat c)
Definition pario.c:133
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 tpcpar.h:100
int format
Definition tpcpar.h:102
IFT h
Optional (but often useful) header information.
Definition tpcpar.h:147
int parNr
Definition tpcpar.h:108
PARN * n
Definition tpcpar.h:112
int unit
Definition tpcpar.h:86
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
Header file for library libtpcextensions.
@ UNIT_UNKNOWN
Unknown unit.
@ TPCERROR_UNSUPPORTED
Unsupported file type.
@ TPCERROR_OK
No error.
Header file for library libtpcift.
Header file for libtpcpar.
@ PAR_FORMAT_UNKNOWN
Unknown format.
Definition tpcpar.h:28
Header file for library libtpctac.