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

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

Webmaster