Annotation of libwww/Library/src/HTString.c, revision 2.8

1.1       timbl       1: /*             Case-independent string comparison              HTString.c
                      2: **
                      3: **     Original version came with listserv implementation.
                      4: **     Version TBL Oct 91 replaces one which modified the strings.
                      5: **     02-Dec-91 (JFG) Added stralloccopy and stralloccat
                      6: **     23 Jan 92 (TBL) Changed strallocc* to 8 char HTSAC* for VM and suchlike
                      7: **      6 Oct 92 (TBL) Moved WWW_TraceFlag in here to be in library
                      8: */
2.8     ! frystyk     9: 
        !            10: #include "tcp.h"
1.1       timbl      11: #include "HTUtils.h"
2.8     ! frystyk    12: #include "HTString.h"                                   /* Implemented here */
1.1       timbl      13: 
                     14: PUBLIC int WWW_TraceFlag = 0;  /* Global trace flag for ALL W3 code */
                     15: 
                     16: #ifndef VC
                     17: #define VC "unknown"
                     18: #endif
                     19: 
                     20: PUBLIC CONST char * HTLibraryVersion = VC; /* String for help screen etc */
                     21: 
                     22: #ifndef VM             /* VM has these already it seems */
                     23:        
                     24: /*     Strings of any length
                     25: **     ---------------------
                     26: */
                     27: PUBLIC int strcasecomp ARGS2 (CONST char*,a, CONST char *,b)
                     28: {
                     29:        CONST char *p =a;
                     30:        CONST char *q =b;
                     31:        for(p=a, q=b; *p && *q; p++, q++) {
                     32:            int diff = TOLOWER(*p) - TOLOWER(*q);
                     33:            if (diff) return diff;
                     34:        }
                     35:        if (*p) return 1;       /* p was longer than q */
                     36:        if (*q) return -1;      /* p was shorter than q */
                     37:        return 0;               /* Exact match */
                     38: }
                     39: 
                     40: 
                     41: /*     With count limit
                     42: **     ----------------
                     43: */
                     44: PUBLIC int strncasecomp ARGS3(CONST char*,a, CONST char *,b, int,n)
                     45: {
                     46:        CONST char *p =a;
                     47:        CONST char *q =b;
                     48:        
                     49:        for(p=a, q=b;; p++, q++) {
                     50:            int diff;
                     51:            if (p == a+n) return 0;     /*   Match up to n characters */
                     52:            if (!(*p && *q)) return *p - *q;
                     53:            diff = TOLOWER(*p) - TOLOWER(*q);
                     54:            if (diff) return diff;
                     55:        }
                     56:        /*NOTREACHED*/
                     57: }
                     58: #endif
                     59: 
2.7       luotonen   60: 
                     61: /*
                     62:  * strcasestr(s1,s2) -- like strstr(s1,s2) but case-insensitive.
                     63:  */
                     64: PUBLIC char * strcasestr ARGS2(char *, s1,
                     65:                               char *,  s2)
                     66: {
                     67:     char * try = s1;
                     68: 
                     69:     if (!s1 || !s2 || !*s2) return s1;
                     70: 
                     71:     while (*try) {
                     72:        if (TOUPPER(*try) == TOUPPER(*s2)) {
                     73:            char * cur1 = try + 1;
                     74:            char * cur2 = s2 + 1;
                     75:            while (*cur1 && *cur2 && TOUPPER(*cur1) == TOUPPER(*cur2)) {
                     76:                cur1++;
                     77:                cur2++;
                     78:            }
                     79:            if (!*cur2) {
                     80:                CTRACE(stderr,
                     81:              "Debug....... strcasestr(s1 = \"%s\", s2 = \"%s\") => \"%s\"\n",
                     82:                       s1,s2,try);
                     83:                return try;
                     84:            }
                     85:        }
                     86:        try++;
                     87:     }
                     88:     CTRACE(stderr,
                     89:           "Debug....... strcasestr(s1 = \"%s\", s2 = \"%s\") => No match\n",
                     90:           s1,s2);
                     91:     return NULL;
                     92: }
                     93: 
                     94: 
                     95: 
1.1       timbl      96: /*     Allocate a new copy of a string, and returns it
                     97: */
                     98: PUBLIC char * HTSACopy
                     99:   ARGS2 (char **,dest, CONST char *,src)
                    100: {
                    101:   if (*dest) free(*dest);
                    102:   if (! src)
                    103:     *dest = NULL;
                    104:   else {
                    105:     *dest = (char *) malloc (strlen(src) + 1);
                    106:     if (*dest == NULL) outofmem(__FILE__, "HTSACopy");
                    107:     strcpy (*dest, src);
                    108:   }
                    109:   return *dest;
                    110: }
                    111: 
2.6       timbl     112: /*     String Allocate and Concatenate
                    113: */
1.1       timbl     114: PUBLIC char * HTSACat
                    115:   ARGS2 (char **,dest, CONST char *,src)
                    116: {
                    117:   if (src && *src) {
                    118:     if (*dest) {
                    119:       int length = strlen (*dest);
                    120:       *dest = (char *) realloc (*dest, length + strlen(src) + 1);
                    121:       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
                    122:       strcpy (*dest + length, src);
                    123:     } else {
                    124:       *dest = (char *) malloc (strlen(src) + 1);
                    125:       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
                    126:       strcpy (*dest, src);
                    127:     }
                    128:   }
                    129:   return *dest;
2.6       timbl     130: }
                    131: 
                    132: 
                    133: /*     Find next Field
                    134: **     ---------------
                    135: **
                    136: ** On entry,
                    137: **     *pstr   points to a string containig white space separated
                    138: **             field, optionlly quoted.
                    139: **
                    140: ** On exit,
                    141: **     *pstr   has been moved to the first delimiter past the
                    142: **             field
                    143: **             THE STRING HAS BEEN MUTILATED by a 0 terminator
                    144: **
                    145: **     returns a pointer to the first field
                    146: */
                    147: PUBLIC char * HTNextField ARGS1(char **, pstr)
                    148: {
                    149:     char * p = *pstr;
                    150:     char * start;                      /* start of field */
                    151:     
                    152:     while(*p && WHITE(*p)) p++;                /* Strip white space */
                    153:     if (!*p) {
                    154:        *pstr = p;
                    155:         return NULL;           /* No first field */
                    156:     }
                    157:     if (*p == '"') {                   /* quoted field */
                    158:         p++;
                    159:        start = p;
                    160:        for(;*p && *p!='"'; p++) {
                    161:            if (*p == '\\' && p[1]) p++;        /* Skip escaped chars */
                    162:        }
                    163:     } else {
                    164:        start = p;
                    165:        while(*p && !WHITE(*p)) p++;    /* Skip first field */
                    166:     }
                    167:     if (*p) *p++ = 0;
                    168:     *pstr = p;
                    169:     return start;
1.1       timbl     170: }

Webmaster