TPCCLIB
Loading...
Searching...
No Matches
roiname.c File Reference

Functions for ROI names. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tpcextensions.h"

Go to the source code of this file.

Functions

char * roinameSubpart (const char *roiname, const char *dlm, const unsigned int si, char *subpart, const unsigned int slen)
 
char * roinameEditByTemplate (const char *template, const char *currname, char *newname, const unsigned int count)
 
char * roinameAddField (char *roiname, const char *field, const unsigned int in, const unsigned int count)
 
int roinameExists (char *roiname)
 Verifies whether TAC name exists or not.
 
int roinameMatch (const char *roiname, const char *test_str, TPCSTATUS *status)
 

Detailed Description

Functions for ROI names.

Definition in file roiname.c.

Function Documentation

◆ roinameAddField()

char * roinameAddField ( char * roiname,
const char * field,
const unsigned int in,
const unsigned int count )

Add a new field into given position inside an existing ROI name. Space character and '_' are accepted as field separators.

Returns
Returns pointer to the edited ROI name, or NULL in case of an error.
See also
roinameSubpart, roinameEditByTemplate, roinameExists, roinameMatch, strdelstr
Parameters
roinameROI name string which will be edited.
fieldString which will be added as a new field in roiname; can be empty in order to add empty field, but NULL is not accepted.
inField index [0..n] for which position new field will be added; enter a large number to add field to the end.
countMax length of the roiname, including space for terminal null. Function returns NULL (error) in case there is not enough space.

Definition at line 106 of file roiname.c.

118 {
119 if(roiname==NULL) return(roiname);
120 if(field==NULL) return((char*)NULL);
121 unsigned int slen=strlen(roiname);
122 unsigned int flen=strlen(field);
123 if(count < (slen+flen+2)) return((char*)NULL);
124 if(slen==0) {
125 if(flen==0) {strlcpy(roiname, "_", count); return(roiname);}
126 else {strlcpy(roiname, field, count); return(roiname);}
127 }
128
129 char *c, subpart[slen+1], tmp[slen+1];
130 unsigned int i=0;
131 strcpy(tmp, roiname); strcpy(roiname, "");
132 c=roinameSubpart(tmp, " _", i, subpart, slen+1);
133 while(c) {
134 if(i==in) {
135 if(i>0) strlcat(roiname, "_", count);
136 strlcat(roiname, field, count);
137 }
138 if(i>0 || i==in) strlcat(roiname, "_", count);
139 strlcat(roiname, subpart, count);
140 c=roinameSubpart(tmp, " _", ++i, subpart, slen+1);
141 }
142 if(i<=in) {
143 if(i>0) strlcat(roiname, "_", count);
144 strlcat(roiname, field, count);
145 }
146 return(roiname);
147}
char * roinameSubpart(const char *roiname, const char *dlm, const unsigned int si, char *subpart, const unsigned int slen)
Definition roiname.c:20
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition stringext.c:592

◆ roinameEditByTemplate()

char * roinameEditByTemplate ( const char * template,
const char * currname,
char * newname,
const unsigned int count )

Make a new ROI name based on template and old ROI name.

Returns
Returns pointer to the new ROI name, or NULL in case of an error.
See also
roinameSubpart, roinameAddField, roinameExists, roinameMatch
Parameters
templateTemplate ROI name, where '_' is used to separate sub-parts, and '@' is replaced by the corresponding sub-part from the 'currname'.
currnameCurrent ROI name, from which sub-parts will be added as replacement of '@' in the template. Space character and '_' are assumed to separate the sub-parts.
newnamePointer to pre-allocated string of length count, into which the new ROI name will be written.
countMax length of new name, including terminal null.

Definition at line 65 of file roiname.c.

76 {
77 if(newname==NULL || count<1) return((char*)NULL);
78 newname[0]=(char)0;
79 if(template==NULL || currname==NULL) return((char*)NULL);
80
81 unsigned int i, len;
82 len=strlen(template); i=strlen(currname); if(i>len) len=i;
83 if(len<1) return(newname);
84 char subpart[++len], *c;
85
86 i=0;
87 c=roinameSubpart(template, "_", i, subpart, len);
88 while(c) {
89 /* If not '@' or if '@' can be replaced with sub-part from original tacname, then copy */
90 if(strcmp(subpart, "@")!=0 || roinameSubpart(currname, " _", i, subpart, 256)) {
91 if(i>0) strlcat(newname, "_", count);
92 strlcat(newname, subpart, count);
93 }
94 c=roinameSubpart(template, "_", ++i, subpart, 256);
95 }
96 return(newname);
97}

◆ roinameExists()

int roinameExists ( char * roiname)

Verifies whether TAC name exists or not.

TAC name string may contain only delimiters like '.', '_', '-', or spaces. Those cases are interpreted as no name in this function.

Returns
1 if valid name exists, 0 if not.
See also
roinameSubpart, roinameEditByTemplate, roinameAddField, roinameMatch
Parameters
roinameROI name string; not edited.

Definition at line 158 of file roiname.c.

161 {
162 if(roiname==NULL) return(0);
163 size_t len, n;
164 len=strlen(roiname); if(len==0) return(0);
165 n=strspn(roiname, " _-.\t"); if(n==len) return(0);
166 return(1);
167}

Referenced by tacEnsureNames(), and tacIndividualNames().

◆ roinameMatch()

int roinameMatch ( const char * roiname,
const char * test_str,
TPCSTATUS * status )

Test whether ROI (TAC) name matches with a test string.

Test string can contain wild cards. If both names contain ROI name separators (' ', '_', or '-'), the corresponding sub-names are tested against each other. If test_str does not contain separators but ROI name does, then names are considered matching if test_str matches with any of sub-names.

Returns
1, in case of match, or 0 if not matched.
Author
Vesa Oikonen
See also
roinameSubpart, roinameEditByTemplate, roinameAddField, roinameExists, fnmatch, strstrNoQuotation
Parameters
roinamePointer to ROI name to be tested.
test_strTest string.
statusPointer to status data; enter NULL if not needed.

Definition at line 183 of file roiname.c.

190 {
191 int verbose=0; if(status!=NULL) verbose=status->verbose;
192 if(verbose>0) printf("%s()\n", __func__);
193 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
194 /* If both string are empty, then that is a match */
195 if((roiname==NULL || strlen(roiname)<1) && (test_str==NULL || strlen(test_str)<1)) {
196 return(1);
197 }
198 /* If just either of those is empty, then that is a no match */
199 if(roiname==NULL || strlen(roiname)<1) {
200 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
201 return(0);
202 }
203 if(test_str==NULL || strlen(test_str)<1) {
204 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
205 return(0);
206 }
207
208 /* Check the number of name sub-parts */
209 int i, len;
210 len=strlen(roiname); i=strlen(test_str); if(i>len) len=i;
211 char subn1[len+1], subn2[len+1];
212 int roisubnr=0, refsubnr=0;
213 for(i=0; i<6; i++) {
214 if(roinameSubpart(roiname, " _-", i, subn1, len+1)==NULL) break;
215 roisubnr++;
216 }
217 for(i=0; i<6; i++) {
218 if(roinameSubpart(test_str, " _-", i, subn2, len+1)==NULL) break;
219 refsubnr++;
220 }
221 if(verbose>3) printf("roisubnr := %d\nrefsubnr := %d\n", roisubnr, refsubnr);
222 if(roisubnr==0 && refsubnr==0) return(1);
223 if(roisubnr==0 || refsubnr==0) return(0);
224 if(refsubnr>1 && roisubnr!=refsubnr) return(0);
225
226 /* If more than one sub-name to test for, then test against corresponding sub-names with
227 wild cards */
228 if(refsubnr>1) {
229 for(i=0; i<refsubnr; i++) {
230 roinameSubpart(roiname, " _-", i, subn1, len+1);
231 if(!strcmp(subn1, ".")) strcpy(subn1, "");
232 roinameSubpart(test_str, " _-", i, subn2, len+1);
233 if(!strcmp(subn2, ".")) strcpy(subn2, "");
234 if(verbose>3) printf(" '%s' vs '%s'\n", subn1, subn2);
235 if(strnlen(subn1, 1)==0 && strnlen(subn2, 1)==0) continue;
236 if(strnlen(subn2, 1)<1 || fncasematch(subn1, subn2)==0 ) return(0);
237 }
238 return(1);
239 }
240
241 /* So, we have just one sub-name in the test string */
242 /* If it is found in any of sub names, accepting wild cards, then we consider it a match */
243 if(roinameSubpart(test_str, " _-", 0, subn2, len+1)==NULL) return(0);
244 if(!strcmp(subn2, ".")) strcpy(subn2, "");
245 for(i=0; i<roisubnr; i++) {
246 if(roinameSubpart(roiname, " _-", i, subn1, len+1)==NULL) continue;
247 if(!strcmp(subn1, ".")) strcpy(subn1, "");
248 if(fncasematch(subn1, subn2)) return(1);
249 }
250 return(0);
251}
int fncasematch(const char *fname, const char *key)
Definition filename.c:139
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566
int verbose
Verbose level, used by statusPrint() etc.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.

Referenced by parSelectTACs(), and tacSelectTACs().

◆ roinameSubpart()

char * roinameSubpart ( const char * roiname,
const char * dlm,
const unsigned int si,
char * subpart,
const unsigned int slen )

Divide ROI name into its sub-parts, based on user-defined delimiter characters (usually underscore and/or space).

Author
Vesa Oikonen
Returns
Returns pointer to the required sub-part, or NULL if not found.
See also
roinameEditByTemplate, roinameAddField, roinameExists, roinameMatch
Parameters
roinamePointer to ROI name. Not modified.
dlmString containing the sub-part delimiters. For example, " _".
siIndex [0..n-1] of sub-part to search for.
subpartPointer to allocated string for sub-part.
slenLength of the sub-part string, including terminating null. Resulting sub-part length will be at maximum slen-1.

Definition at line 20 of file roiname.c.

32 {
33 if(subpart==NULL || slen<1) return(subpart);
34 subpart[0]=(char)0;
35 if(roiname==NULL || strnlen(roiname, 1)==0) return((char*)NULL);
36 if(dlm==NULL || strnlen(dlm, 1)==0) return((char*)NULL);
37
38 unsigned int i=0;
39 char *s=strdup(roiname);
40 char *sp=s; //printf(" si=%d\n", si);
41 do {
42 //printf(" sp='%s'\n", sp);
43 char *p=strpbrk(sp, dlm);
44 //printf(" i=%d p='%s'\n", i, p);
45 if(!p) {
46 if(si==i) {strlcat(subpart, sp, slen); free(s); return(subpart);}
47 free(s); return((char*)NULL);
48 }
49 *p=(char)0;
50 if(si==i) {strlcat(subpart, sp, slen); free(s); return(subpart);}
51 sp=p+1; i++;
52 } while(*sp);
53
54 if(si==i) {strlcat(subpart, sp, slen); free(s); return(subpart);}
55 free(s);
56 return((char*)NULL);
57}
char * strdup(const char *s)
Definition stringext.c:185

Referenced by parReadFIT(), parReadRES(), parWriteFIT(), parWriteRES(), roinameAddField(), roinameEditByTemplate(), and roinameMatch().