TPCCLIB
Loading...
Searching...
No Matches
stringext.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include "tpcextensions.h"
8/*****************************************************************************/
9#include <stdio.h>
10#include <stdlib.h>
11#include <math.h>
12#include <time.h>
13#include <string.h>
14#include <strings.h>
15#include <ctype.h>
16/*****************************************************************************/
17
18/*****************************************************************************/
27 const char *s1,
29 const char *s2
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}
44/*****************************************************************************/
45
46/*****************************************************************************/
55 const char *s1,
57 const char *s2,
59 int i,
61 char *s3,
63 int count
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}
88/*****************************************************************************/
89
90/*****************************************************************************/
100 const char *s1,
102 const char *s2
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}
111/*****************************************************************************/
112
113/*****************************************************************************/
120 const char *s
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}
127/*****************************************************************************/
128
129/*****************************************************************************/
136 char *s,
138 char c1,
140 char c2
141) {
142 char *cptr;
143 if(s==NULL || strlen(s)==0) return;
144 while((cptr=strchr(s, c1))!=NULL) *cptr=c2;
145 return;
146}
147/*****************************************************************************/
148
149/*****************************************************************************/
150#ifndef HAVE_STRCASESTR
157 const char *haystack,
159 const char *needle
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}
175#endif // HAVE_STRCASESTR
176/*****************************************************************************/
177
178/*****************************************************************************/
179#ifndef HAVE_STRDUP
185char *strdup(
187 const char *s
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}
195#endif // HAVE_STRDUP
196/*****************************************************************************/
197
198/*****************************************************************************/
199#ifndef HAVE_STRNDUP
207 const char *s,
209 size_t n
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}
215#endif // HAVE_STRNDUP
216/*****************************************************************************/
217
218/*****************************************************************************/
227 const char *haystack,
229 const char *needle
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}
256/*****************************************************************************/
257
258/*****************************************************************************/
268 char *s1,
270 const char *s2,
272 int maxlen
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}
291/*****************************************************************************/
292
293/*****************************************************************************/
302 char *s
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}
312/*****************************************************************************/
313
314/*****************************************************************************/
324 char *s1,
326 const char *s2,
328 int maxlen
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}
379/*****************************************************************************/
380
381/*****************************************************************************/
391 char *s
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}
401/*****************************************************************************/
402
403/*****************************************************************************/
415 const char *s1,
417 const char *s2,
419 int *next
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}
442/*****************************************************************************/
443
444/*****************************************************************************/
455 char *s
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}
464/*****************************************************************************/
465
466/*****************************************************************************/
478 char *s
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}
486/*****************************************************************************/
487
488/*****************************************************************************/
497 char *str1,
499 const int n,
503 size_t count
504) {
505 char tmp[128]; sprintf(tmp, "%d", n);
506 return strncat(str1, tmp, count);
507}
508/*****************************************************************************/
509
510/*****************************************************************************/
519 char *str1,
521 const double d,
525 size_t count
526) {
527 char tmp[128]; sprintf(tmp, "%g", d);
528 return strncat(str1, tmp, count);
529}
530/*****************************************************************************/
531
532/*****************************************************************************/
541 char *str1,
543 const int n,
545 const int maxn,
549 size_t count
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}
557/*****************************************************************************/
558
559/*****************************************************************************/
560#ifndef HAVE_STRNLEN
566size_t strnlen(
568 const char *s,
571 size_t n
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}
579#endif // HAVE_STRNLEN
580/*****************************************************************************/
581
582/*****************************************************************************/
583#ifndef HAVE_STRLCAT
592size_t strlcat(
594 char *dst,
596 const char *src,
600 size_t dstsize
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}
619#endif // HAVE_STRLCAT
620/*****************************************************************************/
621
622/*****************************************************************************/
623#ifndef HAVE_STRLCPY
632size_t strlcpy(
634 char *dst,
636 const char *src,
640 size_t dstsize
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}
658#endif // HAVE_STRLCPY
659/*****************************************************************************/
660
661/*****************************************************************************/
673 char *s
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}
680/*****************************************************************************/
681
682/*****************************************************************************/
690 char *s1,
692 const char *s2
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}
703/*****************************************************************************/
704
705/*****************************************************************************/
712 char *s,
714 const size_t t
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}
722/*****************************************************************************/
723
724/*****************************************************************************/
733 const char *s
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}
762/*****************************************************************************/
763
764/*****************************************************************************/
771 char *s
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}
781/*****************************************************************************/
782
783/*****************************************************************************/
int strTokenNr(const char *s1, const char *s2)
Definition stringext.c:25
void strReplaceChar(char *s, char c1, char c2)
Definition stringext.c:134
int strInPars(char *s)
Definition stringext.c:453
void strCleanForXML(char *s)
Definition stringext.c:769
char * strstrNoQuotation(const char *haystack, const char *needle)
Definition stringext.c:225
char * strdup(const char *s)
Definition stringext.c:185
char * strncatIntZ(char *str1, const int n, const int maxn, size_t count)
Definition stringext.c:539
int strCleanSpaces(char *s)
Definition stringext.c:300
char * strncatDouble(char *str1, const double d, size_t count)
Definition stringext.c:517
void strCleanPars(char *s)
Definition stringext.c:476
char * strEncodeForXML(const char *s)
Definition stringext.c:731
char * strTrimLeft(char *s, const size_t t)
Definition stringext.c:710
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566
int strncpyCleanSpaces(char *s1, const char *s2, int maxlen)
Definition stringext.c:265
char * strncatInt(char *str1, const int n, size_t count)
Definition stringext.c:495
int strTokenNCpy(const char *s1, const char *s2, int i, char *s3, int count)
Definition stringext.c:53
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
int strncpyClean(char *s1, const char *s2, int maxlen)
Definition stringext.c:321
int strIsSpaceOnly(char *s)
Definition stringext.c:671
int strChrCount(const char *s1, const char *s2)
Definition stringext.c:98
char * strTokenDup(const char *s1, const char *s2, int *next)
Definition stringext.c:413
char * strdelstr(char *s1, const char *s2)
Definition stringext.c:688
int strClean(char *s)
Definition stringext.c:389
int strUppercaseCount(const char *s)
Definition stringext.c:118
char * strcasestr(const char *haystack, const char *needle)
Definition stringext.c:155
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition stringext.c:592
char * strndup(const char *s, size_t n)
Definition stringext.c:205
Header file for library libtpcextensions.