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

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.44    ! frystyk     6: **     @(#) $Id: HTString.c,v 2.43 1998/05/04 19:37:22 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.43      frystyk    18: #include "wwwsys.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: */
2.44    ! frystyk    62: PUBLIC char * HTStrCaseStr (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.43      frystyk   117: }
                    118: 
                    119: PUBLIC char * StrAllocMCopy (char ** dest, ...)
                    120: {
                    121:     va_list pArgs;
                    122:     char * p, * argp;
                    123: 
                    124:     /* How much space do we need? */
                    125:     int needed = 0;
                    126:     va_start(pArgs, dest);
                    127:     while ((p = va_arg(pArgs, char *)) != NULL) 
                    128:        needed += strlen(p);
                    129:     va_end(pArgs);
                    130: 
                    131:     if (*dest) HT_FREE(*dest);
                    132:     if (needed) {
                    133: 
                    134:        /* Allocate that amount of memory */
                    135:        if ((*dest = (char *) HT_MALLOC(needed + 1)) == NULL)
                    136:            HT_OUTOFMEM("HTStrCpy");
                    137:        p = *dest;
                    138: 
                    139:        /* Fill the string */
                    140:        va_start(pArgs, dest);
                    141:        while ((argp = va_arg (pArgs, char *)) != NULL) {
                    142:            strcpy(p, argp);
                    143:            p += strlen(argp);
                    144:        }
                    145:        va_end (pArgs);
                    146:     }
                    147:     return *dest;
                    148: }
                    149: 
                    150: PUBLIC char * StrAllocMCat (char ** dest, ...)
                    151: {
                    152:     va_list pArgs;
                    153:     char * p, * argp;
                    154: 
                    155:     /* How much space do we need? */
                    156:     int needed = 0;
                    157:     va_start(pArgs, dest);
                    158:     while ((p = va_arg(pArgs, char *)) != NULL) 
                    159:        needed += strlen(p);
                    160:     va_end(pArgs);
                    161: 
                    162:     if (needed) {
                    163: 
                    164:        /* (Re) Allocate the amount of memory needed */
                    165:        if (*dest) {
                    166:            int dest_len = strlen(*dest);
                    167:            if ((*dest = (char *) HT_REALLOC(*dest, dest_len + needed + 1)) == NULL)
                    168:                HT_OUTOFMEM("HTStrCat");
                    169:            p = *dest + dest_len;
                    170:        } else {
                    171:            if ((*dest = (char  *) HT_MALLOC(needed + 1)) == NULL)
                    172:                HT_OUTOFMEM("HTStrCat");
                    173:            p = *dest;
                    174:        }
                    175: 
                    176:        /* Fill the string */
                    177:        va_start(pArgs, dest);
                    178:        while ((argp = va_arg (pArgs, char *)) != NULL) {
                    179:            strcpy(p, argp);
                    180:            p += strlen(argp);
                    181:        }
                    182:        va_end (pArgs);
                    183:     }
                    184:     return *dest;
2.6       timbl     185: }
                    186: 
2.31      frystyk   187: /*     String Matching
2.6       timbl     188: **     ---------------
2.29      frystyk   189: **     String comparison function for file names with one wildcard * in the
                    190: **     template. Arguments are:
                    191: **
                    192: **     tmpl    is a template string to match the name against.
                    193: **             agaist, may contain a single wildcard character * which
                    194: **             matches zero or more arbitrary characters.
                    195: **     name    is the name to be matched agaist the template.
                    196: **
2.32      frystyk   197: **     return: - Empty string if perfect match
                    198: **             - pointer to part matched by wildcard if any
                    199: **             - NULL if no match
2.29      frystyk   200: */
2.37      frystyk   201: PUBLIC char * HTStrMatch (const char * tmpl, const char * name)
2.29      frystyk   202: {
                    203:     while (*tmpl && *name && *tmpl==*name) tmpl++, name++;
2.36      frystyk   204:     return ((!*tmpl && !*name) || *tmpl=='*') ? (char *) name : (char *) NULL;
2.29      frystyk   205: }    
                    206: 
2.37      frystyk   207: PUBLIC char * HTStrCaseMatch (const char * tmpl, const char * name)
2.29      frystyk   208: {
                    209:     while (*tmpl && *name && TOUPPER(*tmpl)==TOUPPER(*name)) tmpl++, name++;
2.36      frystyk   210:     return ((!*tmpl && !*name) || *tmpl=='*') ? (char *) name : (char *) NULL;
2.29      frystyk   211: }    
                    212: 
2.23      frystyk   213: /*     Strip white space off a string
                    214: **     ------------------------------
                    215: **     Return value points to first non-white character, or to 0 if none.
                    216: **     All trailing white space is OVERWRITTEN with zero.
                    217: */
2.29      frystyk   218: PUBLIC char * HTStrip (char * s)
2.23      frystyk   219: {
2.31      frystyk   220:     if (s) {
                    221:        char * p=s;
                    222:        for(p=s;*p;p++);                /* Find end of string */
                    223:        for(p--;p>=s;p--) {
2.42      frystyk   224:            if (isspace((int) *p))
2.31      frystyk   225:                *p=0;                   /* Zap trailing blanks */
                    226:            else
                    227:                break;
                    228:        }
2.42      frystyk   229:        while (isspace((int) *s)) s++;  /* Strip leading blanks */
2.23      frystyk   230:     }
                    231:     return s;
2.34      eric      232: }
                    233: 

Webmaster