VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsCRT.h@ 53433

Last change on this file since 53433 was 1, checked in by vboxsync, 55 years ago

import

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette