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

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

Webmaster