TPCCLIB
Loading...
Searching...
No Matches
tacselect.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include "tpcift.h"
8/*****************************************************************************/
9#include <stdio.h>
10#include <stdlib.h>
11#include <math.h>
12#include <time.h>
13#include <string.h>
14/*****************************************************************************/
15#include "tpctac.h"
16/*****************************************************************************/
17
18/*****************************************************************************/
26 TAC *d,
30 const char *region_name,
32 int reset,
34 TPCSTATUS *status
35) {
36 int verbose=0; if(status!=NULL) verbose=status->verbose;
37 if(verbose>0) printf("%s()\n", __func__);
38 /* Check that required data exists */
39 if(d==NULL || d->tacNr<1) {
40 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
41 return 0;
42 }
43
44 int ri, len, match_nr=0;
45
46 /* Check if NULL was given as tac name; if, then select all */
47 if(region_name==NULL) {
48 for(ri=0; ri<d->tacNr; ri++) d->c[ri].sw=1;
49 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
50 return(d->tacNr);
51 }
52
53 /* Reset all selections if required */
54 if(reset!=0) for(ri=0; ri<d->tacNr; ri++) d->c[ri].sw=0;
55
56 /* Remove any quotation marks from name */
57 len=strlen(region_name);
58 char local_name[len+1];
59 if(len>1) len=strncpyClean(local_name, region_name, len+1);
60 else strcpy(local_name, region_name);
61
62 /* If tac name is empty, then select all tacs with missing names */
63 if(len<1) {
64 for(ri=0; ri<d->tacNr; ri++) {
65 if(strlen(d->c[ri].name)==0) {d->c[ri].sw=1; match_nr++; continue;}
66 /* maybe here should also be checked for names consisting of only space characters */
67 }
68 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
69 return(match_nr);
70 }
71
72 /* Check each TAC name */
73 for(ri=0; ri<d->tacNr; ri++) {
74 /* does the name match? */
75 if(roinameMatch(d->c[ri].name, local_name, status)!=0) {
76 d->c[ri].sw=1; match_nr++; continue;
77 }
78 }
79 /* If at least one match was found, then we are ready */
80 if(match_nr>0) {
81 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
82 return(match_nr);
83 }
84
85 /* Next try VOI number, if region_name is suitable */
86 int n;
87 if(sscanf(local_name, "%d", &n)!=1 || n<1 || n>d->tacNr) {
88 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
89 return(0);
90 }
91 d->c[n-1].sw=1; match_nr=1;
92 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
93 return(match_nr);
94}
95/*****************************************************************************/
96
97/*****************************************************************************/
105 TAC *d
106) {
107 int ri, n;
108
109 /* Check the input */
110 if(d==NULL || d->tacNr<1) return(0);
111 /* Check each VOI */
112 for(ri=n=0; ri<d->tacNr; ri++) if(d->c[ri].sw!=0) n++;
113 return(n);
114}
115/*****************************************************************************/
116
117/*****************************************************************************/
124 TAC *d
125) {
126 if(d==NULL || d->tacNr<1) return(-1);
127 for(int i=0; i<d->tacNr; i++) if(d->c[i].sw!=0) return(i);
128 return(-1);
129}
130/*****************************************************************************/
131
132/*****************************************************************************/
141 TAC *d
142) {
143 int ri, len, min_len, i;
144
145 if(d==NULL || d->tacNr<1) return -1;
146 if(tacSelectedTACs(d)<1) return -2;
147 for(ri=0, i=-1, min_len=9999; ri<d->tacNr; ri++) {
148 if(d->c[ri].sw==0) continue;
149 len=strlen(d->c[ri].name);
150 if(strcasestr(d->c[ri].name, "ALL")!=NULL) len-=4;
151 if(strcasestr(d->c[ri].name, "AVG")!=NULL) len-=4;
152 if(strcasestr(d->c[ri].name, "MEAN")!=NULL) len-=5;
153 if(len<min_len) {min_len=len; i=ri;}
154 }
155 if(i<0) return -2; else return i;
156}
157/*****************************************************************************/
158
159/*****************************************************************************/
160#if(0)
172int tacNameMatch(
174 const char *tacname,
176 const char *test_str,
178 TPCSTATUS *status
179) {
180 int verbose=0; if(status!=NULL) verbose=status->verbose;
181 if(verbose>0) printf("%s()\n", __func__);
182 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
183 /* If both string are empty, then that is a match */
184 if((tacname==NULL || strlen(tacname)<1) && (test_str==NULL || strlen(test_str)<1)) {return 1;}
185 /* If just either of those is empty, then that is a no match */
186 if(tacname==NULL || strlen(tacname)<1) {
187 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
188 return 0;
189 }
190 if(test_str==NULL || strlen(test_str)<1) {
191 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
192 return 0;
193 }
194
195 /* Split full names into subnames */
196 char tacsub[3][MAX_TACNAME_LEN+1];
197 char refsub[3][MAX_TACNAME_LEN+1];
198 int ni, tacsubnr=0, refsubnr=0;
199 tacsubnr=tacNameSplit(tacname, tacsub[0], tacsub[1], tacsub[2], MAX_TACNAME_LEN);
200 refsubnr=tacNameSplit(test_str, refsub[0], refsub[1], refsub[2], MAX_TACNAME_LEN);
201 if(verbose>3) {
202 printf("tacsubnr := %d\nsubnames :=", tacsubnr);
203 for(ni=0; ni<tacsubnr; ni++) printf(" '%s'", tacsub[ni]);
204 printf("\n");
205 printf("refsubnr := %d\nsubnames :=", refsubnr);
206 for(ni=0; ni<refsubnr; ni++) printf(" '%s'", refsub[ni]);
207 printf("\n");
208 }
209 if(tacsubnr==0 && refsubnr==0) return(1);
210 if(tacsubnr==0 || refsubnr==0) return(0);
211
212 /* If more than one subname to test for, then test against corresponding
213 subnames with wildcards */
214 if(refsubnr>1) {
215 if(tacsubnr!=refsubnr) return(0);
216 for(ni=0; ni<refsubnr; ni++) {
217 if(strlen(refsub[ni])==0 && strlen(tacsub[ni])==0) continue;
218 if( strlen(refsub[ni])<1 || fncasematch(tacsub[ni], refsub[ni])==0 ) {
219 return(0);}
220 }
221 return(1);
222 }
223
224 /* We have just one subname in the test string */
225 /* If only one subname also in TAC name, then test them with wildcards */
226 if(tacsubnr==1) {
227 if(fncasematch(tacsub[0], refsub[0])==0 ) return(0); else return(1);
228 }
229 /* If it is found in any of sub names, then we consider it a match */
230 for(ni=0; ni<tacsubnr; ni++) if(fncasematch(tacsub[ni], refsub[0])) return(1);
231 return(0);
232}
233#endif
234/*****************************************************************************/
235
236/*****************************************************************************/
int tacNameSplit(const char *rname, char *name1, char *name2, char *name3, unsigned int max_name_len)
Definition dftio.c:26
int fncasematch(const char *fname, const char *key)
Definition filename.c:139
int roinameMatch(const char *roiname, const char *test_str, TPCSTATUS *status)
Definition roiname.c:183
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
int strncpyClean(char *s1, const char *s2, int maxlen)
Definition stringext.c:321
char * strcasestr(const char *haystack, const char *needle)
Definition stringext.c:155
char sw
Definition tpctac.h:77
char name[MAX_TACNAME_LEN+1]
Definition tpctac.h:81
Definition tpctac.h:87
TACC * c
Definition tpctac.h:117
int tacNr
Definition tpctac.h:91
int verbose
Verbose level, used by statusPrint() etc.
int tacSelectedTACs(TAC *d)
Definition tacselect.c:103
int tacSelectTACs(TAC *d, const char *region_name, int reset, TPCSTATUS *status)
Definition tacselect.c:24
int tacFirstSelected(TAC *d)
Definition tacselect.c:122
int tacSelectBestReference(TAC *d)
Definition tacselect.c:139
#define MAX_TACNAME_LEN
Max length of TAC ID name (not including trailing zero)
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.
Header file for library libtpcift.
Header file for library libtpctac.