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

Functions for processing strings with quotation marks. More...

#include "libtpcmisc.h"

Go to the source code of this file.

Functions

char * strstr_noquotation (const char *str1, const char *str2)
 
int strnCopyClean (char *str1, const char *str2, int maxlen)
 

Detailed Description

Functions for processing strings with quotation marks.

Author
Vesa Oikonen

Definition in file quots.c.

Function Documentation

◆ strnCopyClean()

int strnCopyClean ( char * str1,
const char * str2,
int maxlen )

Copy str2 to str1, removing any quotation marks around the string, and making sure that string fits to str2.

Returns
Returns the length of the new string str2.
Parameters
str1Pointer to pre-allocated result string with length of at least maxlen characters, including NULL character
str2Pointer to the original string; not changed in this function
maxlenMax length of str1, including the end NULL

Definition at line 52 of file quots.c.

60 {
61 char *cptr;
62 int i;
63
64 if(str1==NULL || maxlen<1) return(0);
65 str1[0]=(char)0; if(str2==NULL || strlen(str2)<1) return(0);
66 cptr=(char*)str2; cptr+=strspn(str2, "\"\'\t\n\r ");
67 strncpy(str1, cptr, maxlen-1); str1[maxlen-1]=(char)0;
68 i=strlen(str1); if(i<1) return(0);
69 cptr=str1+(i-1);
70 while(i>0) {
71 if(*cptr!='\"' && *cptr!='\'' && *cptr!='\t' && *cptr!='\n' &&
72 *cptr!='\r' && *cptr!=' ')
73 break;
74 i--; str1[i]=(char)0; cptr--;
75 }
76 return(i);
77}

Referenced by upetHeaderReadParameter().

◆ strstr_noquotation()

char * strstr_noquotation ( const char * str1,
const char * str2 )

The strstr_noquotation() function returns a pointer to the first occurrence in the string pointed to by str1, excluding parts that are inside quotation marks "" or '', of the string pointed to by str2.

Returns
Returns NULL pointer if no match is found, or if found, then pointer to the first occurrence.
Parameters
str1Pointer to string to be searched
str2Pointer to string with quotation marks

Definition at line 17 of file quots.c.

22 {
23 unsigned int i, test_len;
24 unsigned int single_quotation=0;
25 unsigned int double_quotation=0;
26 char *cptr;
27
28 if(str1==NULL) return((char*)NULL);
29 if(str2==NULL) return((char*)str1);
30 test_len=strlen(str2); if(test_len<1) return((char*)str1);
31 for(i=0, cptr=(char*)str1; i<strlen(str1); i++, cptr++) {
32 if(*cptr=='\'') {
33 if(single_quotation==0) single_quotation=1; else single_quotation=0;
34 continue;
35 }
36 if(*cptr=='\"') {
37 if(double_quotation==0) double_quotation=1; else double_quotation=0;
38 continue;
39 }
40 if(single_quotation==1 || double_quotation==1) continue;
41 if(strncmp(cptr, str2, test_len)==0) return(cptr);
42 }
43 return((char*)NULL);
44}

Referenced by iftRead().