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

Functions for processing PET study number. More...

#include "libtpcmisc.h"

Go to the source code of this file.

Functions

int studynr_in_fname (char *fname, char *studynr)
 
int studynr_from_fname2 (char *fname, char *studynr, int force)
 
int studynr_from_fname (char *fname, char *studynr)
 
int studynr_match (char *studynr1, char *studynr2)
 
int studynr_validity_check2 (char *studynr, int zero_ok)
 
int studynr_validity_check (char *studynr)
 
int studynr_rm_zeroes (char *studynr)
 
int studynr_to_lowercase (char *studynr)
 

Detailed Description

Functions for processing PET study number.

Author
Vesa Oikonen

Definition in file studynr.c.

Function Documentation

◆ studynr_from_fname()

int studynr_from_fname ( char * fname,
char * studynr )

Extract study number (max MAX_STUDYNR_LEN chars) from filename. This function removes initial zeroes from the number part, and converts uppercase letters to lowercase, if necessary.

Returns
0 if successful.
Parameters
fnameFilename may include path. Filename is not modified.
studynrPointer for the resulting study number. Memory (>=MAX_STUDYNR_LEN+1 chars) for it must be allocated before calling this.

Definition at line 119 of file studynr.c.

125 {
126 return studynr_from_fname2(fname, studynr, 0);
127}
int studynr_from_fname2(char *fname, char *studynr, int force)
Definition studynr.c:67

Referenced by cptReadOne(), dftRead(), imgMicropetCTToEcat7(), imgMicropetPETToEcat7(), resFName2study(), resRead(), tsvRead(), and xelRead().

◆ studynr_from_fname2()

int studynr_from_fname2 ( char * fname,
char * studynr,
int force )

Extract study number (max MAX_STUDYNR_LEN chars) from filename.

This function removes initial zeroes from the number part, and converts uppercase letters to lowercase, if necessary.

Returns
Returns 0 if successful.
Parameters
fnameFilename, which may include the path. Filename is not modified.
studynrPointer for the resulting study number. Memory (>=MAX_STUDYNR_LEN+1 chars) must be allocated before calling this.
forceValidity of studynr is verified (0) or not verified (1)

Definition at line 67 of file studynr.c.

75 {
76 unsigned int i;
77 char *cptr;
78
79 //printf("studynr_from_fname2()\n");
80
81 if(fname==NULL || studynr==NULL) return(1);
82 strcpy(studynr, "");
83
84 /* At first try if studynr_in_fname() works */
85 if(studynr_in_fname(fname, studynr)==0) return(0);
86
87 /* Remove path */
88 cptr=strrchr(fname, '/'); if(cptr==NULL) cptr=strrchr(fname, '\\');
89 if(cptr==NULL) cptr=fname; else cptr++;
90 //i=strlen(cptr); if(i>MAX_STUDYNR_LEN) i=MAX_STUDYNR_LEN;
91 strlcpy(studynr, cptr, MAX_STUDYNR_LEN); //studynr[i]=(char)0;
92 if(force!=0) return(0);
93
94 /* Check that first character is a letter */
95 if(!isalpha((int)studynr[0])) {strcpy(studynr, ""); return(4);}
96 /* Remove everything after the letter+digit parts */
97 for(i=1; i<strlen(studynr); i++) if(!isalpha((int)studynr[i])) break;
98 for(; i<strlen(studynr); i++) if(!isdigit((int)studynr[i])) break;
99 studynr[i]=(char)0;
100 /* Check the length of the study number */
101 if(strlen(studynr)<2) {strcpy(studynr, ""); return(5);}
102 /* Remove initial zeroes from the number part */
103 if(studynr_rm_zeroes(studynr)!=0) {strcpy(studynr, ""); return(6);}
104 /* Convert characters to lower case */
105 if(studynr_to_lowercase(studynr)!=0) {strcpy(studynr, ""); return(7);}
106 /* Check the validity of the study number */
107 if(!studynr_validity_check(studynr)) {strcpy(studynr, ""); return(8);}
108 return(0);
109}
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245
#define MAX_STUDYNR_LEN
Definition libtpcmisc.h:163
int studynr_in_fname(char *fname, char *studynr)
Definition studynr.c:18
int studynr_to_lowercase(char *studynr)
Definition studynr.c:233
int studynr_rm_zeroes(char *studynr)
Definition studynr.c:211
int studynr_validity_check(char *studynr)
Definition studynr.c:196

Referenced by imgGetMicropetHeader(), and studynr_from_fname().

◆ studynr_in_fname()

int studynr_in_fname ( char * fname,
char * studynr )

Find study number (max MAX_STUDYNR_LEN chars) inside filename; Study number must contain 1-5 letters followed by 1-5 digits, if such string is not found then error code is returned.

Initial zeroes are removed if necessary and uppercase letters are changed to lowercase.

Returns
Returns 0 if successful, otherwise <>0.
Parameters
fnameFilename may include path. Filename is not modified.
studynrPointer to string (>=MAX_STUDYNR_LEN+1 chars) where the resulting study number is written.

Definition at line 18 of file studynr.c.

24 {
25 unsigned int i;
26 int ret;
27 char *cptr, *lptr, temp[FILENAME_MAX], temp2[FILENAME_MAX];
28
29 //printf("studynr_in_filename(%s, string)\n", fname);
30 if(fname==NULL || studynr==NULL) return(1);
31 for(i=0; i<=MAX_STUDYNR_LEN; i++) studynr[i]=(char)0;
32 /* Remove path */
33 cptr=strrchr(fname, '/'); if(cptr==NULL) cptr=strrchr(fname, '\\');
34 if(cptr==NULL) cptr=fname; else cptr++;
35 //i=strlen(cptr); if(i>FILENAME_MAX-1) i=FILENAME_MAX-1;
36 strlcpy(temp, cptr, FILENAME_MAX); lptr=temp;
37 /* Verify tokens in filename whether they are valid as study number */
38 cptr=strtok(lptr, "_-+{}!~.()");
39 while(cptr!=NULL && !studynr[0]) {
40 strcpy(temp2, cptr);
41 /* Remove everything after the letter+digit parts */
42 for(i=1; i<strlen(temp2); i++) if(!isalpha((int)temp2[i])) break;
43 for(; i<strlen(temp2); i++) if(!isdigit((int)temp2[i])) break;
44 temp2[i]=(char)0;
45 /* Only then check it */
46 ret=studynr_validity_check2(temp2, 1);
47 if(ret==1) {
48 strcpy(studynr, temp2);
49 if(studynr_rm_zeroes(studynr)!=0) strcpy(studynr, "");
50 if(studynr_to_lowercase(studynr)!=0) strcpy(studynr, "");
51 }
52 cptr=strtok(NULL, "_-+{}!~.()");
53 }
54 if(!studynr[0]) return(2);
55 return 0;
56}
int studynr_validity_check2(char *studynr, int zero_ok)
Definition studynr.c:166

Referenced by studynr_from_fname2().

◆ studynr_match()

int studynr_match ( char * studynr1,
char * studynr2 )

Check whether two valid study numbers are the same. If either of study numbers is shorter than the other, the end parts are compared; thus study numbers that are changed by SPM can be matched. Argument strings are not modified.

Parameters
studynr1compared number
studynr2compared number
Returns
1 if study numbers match exactly, 2 if the match is probable, and zero, if no match is found.

Definition at line 142 of file studynr.c.

142 {
143 int len1, len2;
144 char *cptr1, *cptr2;
145
146 len1=strlen(studynr1); if(len1<2 || len1>MAX_STUDYNR_LEN) return(0);
147 len2=strlen(studynr2); if(len2<2 || len2>MAX_STUDYNR_LEN) return(0);
148 if(len1==len2 && strcmp(studynr1, studynr2)==0) return(1);
149 if(len2>len1) cptr2=studynr2+len2-len1; else cptr2=studynr2;
150 if(len1>len2) cptr1=studynr1+len1-len2; else cptr1=studynr1;
151 if(strcasecmp(cptr1, cptr2)==0) return(2);
152 return(0);
153}

◆ studynr_rm_zeroes()

int studynr_rm_zeroes ( char * studynr)

Remove zeroes from the number part of the PET study number.

Parameters
studynrmodified study number.
Returns
nonzero in case of failure.

Definition at line 211 of file studynr.c.

211 {
212 int i, j, len;
213
214 len=strlen(studynr); if(len<2) return(1);
215 if(isdigit((int)studynr[0])) return(2);
216 if(!isdigit((int)studynr[len-1])) return(3);
217 for(i=1; i<len; i++) if(isdigit((int)studynr[i])) break;
218 for(j=i; j<len; j++) if(studynr[j]!='0') break;
219 if(i==j) return(0); /* no initial zeroes */
220 for(;j<=len; i++, j++) studynr[i]=studynr[j];
221 return(0);
222}

Referenced by studynr_from_fname2(), and studynr_in_fname().

◆ studynr_to_lowercase()

int studynr_to_lowercase ( char * studynr)

Convert the PET study number letters to lowercase. Conversion is not done to non-valid study number.

Parameters
studynrmodified study number.
Returns
nonzero in case of failure.

Definition at line 233 of file studynr.c.

233 {
234 int i, len;
235
236 len=strlen(studynr); if(len<2) return(1);
237 if(isdigit((int)studynr[0])) return(2);
238 if(!isdigit((int)studynr[len-1])) return(3);
239 for(i=0; i<len; i++) {
240 if(isdigit((int)studynr[i])) break;
241 studynr[i]=(char)tolower((int)studynr[i]);
242 }
243 return(0);
244}

Referenced by studynr_from_fname2(), and studynr_in_fname().

◆ studynr_validity_check()

int studynr_validity_check ( char * studynr)

Check that the argument string is a valid TPC study number.

Valid study number here is defined as containing 1-5 letters (upper- or lowercase) followed by at least 1 digit, with total length of max 10 characters.

Returns
1 if study number is valid, zero if not valid or in case of failure.
Parameters
studynrEvaluated study number; not modified.

Definition at line 196 of file studynr.c.

199 {
200 return studynr_validity_check2(studynr, 0);
201}

Referenced by ecat63ReadAllToImg(), ecat63ReadPlaneToImg(), imgGetEcat63MHeader(), imgGetEcat7MHeader(), and studynr_from_fname2().

◆ studynr_validity_check2()

int studynr_validity_check2 ( char * studynr,
int zero_ok )

Check that the argument string is a valid TPC study number.

Valid study number here is defined as containing 1-5 letters (upper- or lowercase) followed by at least 1 digit, with total length of max 10 characters.

Returns
1 if study number is valid, zero if not valid or in case of failure.
Parameters
studynrString to be evaluated as study number. Not modified here.
zero_okNumber part of study number may start with zero (1) or may not (0)

Definition at line 166 of file studynr.c.

171 {
172 int i, j, len;
173
174 len=strlen(studynr); if(len<2 || len>MAX_STUDYNR_LEN) return(0);
175 for(i=0; i<len; i++) if(!isalnum((int)studynr[i])) return(0);
176 for(i=0; i<len; i++) if(!isalpha((int)studynr[i])) break;
177 if(i<1 || i>5) return(0);
178 if(zero_ok==0 && studynr[i]=='0') return(0); /* first digit must be >0 */
179 for(j=0; (i+j)<len; j++) if(!isdigit((int)studynr[i+j])) return(0);
180 //if(j<1 || j>5) return(0);
181 if(j<1 || (i+j)>10) return(0);
182 return(1);
183}

Referenced by studynr_in_fname(), and studynr_validity_check().