TPCCLIB
Loading...
Searching...
No Matches
tacmultx.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/*****************************************************************************/
15#include "tpcextensions.h"
16//#include "tpcift.h"
17#include "tpctac.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
21static char *info[] = {
22 "Multiplies the x column(s) in TAC file by user-specified operand.",
23 "Resulting TAC can be written into new file, or original can be overwritten.",
24 " ",
25 "Usage: @P [Options] tacfile operand [outputfile]",
26 " ",
27 "Options:",
28 " -stdoptions", // List standard options like --help, -v, etc
29 " ",
30 "See also: tacsetx, taclist, tacadd, tacframe, tacunit, tacformat",
31 " ",
32 "Keywords: TAC, tool, simulation, software testing",
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 int ret;
55//char *cptr;
56 char tacfile[FILENAME_MAX], outfile[FILENAME_MAX];
57 TAC tac;
58 double ov=nan("");
59
60
61 /*
62 * Get arguments
63 */
64 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
65 tacInit(&tac);
66 tacfile[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 TPCSTATUS status; statusInit(&status);
76 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
77 status.verbose=verbose-1;
78
79 /* Print help or version? */
80 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
81 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
82 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
83
84 /* Arguments */
85 for(; ai<argc; ai++) {
86 if(!tacfile[0]) {
87 strcpy(tacfile, argv[ai]); continue;
88 } else if(isnan(ov) || ov!=ov) {
89 if(atofCheck(argv[ai], &ov)==0) continue;
90 } else if(!outfile[0]) {
91 strcpy(outfile, argv[ai]); continue;
92 }
93 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
94 return(1);
95 }
96
97 /* Is something missing? */
98 if(!tacfile[0] || isnan(ov)) {tpcPrintUsage(argv[0], info, stdout); return(1);}
99 if(!outfile[0]) strcpy(outfile, tacfile);
100
101
102 /* In verbose mode print arguments and options */
103 if(verbose>1) {
104 for(ai=0; ai<argc; ai++)
105 printf("%s ", argv[ai]);
106 printf("\n");
107 printf("tacfile := %s\n", tacfile);
108 printf("outfile := %s\n", outfile);
109 printf("operand := %g\n", ov);
110 }
111
112
113 /*
114 * Read the file
115 */
116 if(verbose>1) printf("reading %s\n", tacfile);
117 ret=tacRead(&tac, tacfile, &status);
118 if(ret!=TPCERROR_OK) {
119 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
120 tacFree(&tac); return(2);
121 }
122 if(verbose>2) {
123 printf("fileformat := %s\n", tacFormattxt(tac.format));
124 printf("tacNr := %d\n", tac.tacNr);
125 printf("sampleNr := %d\n", tac.sampleNr);
126 printf("xunit := %s\n", unitName(tac.tunit));
127 printf("yunit := %s\n", unitName(tac.cunit));
128 printf("isframe := %d\n", tac.isframe);
129 }
130
131
132 /* Multiply x value(s) by operand */
133 if(verbose>1) printf("multiplying x by %g\n", ov);
134 for(int i=0; i<tac.sampleNr; i++) {
135 tac.x[i]*=ov; tac.x1[i]*=ov; tac.x2[i]*=ov;
136 }
137
138 /*
139 * Save data
140 */
141 if(verbose>1) printf("writing %s\n", outfile);
142 FILE *fp; fp=fopen(outfile, "w");
143 if(fp==NULL) {
144 fprintf(stderr, "Error: cannot open file for writing (%s)\n", outfile);
145 tacFree(&tac); return(11);
146 }
147 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
148 fclose(fp); tacFree(&tac);
149 if(ret!=TPCERROR_OK) {
150 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
151 return(12);
152 }
153 if(verbose>0) printf("%s saved.\n", outfile);
154
155 return(0);
156}
157/*****************************************************************************/
158
159/*****************************************************************************/
int atofCheck(const char *s, double *v)
Definition decpoint.c:94
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
Definition tpctac.h:87
double * x
Definition tpctac.h:97
unit cunit
Definition tpctac.h:105
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
int isframe
Definition tpctac.h:95
double * x2
Definition tpctac.h:101
unit tunit
Definition tpctac.h:109
double * x1
Definition tpctac.h:99
int tacNr
Definition tpctac.h:91
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 tacRead(TAC *d, const char *fname, TPCSTATUS *status)
Definition tacio.c:413
char * tacFormattxt(tacformat c)
Definition tacio.c:98
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
Definition tacio.c:332
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
char * unitName(int unit_code)
Definition units.c:143
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28