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