On x86 compilers without fastcall, simulate it when invoking traces and un-simulate...
[wine-gecko.git] / xpcom / glue / nsCRTGlue.cpp
blob054e3394797483a24fa65b5fadfe7f32ae4a5ceb
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is Mozilla Application Update.
15 * This file is based on code originally located at
16 * mozilla/toolkit/mozapps/update/src/updater/updater.cpp
18 * The Initial Developer of the Original Code is
19 * Benjamin Smedberg <benjamin@smedbergs.us>
21 * Portions created by the Initial Developer are Copyright (C) 2005
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
25 * Darin Fisher <darin@meer.net>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #include "nsCRTGlue.h"
42 #include "nsXPCOM.h"
43 #include "nsDebug.h"
44 #include <string.h>
45 #include <stdio.h>
46 #include <stdarg.h>
48 #ifdef XP_WIN
49 #include <io.h>
50 #endif
52 const char*
53 NS_strspnp(const char *delims, const char *str)
55 const char *d;
56 do {
57 for (d = delims; *d != '\0'; ++d) {
58 if (*str == *d) {
59 ++str;
60 break;
63 } while (*d);
65 return str;
68 char*
69 NS_strtok(const char *delims, char **str)
71 if (!*str)
72 return NULL;
74 char *ret = (char*) NS_strspnp(delims, *str);
76 if (!*ret) {
77 *str = ret;
78 return NULL;
81 char *i = ret;
82 do {
83 for (const char *d = delims; *d != '\0'; ++d) {
84 if (*i == *d) {
85 *i = '\0';
86 *str = ++i;
87 return ret;
90 ++i;
91 } while (*i);
93 *str = NULL;
94 return ret;
97 PRUint32
98 NS_strlen(const PRUnichar *aString)
100 const PRUnichar *end;
102 for (end = aString; *end; ++end) {
103 // empty loop
106 return end - aString;
110 NS_strcmp(const PRUnichar *a, const PRUnichar *b)
112 while (*b) {
113 int r = *a - *b;
114 if (r)
115 return r;
117 ++a;
118 ++b;
121 return *a != '\0';
124 PRUnichar*
125 NS_strdup(const PRUnichar *aString)
127 PRUint32 len = NS_strlen(aString);
128 return NS_strndup(aString, len);
131 PRUnichar*
132 NS_strndup(const PRUnichar *aString, PRUint32 aLen)
134 PRUnichar *newBuf = (PRUnichar*) NS_Alloc((aLen + 1) * sizeof(PRUnichar));
135 if (newBuf) {
136 memcpy(newBuf, aString, aLen * sizeof(PRUnichar));
137 newBuf[aLen] = '\0';
139 return newBuf;
142 char*
143 NS_strdup(const char *aString)
145 PRUint32 len = strlen(aString);
146 char *str = (char*) NS_Alloc(len + 1);
147 if (str) {
148 memcpy(str, aString, len);
149 str[len] = '\0';
151 return str;
154 // This table maps uppercase characters to lower case characters;
155 // characters that are neither upper nor lower case are unaffected.
156 const unsigned char nsLowerUpperUtils::kUpper2Lower[256] = {
157 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
158 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
159 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
160 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
163 // upper band mapped to lower [A-Z] => [a-z]
164 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
165 112,113,114,115,116,117,118,119,120,121,122,
167 91, 92, 93, 94, 95,
168 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
169 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
170 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
171 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
172 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
173 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
174 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
175 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
176 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
177 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
180 const unsigned char nsLowerUpperUtils::kLower2Upper[256] = {
181 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
182 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
183 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
184 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
185 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
186 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
189 // lower band mapped to upper [a-z] => [A-Z]
190 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
191 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
193 123,124,125,126,127,
194 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
195 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
196 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
197 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
198 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
199 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
200 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
201 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
204 PRBool NS_IsUpper(char aChar)
206 return aChar != (char)nsLowerUpperUtils::kUpper2Lower[(unsigned char)aChar];
209 PRBool NS_IsLower(char aChar)
211 return aChar != (char)nsLowerUpperUtils::kLower2Upper[(unsigned char)aChar];
214 PRBool NS_IsAscii(PRUnichar aChar)
216 return (0x0080 > aChar);
219 PRBool NS_IsAscii(const PRUnichar *aString)
221 while(*aString) {
222 if( 0x0080 <= *aString)
223 return PR_FALSE;
224 aString++;
226 return PR_TRUE;
229 PRBool NS_IsAscii(const char *aString)
231 while(*aString) {
232 if( 0x80 & *aString)
233 return PR_FALSE;
234 aString++;
236 return PR_TRUE;
239 PRBool NS_IsAscii(const char* aString, PRUint32 aLength)
241 const char* end = aString + aLength;
242 while (aString < end) {
243 if (0x80 & *aString)
244 return PR_FALSE;
245 ++aString;
247 return PR_TRUE;
250 PRBool NS_IsAsciiAlpha(PRUnichar aChar)
252 return ((aChar >= 'A') && (aChar <= 'Z')) ||
253 ((aChar >= 'a') && (aChar <= 'z'));
256 PRBool NS_IsAsciiWhitespace(PRUnichar aChar)
258 return aChar == ' ' ||
259 aChar == '\r' ||
260 aChar == '\n' ||
261 aChar == '\t';
264 PRBool NS_IsAsciiDigit(PRUnichar aChar)
266 return aChar >= '0' && aChar <= '9';
269 #if defined(XP_WIN) && !defined(WINCE)
270 void
271 printf_stderr(const char *fmt, ...)
273 FILE *fp = _fdopen(_dup(2), "a");
275 va_list args;
276 va_start(args, fmt);
277 vfprintf(fp, fmt, args);
278 va_end(args);
280 fclose(fp);
282 #else
283 void
284 printf_stderr(const char *fmt, ...)
286 va_list args;
287 va_start(args, fmt);
288 vfprintf(stderr, fmt, args);
289 va_end(args);
291 #endif