HTString.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:4k
源码类别:

浏览器

开发平台:

Unix_Linux

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