Back to index

lightning-sunbird  0.9+nobinonly
nsCRT.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #ifndef nsCRT_h___
00038 #define nsCRT_h___
00039 
00040 #include <stdlib.h>
00041 #include <string.h>
00042 #include <ctype.h>
00043 #include "plstr.h"
00044 #include "nscore.h"
00045 #include "prtypes.h"
00046 #include "nsCppSharedAllocator.h"
00047 
00048 #if defined(XP_WIN) || defined(XP_OS2)
00049 #  define NS_LINEBREAK           "\015\012"
00050 #  define NS_LINEBREAK_LEN       2
00051 #else
00052 #  if defined(XP_UNIX) || defined(XP_BEOS)
00053 #    define NS_LINEBREAK         "\012"
00054 #    define NS_LINEBREAK_LEN     1
00055 #  endif /* XP_UNIX */
00056 #endif /* XP_WIN || XP_OS2 */
00057 
00058 extern const PRUnichar kIsoLatin1ToUCS2[256];
00059 
00060 // This macro can be used in a class declaration for classes that want
00061 // to ensure that their instance memory is zeroed.
00062 #define NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW   \
00063   void* operator new(size_t sz) CPP_THROW_NEW { \
00064     void* rv = ::operator new(sz);              \
00065     if (rv) {                                   \
00066       memset(rv, 0, sz);                        \
00067     }                                           \
00068     return rv;                                  \
00069   }                                             \
00070   void operator delete(void* ptr) {             \
00071     ::operator delete(ptr);                     \
00072   }
00073 
00074 // This macro works with the next macro to declare a non-inlined
00075 // version of the above.
00076 #define NS_DECL_ZEROING_OPERATOR_NEW           \
00077   void* operator new(size_t sz) CPP_THROW_NEW; \
00078   void operator delete(void* ptr);
00079 
00080 #define NS_IMPL_ZEROING_OPERATOR_NEW(_class)            \
00081   void* _class::operator new(size_t sz) CPP_THROW_NEW { \
00082     void* rv = ::operator new(sz);                      \
00083     if (rv) {                                           \
00084       memset(rv, 0, sz);                                \
00085     }                                                   \
00086     return rv;                                          \
00087   }                                                     \
00088   void _class::operator delete(void* ptr) {             \
00089     ::operator delete(ptr);                             \
00090   }
00091 
00092 // Freeing helper
00093 #define CRTFREEIF(x) if (x) { nsCRT::free(x); x = 0; }
00094 
00096 
00097 class NS_COM nsCRT {
00098 public:
00099   enum {
00100     TAB='\t'  /* Horizontal Tab */,
00101     LF='\n'   /* Line Feed */,
00102     VTAB='\v' /* Vertical Tab */,
00103     FF='\f'   /* Form Feed */,
00104     CR='\r'   /* Carriage Return */
00105   };
00106 
00107   /***
00108    ***  The following nsCRT::mem* functions are no longer
00109    ***  supported, please use the corresponding lib C
00110    ***  functions instead.
00111    ***
00112    ***  nsCRT::memcpy()
00113    ***  nsCRT::memcmp()
00114    ***  nsCRT::memmove()
00115    ***  nsCRT::memset()
00116    ***  nsCRT::zero()
00117    ***
00118    ***  Additionally, the following char* string utilities
00119    ***  are no longer supported, please use the
00120    ***  corresponding lib C functions instead.
00121    ***
00122    ***  nsCRT::strlen()
00123    ***
00124    ***/
00125 
00130   static PRUint32 strlen(const char* s) {                                       
00131     return PRUint32(::strlen(s));                                               
00132   }                                                                             
00133 
00135   static PRInt32 strcmp(const char* s1, const char* s2) {
00136     return PRInt32(PL_strcmp(s1, s2));
00137   }
00138 
00139   static PRInt32 strncmp(const char* s1, const char* s2,
00140                          PRUint32 aMaxLen) {
00141     return PRInt32(PL_strncmp(s1, s2, aMaxLen));
00142   }
00143 
00145   static PRInt32 strcasecmp(const char* s1, const char* s2) {
00146     return PRInt32(PL_strcasecmp(s1, s2));
00147   }
00148 
00150   static PRInt32 strncasecmp(const char* s1, const char* s2, PRUint32 aMaxLen) {
00151     PRInt32 result=PRInt32(PL_strncasecmp(s1, s2, aMaxLen));
00152     //Egads. PL_strncasecmp is returning *very* negative numbers.
00153     //Some folks expect -1,0,1, so let's temper its enthusiasm.
00154     if (result<0) 
00155       result=-1;
00156     return result;
00157   }
00158 
00159   static PRInt32 strncmp(const char* s1, const char* s2, PRInt32 aMaxLen) {
00160     // inline the first test (assumes strings are not null):
00161     PRInt32 diff = ((const unsigned char*)s1)[0] - ((const unsigned char*)s2)[0];
00162     if (diff != 0) return diff;
00163     return PRInt32(PL_strncmp(s1,s2,unsigned(aMaxLen)));
00164   }
00165   
00166   static char* strdup(const char* str) {
00167     return PL_strdup(str);
00168   }
00169 
00170   static char* strndup(const char* str, PRUint32 len) {
00171     return PL_strndup(str, len);
00172   }
00173 
00174   static void free(char* str) {
00175     PL_strfree(str);
00176   }
00177 
00197   static char* strtok(char* str, const char* delims, char* *newStr); 
00198 
00200   static PRUint32 strlen(const PRUnichar* s);
00201 
00203   static PRInt32 strcmp(const PRUnichar* s1, const PRUnichar* s2);
00205   static PRInt32 strncmp(const PRUnichar* s1, const PRUnichar* s2,
00206                          PRUint32 aMaxLen);
00207 
00208   // You must use nsCRT::free(PRUnichar*) to free memory allocated
00209   // by nsCRT::strdup(PRUnichar*).
00210   static PRUnichar* strdup(const PRUnichar* str);
00211 
00212   // You must use nsCRT::free(PRUnichar*) to free memory allocated
00213   // by strndup(PRUnichar*, PRUint32).
00214   static PRUnichar* strndup(const PRUnichar* str, PRUint32 len);
00215 
00216   static void free(PRUnichar* str) {
00217        nsCppSharedAllocator<PRUnichar> shared_allocator;
00218        shared_allocator.deallocate(str, 0 /*we never new or kept the size*/);
00219   }
00220 
00221   // Computes the hashcode for a c-string, returns the string length as
00222   // an added bonus.
00223   static PRUint32 HashCode(const char* str,
00224                            PRUint32* resultingStrLen = nsnull);
00225 
00226   // Computes the hashcode for a ucs2 string, returns the string length
00227   // as an added bonus.
00228   static PRUint32 HashCode(const PRUnichar* str,
00229                            PRUint32* resultingStrLen = nsnull);
00230 
00231   // Computes the hashcode for a buffer with a specified length.
00232   static PRUint32 BufferHashCode(const PRUnichar* str, PRUint32 strLen);
00233 
00234   // String to longlong
00235   static PRInt64 atoll(const char *str);
00236   
00237   static char ToUpper(char aChar);
00238 
00239   static char ToLower(char aChar);
00240   
00241   static PRBool IsUpper(char aChar);
00242 
00243   static PRBool IsLower(char aChar);
00244 
00245   static PRBool IsAscii(PRUnichar aChar);
00246   static PRBool IsAscii(const PRUnichar* aString);
00247   static PRBool IsAsciiAlpha(PRUnichar aChar);
00248   static PRBool IsAsciiDigit(PRUnichar aChar);
00249   static PRBool IsAsciiSpace(PRUnichar aChar);
00250   static PRBool IsAscii(const char* aString);
00251   static PRBool IsAscii(const char* aString, PRUint32 aLength);
00252 };
00253 
00254 #define FF '\014'
00255 #define TAB '\011'
00256 
00257 #define CRSTR "\015"
00258 #define LFSTR "\012"
00259 #define CRLF "\015\012"     /* A CR LF equivalent string */
00260 
00261 
00262 #if defined(XP_WIN) || defined(XP_OS2)
00263   #define FILE_PATH_SEPARATOR       "\\"
00264   #define FILE_ILLEGAL_CHARACTERS   "/:*?\"<>|"
00265 #elif defined(XP_UNIX) || defined(XP_BEOS)
00266   #define FILE_PATH_SEPARATOR       "/"
00267   #define FILE_ILLEGAL_CHARACTERS   ""
00268 #else
00269   #error need_to_define_your_file_path_separator_and_illegal_characters
00270 #endif
00271 
00272 #define NS_IS_SPACE(VAL) \
00273   (((((intn)(VAL)) & 0x7f) == ((intn)(VAL))) && isspace((intn)(VAL)) )
00274 
00275 #define NS_IS_CNTRL(i)   ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
00276 #define NS_IS_DIGIT(i)   ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
00277 #if defined(XP_WIN) || defined(XP_OS2)
00278 #define NS_IS_ALPHA(VAL) (isascii((int)(VAL)) && isalpha((int)(VAL)))
00279 #else
00280 #define NS_IS_ALPHA(VAL) ((((unsigned int) (VAL)) > 0x7f) ? (int) 0 : isalpha((int)(VAL)))
00281 #endif
00282 
00283 
00284 #endif /* nsCRT_h___ */