TPCCLIB
Loading...
Searching...
No Matches
addtimes.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 <math.h>
13#include <string.h>
14#include <unistd.h>
15/*****************************************************************************/
16#include "libtpcmisc.h"
17#include "libtpccurveio.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
21static char *info[] = {
22 "Add sample times into curve data originally without times,",
23 "for example certain countrate files.",
24 "Added times are 0.5, 1.5, ... sec by default.",
25 " ",
26 "Usage: @P [Options] tac_file(s)",
27 " ",
28 "Options:",
29 " -min",
30 " Times are written in minutes (0.00833, 0.025, ...).",
31 " -cols",
32 " Data may initially contain more than one column.",
33 " -stdoptions", // List standard options like --help, -v, etc
34 " ",
35 "See also: tacsetx, tacframe, tacdecay, tactime, tocr, tac4frpl",
36 " ",
37 "Keywords: TAC, input, countrate, head-curve",
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 ii, fileNr=0, ret, is_min=0, is_many_cols=0, tn, n;
60 char *cptr, iftfile[FILENAME_MAX], tmp[2*FILENAME_MAX], *vptr, *newval;
61 IFT ift;
62 float tv;
63
64
65 /*
66 * Get arguments
67 */
68 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
69 iftInit(&ift);
70 iftfile[0]=(char)0;
71 /* Options */
72 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
73 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(cptr==NULL) continue;
74 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
75 cptr=argv[ai]+1;
76 if(strncasecmp(cptr, "MINUTES", 1)==0) {
77 is_min=1; continue;
78 } else if(strncasecmp(cptr, "COLS", 1)==0) {
79 is_many_cols=1; continue;
80 }
81 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
82 return(1);
83 } else break;
84
85 /* Print help or version? */
86 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
87 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
88 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
89
90 /* In verbose mode print options */
91 if(verbose>1) {
92 printf("is_min := %d\n", is_min);
93 printf("is_many_cols := %d\n", is_many_cols);
94 }
95
96 /*
97 * Do one file at a time, starting from the first non-option
98 * command-line argument
99 */
100 for(; ai<argc; ai++) {
101 strcpy(iftfile, argv[ai]);
102 if(verbose>0) fprintf(stdout, "%s:\n", iftfile);
103
104 /* Read data */
105 ret=iftRead(&ift, iftfile, 0, 0);
106 if(ret) {
107 fprintf(stderr, "Error (%d): %s\n", ret, ift.status);
108 iftEmpty(&ift); return(2);
109 }
110 fileNr++;
111
112 /* Go through data lines */
113 for(ii=0, tn=0; ii<ift.keyNr; ii++) {
114 if(verbose>2) iftWriteItem(&ift, ii, stdout, 0);
115 /* do not change comment lines */
116 if(ift.item[ii].type!=(char)0 && ift.item[ii].type!=' ') continue;
117 /* do not change lines with key */
118 if(strlen(ift.item[ii].key)>0) continue;
119 /* check that there is a value to edit */
120 if(ift.item[ii].value==NULL) continue;
121 /* if required, check that there is only one data column */
122 vptr=strdup(ift.item[ii].value);
123 n=0; cptr=strtok(vptr, " \t");
124 while(cptr!=NULL) {cptr=strtok(NULL, " \t"); n++;}
125 free(vptr);
126 if(is_many_cols==0 && n>1) continue;
127 /* construct the new value with time */
128 newval=malloc((strlen(ift.item[ii].value)+21)*sizeof(char));
129 if(newval==NULL) continue;
130 tv=0.5+(float)tn; if(is_min) tv/=60.0;
131 sprintf(newval, "%f %s", tv, ift.item[ii].value);
132 /* and put it in place */
133 ret=iftReplaceNthValue(&ift, ii, newval, 0);
134 if(ret) {
135 fprintf(stderr, "Error (%d): %s\n", ret, ift.status);
136 free(newval); iftEmpty(&ift); return(4);
137 }
138 tn++; free(newval);
139 }
140 if(tn==0) {
141 fprintf(stderr, "Note: no times added in %s\n", iftfile);
142 iftEmpty(&ift); continue;
143 }
144 fprintf(stdout, " %d time(s) added.\n", tn);
145
146 /* Backup original file */
147 ret=backupExistingFile(iftfile, NULL, tmp);
148 if(ret!=0) {
149 fprintf(stderr, "Error: %s\n", tmp);
150 iftEmpty(&ift); return(12);
151 }
152
153 /* Write edited file */
154 ret=iftWrite(&ift, iftfile, 0);
155 if(ret) {
156 fprintf(stderr, "Error (%d): %s\n", ret, ift.status);
157 iftEmpty(&ift); return(13);
158 }
159 iftEmpty(&ift);
160 } /* next file */
161
162 if(fileNr<1) {
163 fprintf(stderr, "Error: no files processed.\n");
164 return(1);
165 } else if(fileNr>1 && verbose>0) {
166 fprintf(stdout, "%d files processed.\n", fileNr);
167 }
168
169 return(0);
170}
171/*****************************************************************************/
172
173/*****************************************************************************/
int backupExistingFile(char *filename, char *backup_ext, char *status)
Definition backup.c:14
void iftEmpty(IFT *ift)
Definition ift.c:60
void iftInit(IFT *ift)
Definition ift.c:45
int iftReplaceNthValue(IFT *ift, int item, char *value, int verbose)
Definition ift.c:206
int iftWriteItem(IFT *ift, int item, FILE *fp, int verbose)
Definition iftfile.c:221
int iftRead(IFT *ift, char *filename, int is_key_required, int verbose)
Definition iftfile.c:24
int iftWrite(IFT *ift, char *filename, int verbose)
Definition iftfile.c:282
Header file for libtpccurveio.
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:40
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:213
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:383
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:158
int keyNr
Definition libtpcmisc.h:270
const char * status
Definition libtpcmisc.h:277
IFT_KEY_AND_VALUE * item
Definition libtpcmisc.h:279