TPCCLIB
Loading...
Searching...
No Matches
tacsety.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 <unistd.h>
14#include <string.h>
15#include <math.h>
16/*****************************************************************************/
17#include "tpcextensions.h"
18#include "tpctac.h"
19/*****************************************************************************/
20
21/*****************************************************************************/
22static char *info[] = {
23 "Replace specified TAC y (concentration) value(s) with given value.",
24 "If file does not exist, then a new file is created.",
25 " ",
26 "Usage: @P [Options] tacfile tac frame value",
27 " ",
28 "Options:",
29 " -stdoptions", // List standard options like --help, -v, etc
30 " ",
31 "Tac and frame must be set to the sequence number of the TAC and frame;",
32 "set tac and/or frame to 0 to replace value in all TACs and/or frames.",
33 "To simulate missing value(s), set value to 'nan'.",
34 " ",
35 "See also: tacsetx, tacmultx, taclist, tacadd, taclkup, tacunit, taccuty",
36 " ",
37 "Keywords: TAC, tool, software testing",
38 0};
39/*****************************************************************************/
40
41/*****************************************************************************/
42/* Turn on the globbing of the command line, since it is disabled by default in
43 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
44 In Unix&Linux wildcard command line processing is enabled by default. */
45/*
46#undef _CRT_glob
47#define _CRT_glob -1
48*/
49int _dowildcard = -1;
50/*****************************************************************************/
51
52/*****************************************************************************/
56int main(int argc, char **argv)
57{
58 int ai, help=0, version=0, verbose=1;
59 int ret, row=-1, col=-1;
60 double v=nan("");
61//char *cptr;
62 char tacfile[FILENAME_MAX];
63 TAC tac;
64
65 /*
66 * Get arguments
67 */
68 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
69 tacInit(&tac); tacfile[0]=(char)0;
70 /* Options */
71 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
72 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
73 // cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
74 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
75 return(1);
76 } else break; // tac name argument may start with '-'
77
78 TPCSTATUS status; statusInit(&status);
79 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
80 status.verbose=verbose-1;
81
82 /* Print help or version? */
83 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
84 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
85 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
86
87 /* Arguments */
88 ret=0;
89 for(; ai<argc; ai++) {
90 if(!tacfile[0]) {
91 strlcpy(tacfile, argv[ai], FILENAME_MAX); continue;
92 } else if(col<0) {
93 col=atoi(argv[ai]); if(col>=0) continue;
94 } else if(row<0) {
95 row=atoi(argv[ai]); if(row>=0) continue;
96 } else if(ret==0) {
97 if(strncasecmp(argv[ai], "NAN", 2)==0) v=nan("");
98 else v=atofVerified(argv[ai]);
99 ret++; continue;
100 }
101 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
102 return(1);
103 }
104 /* Is something missing? */
105 if(ret==0) {tpcPrintUsage(argv[0], info, stdout); return(1);}
106
107 /* In verbose mode print arguments and options */
108 if(verbose>1) {
109 for(ai=0; ai<argc; ai++)
110 printf("%s ", argv[ai]);
111 printf("\n");
112 printf("tacfile := %s\n", tacfile);
113 printf("row := %d\n", row);
114 printf("col := %d\n", col);
115 printf("value := %g\n", v);
116 }
117
118 /*
119 * If TAC file does not exist, then create TAC data.
120 * If it exists, then read it.
121 */
122 if(access(tacfile, 0) == -1) {
123 if(verbose>1) printf("making TAC data\n");
124 /* If TAC file is to be created, we must know the row and col nr */
125 if(row<1 || col<1) {
126 fprintf(stderr, "Error: invalid TAC or frame.\n");
127 tacFree(&tac); return(2);
128 }
129 ret=tacAllocate(&tac, row, col);
130 if(ret!=TPCERROR_OK) {
131 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
132 tacFree(&tac); return(3);
133 }
134 tac.tacNr=col; tac.sampleNr=row;
135 row=col=0; // all values are filled with the value
137 } else {
138 if(verbose>1) printf("reading %s\n", tacfile);
139 ret=tacRead(&tac, tacfile, &status);
140 if(ret!=TPCERROR_OK) {
141 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
142 tacFree(&tac); return(2);
143 }
144 if(verbose>2) {
145 printf("fileformat := %s\n", tacFormattxt(tac.format));
146 printf("tacNr := %d\n", tac.tacNr);
147 printf("sampleNr := %d\n", tac.sampleNr);
148 printf("xunit := %s\n", unitName(tac.tunit));
149 printf("yunit := %s\n", unitName(tac.cunit));
150 }
151 /* Check sample number */
152 if(tac.sampleNr<row) {
153 fprintf(stderr, "Error: TAC file does not contain frame %d.\n", row);
154 tacFree(&tac); return(3);
155 }
156 /* Check tac number */
157 if(tac.tacNr<col) {
158 fprintf(stderr, "Error: TAC file does not contain tac %d.\n", col);
159 tacFree(&tac); return(4);
160 }
161 }
162
163 /*
164 * Set the y value
165 */
166 for(int i=0; i<tac.tacNr; i++) {
167 if(col!=0 && col!=i+1) continue;
168 for(int j=0; j<tac.sampleNr; j++) {
169 if(row==0 || row==j+1) tac.c[i].y[j]=v;
170 }
171 }
172
173
174 /*
175 * Save TAC data
176 */
177 if(verbose>2) printf("writing %s\n", tacfile);
178 FILE *fp; fp=fopen(tacfile, "w");
179 if(fp==NULL) {
180 fprintf(stderr, "Error: cannot open file for writing (%s)\n", tacfile);
181 tacFree(&tac); return(11);
182 }
183 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
184 fclose(fp); tacFree(&tac);
185 if(ret!=TPCERROR_OK) {
186 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
187 return(12);
188 }
189
190 return(0);
191}
192/*****************************************************************************/
193
194/*****************************************************************************/
196
double atofVerified(const char *s)
Definition decpoint.c:75
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
double * y
Definition tpctac.h:75
Definition tpctac.h:87
unit cunit
Definition tpctac.h:105
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
TACC * c
Definition tpctac.h:117
unit tunit
Definition tpctac.h:109
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
int tacAllocate(TAC *tac, int sampleNr, int tacNr)
Definition tac.c:130
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
@ TAC_FORMAT_PMOD
PMOD TAC format.
Definition tpctac.h:33