TPCCLIB
Loading...
Searching...
No Matches
tacfcont.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 "Ensure that TAC time frames are continuous, without overlaps or gaps.",
23 "Program return code is 0, if time frames are contiguous, or optionally",
24 "were made contiguous.",
25 " ",
26 "Usage: @P [options] file",
27 " ",
28 "Options:",
29 " -fix",
30 " Gaps and overlaps between time frames are fixed, when possible.",
31 " Tiny gaps/overlaps are fixed by editing frame times.",
32 " Larger gaps are filled with new frames, based on linear interpolation",
33 " between frame middle points of adjacent frames.",
34 " -stdoptions", // List standard options like --help, -v, etc
35 " ",
36 "See also: tacframe, tacdelna, simframe, tacsetx",
37 " ",
38 "Keywords: TAC, tool, time",
39 0};
40/*****************************************************************************/
41
42/*****************************************************************************/
43/* Turn on the globbing of the command line, since it is disabled by default in
44 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
45 In Unix&Linux wildcard command line processing is enabled by default. */
46/*
47#undef _CRT_glob
48#define _CRT_glob -1
49*/
50int _dowildcard = -1;
51/*****************************************************************************/
52
53/*****************************************************************************/
57int main(int argc, char **argv)
58{
59 int ai, help=0, version=0, verbose=1;
60 int ret, fixMode=0;
61 char *cptr, tacfile[FILENAME_MAX];
62
63 /*
64 * Get arguments
65 */
66 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
67 tacfile[0]=(char)0;
68 /* Options */
69 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
70 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
71 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
72 if(strcasecmp(cptr, "FIX")==0) {
73 fixMode=1; continue;
74 }
75 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
76 return(1);
77 } else break; // tac name argument may start with '-'
78
79 TPCSTATUS status; statusInit(&status);
80 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
81 status.verbose=verbose-3;
82
83 /* Print help or version? */
84 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
85 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
86 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
87
88 /* The first argument (non-option) is the filename */
89 if(ai<argc) {strlcpy(tacfile, argv[ai], FILENAME_MAX); ai++;}
90 else {fprintf(stderr, "Error: missing filename.\n"); return(1);}
91 if(ai<argc) {fprintf(stderr, "Error: extra command-line argument.\n"); return(1);}
92
93 /* In verbose mode print arguments and options */
94 if(verbose>1) {
95 printf("tacfile := %s\n", tacfile);
96 printf("fixMode := %d\n", fixMode);
97 }
98
99 /* Read the file */
100 if(verbose>1) printf("reading %s\n", tacfile);
101 TAC tac; tacInit(&tac);
102 ret=tacRead(&tac, tacfile, &status);
103 if(ret!=TPCERROR_OK) {
104 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
105 tacFree(&tac); return(2);
106 }
107 if(verbose>2) {
108 printf("fileformat := %s\n", tacFormattxt(tac.format));
109 printf("tacNr := %d\n", tac.tacNr);
110 printf("sampleNr := %d\n", tac.sampleNr);
111 }
112
113 /*
114 * Check if there are gaps or overlaps
115 */
116 ret=tacIsXContiguous(&tac);
117 if(ret==TPCERROR_OK) {
118 fprintf(stdout, "Note: frame times are contiguous.\n");
119 tacFree(&tac); return(0);
120 }
121 if(fixMode==0) {
122 fprintf(stderr, "Warning: frame times are not contiguous.\n");
123 tacFree(&tac); return(ret);
124 }
126 fprintf(stderr, "Error: time frames cannot be fixed.\n");
127 tacFree(&tac); return(ret);
128 }
129
130 /*
131 * Fix frame times
132 */
133 fprintf(stdout, "Note: fixing frame times that are not contiguous.\n");
134 ret=tacSetXContiguous(&tac);
136 fprintf(stderr, "Error: large frame overlap.\n");
137 tacFree(&tac); return(ret);
138 }
139 if(ret!=TPCERROR_OK) {
140 fprintf(stderr, "Error: cannot fix frame times.\n");
141 tacFree(&tac); return(ret);
142 }
143
144
145 /*
146 * Save data
147 */
148 if(verbose>1) printf("writing fixed %s\n", tacfile);
149 FILE *fp; fp=fopen(tacfile, "w");
150 if(fp==NULL) {
151 fprintf(stderr, "Error: cannot open file for writing (%s)\n", tacfile);
152 tacFree(&tac); return(11);
153 }
154 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
155 fclose(fp); tacFree(&tac);
156 if(ret!=TPCERROR_OK) {
157 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
158 return(12);
159 }
160
161 return(0);
162}
163/*****************************************************************************/
164
165/*****************************************************************************/
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 tpctac.h:87
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
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
int tacIsXContiguous(TAC *d)
Check that PET TAC frame times are contiguous, without even tiny overlap or gaps in between.
Definition tacx.c:1130
int tacSetXContiguous(TAC *d)
Set PET TAC frame times contiguous, without even tiny overlap or gaps in between.
Definition tacx.c:1166
Header file for library libtpcextensions.
@ TPCERROR_OVERLAPPING_DATA
Overlapping data.
@ TPCERROR_OK
No error.
@ TPCERROR_LARGE_GAP
Large gap in data.
Header file for library libtpcift.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28