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

2.10      frystyk     1: /*                                                                  HTString.c
                      2: **     DYNAMIC STRING UTILITIES
                      3: **
2.21      frystyk     4: **     (c) COPYRIGHT MIT 1995.
2.10      frystyk     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
2.28      frystyk    12: **       9 Oct 95 (KR)  fixed problem with double quotes in HTNextField
1.1       timbl      13: */
2.8       frystyk    14: 
2.12      frystyk    15: /* Library include files */
                     16: #include "tcp.h"
1.1       timbl      17: #include "HTUtils.h"
2.8       frystyk    18: #include "HTString.h"                                   /* Implemented here */
1.1       timbl      19: 
2.31      frystyk    20: #if WWWTRACE_MODE == WWWTRACE_FILE
2.12      frystyk    21: PUBLIC FILE *WWWTrace = NULL;
                     22: #endif
                     23: 
2.32    ! frystyk    24: #ifndef WWW_WIN_DLL
2.31      frystyk    25: PUBLIC int WWW_TraceFlag = 0;          /* Global trace flag for ALL W3 code */
2.26      frystyk    26: #endif
2.13      frystyk    27: 
                     28: /* ------------------------------------------------------------------------- */
                     29: 
1.1       timbl      30: /*     Strings of any length
                     31: **     ---------------------
                     32: */
2.29      frystyk    33: PUBLIC int strcasecomp (CONST char * a, CONST char * b)
1.1       timbl      34: {
2.13      frystyk    35:     int diff;
                     36:     for( ; *a && *b; a++, b++) {
                     37:        if ((diff = TOLOWER(*a) - TOLOWER(*b)))
                     38:            return diff;
                     39:     }
                     40:     if (*a) return 1;                  /* a was longer than b */
                     41:     if (*b) return -1;                 /* a was shorter than b */
                     42:     return 0;                          /* Exact match */
1.1       timbl      43: }
                     44: 
                     45: 
                     46: /*     With count limit
                     47: **     ----------------
                     48: */
2.29      frystyk    49: PUBLIC int strncasecomp (CONST char * a, CONST char * b, int n)
1.1       timbl      50: {
                     51:        CONST char *p =a;
                     52:        CONST char *q =b;
                     53:        
                     54:        for(p=a, q=b;; p++, q++) {
                     55:            int diff;
                     56:            if (p == a+n) return 0;     /*   Match up to n characters */
                     57:            if (!(*p && *q)) return *p - *q;
                     58:            diff = TOLOWER(*p) - TOLOWER(*q);
                     59:            if (diff) return diff;
                     60:        }
                     61:        /*NOTREACHED*/
                     62: }
                     63: 
2.7       luotonen   64: 
                     65: /*
2.29      frystyk    66: ** strcasestr(s1,s2) -- like strstr(s1,s2) but case-insensitive.
                     67: */
                     68: PUBLIC char * strcasestr (char * s1, char * s2)
2.7       luotonen   69: {
2.9       frystyk    70:     char * ptr = s1;
2.7       luotonen   71: 
                     72:     if (!s1 || !s2 || !*s2) return s1;
                     73: 
2.9       frystyk    74:     while (*ptr) {
                     75:        if (TOUPPER(*ptr) == TOUPPER(*s2)) {
                     76:            char * cur1 = ptr + 1;
2.7       luotonen   77:            char * cur2 = s2 + 1;
                     78:            while (*cur1 && *cur2 && TOUPPER(*cur1) == TOUPPER(*cur2)) {
                     79:                cur1++;
                     80:                cur2++;
                     81:            }
2.29      frystyk    82:            if (!*cur2) return ptr;
2.7       luotonen   83:        }
2.9       frystyk    84:        ptr++;
2.7       luotonen   85:     }
                     86:     return NULL;
                     87: }
                     88: 
                     89: 
                     90: 
1.1       timbl      91: /*     Allocate a new copy of a string, and returns it
                     92: */
2.29      frystyk    93: PUBLIC char * HTSACopy (char ** dest, CONST char * src)
1.1       timbl      94: {
                     95:   if (*dest) free(*dest);
                     96:   if (! src)
                     97:     *dest = NULL;
                     98:   else {
                     99:     *dest = (char *) malloc (strlen(src) + 1);
                    100:     if (*dest == NULL) outofmem(__FILE__, "HTSACopy");
                    101:     strcpy (*dest, src);
                    102:   }
                    103:   return *dest;
                    104: }
                    105: 
2.6       timbl     106: /*     String Allocate and Concatenate
                    107: */
2.29      frystyk   108: PUBLIC char * HTSACat (char ** dest, CONST char * src)
1.1       timbl     109: {
                    110:   if (src && *src) {
                    111:     if (*dest) {
                    112:       int length = strlen (*dest);
                    113:       *dest = (char *) realloc (*dest, length + strlen(src) + 1);
                    114:       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
                    115:       strcpy (*dest + length, src);
                    116:     } else {
                    117:       *dest = (char *) malloc (strlen(src) + 1);
                    118:       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
                    119:       strcpy (*dest, src);
                    120:     }
                    121:   }
                    122:   return *dest;
2.6       timbl     123: }
                    124: 
2.31      frystyk   125: /*     String Matching
2.6       timbl     126: **     ---------------
2.29      frystyk   127: **     String comparison function for file names with one wildcard * in the
                    128: **     template. Arguments are:
                    129: **
                    130: **     tmpl    is a template string to match the name against.
                    131: **             agaist, may contain a single wildcard character * which
                    132: **             matches zero or more arbitrary characters.
                    133: **     name    is the name to be matched agaist the template.
                    134: **
2.32    ! frystyk   135: **     return: - Empty string if perfect match
        !           136: **             - pointer to part matched by wildcard if any
        !           137: **             - NULL if no match
2.29      frystyk   138: */
2.32    ! frystyk   139: PUBLIC char * HTStrMatch (CONST char * tmpl, CONST char * name)
2.29      frystyk   140: {
                    141:     while (*tmpl && *name && *tmpl==*name) tmpl++, name++;
2.32    ! frystyk   142:     return ((!*tmpl && !*name) || *tmpl=='*') ? (char *) name : NULL;
2.29      frystyk   143: }    
                    144: 
2.32    ! frystyk   145: PUBLIC char * HTStrCaseMatch (CONST char * tmpl, CONST char * name)
2.29      frystyk   146: {
                    147:     while (*tmpl && *name && TOUPPER(*tmpl)==TOUPPER(*name)) tmpl++, name++;
2.32    ! frystyk   148:     return ((!*tmpl && !*name) || *tmpl=='*') ? (char *) name : NULL;
2.29      frystyk   149: }    
                    150: 
2.23      frystyk   151: /*     Strip white space off a string
                    152: **     ------------------------------
                    153: **     Return value points to first non-white character, or to 0 if none.
                    154: **     All trailing white space is OVERWRITTEN with zero.
                    155: */
2.29      frystyk   156: PUBLIC char * HTStrip (char * s)
2.23      frystyk   157: {
2.31      frystyk   158:     if (s) {
                    159:        char * p=s;
                    160:        for(p=s;*p;p++);                /* Find end of string */
                    161:        for(p--;p>=s;p--) {
                    162:            if (WHITE(*p))
                    163:                *p=0;                   /* Zap trailing blanks */
                    164:            else
                    165:                break;
                    166:        }
                    167:        while (WHITE(*s)) s++;          /* Strip leading blanks */
2.23      frystyk   168:     }
                    169:     return s;
2.26      frystyk   170: }

Webmaster