home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / HTString.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  3.1 KB  |  136 lines

  1. /*        Case-independent string comparison        HTString.c
  2. **
  3. **    Original version came with listserv implementation.
  4. **    Version TBL Oct 91 replaces one which modified the strings.
  5. **    02-Dec-91 (JFG) Added stralloccopy and stralloccat
  6. **    23 Jan 92 (TBL) Changed strallocc* to 8 char HTSAC* for VM and suchlike
  7. **     6 Oct 92 (TBL) Moved WWW_TraceFlag in here to be in library
  8. */
  9. #include <ctype.h>
  10. #include "HTUtils.h"
  11. #include "tcp.h"
  12.  
  13. #include "LYLeaks.h"
  14.  
  15. PUBLIC int WWW_TraceFlag = 0;    /* Global trace flag for ALL W3 code */
  16.  
  17. #ifndef VC
  18. #define VC "unknown"
  19. #endif
  20.  
  21. PUBLIC CONST char * HTLibraryVersion = VC; /* String for help screen etc */
  22.  
  23. #ifndef VM        /* VM has these already it seems */
  24.     
  25. /*    Strings of any length
  26. **    ---------------------
  27. */
  28. PUBLIC int strcasecomp ARGS2 (CONST char*,a, CONST char *,b)
  29. {
  30.     CONST char *p =a;
  31.     CONST char *q =b;
  32.     for(p=a, q=b; *p && *q; p++, q++) {
  33.         int diff = TOLOWER(*p) - TOLOWER(*q);
  34.         if (diff) return diff;
  35.     }
  36.     if (*p) return 1;    /* p was longer than q */
  37.     if (*q) return -1;    /* p was shorter than q */
  38.     return 0;        /* Exact match */
  39. }
  40.  
  41.  
  42. /*    With count limit
  43. **    ----------------
  44. */
  45. PUBLIC int strncasecomp ARGS3(CONST char*,a, CONST char *,b, int,n)
  46. {
  47.     CONST char *p =a;
  48.     CONST char *q =b;
  49.     
  50.     for(p=a, q=b;; p++, q++) {
  51.         int diff;
  52.         if (p == a+n) return 0;    /*   Match up to n characters */
  53.         if (!(*p && *q)) return *p - *q;
  54.         diff = TOLOWER(*p) - TOLOWER(*q);
  55.         if (diff) return diff;
  56.     }
  57.     /*NOTREACHED*/
  58. }
  59. #endif
  60.  
  61. /*    Allocate a new copy of a string, and returns it
  62. */
  63. PUBLIC char * HTSACopy
  64.   ARGS2 (char **,dest, CONST char *,src)
  65. {
  66.   if (*dest) free(*dest);
  67.   if (! src)
  68.     *dest = NULL;
  69.   else {
  70.     *dest = (char *) malloc (strlen(src) + 1);
  71.     if (*dest == NULL) outofmem(__FILE__, "HTSACopy");
  72.     strcpy (*dest, src);
  73.   }
  74.   return *dest;
  75. }
  76.  
  77. /*    String Allocate and Concatenate
  78. */
  79. PUBLIC char * HTSACat
  80.   ARGS2 (char **,dest, CONST char *,src)
  81. {
  82.   if (src && *src) {
  83.     if (*dest) {
  84.       int length = strlen (*dest);
  85.       *dest = (char *) realloc (*dest, length + strlen(src) + 1);
  86.       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
  87.       strcpy (*dest + length, src);
  88.     } else {
  89.       *dest = (char *) malloc (strlen(src) + 1);
  90.       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
  91.       strcpy (*dest, src);
  92.     }
  93.   }
  94.   return *dest;
  95. }
  96.  
  97.  
  98. /*    Find next Field
  99. **    ---------------
  100. **
  101. ** On entry,
  102. **    *pstr    points to a string containig white space separated
  103. **        field, optionlly quoted.
  104. **
  105. ** On exit,
  106. **    *pstr    has been moved to the first delimiter past the
  107. **        field
  108. **        THE STRING HAS BEEN MUTILATED by a 0 terminator
  109. **
  110. **    returns    a pointer to the first field
  111. */
  112. PUBLIC char * HTNextField ARGS1(char **, pstr)
  113. {
  114.     char * p = *pstr;
  115.     char * start;            /* start of field */
  116.     
  117.     while(*p && WHITE(*p)) p++;        /* Strip white space */
  118.     if (!*p) {
  119.     *pstr = p;
  120.         return NULL;        /* No first field */
  121.     }
  122.     if (*p == '"') {            /* quoted field */
  123.         p++;
  124.     start = p;
  125.     for(;*p && *p!='"'; p++) {
  126.         if (*p == '\\' && p[1]) p++;    /* Skip escaped chars */
  127.     }
  128.     } else {
  129.     start = p;
  130.     while(*p && !WHITE(*p)) p++;    /* Skip first field */
  131.     }
  132.     if (*p) *p++ = 0;
  133.     *pstr = p;
  134.     return start;
  135. }
  136.