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

Finding and verifying TPC study id number. Study number consists of a string of 1-5 letters (upper- or lower-case) followed by 1-5 digits. 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

int studynrVerify (const char *s, int zero_ok)
int studynrStandardize (char *s)
int studynrFromFilename (const char *fname, char *studynr, int force)

Detailed Description

Finding and verifying TPC study id number. Study number consists of a string of 1-5 letters (upper- or lower-case) followed by 1-5 digits.

Definition in file studynr.c.

Function Documentation

◆ studynrFromFilename()

int studynrFromFilename ( const char * fname,
char * studynr,
int force )

Extract study number (max MAX_STUDYNR_LEN chars) from given file name.

If file name string includes path, the path is ignored. This function removes initial zeroes from the numerical part, and converts upper case letters to lower case, if necessary.

Returns
0 if successful.
See also
studynrStandardize, studynrVerify, filenameRmExtensions, filenameRmPath
Parameters
fnameFile name, which may include the path.
studynrPointer for the resulting study number. Memory must be allocated before calling this function (at least MAX_STUDYNR_LEN+1 characters).
forceValidity of studynr is verified (0) or not verified (1).

Definition at line 79 of file studynr.c.

87 {
88 if(studynr==NULL) return(1);
89 strcpy(studynr, "");
90 if(fname==NULL || strnlen(fname, 2)<2) return(1);
91 /* Work with copy of file name */
92 char *temp=strdup(fname);
93 /* Remove path and extensions */
95 /* Check if any part of base file name could be a valid study number */
96 int ret=0, j;
97 size_t i, k, n, len;
98 len=strlen(temp); if(len<1) {free(temp); return(2);}
99 for(i=1; i<len; i++) if(isalpha(temp[i-1]) && isdigit(temp[i])) {
100 j=i-1; while(j>0 && isalpha(temp[j-1])) j--;
101 k=i; while(k<len-1 && isdigit(temp[k+1])) k++;
102 n=k-j+1; if(n>=2 && n<=10) {
103 strlcpy(studynr, temp+j, n+1);
104 if(force==1) {ret=1; break;}
105 if(studynrStandardize(studynr)==0 && studynrVerify(studynr, 0)==0) {
106 ret=2; break;}
107 }
108 }
109 if(ret) {free(temp); return 0;}
110 if(force==0) {free(temp); return 3;}
111 /* ok we have to take something */
112 for(j=0; j<(int)len; j++) if(isalpha(temp[j]) || isdigit(temp[j])) break;
113 for(k=j; k<len-1; k++) if(!isalpha(temp[k+1]) && !isdigit(temp[k+1])) break;
114 n=k-j+1; if(n<1) {free(temp); return 4;}
115 if(n>10) n=10;
116 strlcpy(studynr, temp+j, n+1);
117 free(temp);
118 return 0;
119}
void filenameRmPath(char *s)
Definition filename.c:20
void filenameRmExtensions(char *s)
Definition filename.c:89
char * strdup(const char *s)
Definition stringext.c:185
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
int studynrStandardize(char *s)
Definition studynr.c:47
int studynrVerify(const char *s, int zero_ok)
Definition studynr.c:22

Referenced by tacRead().

◆ studynrStandardize()

int studynrStandardize ( char * s)

Remove initial zeroes in the numerical part of the PET study number, and convert any capital letters to lower-case.

Returns
non-zero in case of failure.
See also
studynrVerify, studynrFromFilename
Parameters
sPointer to the study number string to be edited.

Definition at line 47 of file studynr.c.

50 {
51 if(s==NULL) return(1);
52 size_t i, j, len;
53 len=strlen(s); if(len<2) return(1);
54 if(isdigit((int)s[0])) return(2);
55 if(!isdigit((int)s[len-1])) return(3);
56 /* Convert to lower-case */
57 for(i=0; i<len; i++) {
58 if(isdigit((int)s[i])) break;
59 s[i]=(char)tolower((int)s[i]);
60 }
61 /* Remove initial zeroes */
62 for(i=1; i<len; i++) if(isdigit((int)s[i])) break;
63 for(j=i; j<len; j++) if(s[j]!='0') break;
64 if(i==j) return(0); /* no initial zeroes */
65 for(;j<=len; i++, j++) s[i]=s[j];
66 return(0);
67}

Referenced by studynrFromFilename().

◆ studynrVerify()

int studynrVerify ( const char * s,
int zero_ok )

Check that the argument string is a valid TPC study number containing 1-5 letters (upper or lower case) followed by 1-5 digits.

Author
Vesa Oikonen
Returns
0 if study number is valid, 1 if not valid, or >1 in case of failure.
See also
studynrFromFilename, studynrStandardize
Parameters
sPointer to string to test.
zero_okNumber part of study number may start with zero (1) or may not (0).

Definition at line 22 of file studynr.c.

27 {
28 if(s==NULL) return(2);
29 size_t len, i, j;
30 len=strlen(s); if(len<2 || len>10) return(1);
31 for(i=0; i<len; i++) if(!isalnum((int)s[i])) return(1);
32 for(i=0; i<len; i++) if(!isalpha((int)s[i])) break;
33 if(i<1 || i>5) return(1);
34 if(zero_ok==0 && s[i]=='0') return(1); /* first digit must be >0 */
35 for(j=0; (i+j)<len; j++) if(!isdigit((int)s[i+j])) return(1);
36 if(j<1 || j>5) return(1);
37 return(0);
38}

Referenced by studynrFromFilename().