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

Functions for processing region names. More...

#include "libtpcmisc.h"

Go to the source code of this file.

Functions

int rnameSplit (char *rname, char *name1, char *name2, char *name3, int max_name_len)
 
int rnameRmDots (char *rname1, char *rname2)
 
int rnameMatch (char *rname, int rnr, char *test_str)
 
int rnameCatenate (char *rname, int max_rname_len, char *name1, char *name2, char *name3, char space)
 
int roinameExists (char *roiname)
 Verifies whether TAC name exists or not.
 

Detailed Description

Functions for processing region names.

Author
Vesa Oikonen

Definition in file rname.c.

Function Documentation

◆ rnameCatenate()

int rnameCatenate ( char * rname,
int max_rname_len,
char * name1,
char * name2,
char * name3,
char space )

Construct full TAC name from up to three subnames.

Returns
Returns 0 when successful, <>0 if failed.
Parameters
rnamePointer to string of length max_rname_len, in where the full name will be placed
max_rname_lenLength of full TAC name, not including null char
name1Pointer to 1st subname (anatomical region); NULL if not available
name2Pointer to 1st subname (usually hemisphere); NULL if not available
name3Pointer to 1st subname (usually image plane); NULL if not available
spaceSpacing character, for example ' ', '_', or '-'

Definition at line 189 of file rname.c.

203 {
204 unsigned int len, rlen;
205
206 if(rname==NULL || max_rname_len<1) return 1;
207 if(name1==NULL && name2==NULL && name3==NULL) return 2;
208
209 strcpy(rname, "");
210
211 len=strlen(name1);
212 if(len>0 && len<(unsigned int)max_rname_len && strcmp(name1, ".")!=0)
213 strcpy(rname, name1);
214
215 len=strlen(name2); rlen=strlen(rname);
216 if(len>0 && (len+strlen(rname)+1)<(unsigned int)max_rname_len
217 && strcmp(name2, ".")!=0) {
218 if(rlen>0) {rname[rlen]=(char)space; rname[rlen+1]=(char)0;}
219 strcat(rname, name2);
220 }
221
222 len=strlen(name3); rlen=strlen(rname);
223 if(len>0 && (len+strlen(rname)+1)<(unsigned int)max_rname_len
224 && strcmp(name3, ".")!=0) {
225 if(rlen>0) {rname[rlen]=(char)space; rname[rlen+1]=(char)0;}
226 strcat(rname, name3);
227 }
228
229 if(strlen(rname)<1) return 3;
230 return 0;
231}

Referenced by dftRead(), dftRNameSimplify(), fitRead(), and resRead().

◆ rnameMatch()

int rnameMatch ( char * rname,
int rnr,
char * test_str )

Test whether region name or number matches with a test string. Test string can contain wildcards. If test string contains only one subname, it is tested against whole rname. If it contains 2-3 subnames, those are tested against the corresponding tokens in rname. Subname '.' stands for empty name. Number is tested only if test string contains one token of all digits.

Returns
Returns 1, in case of match, or 0 if not matched.
Parameters
rnameRegion name which is tested
rnrRegion number (1..)
test_strTest string

Definition at line 144 of file rname.c.

151 {
152 char region[3][MAX_REGIONNAME_LEN+1];
153 char test[3][MAX_REGIONNAME_LEN+1];
154 unsigned int ni, m, rtoknr=0, ttoknr=0;
155
156 /* Check the input */
157 if(rname==NULL || strlen(rname)<1) return(0);
158 if(test_str==NULL || strlen(test_str)<1) return(0);
159 /* Split region names into 1-3 subnames */
160 rtoknr=rnameSplit(rname, region[0], region[1], region[2], MAX_REGIONNAME_LEN);
161 ttoknr=rnameSplit(test_str, test[0], test[1], test[2], MAX_REGIONNAME_LEN);
162 if(rtoknr==0 || ttoknr==0) return(0);
163 /* If more than one subname to test for, then test against corresponding
164 subnames */
165 if(ttoknr>1) {
166 for(ni=0; ni<ttoknr; ni++) {
167 if( strcmp(test[ni], ".") && fncasematch(test[ni], region[ni])==0 ) {
168 return(0);}
169 }
170 return(1);
171 }
172 /* If just one subname to test for */
173 /* If it contains only digits, then we assume that it is region number */
174 for(ni=m=0; ni<strlen(test[0]); ni++)
175 if(isdigit((int)test[0][ni])==0) {m++; break;}
176 if(m==0 && (ni=atoi(test[0]))>0) { /* it indeed is an acceptable number */
177 if((unsigned int)rnr==ni) return(1); else return(0);
178 }
179 /* It is name; chack if it is found anywhere in the region name */
180 for(ni=0; ni<rtoknr; ni++) if(fncasematch(test[0], region[ni])) return(1);
181 return(0);
182}
int fncasematch(const char *fname, const char *key)
Definition filename.c:100
#define MAX_REGIONNAME_LEN
Definition libtpcmisc.h:154
int rnameSplit(char *rname, char *name1, char *name2, char *name3, int max_name_len)
Definition rname.c:14

Referenced by dftSelectRegions(), and resSelectRegions().

◆ rnameRmDots()

int rnameRmDots ( char * rname1,
char * rname2 )

Region name may contain dots marking non-existing identification of hemisphere or image plane etc. This function removes the dots and extra space characters.

Returns
Returns <0 in case of an error, otherwise the number of tokens that were left.
Parameters
rname1String which contains the original region name; the modified string is written in next string, if pointer to it is given next.
rname2Pointer to previously allocated string, into which the modified region name will be written. Enter NULL, if previous string is to be modified instead.

Definition at line 99 of file rname.c.

107 {
108 char *temp, *out, *cptr, *lptr;
109 int len, c=0;
110
111 if(rname1==NULL) return -1;
112 len=strlen(rname1);
113 if(len<1) {if(rname2!=NULL) strcpy(rname2, ""); return 0;}
114 temp=malloc(len+1); if(temp==NULL) return -3;
115 strcpy(temp, rname1);
116 if(rname2==NULL) out=rname1; else out=rname2;
117 strcpy(out, "");
118
119 /* remove dots and extra spaces */
120 lptr=temp;
121 cptr=strtok(lptr, " \t\n\r");
122 while(cptr!=NULL) {
123 if(strcmp(cptr, ".")!=0) {
124 if(strlen(out)>0) strcat(out, " ");
125 strcat(out, cptr); c++;
126 }
127 cptr=strtok(NULL, " \t\n\r");
128 }
129 free(temp);
130 return c;
131}

Referenced by plot_fit_svg(), plot_fitrange_svg(), plot_svg(), res2ift(), and rnameSplit().

◆ rnameSplit()

int rnameSplit ( char * rname,
char * name1,
char * name2,
char * name3,
int max_name_len )

Split region name into 1-3 subparts of given max length.

Returns
Returns the number of subparts.
Parameters
rnameRegion name to split (string is not edited)
name1Pointer to 1st subname (anatomical region)
name2Pointer to 2nd subname (usually hemisphere)
name3Pointer to 3rd subname (usually image plane)
max_name_lenMax length of subnames, excluding terminal null

Definition at line 14 of file rname.c.

25 {
26 char temp[MAX_REGIONNAME_LEN+1], temp2[MAX_REGIONNAME_LEN+1];
27 char *cptr, *lptr, space[64];
28 int nr;
29
30 if(rname==NULL || name1==NULL || name2==NULL || name3==NULL) return(0);
31#if(0)
32 printf(" rname := '%s'\n", rname);
33#endif
34 if(max_name_len<1) return(0);
35 name1[0]=name2[0]=name3[0]=(char)0;
36 strncpy(temp, rname, MAX_REGIONNAME_LEN); temp[MAX_REGIONNAME_LEN]=(char)0;
37
38 /* Try to divide long name with only upper case letters as possible dividers */
39 if((int)strlen(rname)>max_name_len) {
40 int nr1, nr2;
41 strcpy(space, " _-."); nr1=strChrCount(temp, space);
42 nr2=strUppercaseCount(temp+1); // not counting first character if that is uppercase
43 if(nr1==0 && nr2>0 && nr2<=3) {
44 nr=0;
45 int len=strlen(temp);
46 int i=1, n=0;
47 cptr=temp;
48 while(i<len && !isupper(temp[i])) i++;
49 n=i; if(n>max_name_len) n=max_name_len;
50 strncpy(name1, cptr, n); name1[n]=(char)0; nr++;
51 cptr=temp+i; n=0;
52 while(i<len && (!isupper(temp[i]) || n==0)) {i++; n++;}
53 if(n>max_name_len) n=max_name_len;
54 if(n>0) {
55 strncpy(name2, cptr, n); name2[n]=(char)0; nr++;
56 cptr=temp+i; n=0;
57 while(i<len && (!isupper(temp[i]) || n==0)) {i++; n++;}
58 if(n>max_name_len) n=max_name_len;
59 if(n>0) {strncpy(name3, cptr, n); name3[n]=(char)0; nr++;}
60 }
61#if(0)
62 printf(" -> subnames := '%s' '%s' '%s'\n", name1, name2, name3);
63#endif
64 return(nr);
65 }
66 }
67
68 nr=rnameRmDots(temp, temp2);
69#if(0)
70 printf("temp = '%s'\n", temp);
71 printf("temp2 = '%s'\n", temp2);
72#endif
73 strcpy(space, " \t");
74 if(nr<3) {
75 if(strChrCount(temp2, space)<2) strcat(space, "_");
76 if(strChrCount(temp2, space)<2) strcat(space, "-");
77 }
78 strcat(space, "\n\r");
79 nr=0; lptr=temp;
80 cptr=strtok(lptr, space); if(cptr==NULL) return(nr);
81 strncpy(name1, cptr, max_name_len); name1[max_name_len]=(char)0; nr++;
82 cptr=strtok(NULL, space); if(cptr==NULL) return(nr);
83 strncpy(name2, cptr, max_name_len); name2[max_name_len]=(char)0; nr++;
84 cptr=strtok(NULL, space); if(cptr==NULL) return(nr);
85 strncpy(name3, cptr, max_name_len); name3[max_name_len]=(char)0; nr++;
86#if(0)
87 printf(" -> subnames := '%s' '%s' '%s'\n", name1, name2, name3);
88#endif
89 return(nr);
90}
int strChrCount(const char *str1, const char *str2)
Definition strext.c:126
int strUppercaseCount(const char *s)
Definition strext.c:146
int rnameRmDots(char *rname1, char *rname2)
Definition rname.c:99

Referenced by csv2dft_a(), csv2dft_b(), csv2dft_linkset(), csv2dft_mat(), dft_fill_hdr_from_IFT(), dftGetPmodTitle(), resRead(), and rnameMatch().

◆ 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.
Parameters
roinameROI name string; not edited

Definition at line 241 of file rname.c.

244 {
245 if(roiname==NULL) return(0);
246 size_t len, n;
247 len=strlen(roiname); if(len==0) return(0);
248 n=strspn(roiname, " _-.\t"); if(n==len) return(0);
249 return(1);
250}