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

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

Webmaster