Annotation of libwww/Library/src/HTString.c, revision 2.40
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.40 ! eric 6: ** @(#) $Id: HTString.c,v 2.39 1996/06/27 19:49:00 manoli 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
1.1 timbl 14: */
2.8 frystyk 15:
2.12 frystyk 16: /* Library include files */
2.37 frystyk 17: #include "sysdep.h"
1.1 timbl 18: #include "HTUtils.h"
2.8 frystyk 19: #include "HTString.h" /* Implemented here */
1.1 timbl 20:
2.31 frystyk 21: #if WWWTRACE_MODE == WWWTRACE_FILE
2.12 frystyk 22: PUBLIC FILE *WWWTrace = NULL;
23: #endif
24:
2.32 frystyk 25: #ifndef WWW_WIN_DLL
2.31 frystyk 26: PUBLIC int WWW_TraceFlag = 0; /* Global trace flag for ALL W3 code */
2.26 frystyk 27: #endif
2.13 frystyk 28:
29: /* ------------------------------------------------------------------------- */
30:
1.1 timbl 31: /* Strings of any length
32: ** ---------------------
33: */
2.37 frystyk 34: PUBLIC int strcasecomp (const char * a, const char * b)
1.1 timbl 35: {
2.13 frystyk 36: int diff;
37: for( ; *a && *b; a++, b++) {
38: if ((diff = TOLOWER(*a) - TOLOWER(*b)))
39: return diff;
40: }
41: if (*a) return 1; /* a was longer than b */
42: if (*b) return -1; /* a was shorter than b */
43: return 0; /* Exact match */
1.1 timbl 44: }
45:
46:
47: /* With count limit
48: ** ----------------
49: */
2.37 frystyk 50: PUBLIC int strncasecomp (const char * a, const char * b, int n)
1.1 timbl 51: {
2.37 frystyk 52: const char *p =a;
53: const char *q =b;
1.1 timbl 54:
55: for(p=a, q=b;; p++, q++) {
56: int diff;
57: if (p == a+n) return 0; /* Match up to n characters */
58: if (!(*p && *q)) return *p - *q;
59: diff = TOLOWER(*p) - TOLOWER(*q);
60: if (diff) return diff;
61: }
62: /*NOTREACHED*/
63: }
64:
2.7 luotonen 65:
66: /*
2.29 frystyk 67: ** strcasestr(s1,s2) -- like strstr(s1,s2) but case-insensitive.
68: */
69: PUBLIC char * strcasestr (char * s1, char * s2)
2.7 luotonen 70: {
2.9 frystyk 71: char * ptr = s1;
2.7 luotonen 72:
73: if (!s1 || !s2 || !*s2) return s1;
74:
2.9 frystyk 75: while (*ptr) {
76: if (TOUPPER(*ptr) == TOUPPER(*s2)) {
77: char * cur1 = ptr + 1;
2.7 luotonen 78: char * cur2 = s2 + 1;
79: while (*cur1 && *cur2 && TOUPPER(*cur1) == TOUPPER(*cur2)) {
80: cur1++;
81: cur2++;
82: }
2.29 frystyk 83: if (!*cur2) return ptr;
2.7 luotonen 84: }
2.9 frystyk 85: ptr++;
2.7 luotonen 86: }
87: return NULL;
88: }
89:
90:
91:
1.1 timbl 92: /* Allocate a new copy of a string, and returns it
93: */
2.37 frystyk 94: PUBLIC char * HTSACopy (char ** dest, const char * src)
1.1 timbl 95: {
2.33 frystyk 96: if (*dest) HT_FREE(*dest);
1.1 timbl 97: if (! src)
98: *dest = NULL;
99: else {
2.33 frystyk 100: if ((*dest = (char *) HT_MALLOC(strlen(src) + 1)) == NULL)
101: HT_OUTOFMEM("HTSACopy");
1.1 timbl 102: strcpy (*dest, src);
103: }
104: return *dest;
105: }
106:
2.6 timbl 107: /* String Allocate and Concatenate
108: */
2.37 frystyk 109: PUBLIC char * HTSACat (char ** dest, const char * src)
1.1 timbl 110: {
111: if (src && *src) {
112: if (*dest) {
113: int length = strlen (*dest);
2.33 frystyk 114: if ((*dest = (char *) HT_REALLOC(*dest, length + strlen(src) + 1)) == NULL)
115: HT_OUTOFMEM("HTSACat");
1.1 timbl 116: strcpy (*dest + length, src);
117: } else {
2.33 frystyk 118: if ((*dest = (char *) HT_MALLOC(strlen(src) + 1)) == NULL)
119: HT_OUTOFMEM("HTSACat");
1.1 timbl 120: strcpy (*dest, src);
121: }
122: }
123: return *dest;
2.6 timbl 124: }
125:
2.31 frystyk 126: /* String Matching
2.6 timbl 127: ** ---------------
2.29 frystyk 128: ** String comparison function for file names with one wildcard * in the
129: ** template. Arguments are:
130: **
131: ** tmpl is a template string to match the name against.
132: ** agaist, may contain a single wildcard character * which
133: ** matches zero or more arbitrary characters.
134: ** name is the name to be matched agaist the template.
135: **
2.32 frystyk 136: ** return: - Empty string if perfect match
137: ** - pointer to part matched by wildcard if any
138: ** - NULL if no match
2.29 frystyk 139: */
2.37 frystyk 140: PUBLIC char * HTStrMatch (const char * tmpl, const char * name)
2.29 frystyk 141: {
142: while (*tmpl && *name && *tmpl==*name) tmpl++, name++;
2.36 frystyk 143: return ((!*tmpl && !*name) || *tmpl=='*') ? (char *) name : (char *) NULL;
2.29 frystyk 144: }
145:
2.37 frystyk 146: PUBLIC char * HTStrCaseMatch (const char * tmpl, const char * name)
2.29 frystyk 147: {
148: while (*tmpl && *name && TOUPPER(*tmpl)==TOUPPER(*name)) tmpl++, name++;
2.36 frystyk 149: return ((!*tmpl && !*name) || *tmpl=='*') ? (char *) name : (char *) NULL;
2.29 frystyk 150: }
151:
2.23 frystyk 152: /* Strip white space off a string
153: ** ------------------------------
154: ** Return value points to first non-white character, or to 0 if none.
155: ** All trailing white space is OVERWRITTEN with zero.
156: */
2.29 frystyk 157: PUBLIC char * HTStrip (char * s)
2.23 frystyk 158: {
2.31 frystyk 159: if (s) {
160: char * p=s;
161: for(p=s;*p;p++); /* Find end of string */
162: for(p--;p>=s;p--) {
163: if (WHITE(*p))
164: *p=0; /* Zap trailing blanks */
165: else
166: break;
167: }
168: while (WHITE(*s)) s++; /* Strip leading blanks */
2.23 frystyk 169: }
170: return s;
2.26 frystyk 171: }
2.34 eric 172:
173: PRIVATE HTTraceCallback * PHTTraceCallback;
174:
175: PUBLIC void HTTrace_setCallback(HTTraceCallback * pCall)
176: {
177: PHTTraceCallback = pCall;
178: }
179:
180: PUBLIC HTTraceCallback * HTTrace_getCallback(void)
181: {
182: return PHTTraceCallback;
183: }
184:
2.37 frystyk 185: PUBLIC int HTTrace(const char * fmt, ...)
2.34 eric 186: {
187: va_list pArgs;
188: va_start(pArgs, fmt);
189: if (PHTTraceCallback)
2.40 ! eric 190: return (*PHTTraceCallback)(fmt, pArgs);
2.39 manoli 191: #ifdef WWW_WIN_WINDOWS
2.34 eric 192: return (0);
193: #else
194: return (vfprintf(stderr, fmt, pArgs));
195: #endif
196: }
197:
Webmaster