Annotation of libwww/Library/src/HTString.c, revision 2.46
2.10 frystyk 1: /* HTString.c
2: ** DYNAMIC STRING UTILITIES
3: **
2.21 frystyk 4: ** (c) COPYRIGHT MIT 1995.
2.10 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
2.46 ! raff 6: ** @(#) $Id: HTString.c,v 2.45 1999/07/31 01:27:55 raff Exp $
1.1 timbl 7: **
8: ** Original version came with listserv implementation.
9: ** Version TBL Oct 91 replaces one which modified the strings.
10: ** 02-Dec-91 (JFG) Added stralloccopy and stralloccat
11: ** 23 Jan 92 (TBL) Changed strallocc* to 8 char HTSAC* for VM and suchlike
12: ** 6 Oct 92 (TBL) Moved WWW_TraceFlag in here to be in library
2.28 frystyk 13: ** 9 Oct 95 (KR) fixed problem with double quotes in HTNextField
2.41 frystyk 14: ** 26 Nov 96 (EGP) moved HTTrace stuff to HTTrace.c
1.1 timbl 15: */
2.8 frystyk 16:
2.12 frystyk 17: /* Library include files */
2.43 frystyk 18: #include "wwwsys.h"
1.1 timbl 19: #include "HTUtils.h"
2.8 frystyk 20: #include "HTString.h" /* Implemented here */
1.1 timbl 21:
2.13 frystyk 22: /* ------------------------------------------------------------------------- */
23:
1.1 timbl 24: /* Strings of any length
25: ** ---------------------
26: */
2.37 frystyk 27: PUBLIC int strcasecomp (const char * a, const char * b)
1.1 timbl 28: {
2.13 frystyk 29: int diff;
30: for( ; *a && *b; a++, b++) {
31: if ((diff = TOLOWER(*a) - TOLOWER(*b)))
32: return diff;
33: }
34: if (*a) return 1; /* a was longer than b */
35: if (*b) return -1; /* a was shorter than b */
36: return 0; /* Exact match */
1.1 timbl 37: }
38:
39:
40: /* With count limit
41: ** ----------------
42: */
2.37 frystyk 43: PUBLIC int strncasecomp (const char * a, const char * b, int n)
1.1 timbl 44: {
2.37 frystyk 45: const char *p =a;
46: const char *q =b;
1.1 timbl 47:
48: for(p=a, q=b;; p++, q++) {
49: int diff;
50: if (p == a+n) return 0; /* Match up to n characters */
51: if (!(*p && *q)) return *p - *q;
52: diff = TOLOWER(*p) - TOLOWER(*q);
53: if (diff) return diff;
54: }
55: /*NOTREACHED*/
56: }
57:
2.7 luotonen 58:
59: /*
2.29 frystyk 60: ** strcasestr(s1,s2) -- like strstr(s1,s2) but case-insensitive.
61: */
2.44 frystyk 62: PUBLIC char * HTStrCaseStr (char * s1, char * s2)
2.7 luotonen 63: {
2.9 frystyk 64: char * ptr = s1;
2.7 luotonen 65:
66: if (!s1 || !s2 || !*s2) return s1;
67:
2.9 frystyk 68: while (*ptr) {
69: if (TOUPPER(*ptr) == TOUPPER(*s2)) {
70: char * cur1 = ptr + 1;
2.7 luotonen 71: char * cur2 = s2 + 1;
72: while (*cur1 && *cur2 && TOUPPER(*cur1) == TOUPPER(*cur2)) {
73: cur1++;
74: cur2++;
75: }
2.29 frystyk 76: if (!*cur2) return ptr;
2.7 luotonen 77: }
2.9 frystyk 78: ptr++;
2.7 luotonen 79: }
80: return NULL;
81: }
82:
2.45 raff 83: /*
84: ** tailcomp(s1,s2) -- like strcmp(s1,s2) but match s1 with the tail of s2
85: ** (used for cookie domain comparison)
86: */
2.46 ! raff 87: PUBLIC int tailcomp(const char * s1, const char * s2)
2.45 raff 88: {
89: int l1 = strlen(s1);
90: int l2 = strlen(s2);
91:
92: if (l1 < l2)
93: s2 += (l2 - l1);
94:
95: return strcmp(s1, s2);
96: }
97:
2.46 ! raff 98: PUBLIC int tailcasecomp(const char * s1, const char * s2)
2.45 raff 99: {
100: int l1 = strlen(s1);
101: int l2 = strlen(s2);
2.7 luotonen 102:
2.45 raff 103: if (l1 < l2)
104: s2 += (l2 - l1);
105:
106: return strcasecomp(s1, s2);
107: }
2.7 luotonen 108:
1.1 timbl 109: /* Allocate a new copy of a string, and returns it
110: */
2.37 frystyk 111: PUBLIC char * HTSACopy (char ** dest, const char * src)
1.1 timbl 112: {
2.33 frystyk 113: if (*dest) HT_FREE(*dest);
1.1 timbl 114: if (! src)
115: *dest = NULL;
116: else {
2.33 frystyk 117: if ((*dest = (char *) HT_MALLOC(strlen(src) + 1)) == NULL)
118: HT_OUTOFMEM("HTSACopy");
1.1 timbl 119: strcpy (*dest, src);
120: }
121: return *dest;
122: }
123:
2.6 timbl 124: /* String Allocate and Concatenate
125: */
2.37 frystyk 126: PUBLIC char * HTSACat (char ** dest, const char * src)
1.1 timbl 127: {
128: if (src && *src) {
129: if (*dest) {
130: int length = strlen (*dest);
2.33 frystyk 131: if ((*dest = (char *) HT_REALLOC(*dest, length + strlen(src) + 1)) == NULL)
132: HT_OUTOFMEM("HTSACat");
1.1 timbl 133: strcpy (*dest + length, src);
134: } else {
2.33 frystyk 135: if ((*dest = (char *) HT_MALLOC(strlen(src) + 1)) == NULL)
136: HT_OUTOFMEM("HTSACat");
1.1 timbl 137: strcpy (*dest, src);
138: }
139: }
140: return *dest;
2.43 frystyk 141: }
142:
143: PUBLIC char * StrAllocMCopy (char ** dest, ...)
144: {
145: va_list pArgs;
146: char * p, * argp;
147:
148: /* How much space do we need? */
149: int needed = 0;
150: va_start(pArgs, dest);
151: while ((p = va_arg(pArgs, char *)) != NULL)
152: needed += strlen(p);
153: va_end(pArgs);
154:
155: if (*dest) HT_FREE(*dest);
156: if (needed) {
157:
158: /* Allocate that amount of memory */
159: if ((*dest = (char *) HT_MALLOC(needed + 1)) == NULL)
160: HT_OUTOFMEM("HTStrCpy");
161: p = *dest;
162:
163: /* Fill the string */
164: va_start(pArgs, dest);
165: while ((argp = va_arg (pArgs, char *)) != NULL) {
166: strcpy(p, argp);
167: p += strlen(argp);
168: }
169: va_end (pArgs);
170: }
171: return *dest;
172: }
173:
174: PUBLIC char * StrAllocMCat (char ** dest, ...)
175: {
176: va_list pArgs;
177: char * p, * argp;
178:
179: /* How much space do we need? */
180: int needed = 0;
181: va_start(pArgs, dest);
182: while ((p = va_arg(pArgs, char *)) != NULL)
183: needed += strlen(p);
184: va_end(pArgs);
185:
186: if (needed) {
187:
188: /* (Re) Allocate the amount of memory needed */
189: if (*dest) {
190: int dest_len = strlen(*dest);
191: if ((*dest = (char *) HT_REALLOC(*dest, dest_len + needed + 1)) == NULL)
192: HT_OUTOFMEM("HTStrCat");
193: p = *dest + dest_len;
194: } else {
195: if ((*dest = (char *) HT_MALLOC(needed + 1)) == NULL)
196: HT_OUTOFMEM("HTStrCat");
197: p = *dest;
198: }
199:
200: /* Fill the string */
201: va_start(pArgs, dest);
202: while ((argp = va_arg (pArgs, char *)) != NULL) {
203: strcpy(p, argp);
204: p += strlen(argp);
205: }
206: va_end (pArgs);
207: }
208: return *dest;
2.6 timbl 209: }
210:
2.31 frystyk 211: /* String Matching
2.6 timbl 212: ** ---------------
2.29 frystyk 213: ** String comparison function for file names with one wildcard * in the
214: ** template. Arguments are:
215: **
216: ** tmpl is a template string to match the name against.
217: ** agaist, may contain a single wildcard character * which
218: ** matches zero or more arbitrary characters.
219: ** name is the name to be matched agaist the template.
220: **
2.32 frystyk 221: ** return: - Empty string if perfect match
222: ** - pointer to part matched by wildcard if any
223: ** - NULL if no match
2.29 frystyk 224: */
2.37 frystyk 225: PUBLIC char * HTStrMatch (const char * tmpl, const char * name)
2.29 frystyk 226: {
227: while (*tmpl && *name && *tmpl==*name) tmpl++, name++;
2.36 frystyk 228: return ((!*tmpl && !*name) || *tmpl=='*') ? (char *) name : (char *) NULL;
2.29 frystyk 229: }
230:
2.37 frystyk 231: PUBLIC char * HTStrCaseMatch (const char * tmpl, const char * name)
2.29 frystyk 232: {
233: while (*tmpl && *name && TOUPPER(*tmpl)==TOUPPER(*name)) tmpl++, name++;
2.36 frystyk 234: return ((!*tmpl && !*name) || *tmpl=='*') ? (char *) name : (char *) NULL;
2.29 frystyk 235: }
236:
2.23 frystyk 237: /* Strip white space off a string
238: ** ------------------------------
239: ** Return value points to first non-white character, or to 0 if none.
240: ** All trailing white space is OVERWRITTEN with zero.
241: */
2.29 frystyk 242: PUBLIC char * HTStrip (char * s)
2.23 frystyk 243: {
2.31 frystyk 244: if (s) {
245: char * p=s;
246: for(p=s;*p;p++); /* Find end of string */
247: for(p--;p>=s;p--) {
2.42 frystyk 248: if (isspace((int) *p))
2.31 frystyk 249: *p=0; /* Zap trailing blanks */
250: else
251: break;
252: }
2.42 frystyk 253: while (isspace((int) *s)) s++; /* Strip leading blanks */
2.23 frystyk 254: }
255: return s;
2.34 eric 256: }
257:
Webmaster