2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1998-04-14 00:24:54 +04:00
|
|
|
#ifndef nsCRT_h___
|
|
|
|
#define nsCRT_h___
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2003-04-01 23:38:12 +04:00
|
|
|
#include <ctype.h>
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "nscore.h"
|
1999-03-22 12:54:46 +03:00
|
|
|
#include "prtypes.h"
|
1999-07-22 22:23:02 +04:00
|
|
|
#include "nsCppSharedAllocator.h"
|
2006-05-03 20:11:14 +04:00
|
|
|
#include "nsCRTGlue.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2005-06-24 03:42:02 +04:00
|
|
|
#if defined(XP_WIN) || defined(XP_OS2)
|
|
|
|
# define NS_LINEBREAK "\015\012"
|
|
|
|
# define NS_LINEBREAK_LEN 2
|
1999-06-22 22:50:12 +04:00
|
|
|
#else
|
2005-06-24 03:42:02 +04:00
|
|
|
# if defined(XP_UNIX) || defined(XP_BEOS)
|
|
|
|
# define NS_LINEBREAK "\012"
|
|
|
|
# define NS_LINEBREAK_LEN 1
|
|
|
|
# endif /* XP_UNIX */
|
|
|
|
#endif /* XP_WIN || XP_OS2 */
|
1999-06-22 22:50:12 +04:00
|
|
|
|
1999-03-22 12:54:46 +03:00
|
|
|
extern const PRUnichar kIsoLatin1ToUCS2[256];
|
|
|
|
|
1999-02-26 22:38:53 +03:00
|
|
|
// This macro can be used in a class declaration for classes that want
|
|
|
|
// to ensure that their instance memory is zeroed.
|
2002-10-08 10:12:00 +04:00
|
|
|
#define NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW \
|
2002-07-03 00:25:30 +04:00
|
|
|
void* operator new(size_t sz) CPP_THROW_NEW { \
|
2002-10-08 10:12:00 +04:00
|
|
|
void* rv = ::operator new(sz); \
|
|
|
|
if (rv) { \
|
|
|
|
memset(rv, 0, sz); \
|
|
|
|
} \
|
|
|
|
return rv; \
|
|
|
|
} \
|
|
|
|
void operator delete(void* ptr) { \
|
|
|
|
::operator delete(ptr); \
|
1999-02-26 22:38:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This macro works with the next macro to declare a non-inlined
|
|
|
|
// version of the above.
|
2002-10-08 10:12:00 +04:00
|
|
|
#define NS_DECL_ZEROING_OPERATOR_NEW \
|
|
|
|
void* operator new(size_t sz) CPP_THROW_NEW; \
|
1999-02-26 22:38:53 +03:00
|
|
|
void operator delete(void* ptr);
|
|
|
|
|
2002-10-08 10:12:00 +04:00
|
|
|
#define NS_IMPL_ZEROING_OPERATOR_NEW(_class) \
|
2002-07-03 00:25:30 +04:00
|
|
|
void* _class::operator new(size_t sz) CPP_THROW_NEW { \
|
2002-10-08 10:12:00 +04:00
|
|
|
void* rv = ::operator new(sz); \
|
|
|
|
if (rv) { \
|
|
|
|
memset(rv, 0, sz); \
|
|
|
|
} \
|
|
|
|
return rv; \
|
|
|
|
} \
|
|
|
|
void _class::operator delete(void* ptr) { \
|
|
|
|
::operator delete(ptr); \
|
1999-02-26 22:38:53 +03:00
|
|
|
}
|
1999-02-09 07:43:58 +03:00
|
|
|
|
1999-08-23 12:27:42 +04:00
|
|
|
// Freeing helper
|
|
|
|
#define CRTFREEIF(x) if (x) { nsCRT::free(x); x = 0; }
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
/// This is a wrapper class around all the C runtime functions.
|
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
class NS_COM nsCRT {
|
1998-04-14 00:24:54 +04:00
|
|
|
public:
|
2001-04-18 09:59:43 +04:00
|
|
|
enum {
|
|
|
|
LF='\n' /* Line Feed */,
|
|
|
|
VTAB='\v' /* Vertical Tab */,
|
|
|
|
CR='\r' /* Carriage Return */
|
|
|
|
};
|
1999-02-09 07:19:58 +03:00
|
|
|
|
2002-10-08 10:12:00 +04:00
|
|
|
/***
|
2002-02-28 07:11:37 +03:00
|
|
|
*** The following nsCRT::mem* functions are no longer
|
2002-10-08 10:12:00 +04:00
|
|
|
*** supported, please use the corresponding lib C
|
|
|
|
*** functions instead.
|
2002-02-28 07:11:37 +03:00
|
|
|
***
|
|
|
|
*** nsCRT::memcpy()
|
|
|
|
*** nsCRT::memcmp()
|
|
|
|
*** nsCRT::memmove()
|
|
|
|
*** nsCRT::memset()
|
|
|
|
*** nsCRT::zero()
|
|
|
|
***
|
2002-10-08 10:12:00 +04:00
|
|
|
*** Additionally, the following char* string utilities
|
|
|
|
*** are no longer supported, please use the
|
|
|
|
*** corresponding lib C functions instead.
|
2002-02-28 07:11:37 +03:00
|
|
|
***
|
|
|
|
*** nsCRT::strlen()
|
|
|
|
***
|
|
|
|
***/
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2003-03-15 05:31:17 +03:00
|
|
|
/** Compute the string length of s
|
|
|
|
@param s the string in question
|
|
|
|
@return the length of s
|
|
|
|
*/
|
|
|
|
static PRUint32 strlen(const char* s) {
|
|
|
|
return PRUint32(::strlen(s));
|
|
|
|
}
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
/// Compare s1 and s2.
|
|
|
|
static PRInt32 strcmp(const char* s1, const char* s2) {
|
2000-03-23 01:55:10 +03:00
|
|
|
return PRInt32(PL_strcmp(s1, s2));
|
1999-02-06 06:56:17 +03:00
|
|
|
}
|
|
|
|
|
1999-06-05 05:15:00 +04:00
|
|
|
static PRInt32 strncmp(const char* s1, const char* s2,
|
1999-02-06 06:56:17 +03:00
|
|
|
PRUint32 aMaxLen) {
|
|
|
|
return PRInt32(PL_strncmp(s1, s2, aMaxLen));
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Case-insensitive string comparison.
|
|
|
|
static PRInt32 strcasecmp(const char* s1, const char* s2) {
|
|
|
|
return PRInt32(PL_strcasecmp(s1, s2));
|
|
|
|
}
|
|
|
|
|
1998-04-30 20:40:38 +04:00
|
|
|
/// Case-insensitive string comparison with length
|
1999-02-06 06:56:17 +03:00
|
|
|
static PRInt32 strncasecmp(const char* s1, const char* s2, PRUint32 aMaxLen) {
|
1999-12-02 13:14:38 +03:00
|
|
|
PRInt32 result=PRInt32(PL_strncasecmp(s1, s2, aMaxLen));
|
2002-10-08 10:12:00 +04:00
|
|
|
//Egads. PL_strncasecmp is returning *very* negative numbers.
|
|
|
|
//Some folks expect -1,0,1, so let's temper its enthusiasm.
|
|
|
|
if (result<0)
|
1999-12-02 13:14:38 +03:00
|
|
|
result=-1;
|
|
|
|
return result;
|
1998-04-30 20:40:38 +04:00
|
|
|
}
|
|
|
|
|
1999-02-10 11:22:47 +03:00
|
|
|
static PRInt32 strncmp(const char* s1, const char* s2, PRInt32 aMaxLen) {
|
1999-08-24 12:45:17 +04:00
|
|
|
// inline the first test (assumes strings are not null):
|
|
|
|
PRInt32 diff = ((const unsigned char*)s1)[0] - ((const unsigned char*)s2)[0];
|
|
|
|
if (diff != 0) return diff;
|
2000-03-23 01:55:10 +03:00
|
|
|
return PRInt32(PL_strncmp(s1,s2,unsigned(aMaxLen)));
|
1999-02-10 11:22:47 +03:00
|
|
|
}
|
|
|
|
|
1998-12-16 08:40:20 +03:00
|
|
|
static char* strdup(const char* str) {
|
|
|
|
return PL_strdup(str);
|
|
|
|
}
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
static char* strndup(const char* str, PRUint32 len) {
|
|
|
|
return PL_strndup(str, len);
|
|
|
|
}
|
|
|
|
|
1999-04-22 08:25:17 +04:00
|
|
|
static void free(char* str) {
|
|
|
|
PL_strfree(str);
|
|
|
|
}
|
|
|
|
|
1999-02-06 06:56:17 +03:00
|
|
|
/**
|
1999-05-05 00:28:24 +04:00
|
|
|
|
1999-02-06 06:56:17 +03:00
|
|
|
How to use this fancy (thread-safe) version of strtok:
|
|
|
|
|
2002-10-08 10:12:00 +04:00
|
|
|
void main(void) {
|
|
|
|
printf("%s\n\nTokens:\n", string);
|
1999-02-06 06:56:17 +03:00
|
|
|
// Establish string and get the first token:
|
|
|
|
char* newStr;
|
2002-10-08 10:12:00 +04:00
|
|
|
token = nsCRT::strtok(string, seps, &newStr);
|
|
|
|
while (token != NULL) {
|
1999-02-06 06:56:17 +03:00
|
|
|
// While there are tokens in "string"
|
2002-10-08 10:12:00 +04:00
|
|
|
printf(" %s\n", token);
|
1999-02-06 06:56:17 +03:00
|
|
|
// Get next token:
|
2002-10-08 10:12:00 +04:00
|
|
|
token = nsCRT::strtok(newStr, seps, &newStr);
|
1999-02-06 06:56:17 +03:00
|
|
|
}
|
|
|
|
}
|
1999-05-05 00:28:24 +04:00
|
|
|
* WARNING - STRTOK WHACKS str THE FIRST TIME IT IS CALLED *
|
|
|
|
* MAKE A COPY OF str IF YOU NEED TO USE IT AFTER strtok() *
|
1999-02-06 06:56:17 +03:00
|
|
|
*/
|
|
|
|
static char* strtok(char* str, const char* delims, char* *newStr);
|
|
|
|
|
2006-05-03 20:11:14 +04:00
|
|
|
static PRUint32 strlen(const PRUnichar* s) {
|
2006-05-20 02:47:53 +04:00
|
|
|
// XXXbsmedberg: remove this null-check at some point
|
2006-05-20 02:37:17 +04:00
|
|
|
if (!s) {
|
|
|
|
NS_ERROR("Passing null to nsCRT::strlen");
|
|
|
|
return 0;
|
|
|
|
}
|
2006-05-03 20:11:14 +04:00
|
|
|
return NS_strlen(s);
|
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
/// Like strcmp except for ucs2 strings
|
|
|
|
static PRInt32 strcmp(const PRUnichar* s1, const PRUnichar* s2);
|
|
|
|
/// Like strcmp except for ucs2 strings
|
|
|
|
static PRInt32 strncmp(const PRUnichar* s1, const PRUnichar* s2,
|
1999-02-06 06:56:17 +03:00
|
|
|
PRUint32 aMaxLen);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2002-10-08 10:12:00 +04:00
|
|
|
// You must use nsCRT::free(PRUnichar*) to free memory allocated
|
|
|
|
// by nsCRT::strdup(PRUnichar*).
|
1998-12-16 08:40:20 +03:00
|
|
|
static PRUnichar* strdup(const PRUnichar* str);
|
|
|
|
|
2002-10-08 10:12:00 +04:00
|
|
|
// You must use nsCRT::free(PRUnichar*) to free memory allocated
|
|
|
|
// by strndup(PRUnichar*, PRUint32).
|
2000-08-10 10:19:37 +04:00
|
|
|
static PRUnichar* strndup(const PRUnichar* str, PRUint32 len);
|
|
|
|
|
1999-04-22 08:25:17 +04:00
|
|
|
static void free(PRUnichar* str) {
|
1999-07-22 22:23:02 +04:00
|
|
|
nsCppSharedAllocator<PRUnichar> shared_allocator;
|
|
|
|
shared_allocator.deallocate(str, 0 /*we never new or kept the size*/);
|
1999-04-22 08:25:17 +04:00
|
|
|
}
|
1999-06-26 10:32:35 +04:00
|
|
|
|
2002-02-16 22:50:32 +03:00
|
|
|
// Computes the hashcode for a c-string, returns the string length as
|
|
|
|
// an added bonus.
|
|
|
|
static PRUint32 HashCode(const char* str,
|
|
|
|
PRUint32* resultingStrLen = nsnull);
|
|
|
|
|
2007-07-12 00:46:43 +04:00
|
|
|
// Computes the hashcode for a length number of bytes of c-string data.
|
|
|
|
static PRUint32 HashCode(const char* start, PRUint32 length);
|
|
|
|
|
2002-02-16 22:50:32 +03:00
|
|
|
// Computes the hashcode for a ucs2 string, returns the string length
|
|
|
|
// as an added bonus.
|
|
|
|
static PRUint32 HashCode(const PRUnichar* str,
|
|
|
|
PRUint32* resultingStrLen = nsnull);
|
|
|
|
|
2007-07-12 00:46:43 +04:00
|
|
|
// Computes a hashcode for a length number of UTF16
|
|
|
|
// characters. Returns the same hash code as the HashCode method
|
|
|
|
// taking a |char*| would if the string were converted to UTF8. This
|
|
|
|
// hash function treats invalid UTF16 data as 0xFFFD (0xEFBFBD in
|
|
|
|
// UTF-8).
|
|
|
|
static PRUint32 HashCodeAsUTF8(const PRUnichar* start, PRUint32 length);
|
2005-11-04 22:52:18 +03:00
|
|
|
|
2002-09-26 11:41:53 +04:00
|
|
|
// Computes the hashcode for a buffer with a specified length.
|
|
|
|
static PRUint32 BufferHashCode(const PRUnichar* str, PRUint32 strLen);
|
|
|
|
|
2001-10-18 08:23:25 +04:00
|
|
|
// String to longlong
|
|
|
|
static PRInt64 atoll(const char *str);
|
1999-03-02 21:24:42 +03:00
|
|
|
|
2006-05-03 20:11:14 +04:00
|
|
|
static char ToUpper(char aChar) { return NS_ToUpper(aChar); }
|
|
|
|
static char ToLower(char aChar) { return NS_ToLower(aChar); }
|
2000-08-23 21:27:06 +04:00
|
|
|
|
2006-05-03 20:11:14 +04:00
|
|
|
static PRBool IsUpper(char aChar) { return NS_IsUpper(aChar); }
|
|
|
|
static PRBool IsLower(char aChar) { return NS_IsLower(aChar); }
|
|
|
|
|
|
|
|
static PRBool IsAscii(PRUnichar aChar) { return NS_IsAscii(aChar); }
|
|
|
|
static PRBool IsAscii(const PRUnichar* aString) { return NS_IsAscii(aString); }
|
|
|
|
static PRBool IsAsciiAlpha(PRUnichar aChar) { return NS_IsAsciiAlpha(aChar); }
|
|
|
|
static PRBool IsAsciiDigit(PRUnichar aChar) { return NS_IsAsciiDigit(aChar); }
|
|
|
|
static PRBool IsAsciiSpace(PRUnichar aChar) { return NS_IsAsciiWhitespace(aChar); }
|
|
|
|
static PRBool IsAscii(const char* aString) { return NS_IsAscii(aString); }
|
|
|
|
static PRBool IsAscii(const char* aString, PRUint32 aLength) { return NS_IsAscii(aString, aLength); }
|
1998-04-14 00:24:54 +04:00
|
|
|
};
|
|
|
|
|
2006-05-03 20:11:14 +04:00
|
|
|
#define FF '\f'
|
|
|
|
#define TAB '\t'
|
2000-03-12 10:47:09 +03:00
|
|
|
|
2001-04-18 09:59:43 +04:00
|
|
|
#define CRSTR "\015"
|
|
|
|
#define LFSTR "\012"
|
|
|
|
#define CRLF "\015\012" /* A CR LF equivalent string */
|
2000-03-12 10:47:09 +03:00
|
|
|
|
2002-02-12 06:57:39 +03:00
|
|
|
|
2007-06-27 04:45:52 +04:00
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
#define FILE_PATH_SEPARATOR "/"
|
|
|
|
#define FILE_ILLEGAL_CHARACTERS ":"
|
|
|
|
#elif defined(XP_WIN) || defined(XP_OS2)
|
2007-03-23 02:01:14 +03:00
|
|
|
#define FILE_PATH_SEPARATOR "\\"
|
|
|
|
#define FILE_ILLEGAL_CHARACTERS "/:*?\"<>|"
|
2002-02-12 07:34:36 +03:00
|
|
|
#elif defined(XP_UNIX) || defined(XP_BEOS)
|
2007-03-23 02:01:14 +03:00
|
|
|
#define FILE_PATH_SEPARATOR "/"
|
|
|
|
#define FILE_ILLEGAL_CHARACTERS ""
|
2002-02-12 08:17:18 +03:00
|
|
|
#else
|
2002-02-12 06:57:39 +03:00
|
|
|
#error need_to_define_your_file_path_separator_and_illegal_characters
|
|
|
|
#endif
|
|
|
|
|
2003-04-01 23:38:12 +04:00
|
|
|
#define NS_IS_SPACE(VAL) \
|
|
|
|
(((((intn)(VAL)) & 0x7f) == ((intn)(VAL))) && isspace((intn)(VAL)) )
|
|
|
|
|
|
|
|
#define NS_IS_CNTRL(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
|
|
|
|
#define NS_IS_DIGIT(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
|
2003-04-02 12:31:39 +04:00
|
|
|
#if defined(XP_WIN) || defined(XP_OS2)
|
2003-04-01 23:38:12 +04:00
|
|
|
#define NS_IS_ALPHA(VAL) (isascii((int)(VAL)) && isalpha((int)(VAL)))
|
|
|
|
#else
|
|
|
|
#define NS_IS_ALPHA(VAL) ((((unsigned int) (VAL)) > 0x7f) ? (int) 0 : isalpha((int)(VAL)))
|
|
|
|
#endif
|
|
|
|
|
2002-02-12 06:57:39 +03:00
|
|
|
|
2001-04-18 09:59:43 +04:00
|
|
|
#endif /* nsCRT_h___ */
|