1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
46 #include "nsCppSharedAllocator.h"
47 #include "nsCRTGlue.h"
49 #if defined(XP_WIN) || defined(XP_OS2)
50 # define NS_LINEBREAK "\015\012"
51 # define NS_LINEBREAK_LEN 2
53 # if defined(XP_UNIX) || defined(XP_BEOS)
54 # define NS_LINEBREAK "\012"
55 # define NS_LINEBREAK_LEN 1
57 #endif /* XP_WIN || XP_OS2 */
59 extern const PRUnichar kIsoLatin1ToUCS2
[256];
61 // This macro can be used in a class declaration for classes that want
62 // to ensure that their instance memory is zeroed.
63 #define NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW \
64 void* operator new(size_t sz) CPP_THROW_NEW { \
65 void* rv = ::operator new(sz); \
71 void operator delete(void* ptr) { \
72 ::operator delete(ptr); \
75 // This macro works with the next macro to declare a non-inlined
76 // version of the above.
77 #define NS_DECL_ZEROING_OPERATOR_NEW \
78 void* operator new(size_t sz) CPP_THROW_NEW; \
79 void operator delete(void* ptr);
81 #define NS_IMPL_ZEROING_OPERATOR_NEW(_class) \
82 void* _class::operator new(size_t sz) CPP_THROW_NEW { \
83 void* rv = ::operator new(sz); \
89 void _class::operator delete(void* ptr) { \
90 ::operator delete(ptr); \
94 #define CRTFREEIF(x) if (x) { nsCRT::free(x); x = 0; }
96 /// This is a wrapper class around all the C runtime functions.
101 LF
='\n' /* Line Feed */,
102 VTAB
='\v' /* Vertical Tab */,
103 CR
='\r' /* Carriage Return */
107 *** The following nsCRT::mem* functions are no longer
108 *** supported, please use the corresponding lib C
109 *** functions instead.
117 *** Additionally, the following char* string utilities
118 *** are no longer supported, please use the
119 *** corresponding lib C functions instead.
125 /** Compute the string length of s
126 @param s the string in question
127 @return the length of s
129 static PRUint32
strlen(const char* s
) {
130 return PRUint32(::strlen(s
));
133 /// Compare s1 and s2.
134 static PRInt32
strcmp(const char* s1
, const char* s2
) {
135 return PRInt32(PL_strcmp(s1
, s2
));
138 static PRInt32
strncmp(const char* s1
, const char* s2
,
140 return PRInt32(PL_strncmp(s1
, s2
, aMaxLen
));
143 /// Case-insensitive string comparison.
144 static PRInt32
strcasecmp(const char* s1
, const char* s2
) {
145 return PRInt32(PL_strcasecmp(s1
, s2
));
148 /// Case-insensitive string comparison with length
149 static PRInt32
strncasecmp(const char* s1
, const char* s2
, PRUint32 aMaxLen
) {
150 PRInt32 result
=PRInt32(PL_strncasecmp(s1
, s2
, aMaxLen
));
151 //Egads. PL_strncasecmp is returning *very* negative numbers.
152 //Some folks expect -1,0,1, so let's temper its enthusiasm.
158 static PRInt32
strncmp(const char* s1
, const char* s2
, PRInt32 aMaxLen
) {
159 // inline the first test (assumes strings are not null):
160 PRInt32 diff
= ((const unsigned char*)s1
)[0] - ((const unsigned char*)s2
)[0];
161 if (diff
!= 0) return diff
;
162 return PRInt32(PL_strncmp(s1
,s2
,unsigned(aMaxLen
)));
165 static char* strdup(const char* str
) {
166 return PL_strdup(str
);
169 static char* strndup(const char* str
, PRUint32 len
) {
170 return PL_strndup(str
, len
);
173 static void free(char* str
) {
179 How to use this fancy (thread-safe) version of strtok:
182 printf("%s\n\nTokens:\n", string);
183 // Establish string and get the first token:
185 token = nsCRT::strtok(string, seps, &newStr);
186 while (token != NULL) {
187 // While there are tokens in "string"
188 printf(" %s\n", token);
190 token = nsCRT::strtok(newStr, seps, &newStr);
193 * WARNING - STRTOK WHACKS str THE FIRST TIME IT IS CALLED *
194 * MAKE A COPY OF str IF YOU NEED TO USE IT AFTER strtok() *
196 static char* strtok(char* str
, const char* delims
, char* *newStr
);
198 static PRUint32
strlen(const PRUnichar
* s
) {
199 // XXXbsmedberg: remove this null-check at some point
201 NS_ERROR("Passing null to nsCRT::strlen");
207 /// Like strcmp except for ucs2 strings
208 static PRInt32
strcmp(const PRUnichar
* s1
, const PRUnichar
* s2
);
209 /// Like strcmp except for ucs2 strings
210 static PRInt32
strncmp(const PRUnichar
* s1
, const PRUnichar
* s2
,
213 // You must use nsCRT::free(PRUnichar*) to free memory allocated
214 // by nsCRT::strdup(PRUnichar*).
215 static PRUnichar
* strdup(const PRUnichar
* str
);
217 // You must use nsCRT::free(PRUnichar*) to free memory allocated
218 // by strndup(PRUnichar*, PRUint32).
219 static PRUnichar
* strndup(const PRUnichar
* str
, PRUint32 len
);
221 static void free(PRUnichar
* str
) {
222 nsCppSharedAllocator
<PRUnichar
> shared_allocator
;
223 shared_allocator
.deallocate(str
, 0 /*we never new or kept the size*/);
226 // Computes the hashcode for a c-string, returns the string length as
228 static PRUint32
HashCode(const char* str
,
229 PRUint32
* resultingStrLen
= nsnull
);
231 // Computes the hashcode for a length number of bytes of c-string data.
232 static PRUint32
HashCode(const char* start
, PRUint32 length
);
234 // Computes the hashcode for a ucs2 string, returns the string length
235 // as an added bonus.
236 static PRUint32
HashCode(const PRUnichar
* str
,
237 PRUint32
* resultingStrLen
= nsnull
);
239 // Computes a hashcode for a length number of UTF16
240 // characters. Returns the same hash code as the HashCode method
241 // taking a |char*| would if the string were converted to UTF8. This
242 // hash function treats invalid UTF16 data as 0xFFFD (0xEFBFBD in
244 static PRUint32
HashCodeAsUTF8(const PRUnichar
* start
, PRUint32 length
);
246 // Computes the hashcode for a buffer with a specified length.
247 static PRUint32
BufferHashCode(const PRUnichar
* str
, PRUint32 strLen
);
249 // String to longlong
250 static PRInt64
atoll(const char *str
);
252 static char ToUpper(char aChar
) { return NS_ToUpper(aChar
); }
253 static char ToLower(char aChar
) { return NS_ToLower(aChar
); }
255 static PRBool
IsUpper(char aChar
) { return NS_IsUpper(aChar
); }
256 static PRBool
IsLower(char aChar
) { return NS_IsLower(aChar
); }
258 static PRBool
IsAscii(PRUnichar aChar
) { return NS_IsAscii(aChar
); }
259 static PRBool
IsAscii(const PRUnichar
* aString
) { return NS_IsAscii(aString
); }
260 static PRBool
IsAsciiAlpha(PRUnichar aChar
) { return NS_IsAsciiAlpha(aChar
); }
261 static PRBool
IsAsciiDigit(PRUnichar aChar
) { return NS_IsAsciiDigit(aChar
); }
262 static PRBool
IsAsciiSpace(PRUnichar aChar
) { return NS_IsAsciiWhitespace(aChar
); }
263 static PRBool
IsAscii(const char* aString
) { return NS_IsAscii(aString
); }
264 static PRBool
IsAscii(const char* aString
, PRUint32 aLength
) { return NS_IsAscii(aString
, aLength
); }
272 #define CRLF "\015\012" /* A CR LF equivalent string */
275 #if defined(XP_MACOSX)
276 #define FILE_PATH_SEPARATOR "/"
277 #define OS_FILE_ILLEGAL_CHARACTERS ":"
278 #elif defined(XP_WIN) || defined(XP_OS2)
279 #define FILE_PATH_SEPARATOR "\\"
280 #define OS_FILE_ILLEGAL_CHARACTERS "/:*?\"<>|"
281 #elif defined(XP_UNIX) || defined(XP_BEOS)
282 #define FILE_PATH_SEPARATOR "/"
283 #define OS_FILE_ILLEGAL_CHARACTERS ""
285 #error need_to_define_your_file_path_separator_and_illegal_characters
288 // Not all these control characters are illegal in all OSs, but we don't really
289 // want them appearing in filenames
290 #define CONTROL_CHARACTERS "\001\002\003\004\005\006\007" \
291 "\010\011\012\013\014\015\016\017" \
292 "\020\021\022\023\024\025\026\027" \
293 "\030\031\032\033\034\035\036\037"
295 #define FILE_ILLEGAL_CHARACTERS CONTROL_CHARACTERS OS_FILE_ILLEGAL_CHARACTERS
297 #define NS_IS_SPACE(VAL) \
298 (((((intn)(VAL)) & 0x7f) == ((intn)(VAL))) && isspace((intn)(VAL)) )
300 #define NS_IS_CNTRL(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
301 #define NS_IS_DIGIT(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
302 #if defined(XP_WIN) || defined(XP_OS2)
303 #define NS_IS_ALPHA(VAL) (isascii((int)(VAL)) && isalpha((int)(VAL)))
305 #define NS_IS_ALPHA(VAL) ((((unsigned int) (VAL)) > 0x7f) ? (int) 0 : isalpha((int)(VAL)))
309 #endif /* nsCRT_h___ */