TPCCLIB
Loading...
Searching...
No Matches
tacsplit.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 <unistd.h>
15#include <math.h>
16/*****************************************************************************/
17#include "tpcextensions.h"
18#include "tpcift.h"
19#include "tpctac.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "Breaks up a TAC file into separate files, each containing one TAC.",
25 "New files will be written in the current working directory by default.",
26 " ",
27 "Usage: @P [options] filename",
28 " ",
29 "Options:",
30 " -name=<filename_tacid | filename_nr | tacid | nr>",
31 " Set the naming of output files. Options are the name of original",
32 " filename followed by TAC name (default) or number, or just TAC name",
33 " or TAC number.",
34 " --force",
35 " Splitted TACs are saved even when they will overwrite existing files",
36 " or do not contain any data.",
37 " -keeppath",
38 " Splitted files are saved in the same folder as the original file.",
39 " -stdoptions", // List standard options like --help, -v, etc
40 " ",
41 "Example:",
42 " @P -name=filename_tacid iea345.tac",
43 " ",
44 "See also: taclist, tacadd, taccut, tacformat, tacren, tacdel, tacsort",
45 " ",
46 "Keywords: TAC, tool",
47 0};
48/*****************************************************************************/
49
50/*****************************************************************************/
51/* Turn on the globbing of the command line, since it is disabled by default in
52 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
53 In Unix&Linux wildcard command line processing is enabled by default. */
54/*
55#undef _CRT_glob
56#define _CRT_glob -1
57*/
58int _dowildcard = -1;
59/*****************************************************************************/
60
61/*****************************************************************************/
65int main(int argc, char **argv)
66{
67 int ai, help=0, version=0, verbose=1;
68 int ret;
70 int nameType=0;
71 int force=0;
72 int keeppath=0;
73 char *cptr, tacfile[FILENAME_MAX], outfile[FILENAME_MAX];
74 TAC tac;
75
76
77 /*
78 * Get arguments
79 */
80 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
81 tacInit(&tac);
82 tacfile[0]=(char)0;
83 /* Options */
84 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
85 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
86 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
87 if(strcasecmp(cptr, "FORCE")==0) {
88 force=1; continue;
89 } else if(strncasecmp(cptr, "KEEPPATH", 2)==0) {
90 keeppath=1; continue;
91 } else if(strncasecmp(cptr, "NAME=", 5)==0) {
92 cptr+=5;
93 if(strcasecmp(cptr, "filename_tacid")==0) {nameType=0; continue;}
94 if(strcasecmp(cptr, "filename_nr")==0) {nameType=1; continue;}
95 if(strcasecmp(cptr, "tacid")==0) {nameType=2; continue;}
96 if(strcasecmp(cptr, "nr")==0) {nameType=3; continue;}
97 if(strcasecmp(cptr, "fn_tacid")==0) {nameType=0; continue;}
98 if(strcasecmp(cptr, "fn_nr")==0) {nameType=1; continue;}
99 }
100 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
101 return(1);
102 } else break; // tac name argument may start with '-'
103
104 /* Print help or version? */
105 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
106 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
107 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
108
109 TPCSTATUS status; statusInit(&status);
110 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
111 status.verbose=verbose-1;
112
113 /* Arguments */
114 if(ai<argc) {strlcpy(tacfile, argv[ai], FILENAME_MAX); ai++;}
115 if(ai<argc) {fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]); return(1);}
116
117 /* Is something missing? */
118 if(!tacfile[0]) {tpcPrintUsage(argv[0], info, stdout); return(1);}
119
120 /* In verbose mode print arguments and options */
121 if(verbose>1) {
122 for(ai=0; ai<argc; ai++) printf("%s ", argv[ai]);
123 printf("\n");
124 printf("tacfile := %s\n", tacfile);
125 printf("nameType := %d\n", nameType);
126 printf("force := %d\n", force);
127 printf("keeppath := %d\n", keeppath);
128 }
129
130
131 /*
132 * Read the file
133 */
134 if(verbose>1) printf("reading %s\n", tacfile);
135 ret=tacRead(&tac, tacfile, &status);
136 if(ret!=TPCERROR_OK) {
137 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
138 tacFree(&tac); return(2);
139 }
140 if(verbose>2) {
141 printf("fileformat := %s\n", tacFormattxt(tac.format));
142 printf("tacNr := %d\n", tac.tacNr);
143 printf("sampleNr := %d\n", tac.sampleNr);
144 printf("xunit := %s\n", unitName(tac.tunit));
145 printf("yunit := %s\n", unitName(tac.cunit));
146 }
147 /* Check that there are more than one TAC */
148 if(tac.tacNr<2) {
149 fprintf(stderr, "Error: %s contains only one TAC.\n", tacfile);
150 tacFree(&tac); return(3);
151 }
152
153
154 /*
155 * If TAC names are to be used, then check that names are all individual
156 */
157 if(nameType==0 || nameType==2) {
158 if(!tacIndividualNames(&tac)) {
159 fprintf(stderr, "Error: %s does not contain individual TAC names.\n", tacfile);
160 tacFree(&tac); return(4);
161 }
162 }
163
164 /*
165 * Check that none of the new files will not overwrite anything
166 * unless user wants to overwrite files.
167 */
168 if(force==0) for(int i=0; i<tac.tacNr; i++) {
169 /* Make the new filename for this TAC */
170 outfile[0]=(char)0;
171 /* If original filename is to included, then add it */
172 if(nameType==0 || nameType==1) {
173 strcat(outfile, tacfile);
174 if(keeppath==0) filenameRmPath(outfile);
175 filenameRmExtensions(outfile);
176 strcat(outfile, "_");
177 } else if(keeppath!=0) { // or just the path, if requested
178 strcpy(outfile, tacfile);
179 char *cptr=strrchr(outfile, '/'); if(cptr==NULL) cptr=strrchr(outfile, '\\');
180 if(cptr!=NULL) {cptr++; *cptr=(char)0;}
181 }
182 /* Add either TAC name or number */
183 if(nameType==0 || nameType==2) {
184 strncat(outfile, tac.c[i].name, FILENAME_MAX-strlen(outfile)-1);
185 } else {
186 strncatIntZ(outfile, 1+i, tac.tacNr, FILENAME_MAX-strlen(outfile)-1);
187 }
188 /* Add the original extension */
189 strncat(outfile, filenameGetExtensions(tacfile), FILENAME_MAX-strlen(outfile)-1);
190 if(verbose>2) printf(" %s\n", outfile);
191 /* Check that file does not exist */
192 if(access(outfile, 0)==-1) continue;
193 fprintf(stderr, "Error: %s exists; use option -force to overwrite.\n", outfile);
194 return(10);
195 }
196
197 /*
198 * Save each TAC
199 */
200 FILE *fp;
201 int origTacNr=tac.tacNr;
202 tac.tacNr=1;
203 for(int i=0; i<origTacNr; i++) {
204 /* Make the new filename for this TAC */
205 outfile[0]=(char)0;
206 /* If original filename is to included, then add it */
207 if(nameType==0 || nameType==1) {
208 strcat(outfile, tacfile);
209 if(keeppath==0) filenameRmPath(outfile);
210 filenameRmExtensions(outfile);
211 strcat(outfile, "_");
212 } else if(keeppath!=0) { // or just the path, if requested
213 strcpy(outfile, tacfile);
214 char *cptr;
215 cptr=strrchr(outfile, '/'); if(cptr==NULL) cptr=strrchr(outfile, '\\');
216 if(cptr!=NULL) {cptr++; *cptr=(char)0;}
217 }
218 /* Add either TAC name or number */
219 if(nameType==0 || nameType==2) {
220 strncat(outfile, tac.c[i].name, FILENAME_MAX-strlen(outfile)-1);
221 } else {
222 strncatIntZ(outfile, 1+i, tac.tacNr, FILENAME_MAX-strlen(outfile)-1);
223 }
224 /* Add the original extension */
225 strncat(outfile, filenameGetExtensions(tacfile), FILENAME_MAX-strlen(outfile)-1);
226 if(verbose>2) printf(" %s\n", outfile);
227 /* If this is not the first TAC, then copy it to the first place */
228 if(i>0) {
229 ret=tacCopyTacc(tac.c+i, tac.c, tac.sampleNr);
230 if(ret!=0) {
231 fprintf(stderr, "Error: cannot process TACs.\n");
232 tacFree(&tac); return(9);
233 }
234 }
235 /* If all samples in this TAC are missing (NaN), then save it only in force mode */
236 if(force==0 && tacNotNaNs(&tac, -1)<1) continue;
237 /* Save the TAC at the first position */
238 if(verbose>2) printf("writing %s\n", outfile);
239 fp=fopen(outfile, "w");
240 if(fp==NULL) {
241 fprintf(stderr, "Error: cannot open file for writing (%s)\n", outfile);
242 tacFree(&tac); return(11);
243 }
244 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
245 fclose(fp);
246 if(ret!=TPCERROR_OK) {
247 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
248 tacFree(&tac); return(12);
249 }
250 } // next TAC
251 tacFree(&tac);
252
253 return(0);
254}
255/*****************************************************************************/
256
257/*****************************************************************************/
char * filenameGetExtensions(const char *s)
Get all extensions of a file name.
Definition filename.c:203
void filenameRmPath(char *s)
Definition filename.c:20
void filenameRmExtensions(char *s)
Definition filename.c:89
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
char * strncatIntZ(char *str1, const int n, const int maxn, size_t count)
Definition stringext.c:539
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
char name[MAX_TACNAME_LEN+1]
Definition tpctac.h:81
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
void tacInit(TAC *tac)
Definition tac.c:24
int tacCopyTacc(TACC *d1, TACC *d2, int sampleNr)
Definition tac.c:233
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
int tacIndividualNames(TAC *tac)
Definition tacname.c:24
int tacNotNaNs(TAC *tac, const int i)
Definition tacnan.c:84
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
char * unitName(int unit_code)
Definition units.c:143
Header file for library libtpcift.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28