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

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.11    ! roeber     14: #include "sysdep.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: 
2.11    ! roeber     26: #ifndef HAVE_STRCASECOMP
1.1       timbl      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: }
2.11    ! roeber     43: #endif
1.1       timbl      44: 
2.11    ! roeber     45: #ifndef HAVE_STRNCASECOMP
1.1       timbl      46: 
                     47: /*     With count limit
                     48: **     ----------------
                     49: */
                     50: PUBLIC int strncasecomp ARGS3(CONST char*,a, CONST char *,b, int,n)
                     51: {
                     52:        CONST char *p =a;
                     53:        CONST char *q =b;
                     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: #endif
                     65: 
2.7       luotonen   66: 
                     67: /*
                     68:  * strcasestr(s1,s2) -- like strstr(s1,s2) but case-insensitive.
                     69:  */
                     70: PUBLIC char * strcasestr ARGS2(char *, s1,
                     71:                               char *,  s2)
                     72: {
2.9       frystyk    73:     char * ptr = s1;
2.7       luotonen   74: 
                     75:     if (!s1 || !s2 || !*s2) return s1;
                     76: 
2.9       frystyk    77:     while (*ptr) {
                     78:        if (TOUPPER(*ptr) == TOUPPER(*s2)) {
                     79:            char * cur1 = ptr + 1;
2.7       luotonen   80:            char * cur2 = s2 + 1;
                     81:            while (*cur1 && *cur2 && TOUPPER(*cur1) == TOUPPER(*cur2)) {
                     82:                cur1++;
                     83:                cur2++;
                     84:            }
                     85:            if (!*cur2) {
                     86:                CTRACE(stderr,
                     87:              "Debug....... strcasestr(s1 = \"%s\", s2 = \"%s\") => \"%s\"\n",
2.9       frystyk    88:                       s1,s2,ptr);
                     89:                return ptr;
2.7       luotonen   90:            }
                     91:        }
2.9       frystyk    92:        ptr++;
2.7       luotonen   93:     }
                     94:     CTRACE(stderr,
                     95:           "Debug....... strcasestr(s1 = \"%s\", s2 = \"%s\") => No match\n",
                     96:           s1,s2);
                     97:     return NULL;
                     98: }
                     99: 
                    100: 
                    101: 
1.1       timbl     102: /*     Allocate a new copy of a string, and returns it
                    103: */
                    104: PUBLIC char * HTSACopy
                    105:   ARGS2 (char **,dest, CONST char *,src)
                    106: {
                    107:   if (*dest) free(*dest);
                    108:   if (! src)
                    109:     *dest = NULL;
                    110:   else {
                    111:     *dest = (char *) malloc (strlen(src) + 1);
                    112:     if (*dest == NULL) outofmem(__FILE__, "HTSACopy");
                    113:     strcpy (*dest, src);
                    114:   }
                    115:   return *dest;
                    116: }
                    117: 
2.6       timbl     118: /*     String Allocate and Concatenate
                    119: */
1.1       timbl     120: PUBLIC char * HTSACat
                    121:   ARGS2 (char **,dest, CONST char *,src)
                    122: {
                    123:   if (src && *src) {
                    124:     if (*dest) {
                    125:       int length = strlen (*dest);
                    126:       *dest = (char *) realloc (*dest, length + strlen(src) + 1);
                    127:       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
                    128:       strcpy (*dest + length, src);
                    129:     } else {
                    130:       *dest = (char *) malloc (strlen(src) + 1);
                    131:       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
                    132:       strcpy (*dest, src);
                    133:     }
                    134:   }
                    135:   return *dest;
2.6       timbl     136: }
                    137: 
                    138: 
                    139: /*     Find next Field
                    140: **     ---------------
                    141: **
                    142: ** On entry,
                    143: **     *pstr   points to a string containig white space separated
                    144: **             field, optionlly quoted.
                    145: **
                    146: ** On exit,
                    147: **     *pstr   has been moved to the first delimiter past the
                    148: **             field
                    149: **             THE STRING HAS BEEN MUTILATED by a 0 terminator
                    150: **
                    151: **     returns a pointer to the first field
                    152: */
                    153: PUBLIC char * HTNextField ARGS1(char **, pstr)
                    154: {
                    155:     char * p = *pstr;
                    156:     char * start;                      /* start of field */
                    157:     
                    158:     while(*p && WHITE(*p)) p++;                /* Strip white space */
                    159:     if (!*p) {
                    160:        *pstr = p;
                    161:         return NULL;           /* No first field */
                    162:     }
                    163:     if (*p == '"') {                   /* quoted field */
                    164:         p++;
                    165:        start = p;
                    166:        for(;*p && *p!='"'; p++) {
                    167:            if (*p == '\\' && p[1]) p++;        /* Skip escaped chars */
                    168:        }
                    169:     } else {
                    170:        start = p;
                    171:        while(*p && !WHITE(*p)) p++;    /* Skip first field */
                    172:     }
                    173:     if (*p) *p++ = 0;
                    174:     *pstr = p;
                    175:     return start;
1.1       timbl     176: }

Webmaster