home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libpics / htstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.9 KB  |  252 lines

  1.  
  2. /*  W3 Copyright statement 
  3. Copyright 1995 by: Massachusetts Institute of Technology (MIT), INRIA</H2>
  4.  
  5. This W3C software is being provided by the copyright holders under the
  6. following license. By obtaining, using and/or copying this software,
  7. you agree that you have read, understood, and will comply with the
  8. following terms and conditions: 
  9.  
  10. Permission to use, copy, modify, and distribute this software and its
  11. documentation for any purpose and without fee or royalty is hereby
  12. granted, provided that the full text of this NOTICE appears on
  13. <EM>ALL</EM> copies of the software and documentation or portions
  14. thereof, including modifications, that you make. 
  15.  
  16. <B>THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
  17. REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  BY WAY OF EXAMPLE,
  18. BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
  19. WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
  20. THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
  21. THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
  22. COPYRIGHT HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE
  23. OR DOCUMENTATION.
  24.  
  25. The name and trademarks of copyright holders may NOT be used
  26. in advertising or publicity pertaining to the software without
  27. specific, written prior permission.  Title to copyright in this
  28. software and any associated documentation will at all times remain
  29. with copyright holders. 
  30. */
  31. /*                                     HTString.c
  32. **    DYNAMIC STRING UTILITIES
  33. **
  34. **    (c) COPYRIGHT MIT 1995.
  35. **    Please first read the full copyright statement in the file COPYRIGH.
  36. **    @(#) $Id: htstring.c,v 3.1 1998/03/28 03:32:07 ltabb Exp $
  37. **
  38. **    Original version came with listserv implementation.
  39. **    Version TBL Oct 91 replaces one which modified the strings.
  40. **    02-Dec-91 (JFG) Added stralloccopy and stralloccat
  41. **    23 Jan 92 (TBL) Changed strallocc* to 8 char HTSAC* for VM and suchlike
  42. **     6 Oct 92 (TBL) Moved WWW_TraceFlag in here to be in library
  43. **       9 Oct 95 (KR)  fixed problem with double quotes in HTNextField
  44. */
  45.  
  46. /* Library include files */
  47. /* --- BEGIN added by mharmsen@netscape.com on 7/9/97 --- */
  48. #include "xp.h"
  49. /* --- END added by mharmsen@netscape.com on 7/9/97 --- */
  50. /* #include "sysdep.h"  jhines -- 7/9/97 */
  51. #include "htutils.h"
  52. #include "htstring.h"                     /* Implemented here */
  53.  
  54. #if WWWTRACE_MODE == WWWTRACE_FILE
  55. PUBLIC FILE *WWWTrace = NULL;
  56. #endif
  57.  
  58. #ifndef WWW_WIN_DLL
  59. PUBLIC int WWW_TraceFlag = 0;        /* Global trace flag for ALL W3 code */
  60. #endif
  61.  
  62. /* ------------------------------------------------------------------------- */
  63.  
  64. /*    Strings of any length
  65. **    ---------------------
  66. */
  67. /* --- BEGIN removed by mharmsen@netscape.com on 7/9/97 --- */
  68. /************************************************************************/
  69. /* PUBLIC int strcasecomp (const char * a, const char * b)              */
  70. /* {                                                                    */
  71. /*     int diff;                                                        */
  72. /*     for( ; *a && *b; a++, b++) {                                     */
  73. /*    if ((diff = TOLOWER(*a) - TOLOWER(*b)))                         */
  74. /*        return diff;                                                */
  75. /*    }                                                                 */
  76. /*    if (*a) return 1;            /* a was longer than b          */
  77. /*    if (*b) return -1;            /* a was shorter than b */
  78. /*    return 0;                 /* Exact match                  */
  79. /*}                                                                     */
  80. /************************************************************************/
  81. /* --- END removed by mharmsen@netscape.com on 7/9/97 --- */
  82.  
  83.  
  84. /*    With count limit
  85. **    ----------------
  86. */
  87. /* --- BEGIN removed by mharmsen@netscape.com on 7/9/97 --- */
  88. /**********************************************************************/
  89. /* PUBLIC int strncasecomp (const char * a, const char * b, int n)    */
  90. /* {                                                                  */
  91. /*    const char *p =a;                                             */
  92. /*    const char *q =b;                                             */
  93. /*                                                                  */
  94. /*    for(p=a, q=b;; p++, q++) {                                    */
  95. /*        int diff;                                                 */
  96. /*        if (p == a+n) return 0;    /*   Match up to n characters */
  97. /*        if (!(*p && *q)) return *p - *q;                          */
  98. /*        diff = TOLOWER(*p) - TOLOWER(*q);                         */
  99. /*        if (diff) return diff;                                    */
  100. /*    }                                                             */
  101. /*    /*NOTREACHED                                                  */
  102. /*}                                                                   */
  103. /**********************************************************************/
  104. /* --- END removed by mharmsen@netscape.com on 7/9/97 --- */
  105.  
  106.  
  107. /*
  108. ** strcasestr(s1,s2) -- like strstr(s1,s2) but case-insensitive.
  109. */
  110. /* --- BEGIN removed by mharmsen@netscape.com on 7/9/97 --- */
  111. /*************************************************************************/
  112. /* PUBLIC char * strcasestr (char * s1, char * s2)                       */
  113. /* {                                                                     */
  114. /*    char * ptr = s1;                                                   */
  115. /*                                                                       */
  116. /*    if (!s1 || !s2 || !*s2) return s1;                                 */
  117. /*                                                                       */
  118. /*    while (*ptr) {                                                     */
  119. /*    if (TOUPPER(*ptr) == TOUPPER(*s2)) {                             */
  120. /*        char * cur1 = ptr + 1;                                       */
  121. /*        char * cur2 = s2 + 1;                                        */
  122. /*        while (*cur1 && *cur2 && TOUPPER(*cur1) == TOUPPER(*cur2)) { */
  123. /*        cur1++;                                                  */
  124. /*        cur2++;                                                  */
  125. /*        }                                                            */
  126. /*        if (!*cur2)    return ptr;                                      */
  127. /*    }                                                                */
  128. /*    ptr++;                                                           */
  129. /*    }                                                                  */
  130. /*    return NULL;                                                       */
  131. /*}                                                                      */
  132. /*************************************************************************/
  133. /* --- END removed by mharmsen@netscape.com on 7/9/97 --- */
  134.  
  135.  
  136. #if 0 /* LJM: not needed */
  137.  
  138. /*    Allocate a new copy of a string, and returns it
  139. */
  140. PUBLIC char * HTSACopy (char ** dest, const char * src)
  141. {
  142.   if (*dest) HT_FREE(*dest);
  143.   if (! src)
  144.     *dest = NULL;
  145.   else {
  146.     /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  147.     if ((*dest  = (char  *) HT_MALLOC(XP_STRLEN(src) + 1)) == NULL)
  148.         HT_OUTOFMEM("HTSACopy");
  149.     XP_STRCPY (*dest, src);
  150.     /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  151.   }
  152.   return *dest;
  153. }
  154.  
  155. /*    String Allocate and Concatenate
  156. */
  157. PUBLIC char * HTSACat (char ** dest, const char * src)
  158. {
  159.   if (src && *src) {
  160.     /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  161.     if (*dest) {
  162.       int length = XP_STRLEN (*dest);
  163.       if ((*dest  = (char  *) HT_REALLOC(*dest, length + XP_STRLEN(src) + 1)) == NULL)
  164.           HT_OUTOFMEM("HTSACat");
  165.       XP_STRCPY (*dest + length, src);
  166.     } else {
  167.       if ((*dest  = (char  *) HT_MALLOC(XP_STRLEN(src) + 1)) == NULL)
  168.           HT_OUTOFMEM("HTSACat");
  169.       XP_STRCPY (*dest, src);
  170.     }
  171.     /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  172.   }
  173.   return *dest;
  174. }
  175.  
  176. #endif /* 0 */
  177.  
  178. /*    String Matching
  179. **    ---------------
  180. **    String comparison function for file names with one wildcard * in the
  181. **    template. Arguments are:
  182. **
  183. **    tmpl    is a template string to match the name against.
  184. **        agaist, may contain a single wildcard character * which
  185. **        matches zero or more arbitrary characters.
  186. **    name    is the name to be matched agaist the template.
  187. **
  188. **    return:    - Empty string if perfect match
  189. **        - pointer to part matched by wildcard if any
  190. **        - NULL if no match
  191. */
  192. PUBLIC char * HTStrMatch (const char * tmpl, const char * name)
  193. {
  194.     while (*tmpl && *name && *tmpl==*name) tmpl++, name++;
  195.     return ((!*tmpl && !*name) || *tmpl=='*') ? (char *) name : (char *) NULL;
  196. }    
  197.  
  198. PUBLIC char * HTStrCaseMatch (const char * tmpl, const char * name)
  199. {
  200.     /* --- BEGIN converted by mharmsen@netscape.com on 7/9/97 --- */
  201.     while (*tmpl && *name && XP_TO_UPPER(*tmpl)==XP_TO_UPPER(*name)) tmpl++, name++;
  202.     return ((!*tmpl && !*name) || *tmpl=='*') ? (char *) name : (char *) NULL;
  203.     /* --- END converted by mharmsen@netscape.com on 7/9/97 --- */
  204. }    
  205.  
  206. /*    Strip white space off a string
  207. **    ------------------------------
  208. **    Return value points to first non-white character, or to 0 if none.
  209. **    All trailing white space is OVERWRITTEN with zero.
  210. */
  211. PUBLIC char * HTStrip (char * s)
  212. {
  213.     if (s) {
  214.     char * p=s;
  215.     for(p=s;*p;p++) {;}    /* Find end of string */
  216.     for(p--;p>=s;p--) {
  217.         if (WHITE(*p))
  218.         *p=0;            /* Zap trailing blanks */
  219.         else
  220.         break;
  221.     }
  222.     while (WHITE(*s)) s++;        /* Strip leading blanks */
  223.     }
  224.     return s;
  225. }
  226.  
  227. PRIVATE HTTraceCallback * PHTTraceCallback;
  228.  
  229. PUBLIC void HTTrace_setCallback(HTTraceCallback * pCall)
  230. {
  231.     PHTTraceCallback = pCall;
  232. }
  233.  
  234. PUBLIC HTTraceCallback * HTTrace_getCallback(void)
  235. {
  236.     return PHTTraceCallback;
  237. }
  238.  
  239. PUBLIC int HTTrace(const char * fmt, ...)
  240. {
  241.     va_list pArgs;
  242.     va_start(pArgs, fmt);
  243.     if (PHTTraceCallback)
  244.     (*PHTTraceCallback)(fmt, pArgs);
  245. #ifdef WWW_WIN_WINDOWS
  246.     return (0);
  247. #else
  248.     return (vfprintf(stderr, fmt, pArgs));
  249. #endif
  250. }
  251.  
  252.