home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / include / xp_str.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.4 KB  |  237 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef XPSTRING_H
  20. #define XPSTRING_H
  21.  
  22. #include <string.h>
  23.  
  24. #define XP_STRCASECMP  strcasecomp
  25. #define XP_STRNCASECMP strncasecomp
  26. #define XP_STRCASESTR strcasestr
  27. #define XP_STRNCHR strnchr
  28.  
  29. #define XP_TO_UPPER(i)  xp_toupper(i)    
  30. #define XP_TO_LOWER(i)     xp_tolower(i)
  31.  
  32. #ifdef XP_UNIX /* because I can't verify yet.. */
  33. #ifdef DEBUG
  34.     XP_BEGIN_PROTOS
  35.         extern char * NOT_NULL (const char *x);
  36.     XP_END_PROTOS
  37. #else
  38. #    define NOT_NULL(X)    X
  39. #endif
  40.  
  41. #define XP_STRLEN(s)                 strlen(NOT_NULL(s))
  42. #define XP_STRCMP(a, b)               strcmp(NOT_NULL(a), NOT_NULL(b))
  43. #define XP_STRNCMP(a, b, n)       strncmp(NOT_NULL(a), NOT_NULL(b), (n))
  44. #define XP_STRCPY(d, s)               strcpy(NOT_NULL(d), NOT_NULL(s))
  45. #define XP_STRCHR                     strchr
  46. #define XP_STRRCHR                    strrchr
  47. #define XP_STRTOK                     strtok
  48. #define XP_STRCAT                     strcat
  49. #define XP_STRNCAT                    strncat
  50. #define XP_STRSTR                     strstr
  51. #define XP_STRTOUL                    strtoul
  52.  
  53. /* 
  54.     XP_FILENAMECMP compares two filenames, treating case differences
  55.        appropriately for this OS. 
  56.    */
  57.  
  58. #ifdef XP_WIN 
  59. #define     XP_FILENAMECMP         stricmp    
  60. #else
  61. #define     XP_FILENAMECMP        XP_STRCMP
  62. #endif
  63.  
  64.  
  65. #if !defined(XP_WIN) && !defined(XP_OS2)
  66.     /* strdup is not an ANSI function */
  67.     XP_BEGIN_PROTOS
  68.     extern char * strdup (const char * s);
  69.     XP_END_PROTOS
  70. #endif
  71.  
  72.  
  73. #define XP_STRDUP(s)              strdup((s))
  74. #define XP_MEMCPY(d, s, n)        memcpy((d), (s), (n))
  75.  
  76. /* NOTE: XP_MEMMOVE gurantees that overlaps will be properly handled */
  77. #if defined( __sun) && !defined(__svr4__)
  78. #define XP_MEMMOVE(Dest,Src,Len)  bcopy((Src),(Dest),(Len))
  79. #else
  80. #define XP_MEMMOVE(Dest,Src,Len)  memmove((Dest),(Src),(Len))
  81. #endif /* __sun */
  82.  
  83. #define XP_MEMSET                     memset
  84. #define XP_SPRINTF                    sprintf
  85. #define XP_SAFE_SPRINTF        PR_snprintf
  86. #define XP_MEMCMP                     memcmp
  87. #define XP_VSPRINTF                   vsprintf
  88.  
  89. #define XP_IS_SPACE(VAL)                \
  90.     (((((intn)(VAL)) & 0x7f) == ((intn)(VAL))) && isspace((intn)(VAL)) )
  91.  
  92. #define XP_IS_CNTRL(i)     ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
  93. #define XP_IS_DIGIT(i)     ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
  94.  
  95. #ifdef XP_WIN 
  96. #define XP_IS_ALPHA(VAL)         (isascii((int)(VAL)) && isalpha((int)(VAL)))
  97. #else
  98. #define XP_IS_ALPHA(VAL)     ((((unsigned int) (VAL)) > 0x7f) ? FALSE : isalpha((int)(VAL)))
  99. #endif
  100.  
  101. #define XP_ATOI(PTR)                    (atoi((PTR)))
  102. #define XP_BZERO(a,b)               memset(a,0,b)
  103.  
  104. /* NOTE: XP_BCOPY gurantees that overlaps will be properly handled */
  105. #ifdef XP_WIN16
  106.  
  107.     XP_BEGIN_PROTOS
  108.     extern void WIN16_bcopy(char *, char *, unsigned long);
  109.     XP_END_PROTOS
  110.  
  111.     #define XP_BCOPY(PTR_FROM, PTR_TO, LEN) \
  112.                     (WIN16_bcopy((char *) (PTR_FROM), (char *)(PTR_TO), (LEN)))
  113.  
  114. #else
  115.  
  116.     #define XP_BCOPY(Src,Dest,Len)  XP_MEMMOVE((Dest),(Src),(Len))
  117.  
  118. #endif /* XP_WIN16 */
  119.  
  120.  
  121. /*
  122.     Random stuff in a random place
  123. */
  124. #if defined (XP_WIN) || defined (XP_OS2)
  125. #define XP_RANDOM         rand
  126. #define XP_SRANDOM(seed)    srand((seed))
  127. #endif
  128. #ifdef XP_MAC
  129. #define XP_RANDOM         rand
  130. #define XP_SRANDOM(seed)    srand((seed))
  131. #endif
  132. #ifdef XP_UNIX
  133. #if !defined(XP_RANDOM) || !defined(XP_SRANDOM)   /* defined in both xp_mcom.h and xp_str.h */
  134. #if defined(UNIXWARE) || defined(_INCLUDE_HPUX_SOURCE) || (defined(__sun) && defined(__svr4__)) || defined(SNI) || defined(NCR)
  135. #define XP_RANDOM        rand
  136. #define XP_SRANDOM(seed)    srand((seed))
  137. #else
  138. #define XP_RANDOM        random
  139. #define XP_SRANDOM(seed)    srandom((seed))
  140. #endif
  141. #endif
  142. #endif
  143.  
  144. #endif /* XP_UNIX -- ifdef'd because I can't yet verify */
  145. XP_BEGIN_PROTOS
  146.  
  147. /*
  148. ** Some basic string things
  149. */
  150.  
  151. /*
  152. ** Concatenate s1 to s2. If s1 is NULL then a copy of s2 is
  153. ** returned. Otherwise, s1 is realloc'd and s2 is concatenated, with the
  154. ** new value of s1 being returned.
  155. */
  156. extern char *XP_AppendStr(char *s1, const char *s2);
  157.  
  158.  
  159. /*
  160. ** Concatenate a bunch of strings. The variable length argument list must
  161. ** be terminated with a NULL. This result is a DS_Alloc'd string.
  162. */
  163. extern char *XP_Cat(char *s1, ...);
  164.  
  165. /* Fast table driven tolower and toupper functions.  
  166.  ** 
  167.  ** Only works on first 128 ascii 
  168.  */
  169. int xp_tolower(int c);
  170. int xp_toupper(int c);
  171.  
  172. /*
  173.  * Case-insensitive string comparison
  174.  */
  175. extern int strcasecomp  (const char *a, const char *b);
  176. extern int strncasecomp (const char *a, const char *b, int n);
  177.  
  178. /* find a char within a specified length string 
  179.  */
  180. extern char * strnchr (const char * str, const char single, int32 len);
  181.  
  182. /* find a substring within a string with a case insensitive search
  183.  */
  184. extern char * strcasestr (const char * str, const char * substr);
  185.  
  186. /* find a substring within a specified length string with a case 
  187.  * insensitive search
  188.  */
  189. extern char * strncasestr (const char * str, const char * substr, int32 len);
  190.  
  191. /* thread safe version of strtok.  placeHolder contains the remainder of the 
  192.    string being tokenized and is used as the starting point of the next search */
  193.    
  194. /* example:  (1st call)    token = XP_STRTOK_R(tokenString, "<token>", &placeHolder);
  195.              (2nd call)    token = XP_STRTOK_R(nil, "<token>", &placeHolder);*/
  196. extern char * XP_STRTOK_R(char *s1, const char *s2, char **placeHolder);
  197.  
  198. /*
  199.  * Malloc'd string manipulation
  200.  *
  201.  * notice that they are dereferenced by the define!
  202.  */
  203. #define StrAllocCopy(dest, src) NET_SACopy (&(dest), src)
  204. #define StrAllocCat(dest, src)  NET_SACat  (&(dest), src)
  205. extern char * NET_SACopy (char **dest, const char *src);
  206. extern char * NET_SACat  (char **dest, const char *src);
  207.  
  208. /*
  209.  * Malloc'd block manipulation
  210.  *
  211.  * Lengths are necessary here :(
  212.  *
  213.  * notice that they are dereferenced by the define!
  214.  */
  215. #define BlockAllocCopy(dest, src, src_length) NET_BACopy((char**)&(dest), src, src_length)
  216. #define BlockAllocCat(dest, dest_length, src, src_length)  NET_BACat(&(dest), dest_length, src, src_length)
  217. extern char * NET_BACopy (char **dest, const char *src, size_t src_length);
  218. extern char * NET_BACat  (char **dest, size_t dest_length, const char *src, size_t src_length);
  219.  
  220. extern char * XP_StripLine (char *s);
  221.  
  222. /* Match = 0, NoMatch = 1, Abort = -1 */
  223. /* Based loosely on sections of wildmat.c by Rich Salz */
  224. extern int xp_RegExpSearch(char *str, char *exp);
  225.  
  226. /* 
  227.  * These are "safe" versions of the runtime library routines. The RTL
  228.  * versions do not null-terminate dest IFF strlen(src) >= destLength.
  229.  * These versions always null-terminate, which is why they're safe.
  230.  */
  231. extern char *XP_STRNCAT_SAFE (char *dest, const char *src, size_t len);
  232. extern char *XP_STRNCPY_SAFE (char *dest, const char *src, size_t len);
  233.  
  234. XP_END_PROTOS
  235.  
  236. #endif /* XPSTRING_H */
  237.