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
|
---|
4 | *
|
---|
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/
|
---|
9 | *
|
---|
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
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
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.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
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.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 | #ifndef nsCRT_h___
|
---|
38 | #define nsCRT_h___
|
---|
39 |
|
---|
40 | #include <iprt/mem.h>
|
---|
41 | #include <iprt/string.h>
|
---|
42 |
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <string.h>
|
---|
45 | #include <ctype.h>
|
---|
46 | #include "nsMemory.h"
|
---|
47 | #include "nscore.h"
|
---|
48 | #include "prtypes.h"
|
---|
49 |
|
---|
50 | #ifdef HAVE_CPP_NUMERIC_LIMITS
|
---|
51 | #include <limits>
|
---|
52 | #else
|
---|
53 | #include <limits.h>
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | #ifdef XP_MAC
|
---|
57 | # define NS_LINEBREAK "\015"
|
---|
58 | # define NS_LINEBREAK_LEN 1
|
---|
59 | #else
|
---|
60 | # if defined(XP_WIN) || defined(XP_OS2)
|
---|
61 | # define NS_LINEBREAK "\015\012"
|
---|
62 | # define NS_LINEBREAK_LEN 2
|
---|
63 | # else
|
---|
64 | # if defined(XP_UNIX) || defined(XP_BEOS)
|
---|
65 | # define NS_LINEBREAK "\012"
|
---|
66 | # define NS_LINEBREAK_LEN 1
|
---|
67 | # endif /* XP_UNIX */
|
---|
68 | # endif /* XP_WIN || XP_OS2 */
|
---|
69 | #endif /* XP_MAC */
|
---|
70 |
|
---|
71 | extern const PRUnichar kIsoLatin1ToUCS2[256];
|
---|
72 |
|
---|
73 | // This macro can be used in a class declaration for classes that want
|
---|
74 | // to ensure that their instance memory is zeroed.
|
---|
75 | #define NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW \
|
---|
76 | void* operator new(size_t sz) CPP_THROW_NEW { \
|
---|
77 | void* rv = ::operator new(sz); \
|
---|
78 | if (rv) { \
|
---|
79 | memset(rv, 0, sz); \
|
---|
80 | } \
|
---|
81 | return rv; \
|
---|
82 | } \
|
---|
83 | void operator delete(void* ptr) { \
|
---|
84 | ::operator delete(ptr); \
|
---|
85 | }
|
---|
86 |
|
---|
87 | // This macro works with the next macro to declare a non-inlined
|
---|
88 | // version of the above.
|
---|
89 | #define NS_DECL_ZEROING_OPERATOR_NEW \
|
---|
90 | void* operator new(size_t sz) CPP_THROW_NEW; \
|
---|
91 | void operator delete(void* ptr);
|
---|
92 |
|
---|
93 | #define NS_IMPL_ZEROING_OPERATOR_NEW(_class) \
|
---|
94 | void* _class::operator new(size_t sz) CPP_THROW_NEW { \
|
---|
95 | void* rv = ::operator new(sz); \
|
---|
96 | if (rv) { \
|
---|
97 | memset(rv, 0, sz); \
|
---|
98 | } \
|
---|
99 | return rv; \
|
---|
100 | } \
|
---|
101 | void _class::operator delete(void* ptr) { \
|
---|
102 | ::operator delete(ptr); \
|
---|
103 | }
|
---|
104 |
|
---|
105 | // Freeing helper
|
---|
106 | #define CRTFREEIF(x) if (x) { nsCRT::free(x); x = 0; }
|
---|
107 |
|
---|
108 | /// This is a wrapper class around all the C runtime functions.
|
---|
109 |
|
---|
110 | class NS_COM nsCRT {
|
---|
111 | public:
|
---|
112 | enum {
|
---|
113 | TAB='\t' /* Horizontal Tab */,
|
---|
114 | LF='\n' /* Line Feed */,
|
---|
115 | VTAB='\v' /* Vertical Tab */,
|
---|
116 | FF='\f' /* Form Feed */,
|
---|
117 | CR='\r' /* Carriage Return */
|
---|
118 | };
|
---|
119 |
|
---|
120 | /***
|
---|
121 | *** The following nsCRT::mem* functions are no longer
|
---|
122 | *** supported, please use the corresponding lib C
|
---|
123 | *** functions instead.
|
---|
124 | ***
|
---|
125 | *** nsCRT::memcpy()
|
---|
126 | *** nsCRT::memcmp()
|
---|
127 | *** nsCRT::memmove()
|
---|
128 | *** nsCRT::memset()
|
---|
129 | *** nsCRT::zero()
|
---|
130 | ***
|
---|
131 | *** Additionally, the following char* string utilities
|
---|
132 | *** are no longer supported, please use the
|
---|
133 | *** corresponding lib C functions instead.
|
---|
134 | ***
|
---|
135 | *** nsCRT::strlen()
|
---|
136 | ***
|
---|
137 | ***/
|
---|
138 |
|
---|
139 | /** Compute the string length of s
|
---|
140 | @param s the string in question
|
---|
141 | @return the length of s
|
---|
142 | */
|
---|
143 | static PRUint32 strlen(const char* s) {
|
---|
144 | return PRUint32(::strlen(s));
|
---|
145 | }
|
---|
146 |
|
---|
147 | static char* strdup(const char* str) {
|
---|
148 | return RTStrDup(str);
|
---|
149 | }
|
---|
150 |
|
---|
151 | static char* strndup(const char* str, PRUint32 len) {
|
---|
152 | return RTStrDupN(str, len);
|
---|
153 | }
|
---|
154 |
|
---|
155 | static void free(char* str) {
|
---|
156 | RTStrFree(str);
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 |
|
---|
161 | How to use this fancy (thread-safe) version of strtok:
|
---|
162 |
|
---|
163 | void main(void) {
|
---|
164 | printf("%s\n\nTokens:\n", string);
|
---|
165 | // Establish string and get the first token:
|
---|
166 | char* newStr;
|
---|
167 | token = nsCRT::strtok(string, seps, &newStr);
|
---|
168 | while (token != NULL) {
|
---|
169 | // While there are tokens in "string"
|
---|
170 | printf(" %s\n", token);
|
---|
171 | // Get next token:
|
---|
172 | token = nsCRT::strtok(newStr, seps, &newStr);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | * WARNING - STRTOK WHACKS str THE FIRST TIME IT IS CALLED *
|
---|
176 | * MAKE A COPY OF str IF YOU NEED TO USE IT AFTER strtok() *
|
---|
177 | */
|
---|
178 | static char* strtok(char* str, const char* delims, char* *newStr);
|
---|
179 |
|
---|
180 | /// Like strlen except for ucs2 strings
|
---|
181 | static PRUint32 strlen(const PRUnichar* s);
|
---|
182 |
|
---|
183 | /// Like strcmp except for ucs2 strings
|
---|
184 | static PRInt32 strcmp(const PRUnichar* s1, const PRUnichar* s2);
|
---|
185 | /// Like strcmp except for ucs2 strings
|
---|
186 | static PRInt32 strncmp(const PRUnichar* s1, const PRUnichar* s2,
|
---|
187 | PRUint32 aMaxLen);
|
---|
188 |
|
---|
189 | // You must use nsCRT::free(PRUnichar*) to free memory allocated
|
---|
190 | // by nsCRT::strdup(PRUnichar*).
|
---|
191 | static PRUnichar* strdup(const PRUnichar* str);
|
---|
192 |
|
---|
193 | // You must use nsCRT::free(PRUnichar*) to free memory allocated
|
---|
194 | // by strndup(PRUnichar*, PRUint32).
|
---|
195 | static PRUnichar* strndup(const PRUnichar* str, PRUint32 len);
|
---|
196 |
|
---|
197 | static void free(PRUnichar* str) {
|
---|
198 | RTMemFree(str);
|
---|
199 | }
|
---|
200 |
|
---|
201 | // Computes the hashcode for a c-string, returns the string length as
|
---|
202 | // an added bonus.
|
---|
203 | static PRUint32 HashCode(const char* str,
|
---|
204 | PRUint32* resultingStrLen = nsnull);
|
---|
205 |
|
---|
206 | // Computes the hashcode for a ucs2 string, returns the string length
|
---|
207 | // as an added bonus.
|
---|
208 | static PRUint32 HashCode(const PRUnichar* str,
|
---|
209 | PRUint32* resultingStrLen = nsnull);
|
---|
210 |
|
---|
211 | // Computes a hashcode for a ucs2 string that returns the same thing
|
---|
212 | // as the HashCode method taking a |char*| would if the string were
|
---|
213 | // converted to UTF8. Returns the string length as an added bonus.
|
---|
214 | static PRUint32 HashCodeAsUTF8(const PRUnichar* str,
|
---|
215 | PRUint32* resultingStrLen = nsnull);
|
---|
216 |
|
---|
217 | // Computes the hashcode for a buffer with a specified length.
|
---|
218 | static PRUint32 BufferHashCode(const char* str, PRUint32 strLen);
|
---|
219 |
|
---|
220 | // Computes the hashcode for a buffer with a specified length.
|
---|
221 | static PRUint32 BufferHashCode(const PRUnichar* str, PRUint32 strLen);
|
---|
222 |
|
---|
223 | // String to longlong
|
---|
224 | static PRInt64 atoll(const char *str);
|
---|
225 |
|
---|
226 | static char ToUpper(char aChar);
|
---|
227 |
|
---|
228 | static char ToLower(char aChar);
|
---|
229 |
|
---|
230 | static PRBool IsUpper(char aChar);
|
---|
231 |
|
---|
232 | static PRBool IsLower(char aChar);
|
---|
233 |
|
---|
234 | static PRBool IsAscii(PRUnichar aChar);
|
---|
235 | static PRBool IsAscii(const PRUnichar* aString);
|
---|
236 | static PRBool IsAsciiAlpha(PRUnichar aChar);
|
---|
237 | static PRBool IsAsciiDigit(PRUnichar aChar);
|
---|
238 | static PRBool IsAsciiSpace(PRUnichar aChar);
|
---|
239 | static PRBool IsAscii(const char* aString);
|
---|
240 | static PRBool IsAscii(const char* aString, PRUint32 aLength);
|
---|
241 | };
|
---|
242 |
|
---|
243 | #define FF '\014'
|
---|
244 | #define TAB '\011'
|
---|
245 |
|
---|
246 | #define CRSTR "\015"
|
---|
247 | #define LFSTR "\012"
|
---|
248 | #define CRLF "\015\012" /* A CR LF equivalent string */
|
---|
249 |
|
---|
250 |
|
---|
251 | #if defined(XP_MAC)
|
---|
252 | #define FILE_PATH_SEPARATOR ":"
|
---|
253 | #define FILE_ILLEGAL_CHARACTERS ""
|
---|
254 | #elif defined(XP_WIN) || defined(XP_OS2)
|
---|
255 | #define FILE_PATH_SEPARATOR "\\"
|
---|
256 | #define FILE_ILLEGAL_CHARACTERS "/:*?\"<>|"
|
---|
257 | #elif defined(XP_UNIX) || defined(XP_BEOS)
|
---|
258 | #define FILE_PATH_SEPARATOR "/"
|
---|
259 | #define FILE_ILLEGAL_CHARACTERS ""
|
---|
260 | #else
|
---|
261 | #error need_to_define_your_file_path_separator_and_illegal_characters
|
---|
262 | #endif
|
---|
263 |
|
---|
264 | #define NS_IS_SPACE(VAL) \
|
---|
265 | (((((intn)(VAL)) & 0x7f) == ((intn)(VAL))) && isspace((intn)(VAL)) )
|
---|
266 |
|
---|
267 | #define NS_IS_CNTRL(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
|
---|
268 | #define NS_IS_DIGIT(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
|
---|
269 | #if defined(XP_WIN) || defined(XP_OS2)
|
---|
270 | #define NS_IS_ALPHA(VAL) (isascii((int)(VAL)) && isalpha((int)(VAL)))
|
---|
271 | #else
|
---|
272 | #define NS_IS_ALPHA(VAL) ((((unsigned int) (VAL)) > 0x7f) ? (int) 0 : isalpha((int)(VAL)))
|
---|
273 | #endif
|
---|
274 |
|
---|
275 |
|
---|
276 | #endif /* nsCRT_h___ */
|
---|