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

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.
2.42    ! frystyk     6: **     @(#) $Id: HTString.c,v 2.41 1996/11/30 23:31:51 frystyk Exp $
1.1       timbl       7: **
                      8: **     Original version came with listserv implementation.
                      9: **     Version TBL Oct 91 replaces one which modified the strings.
                     10: **     02-Dec-91 (JFG) Added stralloccopy and stralloccat
                     11: **     23 Jan 92 (TBL) Changed strallocc* to 8 char HTSAC* for VM and suchlike
                     12: **      6 Oct 92 (TBL) Moved WWW_TraceFlag in here to be in library
2.28      frystyk    13: **       9 Oct 95 (KR)  fixed problem with double quotes in HTNextField
2.41      frystyk    14: **     26 Nov 96 (EGP) moved HTTrace stuff to HTTrace.c
1.1       timbl      15: */
2.8       frystyk    16: 
2.12      frystyk    17: /* Library include files */
2.37      frystyk    18: #include "sysdep.h"
1.1       timbl      19: #include "HTUtils.h"
2.8       frystyk    20: #include "HTString.h"                                   /* Implemented here */
1.1       timbl      21: 
2.13      frystyk    22: /* ------------------------------------------------------------------------- */
                     23: 
1.1       timbl      24: /*     Strings of any length
                     25: **     ---------------------
                     26: */
2.37      frystyk    27: PUBLIC int strcasecomp (const char * a, const char * b)
1.1       timbl      28: {
2.13      frystyk    29:     int diff;
                     30:     for( ; *a && *b; a++, b++) {
                     31:        if ((diff = TOLOWER(*a) - TOLOWER(*b)))
                     32:            return diff;
                     33:     }
                     34:     if (*a) return 1;                  /* a was longer than b */
                     35:     if (*b) return -1;                 /* a was shorter than b */
                     36:     return 0;                          /* Exact match */
1.1       timbl      37: }
                     38: 
                     39: 
                     40: /*     With count limit
                     41: **     ----------------
                     42: */
2.37      frystyk    43: PUBLIC int strncasecomp (const char * a, const char * b, int n)
1.1       timbl      44: {
2.37      frystyk    45:        const char *p =a;
                     46:        const char *q =b;
1.1       timbl      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: 
2.7       luotonen   58: 
                     59: /*
2.29      frystyk    60: ** strcasestr(s1,s2) -- like strstr(s1,s2) but case-insensitive.
                     61: */
                     62: PUBLIC char * strcasestr (char * s1, char * s2)
2.7       luotonen   63: {
2.9       frystyk    64:     char * ptr = s1;
2.7       luotonen   65: 
                     66:     if (!s1 || !s2 || !*s2) return s1;
                     67: 
2.9       frystyk    68:     while (*ptr) {
                     69:        if (TOUPPER(*ptr) == TOUPPER(*s2)) {
                     70:            char * cur1 = ptr + 1;
2.7       luotonen   71:            char * cur2 = s2 + 1;
                     72:            while (*cur1 && *cur2 && TOUPPER(*cur1) == TOUPPER(*cur2)) {
                     73:                cur1++;
                     74:                cur2++;
                     75:            }
2.29      frystyk    76:            if (!*cur2) return ptr;
2.7       luotonen   77:        }
2.9       frystyk    78:        ptr++;
2.7       luotonen   79:     }
                     80:     return NULL;
                     81: }
                     82: 
                     83: 
                     84: 
1.1       timbl      85: /*     Allocate a new copy of a string, and returns it
                     86: */
2.37      frystyk    87: PUBLIC char * HTSACopy (char ** dest, const char * src)
1.1       timbl      88: {
2.33      frystyk    89:   if (*dest) HT_FREE(*dest);
1.1       timbl      90:   if (! src)
                     91:     *dest = NULL;
                     92:   else {
2.33      frystyk    93:     if ((*dest  = (char  *) HT_MALLOC(strlen(src) + 1)) == NULL)
                     94:         HT_OUTOFMEM("HTSACopy");
1.1       timbl      95:     strcpy (*dest, src);
                     96:   }
                     97:   return *dest;
                     98: }
                     99: 
2.6       timbl     100: /*     String Allocate and Concatenate
                    101: */
2.37      frystyk   102: PUBLIC char * HTSACat (char ** dest, const char * src)
1.1       timbl     103: {
                    104:   if (src && *src) {
                    105:     if (*dest) {
                    106:       int length = strlen (*dest);
2.33      frystyk   107:       if ((*dest  = (char  *) HT_REALLOC(*dest, length + strlen(src) + 1)) == NULL)
                    108:           HT_OUTOFMEM("HTSACat");
1.1       timbl     109:       strcpy (*dest + length, src);
                    110:     } else {
2.33      frystyk   111:       if ((*dest  = (char  *) HT_MALLOC(strlen(src) + 1)) == NULL)
                    112:           HT_OUTOFMEM("HTSACat");
1.1       timbl     113:       strcpy (*dest, src);
                    114:     }
                    115:   }
                    116:   return *dest;
2.6       timbl     117: }
                    118: 
2.31      frystyk   119: /*     String Matching
2.6       timbl     120: **     ---------------
2.29      frystyk   121: **     String comparison function for file names with one wildcard * in the
                    122: **     template. Arguments are:
                    123: **
                    124: **     tmpl    is a template string to match the name against.
                    125: **             agaist, may contain a single wildcard character * which
                    126: **             matches zero or more arbitrary characters.
                    127: **     name    is the name to be matched agaist the template.
                    128: **
2.32      frystyk   129: **     return: - Empty string if perfect match
                    130: **             - pointer to part matched by wildcard if any
                    131: **             - NULL if no match
2.29      frystyk   132: */
2.37      frystyk   133: PUBLIC char * HTStrMatch (const char * tmpl, const char * name)
2.29      frystyk   134: {
                    135:     while (*tmpl && *name && *tmpl==*name) tmpl++, name++;
2.36      frystyk   136:     return ((!*tmpl && !*name) || *tmpl=='*') ? (char *) name : (char *) NULL;
2.29      frystyk   137: }    
                    138: 
2.37      frystyk   139: PUBLIC char * HTStrCaseMatch (const char * tmpl, const char * name)
2.29      frystyk   140: {
                    141:     while (*tmpl && *name && TOUPPER(*tmpl)==TOUPPER(*name)) tmpl++, name++;
2.36      frystyk   142:     return ((!*tmpl && !*name) || *tmpl=='*') ? (char *) name : (char *) NULL;
2.29      frystyk   143: }    
                    144: 
2.23      frystyk   145: /*     Strip white space off a string
                    146: **     ------------------------------
                    147: **     Return value points to first non-white character, or to 0 if none.
                    148: **     All trailing white space is OVERWRITTEN with zero.
                    149: */
2.29      frystyk   150: PUBLIC char * HTStrip (char * s)
2.23      frystyk   151: {
2.31      frystyk   152:     if (s) {
                    153:        char * p=s;
                    154:        for(p=s;*p;p++);                /* Find end of string */
                    155:        for(p--;p>=s;p--) {
2.42    ! frystyk   156:            if (isspace((int) *p))
2.31      frystyk   157:                *p=0;                   /* Zap trailing blanks */
                    158:            else
                    159:                break;
                    160:        }
2.42    ! frystyk   161:        while (isspace((int) *s)) s++;  /* Strip leading blanks */
2.23      frystyk   162:     }
                    163:     return s;
2.34      eric      164: }
                    165: 

Webmaster