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

String processing functions. More...

#include "tpcclibConfig.h"
#include "tpcextensions.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>

Go to the source code of this file.

Functions

int strTokenNr (const char *s1, const char *s2)
 
int strTokenNCpy (const char *s1, const char *s2, int i, char *s3, int count)
 
int strChrCount (const char *s1, const char *s2)
 
int strUppercaseCount (const char *s)
 
void strReplaceChar (char *s, char c1, char c2)
 
char * strcasestr (const char *haystack, const char *needle)
 
char * strdup (const char *s)
 
char * strndup (const char *s, size_t n)
 
char * strstrNoQuotation (const char *haystack, const char *needle)
 
int strncpyCleanSpaces (char *s1, const char *s2, int maxlen)
 
int strCleanSpaces (char *s)
 
int strncpyClean (char *s1, const char *s2, int maxlen)
 
int strClean (char *s)
 
char * strTokenDup (const char *s1, const char *s2, int *next)
 
int strInPars (char *s)
 
void strCleanPars (char *s)
 
char * strncatInt (char *str1, const int n, size_t count)
 
char * strncatDouble (char *str1, const double d, size_t count)
 
char * strncatIntZ (char *str1, const int n, const int maxn, size_t count)
 
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)
 
int strIsSpaceOnly (char *s)
 
char * strdelstr (char *s1, const char *s2)
 
char * strTrimLeft (char *s, const size_t t)
 
char * strEncodeForXML (const char *s)
 
void strCleanForXML (char *s)
 

Detailed Description

String processing functions.

Definition in file stringext.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.
See also
strdelstr, strstrNoQuotation
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 155 of file stringext.c.

160 {
161 if(!haystack || !*haystack || !needle || !*needle) return 0;
162
163 const char *s=haystack, *p=needle;
164 do {
165 if(!*p) return(char*)haystack;
166 if((*p==*s) || (tolower(*p)==tolower(*s))) {
167 p++; s++;
168 } else {
169 p=needle; if(!*s) return(NULL);
170 s=++haystack;
171 }
172 } while(1);
173 return *p ? NULL : (char*)haystack;
174}

Referenced by csvSearchField(), dcmImgIsotope(), dcmMListRead(), elementIdentify(), iftSearchKey(), iftSearchValue(), imgFormatFromFName(), parFormatFromExtension(), parReadRES(), parWriteRES(), strdelstr(), tacReadInveonCSV(), tacSelectBestReference(), tacWritePMOD(), tacWriteXML(), and unitIdentifyFilename().

◆ strChrCount()

int strChrCount ( const char * s1,
const char * s2 )

Count how many times characters specified in string s2 are found in string s1.

Search is case-sensitive.

Author
Vesa Oikonen
Returns
Returns the nr of characters of s2 found in s1.
See also
strReplaceChar, strstrNoQuotation, strUppercaseCount
Parameters
s1String to search for characters; not modified.
s2String containing characters which are searched for; not modified.

Definition at line 98 of file stringext.c.

103 {
104 unsigned int n=0, i, j;
105 if(s1==NULL || s2==NULL || strlen(s1)==0 || strlen(s2)==0) return (int)n;
106 for(i=0; i<strlen(s1); i++)
107 for(j=0; j<strlen(s2); j++)
108 if(s1[i]==s2[j]) n++;
109 return (int)n;
110}

Referenced by strncpyClean().

◆ strClean()

int strClean ( char * s)

Removes any initial and trailing space characters from specified string s. Removes also quotation marks ("" or '') that may be around the string s. Quotes or space characters in the middle of the string are not removed.

Returns
0 when successful, otherwise >0.
Author
Vesa Oikonen
See also
strncpyCleanSpaces, strCleanSpaces, strIsSpaceOnly, strCleanPars
Parameters
sPointer to the string.

Definition at line 389 of file stringext.c.

392 {
393 if(s==NULL) return 0;
394 int len=strlen(s); if(len<0) return 0;
395 char *s2; s2=calloc(len+1, sizeof(char)); if(s2==NULL) return(1);
396 int n=strncpyClean(s2, s, len+1);
397 if(n<1) strcpy(s, ""); else strcpy(s, s2);
398 free(s2);
399 return 0;
400}
int strncpyClean(char *s1, const char *s2, int maxlen)
Definition stringext.c:321

Referenced by iftPutFromString(), iftPutFromStringWithSpaceSeparator(), imgReadDICOM(), tacNameSplit(), and tacRead4DM().

◆ 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 769 of file stringext.c.

772 {
773 if(s==NULL || *s=='\0') return;
774 strReplaceChar(s, '&', '-');
775 strReplaceChar(s, '\'', '-');
776 strReplaceChar(s, '\"', '-');
777 strReplaceChar(s, '<', '-');
778 strReplaceChar(s, '>', '-');
779 return;
780}
void strReplaceChar(char *s, char c1, char c2)
Definition stringext.c:134

◆ strCleanPars()

void strCleanPars ( char * s)

Remove parenthesis (s), [s], or {s} from around the string s.

Only one set of pars are removed, and only if the same pars are as the first and last character of the string.

Precondition
If necessary, remove possible spaces or quotation marks before this.
See also
strInPars, strCleanSpaces
Author
Vesa Oikonen
Parameters
sPointer to the string.

Definition at line 476 of file stringext.c.

479 {
480 if(s==NULL || strInPars(s)==0) return;
481 int len=strlen(s);
482 s[--len]=(char)0;
483 for(int i=1; i<=len; i++) s[i-1]=s[i];
484 return;
485}
int strInPars(char *s)
Definition stringext.c:453

Referenced by iftGetDoubleWithUnit(), tacRead4DM(), tacReadCarimasTxt(), and tacReadDFT().

◆ 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
See also
strClean, strncpyClean, strncpyCleanSpaces, strIsSpaceOnly
Parameters
sPointer to the string.

Definition at line 300 of file stringext.c.

303 {
304 if(s==NULL) return 0;
305 int len=strlen(s); if(len<0) return 0;
306 char *s2; s2=strdup(s); if(s2==NULL) return(1);
307 int n=strncpyCleanSpaces(s2, s, len+1);
308 if(n<1) strcpy(s, ""); else strcpy(s, s2);
309 free(s2);
310 return 0;
311}
char * strdup(const char *s)
Definition stringext.c:185
int strncpyCleanSpaces(char *s1, const char *s2, int maxlen)
Definition stringext.c:265

Referenced by csvCleanSpaces(), dcmImgIsotope(), and strncpyClean().

◆ strdelstr()

char * strdelstr ( char * s1,
const char * s2 )

Find (case-insensitive) s2 in s1, and delete it from s1.

Returns
Returns pointer to s1 at where the deletion stopped, or to the start of s1 in case s2 was not found.
See also
strReplaceChar, strClean, strTrimleft
Parameters
s1Pointer to string in which sub-string s2 is searched; modified.
s2Pointer to sub-string which is searched for in source string s1.

Definition at line 688 of file stringext.c.

693 {
694 if(s1==NULL || s2==NULL) return(s1); //printf("strdelstr('%s', '%s')\n", s1, s2);
695 int n=strlen(s2); if(n<1) return(s1);
696 char *cptr, *rptr;
697 cptr=strcasestr(s1, s2);
698 if(cptr==NULL || *cptr==(char)0) return(s1);
699 rptr=cptr;
700 while(*cptr) {*cptr=cptr[n]; cptr++;}
701 return(rptr);
702}
char * strcasestr(const char *haystack, const char *needle)
Definition stringext.c:155

◆ strdup()

char * strdup ( const char * s)

Allocates memory and copies into it the string addressed by s, including the terminating character.

Postcondition
Remember to free the memory from the returned pointer after last use.
Returns
pointer to allocated string, or NULL in case of error.
Parameters
sString to be duplicated.

Definition at line 185 of file stringext.c.

188 {
189 if(s==NULL) return NULL;
190 size_t length=strlen(s)+1;
191 void *r;
192 r=calloc(length, sizeof(char)); if(r==NULL) return NULL;
193 return (char*)memcpy(r, s, length);
194}

Referenced by atofCheck(), atofVerified(), csvCellReplace(), csvDuplicate(), csvPutString(), dcmFileList(), dcmMListRead(), iftPut(), iftPutFromString(), iftPutFromStringWithSpaceSeparator(), iftReplaceKey(), iftReplaceValue(), parCombineTACs(), parCompareParameterNames(), parCompareTacNames(), parSelectByAnother(), pathCreate(), pathExist(), roinameSubpart(), statusSet(), strCleanSpaces(), strTokenDup(), studynrFromFilename(), tacNameSplit(), tacRead(), tacReadCarimasTxt(), tacReadCSV(), tacReadDFT(), tacReadHRRTHC(), tacReadPMOD(), tpcHtmlUsage(), tpcPrintBuild(), tpcPrintUsage(), and tpcProgramName().

◆ 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
strCleanForXML
Parameters
sPointer to the string to be encoded.

Definition at line 731 of file stringext.c.

734 {
735 if(s==NULL) return(NULL);
736 /* Count the characters needing encoding */
737 int n=0;
738 for(size_t i=0; i<strlen(s); i++) {
739 if(s[i]=='&') {n++; continue;}
740 if(s[i]=='\'') {n++; continue;}
741 if(s[i]=='\"') {n++; continue;}
742 if(s[i]=='<') {n++; continue;}
743 if(s[i]=='>') {n++; continue;}
744 }
745 if(n==0) return(NULL);
746 /* Allocate memory for new string (one char to max 6 chars) */
747 n*=5; n+=strlen(s)+1;
748 char *ns=(char*)malloc(n*sizeof(char));
749 if(ns==NULL) return(NULL);
750 /* Process the string */
751 for(int i=0; i<n; i++) ns[i]=(char)0;
752 for(size_t i=0; i<strlen(s); i++) {
753 if(s[i]=='&') {strcat(ns, "&amp;"); continue;}
754 if(s[i]=='\'') {strcat(ns, "&apos;"); continue;}
755 if(s[i]=='\"') {strcat(ns, "&quot;"); continue;}
756 if(s[i]=='<') {strcat(ns, "&lt;"); continue;}
757 if(s[i]=='>') {strcat(ns, "&gt;"); continue;}
758 ns[strlen(ns)]=s[i];
759 }
760 return(ns);
761}

Referenced by parWriteXML(), and tacWriteXML().

◆ strInPars()

int strInPars ( char * s)

Checks whether string is inside parenthesis (s), [s], or {s}, that is, the same pars as the first and last character of the string.

Precondition
If necessary, remove possible spaces or quotation marks before this.
Returns
0 if not in pars, 1 if inside pars.
Author
Vesa Oikonen
See also
strCleanPars, strCleanSpaces, strIsValidNumber
Parameters
sPointer to the string.

Definition at line 453 of file stringext.c.

456 {
457 if(s==NULL) return 0;
458 int len=strlen(s); if(len<2) return 0;
459 if(s[0]=='(' && s[len-1]==')') return 1;
460 if(s[0]=='{' && s[len-1]=='}') return 1;
461 if(s[0]=='[' && s[len-1]==']') return 1;
462 return 0;
463}

Referenced by strCleanPars(), and tacReadDFT().

◆ strIsSpaceOnly()

int strIsSpaceOnly ( char * s)

Checks whether string contains only space characters.

Text files saved in text editor or Excel may accidentally contain empty looking lines which still may contain tabs and spaces, leading to problems when reading them for example as CSV files.

Returns
0 if lines contains something more than spaces, 1 if only space characters or string is empty.
Author
Vesa Oikonen
See also
strClean, strncpyCleanSpaces
Parameters
sPointer to the string.

Definition at line 671 of file stringext.c.

674 {
675 if(s==NULL) return(1);
676 int len=strlen(s); if(len<1) return(1);
677 for(int i=0; i<len; i++) if(!isspace(s[i])) return(0);
678 return 1;
679}

Referenced by csvRead().

◆ 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 592 of file stringext.c.

601 {
602 char *d;
603 const char *s=src;
604 size_t dlen, n;
605
606 /* Find the current length of dst */
607 dlen=strnlen(dst, dstsize);
608 if(s==NULL) return(dlen);
609 n=dstsize-dlen;
610 if(n==0) return(dlen+strlen(s));
611 d=dst+dlen;
612 while(*s!='\0') {
613 if(n!=1) {*d=*s; d++; n--;}
614 s++;
615 }
616 *d='\0';
617 return(dlen+(s-src));
618}
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566

Referenced by fileBackup(), micropetExists(), parReadFIT(), parReadRES(), roinameAddField(), roinameEditByTemplate(), roinameSubpart(), and tpcProgramName().

◆ 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 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 632 of file stringext.c.

641 {
642 if(dstsize>0) dst[0]='\0';
643 if(strlen(src)==0) return(0);
644
645 char *d=dst;
646 const char *s=src;
647 size_t n;
648
649 /* Copy as many chars as allowed */
650 n=dstsize;
651 if(n!=0) while(--n!=0) {*d=*s; if(*d=='\0') {d++; s++; break;} d++; s++;}
652 if(n==0) { // not enough space, add NUL, and check how much space were needed
653 if(dstsize!=0) *d='\0';
654 while(*s++) {}
655 }
656 return(s-src-1);
657}

Referenced by abssWrite(), anaExists(), dcmAddItem(), dcmFileRead(), dcmMListRead(), dcmSameImage(), ecatReadMainheader(), ecatWriteMainheader(), fileBackup(), imgCopyHeader(), imgSetNiftiHeader(), imgWriteNifti(), micropetExists(), niftiCreateFNames(), niftiExists(), parReadCSV(), parReadFIT(), parReadRES(), parWriteFIT(), roinameAddField(), strDateRead(), strDateTimeValid(), strncpyClean(), strncpyCleanSpaces(), strTokenDup(), strTokenNCpy(), studynrFromFilename(), tacGetHeaderIsotope(), tacGetHeaderStudynr(), tacNameSplit(), tacRead4DM(), tacReadCarimasTxt(), tacReadCSV(), tacReadDFT(), tacReadInveonCSV(), tacReadMat(), tacReadOldAllogg(), tacReadPMOD(), tacReadQView(), tacReadSIF(), tacReadSimple(), and tpcProgramName().

◆ strncatDouble()

char * strncatDouble ( char * str1,
const double d,
size_t count )

Concatenate not more than count characters of the string representation of double d to the string pointed to by str1. String is terminated with a null.

See also
strncatInt
Returns
Pointer to str1.
Parameters
str1String to be catenated.
dDouble value to be catenated as string into str1.
countNo more than count characters are concatenated to str1. If str1 was allocated like char str1[MAX_LEN], then set count=MAX_LEN-strlen(str1)-1.

Definition at line 517 of file stringext.c.

526 {
527 char tmp[128]; sprintf(tmp, "%g", d);
528 return strncat(str1, tmp, count);
529}

◆ strncatInt()

char * strncatInt ( char * str1,
const int n,
size_t count )

Concatenate not more than count characters of the string representation of integer n to the string pointed to by str1. String is terminated with a null.

See also
strncatDouble, strncatIntZ, strIsValidNumber
Returns
Pointer to str1.
Parameters
str1String to be catenated.
nInteger value to be catenated as string into str1.
countNo more than count characters are concatenated to str1. If str1 was allocated like char str1[MAX_LEN], then set count=MAX_LEN-strlen(str1)-1.

Definition at line 495 of file stringext.c.

504 {
505 char tmp[128]; sprintf(tmp, "%d", n);
506 return strncat(str1, tmp, count);
507}

◆ strncatIntZ()

char * strncatIntZ ( char * str1,
const int n,
const int maxn,
size_t count )

Concatenate not more than count characters of the zero-padded string representation of integer n to the string pointed to by str1. String is terminated with a null.

See also
strncatInt, strncatDouble
Returns
Pointer to str1.
Parameters
str1String to be catenated.
nInteger value to be catenated as string into str1.
maxnInteger value which is large enough that it would not need to be zero-padded.
countNo more than count characters are concatenated to str1. If str1 was allocated like char str1[MAX_LEN], then set count=MAX_LEN-strlen(str1)-1.

Definition at line 539 of file stringext.c.

550 {
551 if(n<0) strncat(str1, "-", count);
552 char tmp[128]; sprintf(tmp, "%d", abs(maxn));
553 int len=strlen(tmp);
554 sprintf(tmp, "%0*d", len, abs(n));
555 return strncat(str1, tmp, count);
556}

◆ strncpyClean()

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

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

Returns
the length of the new string s1.
Author
Vesa Oikonen
See also
strncpyCleanSpaces, strCleanSpaces, strClean, strIsSpaceOnly, strstrNoQuotation
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 321 of file stringext.c.

329 {
330 if(s1==NULL) return(0);
331 s1[0]=(char)0; if(maxlen<1) return(0);
332 if(maxlen<2) {strcpy(s1, ""); return(0);}
333 if(s2==NULL || strlen(s2)<1) return(0);
334
335 char *tmp;
336 int i, sqn, dqn, m;
337
338 /* Allocate temp string */
339 tmp=calloc(strlen(s2)+1, sizeof(char));
340
341 /* Trim space characters */
342 i=strncpyCleanSpaces(tmp, s2, strlen(s2)+1);
343 if(i<1) {strcpy(s1, ""); free(tmp); return(0);}
344
345 /* Loop as long as there is something to trim */
346 i=1;
347 while(i) {
348 m=0; // nothing modified so far
349 /* Count quotes */
350 sqn=strChrCount(tmp, "\'");
351 dqn=strChrCount(tmp, "\"");
352 if(sqn==0 && dqn==0) {i=0; break;}
353 /* Trim matching quotation marks */
354 if(dqn>=2 && tmp[0]=='\"' && tmp[strlen(tmp)-1]=='\"') {
355 tmp[0]=' '; tmp[strlen(tmp)-1]=(char)0; dqn-=2; m+=2;
356 }
357 if(sqn>=2 && tmp[0]=='\'' && tmp[strlen(tmp)-1]=='\'') {
358 tmp[0]=' '; tmp[strlen(tmp)-1]=(char)0; sqn-=2; m+=2;
359 }
360 /* Trim orphan quotation marks */
361 if(sqn%2) {
362 if(tmp[0]=='\'') {tmp[0]=' '; sqn--; m++;}
363 else if(tmp[strlen(tmp)-1]=='\'') {tmp[strlen(tmp)-1]=(char)0; sqn--; m++;}
364 }
365 if(dqn%2) {
366 if(tmp[0]=='\"') {tmp[0]=' '; dqn--; m++;}
367 else if(tmp[strlen(tmp)-1]=='\"') {tmp[strlen(tmp)-1]=(char)0; dqn--; m++;}
368 }
369 /* If nothing was modified, then do not continue */
370 if(m==0) {i=0; break;}
371 /* Trim spaces, quit if error encountered */
372 if(strCleanSpaces(tmp)) i=0;
373 }
374
375 strlcpy(s1, tmp, maxlen);
376 free(tmp);
377 return(strlen(s1));
378}
int strCleanSpaces(char *s)
Definition stringext.c:300
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
int strChrCount(const char *s1, const char *s2)
Definition stringext.c:98

Referenced by imgReadDICOM(), parSelectParameters(), parSelectTACs(), strClean(), and tacSelectTACs().

◆ 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
See also
strCleanSpaces, strncpyClean, strIsSpaceOnly
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 265 of file stringext.c.

273 {
274 if(s1==NULL) return(0);
275 s1[0]=(char)0; if(maxlen<1) return(0);
276 if(maxlen<2) {strcpy(s1, ""); return(0);}
277 if(s2==NULL || strlen(s2)<1) return(0);
278
279 char *cptr;
280 int i;
281
282 cptr=(char*)s2; cptr+=strspn(s2, "\t\n\r ");
283 strlcpy(s1, cptr, maxlen); i=strlen(s1); if(i<1) return(0);
284 cptr=s1+(i-1);
285 while(i>0) {
286 if(*cptr!='\t' && *cptr!='\n' && *cptr!='\r' && *cptr!=' ') break;
287 i--; s1[i]=(char)0; cptr--;
288 }
289 return(i);
290}

Referenced by niftiBasename(), parReadFIT(), strCleanSpaces(), strncpyClean(), and tacRead4DM().

◆ strndup()

char * strndup ( const char * s,
size_t n )

Allocates memory and copies into it first n characters from string addressed by s, and adds the terminating character.

Postcondition
Remember to free the memory from the returned pointer after last use.
Returns
pointer to allocated string, or NULL in case of error.
Parameters
sString to be duplicated.
nMax nr of characters to copy, not including terminal zero.

Definition at line 205 of file stringext.c.

210 {
211 size_t length=strnlen(s, n);
212 void *r; r=calloc(length+1, sizeof(char)); if(r==NULL) return NULL;
213 return (char*)memcpy(r, s, length);
214}

Referenced by csvPutLine(), csvPutLineWithSpaces(), iftPutFromString(), iftPutFromStringWithSpaceSeparator(), and tpcProgramName().

◆ 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, whicever 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 566 of file stringext.c.

572 {
573 if(s==NULL) return(0);
574 char *ps=(char*)s;
575 size_t i=0;
576 while(i<n && *ps!='\0') {i++; ps++;}
577 return(i);
578}

Referenced by abssIdFromFName(), anaExists(), anaReadHeader(), atoiCheck(), dcmAddItem(), dcmDA2intl(), dcmDT2intl(), dcmFileList(), dcmFileRead(), dcmFileRemove(), dcmFileWrite(), dcmSOPIdentify(), dcmSOPUIDName(), dcmTM2intl(), dcmTrUID(), elementIdentify(), fileExist(), filenameGetExtension(), filenameGetExtensions(), filenameRmExtension(), filenameRmExtensions(), filenameRmFile(), filenameRmPath(), imgFormatFromFName(), imgRead(), imgReadDICOM(), imgReadNifti(), imgWrite(), imgWriteDICOM(), imgWriteNifti(), isotopeIdentify(), micropetExists(), micropetHeaderRead(), mtgaPlotSVG(), niftiExists(), niftiReadHeader(), niftiWriteHeader(), parCombineTACs(), parSelectByAnother(), parWriteFIT(), pathCreate(), pathFileList(), pathFileNr(), roinameMatch(), roinameSubpart(), strDateRead(), strDateTimeValid(), strDateValid(), strDateValid2(), strDateValid3(), strIsValidNumber(), strlcat(), strndup(), strTimeValid(), strTokenNr(), studynrFromFilename(), tacPlotFitSVG(), tacPlotHistogramSVG(), tacPlotLineSVG(), tacReadAllogg(), tacReadGEMS(), tacReadMat(), tacReadModelingData(), tacReadModelingInput(), tacReadOldAllogg(), tacReadQView(), tacReadReference(), tacReadScanditronics(), tpcHtmlUsage(), tpcProcessStdOptions(), tpcProgramName(), tpcYesNo(), unitIdentify(), and unitIdentifyFilename().

◆ strReplaceChar()

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

Replace all characters c1 in string s with character c2.

See also
strChrCount, strdelstr, strTrimleft, strstrNoQuotation
Author
Vesa Oikonen
Parameters
sPointer 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 134 of file stringext.c.

141 {
142 char *cptr;
143 if(s==NULL || strlen(s)==0) return;
144 while((cptr=strchr(s, c1))!=NULL) *cptr=c2;
145 return;
146}

Referenced by csvPutDouble(), parCombineTACs(), parCompareParameterNames(), parCompareTacNames(), parSelectByAnother(), parToIFT(), strCleanForXML(), and tacWritePMOD().

◆ strstrNoQuotation()

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

Version of strstr() which ignores any matches inside quotation marks "" or ''.

Returns
a pointer to the beginning of the first occurrence, or NULL if not found.
Author
Vesa Oikonen
Remarks
This function is an enhanced version of the one from libtpcmisc.
See also
strClean, strCleanSpaces
Parameters
haystackPointer to string to be searched.
needlePointer to string with quotation marks.

Definition at line 225 of file stringext.c.

230 {
231 if(haystack==NULL) return((char*)NULL);
232 if(needle==NULL) return((char*)haystack);
233
234 size_t i, test_len;
235 int single_quotation=0;
236 int double_quotation=0;
237 char *cptr;
238
239 test_len=strlen(needle); if(test_len<1) return((char*)haystack);
240 for(i=0, cptr=(char*)haystack; i<strlen(haystack); i++, cptr++) {
241 if(*cptr=='\'') {
242 if(single_quotation==0 && strchr(cptr+1, '\'')!=NULL) single_quotation=1;
243 else single_quotation=0;
244 continue;
245 }
246 if(*cptr=='\"') {
247 if(double_quotation==0 && strchr(cptr+1, '\"')!=NULL) double_quotation=1;
248 else double_quotation=0;
249 continue;
250 }
251 if(single_quotation==1 || double_quotation==1) continue;
252 if(strncmp(cptr, needle, test_len)==0) return(cptr);
253 }
254 return((char*)NULL);
255}

Referenced by csvRead(), iftPutFromString(), and iftPutFromStringWithSpaceSeparator().

◆ 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
See also
strTokenNCpy, strstrNoQuotation
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 413 of file stringext.c.

420 {
421 if(next!=NULL) *next=0;
422 if(s1==NULL) return NULL;
423
424 char *s3=NULL, *cptr;
425 size_t j;
426
427 /* If no delimiters, then return copy of s1 */
428 if(s2==NULL || strlen(s2)<1) {
429 s3=strdup(s1); if(next!=NULL) *next=strlen(s1);
430 return s3;
431 }
432 /* Pass initial delimiter characters */
433 cptr=(char*)s1; j=strspn(cptr, s2); cptr+=j; if(next!=NULL) *next=j;
434 /* calculate characters between delimiters */
435 j=strcspn(cptr, s2); if(j==0) {return NULL;}
436 if(next!=NULL) *next+=j;
437 /* Allocate space for token */
438 s3=calloc(j+1, sizeof(char)); if(s3==NULL) return NULL;
439 strlcpy(s3, cptr, j+1);
440 return s3;
441}

Referenced by csvRead(), iftRead(), tacFormatDetermine(), and tacRead4DM().

◆ strTokenNCpy()

int strTokenNCpy ( const char * s1,
const char * s2,
int i,
char * s3,
int count )

Copy the i'th token in the string s1 into string s3. The characters making up the string s2 are the delimiters that determine the tokens.

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

Definition at line 53 of file stringext.c.

64 {
65 int j=0, n=0;
66 char *cptr;
67 if(s3!=NULL && count>0) s3[0]=(char)0;
68 if(s1==NULL || s2==NULL || strlen(s1)==0 || strlen(s2)==0) return(0);
69 if(i<1 || s3==NULL || count<2) return(0);
70
71 cptr=(char*)s1;
72 do {
73 // pass delimiter characters
74 j=strspn(cptr, s2); cptr+=j;
75 // pass characters between delimiters
76 j=strcspn(cptr, s2); if(j>0) n++;
77 // if this is the required token nr, then stop here
78 if(n==i) {
79 if(j>count-1) j=count-1;
80 strlcpy(s3, cptr, j+1); //strncpy(s3, cptr, j); s3[j]=(char)0;
81 break;
82 }
83 cptr+=j;
84 } while(j>0);
85 if(n>i) {s3[0]=(char)0; return(0);}
86 return(j);
87}

Referenced by atofList(), doubleGetWithUnit(), floatGetWithUnit(), iftGetDoubleWithUnit(), intlistAddFromString(), intlistExpandFromString(), parFromIFT(), parReadCSV(), parReadRES(), and tacReadSIF().

◆ strTokenNr()

int strTokenNr ( const char * s1,
const char * s2 )

Calculate the number of tokens in the string s1. The characters making up the string s2 are the delimiters that determine the token.

See also
strTokenNCpy, strTokenDup
Returns
Returns the nr of tokens.
Author
Vesa Oikonen
Parameters
s1String from where tokens are calculated; not modified in any way.
s2String containing character delimiters.

Definition at line 25 of file stringext.c.

30 {
31 int i=0, n=0;
32 char *cptr;
33 if(s1==NULL || s2==NULL || strnlen(s1, 1)==0 || strnlen(s2, 1)==0) return(0);
34
35 cptr=(char*)s1;
36 do {
37 // pass delimiter characters
38 i=strspn(cptr, s2); cptr+=i;
39 // pass characters between delimiters
40 i=strcspn(cptr, s2); cptr+=i; if(i>0) n++;
41 } while(i>0);
42 return(n);
43}

Referenced by atofList(), doubleGetWithUnit(), floatGetWithUnit(), intlistAddFromString(), intlistExpandFromString(), parFromIFT(), parReadRES(), and tacNameSplit().

◆ strTrimLeft()

char * strTrimLeft ( char * s,
const size_t t )

Strip string of t characters from the left.

Returns
Returns pointer to the string.
See also
filenameGetExtension
Parameters
sPointer to the string to be trimmed. Overwritten.
tNumber of characters to remove.

Definition at line 710 of file stringext.c.

715 {
716 if(s==NULL || t==0) return(s);
717 size_t len=strlen(s); if(len<1) return(s);
718 if(t>=len) {s[0]=(char)0; return(s);}
719 memmove(s, s+t, 1+len-t);
720 return(s);
721}

◆ 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 118 of file stringext.c.

121 {
122 unsigned int n=0, i;
123 if(s==NULL || strlen(s)==0) return (int)n;
124 for(i=0; i<strlen(s); i++) if(isupper(s[i])) n++;
125 return (int)n;
126}