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

Utility functions for processing strings. More...

#include "libtpcmisc.h"
#include <string.h>

Go to the source code of this file.

Functions

int strTokenNr (const char *str1, const char *str2)
 
int strTokenNCpy (const char *str1, const char *str2, int i, char *str3, int count)
 
char * strTokenDup (const char *s1, const char *s2, int *next)
 
int strChrCount (const char *str1, const char *str2)
 
int strUppercaseCount (const char *s)
 
void strReplaceChar (char *str, char c1, char c2)
 
size_t strnlen (const char *s, size_t n)
 
size_t strlcat (char *dst, const char *src, size_t dstsize)
 
size_t strlcpy (char *dst, const char *src, size_t dstsize)
 
char * strcasestr (const char *haystack, const char *needle)
 
int strncpyCleanSpaces (char *s1, const char *s2, int maxlen)
 
int strCleanSpaces (char *s)
 
char * strEncodeForXML (const char *s)
 
void strCleanForXML (char *s)
 

Detailed Description

Utility functions for processing strings.

Author
Vesa Oikonen

Definition in file strext.c.

Function Documentation

◆ strcasestr()

char * strcasestr ( const char * haystack,
const char * needle )

Case-insensitive version of strstr().

Returns
a pointer to the beginning of the first occurrence, or NULL if not found.
Parameters
haystackPointer to string in which sub-string needle is searched.
needlePointer to sub-string which is searched for in source string haystack.

Definition at line 279 of file strext.c.

284 {
285 if(!haystack || !*haystack || !needle || !*needle) return 0;
286
287 const char *s=haystack, *p=needle;
288 do {
289 if(!*p) return(char*)haystack;
290 if((*p==*s) || (tolower(*p)==tolower(*s))) {
291 p++; s++;
292 } else {
293 p=needle; if(!*s) return(NULL);
294 s=++haystack;
295 }
296 } while(1);
297 return *p ? NULL : (char*)haystack;
298}

Referenced by cunitFromFilename(), dftFormat(), iftReadValue(), and resWriteHTML_table().

◆ strChrCount()

int strChrCount ( const char * str1,
const char * str2 )

Count how many times specified characters are found in a string. Search is case-sensitive.

See also
strTokenNr, strReplaceChar, strUppercaseCount
Returns
Returns the nr of characters of str2 found in str1.
Parameters
str1String to search for characters; not modified.
str2String containing characters which are searched for; not modified.

Definition at line 126 of file strext.c.

131 {
132 unsigned int n=0, i, j;
133 if(str1==NULL || str2==NULL || strlen(str1)==0 || strlen(str2)==0) return n;
134 for(i=0; i<strlen(str1); i++)
135 for(j=0; j<strlen(str2); j++)
136 if(str1[i]==str2[j]) n++;
137 return n;
138}

Referenced by rnameSplit().

◆ strCleanForXML()

void strCleanForXML ( char * s)

Remove from string those characters that would require encoding in XML. Replaced by character '-'.

See also
strEncodeForXML
Parameters
sPointer to the string to be cleaned.

Definition at line 402 of file strext.c.

405 {
406 if(s==NULL || *s=='\0') return;
407 strReplaceChar(s, '&', '-');
408 strReplaceChar(s, '\'', '-');
409 strReplaceChar(s, '\"', '-');
410 strReplaceChar(s, '<', '-');
411 strReplaceChar(s, '>', '-');
412 return;
413}
void strReplaceChar(char *str, char c1, char c2)
Definition strext.c:159

◆ strCleanSpaces()

int strCleanSpaces ( char * s)

Removes any initial and trailing space characters from specified string s.

Space characters in the middle of the string are not removed.

Returns
0 when successful, otherwise >0.
Author
Vesa Oikonen
Parameters
sPointer to the string.

Definition at line 343 of file strext.c.

346 {
347 if(s==NULL) return 0;
348 int len=strlen(s); if(len<0) return 0;
349 char *s2; s2=strdup(s); if(s2==NULL) return(1);
350 int n=strncpyCleanSpaces(s2, s, len+1);
351 if(n<1) strcpy(s, ""); else strcpy(s, s2);
352 free(s2);
353 return 0;
354}
int strncpyCleanSpaces(char *s1, const char *s2, int maxlen)
Definition strext.c:308

Referenced by csvRead().

◆ strEncodeForXML()

char * strEncodeForXML ( const char * s)

Encode special characters for XML, including SVG.

Postcondition
Free the memory of returned string pointer.
Returns
Returns pointer to encoded string. NULL is returned in case of an error, or if encoding is not necessary.
See also
svg_str_encode
Parameters
sPointer to the string to be encoded.

Definition at line 364 of file strext.c.

367 {
368 if(s==NULL) return(NULL);
369 /* Count the characters needing encoding */
370 int n=0;
371 for(size_t i=0; i<strlen(s); i++) {
372 if(s[i]=='&') {n++; continue;}
373 if(s[i]=='\'') {n++; continue;}
374 if(s[i]=='\"') {n++; continue;}
375 if(s[i]=='<') {n++; continue;}
376 if(s[i]=='>') {n++; continue;}
377 }
378 if(n==0) return(NULL);
379 /* Allocate memory for new string (one char to max 6 chars) */
380 n*=5; n+=strlen(s)+1;
381 char *ns=(char*)malloc(n*sizeof(char));
382 if(ns==NULL) return(NULL);
383 /* Process the string */
384 for(int i=0; i<n; i++) ns[i]=(char)0;
385 for(size_t i=0; i<strlen(s); i++) {
386 if(s[i]=='&') {strcat(ns, "&amp;"); continue;}
387 if(s[i]=='\'') {strcat(ns, "&apos;"); continue;}
388 if(s[i]=='\"') {strcat(ns, "&quot;"); continue;}
389 if(s[i]=='<') {strcat(ns, "&lt;"); continue;}
390 if(s[i]=='>') {strcat(ns, "&gt;"); continue;}
391 ns[strlen(ns)]=s[i];
392 }
393 return(ns);
394}

Referenced by dftWriteHTML(), and resWriteHTML_table().

◆ strlcat()

size_t strlcat ( char * dst,
const char * src,
size_t dstsize )

Safer version of strncat. At most dstsize-1 characters are appended from the source string to destination string. Destination string will be NUL terminated, unless dstsize <= strlen(dst).

Remarks
Included in POSIX but not in GCC.
Returns
the size of the buffer that would have been needed for the destination string; if >=dstsize, then truncation occurred.
Parameters
dstDestination string.
srcSource string.
dstsizeThe actual length of buffer allocated for the destination string; for example, destination string has been allocated as char dst[dstsize];

Definition at line 206 of file strext.c.

214 {
215 char *d;
216 const char *s=src;
217 size_t dlen, n;
218
219 /* Find the current length of dst */
220 dlen=strnlen(dst, dstsize);
221 if(s==NULL) return(dlen);
222 n=dstsize-dlen;
223 if(n==0) return(dlen+strlen(s));
224 d=dst+dlen;
225 while(*s!='\0') {
226 if(n!=1) {*d=*s; d++; n--;}
227 s++;
228 }
229 *d='\0';
230 return(dlen+(s-src));
231}
size_t strnlen(const char *s, size_t n)
Definition strext.c:181

Referenced by anaExists(), backupExistingFile(), dftRead(), and hrrtMakeCalHdr().

◆ strlcpy()

size_t strlcpy ( char * dst,
const char * src,
size_t dstsize )

Safer version of strncpy or strcpy.

At most dstsize-1 characters are copied from the source string to the destination string. Destination string will be NUL terminated.

Remarks
Included in POSIX but not in GCC.
Returns
the size of the buffer that would have been needed for the destination string; if >=dstsize, then truncation occurred.
Parameters
dstDestination string.
srcSource string.
dstsizeThe actual length of buffer allocated for the destination string; for example, destination string has been allocated as char dst[dstsize];

Definition at line 245 of file strext.c.

253 {
254 if(dstsize>0) dst[0]='\0';
255 if(strlen(src)==0) return(0);
256
257 char *d=dst;
258 const char *s=src;
259 size_t n;
260
261 /* Copy as many chars as allowed */
262 n=dstsize;
263 if(n!=0) while(--n!=0) {*d=*s; if(*d=='\0') {d++; s++; break;} d++; s++;}
264 if(n==0) { // not enough space, add NUL, and check how much space were needed
265 if(dstsize!=0) *d='\0';
266 while(*s++) {}
267 }
268 return(s-src-1);
269}

Referenced by anaEditHeader(), anaExists(), backupExistingFile(), csv2dft_b(), csv2dft_mat(), dcmAddItem(), dcmFileRead(), dft_fill_hdr_from_IFT(), dftAllocateWithIMG(), dftRead(), dftWrite(), ecat63AddImg(), ecat63EditMHeader(), ecat63ReadAllToImg(), ecat63ReadPlaneToImg(), ecat63WriteAllImg(), ecat7EditMHeader(), ecat7EditVHeader(), ecatCopy63to7mainheader(), ecatCopy7to63mainheader(), fitRead(), hrrtMakeCalHdr(), img2sif(), imgGetAnalyzeHeader(), imgGetEcat63MHeader(), imgGetEcat7MHeader(), imgGetMicropetSIF(), imgReadAnalyze(), imgReadAnalyzeHeader(), imgReadNiftiHeader(), imgSetEcat63MHeader(), imgSetEcat7MHeader(), imgSetNiftiHeader(), imgWriteAnalyze(), niftiCreateFNames(), niftiExists(), niftiHeaderToIFT(), resRead(), selectEcat931Calibrationfile(), sif2img(), strncpyCleanSpaces(), strTokenDup(), strTokenNCpy(), studynr_from_fname2(), studynr_in_fname(), upetExists(), and xelRead().

◆ strncpyCleanSpaces()

int strncpyCleanSpaces ( char * s1,
const char * s2,
int maxlen )

Version of strncpy() which as usual copies s2 to s1, but without any space characters or line end characters that may be around the string s2.

Returns
the length of the new string s1.
Author
Vesa Oikonen
Parameters
s1Pointer to pre-allocated result string with length of at least maxlen characters, including NULL character.
s2Pointer to the original string.
maxlenMax length of s1, including the trailing zero.

Definition at line 308 of file strext.c.

316 {
317 if(s1==NULL) return(0);
318 s1[0]=(char)0; if(maxlen<1) return(0);
319 if(maxlen<2) {strcpy(s1, ""); return(0);}
320 if(s2==NULL || strlen(s2)<1) return(0);
321
322 char *cptr;
323 int i;
324
325 cptr=(char*)s2; cptr+=strspn(s2, "\t\n\r ");
326 strlcpy(s1, cptr, maxlen); i=strlen(s1); if(i<1) return(0);
327 cptr=s1+(i-1);
328 while(i>0) {
329 if(*cptr!='\t' && *cptr!='\n' && *cptr!='\r' && *cptr!=' ') break;
330 i--; s1[i]=(char)0; cptr--;
331 }
332 return(i);
333}
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition strext.c:245

Referenced by roi_read(), roiRead(), and strCleanSpaces().

◆ strnlen()

size_t strnlen ( const char * s,
size_t n )

Safer version of strlen, in case the argument s is not NUL terminated string. Computes the length of string s, but never scans beyond the n first bytes of the string.

Remarks
Included in POSIX and GCC, so this implementation may not be needed.
Returns
same as strlen() or n, whichever is smaller.
Parameters
sPointer to string, or character array, that may not be NULL terminated.
nThe actual length of buffer allocated for the string; for example, string could have been allocated as char s[n];

Definition at line 181 of file strext.c.

187 {
188 if(s==NULL) return(0);
189 char *ps=(char*)s;
190 size_t i=0;
191 while(i<n && *ps!='\0') {i++; ps++;}
192 return(i);
193}

Referenced by csv2dft_mat(), dcmAddItem(), dcmDA2intl(), dcmDT2intl(), dcmFileRead(), dcmFileWrite(), dcmSOPIdentify(), dcmSOPUIDName(), dcmTM2intl(), dcmTrUID(), dftWrite(), irdRead(), sifRead(), strlcat(), and tpcHtmlUsage().

◆ strReplaceChar()

void strReplaceChar ( char * str,
char c1,
char c2 )

Replace certain characters in string with another character.

Parameters
strPointer to string in which the character is replaced.
c1Character to be replaced.
c2Character to use instead. If NULL, then only the first character is replaced.

Definition at line 159 of file strext.c.

166 {
167 char *cptr;
168 if(strlen(str)==0) return;
169 while((cptr=strchr(str, c1))!=NULL) *cptr=c2;
170 return;
171}

Referenced by dftWrite(), fitRead(), roi_read(), roiRead(), and strCleanForXML().

◆ strTokenDup()

char * strTokenDup ( const char * s1,
const char * s2,
int * next )

Search the string s1 for the first token. The characters making up the string s2 are the delimiters that determine the tokens.

Returns
Returns pointer to a copy of the token string, or NULL in case of an error or if no token found.
Postcondition
Remember to free the memory from the returned pointer after last use.
Author
Vesa Oikonen
Parameters
s1String from where tokens are searched; not modified in any way.
s2String containing character delimiters.
nextIndex of s1 where the token ended; set to NULL, if not needed.

Definition at line 89 of file strext.c.

96 {
97 if(next!=NULL) *next=0;
98 if(s1==NULL) return NULL;
99
100 char *s3=NULL, *cptr;
101 size_t j;
102
103 /* If no delimiters, then return copy of s1 */
104 if(s2==NULL || strlen(s2)<1) {
105 s3=strdup(s1); if(next!=NULL) *next=strlen(s1);
106 return s3;
107 }
108 /* Pass initial delimiter characters */
109 cptr=(char*)s1; j=strspn(cptr, s2); cptr+=j; if(next!=NULL) *next=j;
110 /* calculate characters between delimiters */
111 j=strcspn(cptr, s2); if(j==0) {return NULL;}
112 if(next!=NULL) *next+=j;
113 /* Allocate space for token */
114 s3=calloc(j+1, sizeof(char)); if(s3==NULL) return NULL;
115 strlcpy(s3, cptr, j+1);
116 return s3;
117}

Referenced by fitRead(), and textfileReadLines().

◆ strTokenNCpy()

int strTokenNCpy ( const char * str1,
const char * str2,
int i,
char * str3,
int count )

The strTokenNCpy() function copies the i'th token in the string pointed to by str1 into string pointed to by str3. The characters making up the string pointed to by str2 are the delimiters that determine the token.

See also
strTokenNr, strTokenDup
Returns
Returns the length of token, 0 if no token(s) found.
Parameters
str1String from where tokens are searched; not modified in any way.
str2String containing character delimiters.
iToken number to copy (1..nr of tokens).
str3String array into where the token is copied; string will be null terminated.
countLength of str3, including terminal null

Definition at line 45 of file strext.c.

56 {
57 int j=0, n=0;
58 char *cptr;
59 if(str1==NULL || str2==NULL || strlen(str1)==0 || strlen(str2)==0) return(0);
60 if(i<1 || str3==NULL || count<2) return(0);
61
62 cptr=(char*)str1;
63 do {
64 // pass delimiter characters
65 j=strspn(cptr, str2); cptr+=j;
66 // pass characters between delimiters
67 j=strcspn(cptr, str2); if(j>0) n++;
68 // if this is the required token nr, then stop here
69 if(n==i) {
70 if(j>count-1) j=count-1;
71 strlcpy(str3, cptr, j+1); //strncpy(str3, cptr, j); str3[j]=(char)0;
72 break;
73 }
74 cptr+=j;
75 } while(j>0);
76 if(n>i) {str3[0]=(char)0; return(0);}
77 return(j);
78}

Referenced by fitRead(), integerListAddFromString(), integerListExpandFromString(), pxlRead(), resRead(), and roi_read().

◆ strTokenNr()

int strTokenNr ( const char * str1,
const char * str2 )

The strTokenNr() function returns the number of tokens in the string pointed to by str1. The characters making up the string pointed to by str2 are the delimiters that determine the token.

See also
strTokenNCpy, strChrCount
Returns
Returns the nr of tokens.
Parameters
str1String from where tokens are calculated; not modified in any way.
str2String containing character delimiters.

Definition at line 17 of file strext.c.

22 {
23 int i=0, n=0;
24 char *cptr;
25 if(str1==NULL || str2==NULL || strlen(str1)==0 || strlen(str2)==0) return(0);
26
27 cptr=(char*)str1;
28 do {
29 // pass delimiter characters
30 i=strspn(cptr, str2); cptr+=i;
31 // pass characters between delimiters
32 i=strcspn(cptr, str2); cptr+=i; if(i>0) n++;
33 } while(i>0);
34 return(n);
35}

Referenced by fitRead(), integerListAddFromString(), integerListExpandFromString(), pxlRead(), resRead(), and roi_read().

◆ strUppercaseCount()

int strUppercaseCount ( const char * s)

Count how many upper case characters are found in string.

Returns
Returns the nr of upper case characters found in s.
See also
strChrCount
Parameters
sString to search for upper case characters; not modified.

Definition at line 146 of file strext.c.

149 {
150 unsigned int n=0, i;
151 if(s==NULL || strlen(s)==0) return (int)n;
152 for(i=0; i<strlen(s); i++) if(isupper(s[i])) n++;
153 return (int)n;
154}

Referenced by rnameSplit().