TPCCLIB
Loading...
Searching...
No Matches
tacsort.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 "Sort PET TAC data by the region name or sample time.",
23 " ",
24 "Usage: @P [options] filename(s)",
25 " ",
26 "Options:",
27 " -sort=<name|auc|time>",
28 " TACs are sorted by regional name (default), AUC, or TAC samples",
29 " (\"frames\") are sorted by increasing sample time.",
30 " -stdoptions", // List standard options like --help, -v, etc
31 " ",
32 "Example:",
33 " @P -sort=time iea*.tac",
34 " ",
35 "See also: tacmatch, taclist, tacren, taccat, tacadd, tacframe",
36 " ",
37 "Keywords: TAC, tool, sorting, time",
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;
60 int sortBy=1; // 1=name, 2=name2, 3=name3, 4=time, 5=AUC
61 char *cptr, tacfile[FILENAME_MAX];
62 TAC tac;
63
64
65 /*
66 * Get arguments
67 */
68 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
69 tacInit(&tac);
70 tacfile[0]=(char)0;
71 /* Options */
72 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
73 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
74 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
75 if(strncasecmp(cptr, "SORT=", 5)==0) {
76 cptr+=5;
77 if(strcasecmp(cptr, "NAME")==0) {sortBy=1; continue;}
78 if(strcasecmp(cptr, "NAME2")==0) {sortBy=2; continue;}
79 if(strcasecmp(cptr, "NAME3")==0) {sortBy=3; continue;}
80 if(strcasecmp(cptr, "TIME")==0) {sortBy=4; continue;}
81 if(strcasecmp(cptr, "X")==0) {sortBy=4; continue;}
82 if(strcasecmp(cptr, "AUC")==0) {sortBy=5; continue;}
83 }
84 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
85 return(1);
86 } else break; // tac name argument may start with '-'
87
88 TPCSTATUS status; statusInit(&status);
89 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
90 status.verbose=verbose-1;
91
92 /* Print help or version? */
93 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
94 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
95 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
96
97 /* In verbose mode print arguments and options */
98 if(verbose>1) {
99 printf("sortBy := %d\n", sortBy);
100 }
101
102 /* Check options */
103 if(sortBy==2 || sortBy==3) {
104 fprintf(stderr, "Error: specified sort option is not yet supported.\n");
105 return(1);
106 }
107
108 /* Process other arguments, starting from the first non-option */
109 int n=0;
110 for(; ai<argc; ai++) {
111 /* It should be the TAC filename */
112 strcpy(tacfile, argv[ai]);
113 /* Read TAC file */
114 if(verbose>1) printf("reading %s\n", tacfile);
115 else if(verbose==1) printf(" %s\n", tacfile);
116 ret=tacRead(&tac, tacfile, &status);
117 if(ret!=TPCERROR_OK) {
118 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
119 tacFree(&tac); return(2);
120 }
121 if(verbose>1) {
122 printf("fileformat := %s\n", tacFormattxt(tac.format));
123 printf("tacNr := %d\n", tac.tacNr);
124 printf("sampleNr := %d\n", tac.sampleNr);
125 }
126 /* Sort */
127 if(verbose>1) printf("sorting %s\n", tacfile);
128 if(sortBy==1)
129 ret=tacSortByName(&tac, &status);
130 else if(sortBy==4)
131 ret=tacSortByTime(&tac, &status);
132 else if(sortBy==5)
133 ret=tacSortByAUC(&tac, &status);
134 if(ret) {
135 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
136 tacFree(&tac); return(3);
137 }
138 /* Save sorted data */
139 if(verbose>1) printf("writing %s\n", tacfile);
140 FILE *fp; fp=fopen(tacfile, "w");
141 if(fp==NULL) {
142 fprintf(stderr, "Error: cannot open file for writing (%s)\n", tacfile);
143 tacFree(&tac); return(5);
144 }
145 ret=tacWrite(&tac, fp, TAC_FORMAT_UNKNOWN, 1, &status);
146 fclose(fp);
147 if(ret!=TPCERROR_OK) {
148 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
149 tacFree(&tac); return(6);
150 }
151 tacFree(&tac);
152 n++;
153 } // next file
154 if(n==0) {
155 fprintf(stderr, "Error: no TAC(s) were sorted.\n");
156 tacFree(&tac); return(2);
157 }
158
159 tacFree(&tac);
160 return(0);
161}
162/*****************************************************************************/
163
164/*****************************************************************************/
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
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 tacSortByTime(TAC *d, TPCSTATUS *status)
Definition tacorder.c:74
int tacSortByAUC(TAC *d, TPCSTATUS *status)
Definition tacorder.c:205
int tacSortByName(TAC *d, TPCSTATUS *status)
Definition tacorder.c:178
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpcift.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Definition tpctac.h:28