1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
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/
8 : *
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.
13 : *
14 : * The Original Code is Mozilla Application Update.
15 : * This file is based on code originally located at
16 : * mozilla/toolkit/mozapps/update/updater/updater.cpp
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Benjamin Smedberg <benjamin@smedbergs.us>
20 : *
21 : * Portions created by the Initial Developer are Copyright (C) 2005
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Darin Fisher <darin@meer.net>
26 : *
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.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsCRTGlue.h"
42 : #include "nsXPCOM.h"
43 : #include "nsDebug.h"
44 : #include <string.h>
45 : #include <stdio.h>
46 : #include <stdarg.h>
47 :
48 : #ifdef XP_WIN
49 : #include <io.h>
50 : #endif
51 :
52 : #ifdef ANDROID
53 : #include <android/log.h>
54 : #endif
55 :
56 : const char*
57 0 : NS_strspnp(const char *delims, const char *str)
58 : {
59 : const char *d;
60 0 : do {
61 0 : for (d = delims; *d != '\0'; ++d) {
62 0 : if (*str == *d) {
63 0 : ++str;
64 0 : break;
65 : }
66 : }
67 : } while (*d);
68 :
69 0 : return str;
70 : }
71 :
72 : char*
73 0 : NS_strtok(const char *delims, char **str)
74 : {
75 0 : if (!*str)
76 0 : return NULL;
77 :
78 0 : char *ret = (char*) NS_strspnp(delims, *str);
79 :
80 0 : if (!*ret) {
81 0 : *str = ret;
82 0 : return NULL;
83 : }
84 :
85 0 : char *i = ret;
86 0 : do {
87 0 : for (const char *d = delims; *d != '\0'; ++d) {
88 0 : if (*i == *d) {
89 0 : *i = '\0';
90 0 : *str = ++i;
91 0 : return ret;
92 : }
93 : }
94 0 : ++i;
95 : } while (*i);
96 :
97 0 : *str = NULL;
98 0 : return ret;
99 : }
100 :
101 : PRUint32
102 7 : NS_strlen(const PRUnichar *aString)
103 : {
104 : const PRUnichar *end;
105 :
106 7 : for (end = aString; *end; ++end) {
107 : // empty loop
108 : }
109 :
110 7 : return end - aString;
111 : }
112 :
113 : int
114 0 : NS_strcmp(const PRUnichar *a, const PRUnichar *b)
115 : {
116 0 : while (*b) {
117 0 : int r = *a - *b;
118 0 : if (r)
119 0 : return r;
120 :
121 0 : ++a;
122 0 : ++b;
123 : }
124 :
125 0 : return *a != '\0';
126 : }
127 :
128 : PRUnichar*
129 0 : NS_strdup(const PRUnichar *aString)
130 : {
131 0 : PRUint32 len = NS_strlen(aString);
132 0 : return NS_strndup(aString, len);
133 : }
134 :
135 : PRUnichar*
136 0 : NS_strndup(const PRUnichar *aString, PRUint32 aLen)
137 : {
138 0 : PRUnichar *newBuf = (PRUnichar*) NS_Alloc((aLen + 1) * sizeof(PRUnichar));
139 0 : if (newBuf) {
140 0 : memcpy(newBuf, aString, aLen * sizeof(PRUnichar));
141 0 : newBuf[aLen] = '\0';
142 : }
143 0 : return newBuf;
144 : }
145 :
146 : char*
147 0 : NS_strdup(const char *aString)
148 : {
149 0 : PRUint32 len = strlen(aString);
150 0 : char *str = (char*) NS_Alloc(len + 1);
151 0 : if (str) {
152 0 : memcpy(str, aString, len);
153 0 : str[len] = '\0';
154 : }
155 0 : return str;
156 : }
157 :
158 : // This table maps uppercase characters to lower case characters;
159 : // characters that are neither upper nor lower case are unaffected.
160 : const unsigned char nsLowerUpperUtils::kUpper2Lower[256] = {
161 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
162 : 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
163 : 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
164 : 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
165 : 64,
166 :
167 : // upper band mapped to lower [A-Z] => [a-z]
168 : 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,
170 :
171 : 91, 92, 93, 94, 95,
172 : 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
173 : 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
174 : 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
175 : 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
176 : 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
177 : 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
178 : 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
179 : 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
180 : 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
181 : 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
182 : };
183 :
184 : const unsigned char nsLowerUpperUtils::kLower2Upper[256] = {
185 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
186 : 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
187 : 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
188 : 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
189 : 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
190 : 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
191 : 96,
192 :
193 : // lower band mapped to upper [a-z] => [A-Z]
194 : 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
195 : 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
196 :
197 : 123,124,125,126,127,
198 : 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
199 : 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
200 : 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
201 : 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
202 : 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
203 : 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
204 : 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
205 : 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
206 : };
207 :
208 0 : bool NS_IsUpper(char aChar)
209 : {
210 0 : return aChar != (char)nsLowerUpperUtils::kUpper2Lower[(unsigned char)aChar];
211 : }
212 :
213 0 : bool NS_IsLower(char aChar)
214 : {
215 0 : return aChar != (char)nsLowerUpperUtils::kLower2Upper[(unsigned char)aChar];
216 : }
217 :
218 239 : bool NS_IsAscii(PRUnichar aChar)
219 : {
220 239 : return (0x0080 > aChar);
221 : }
222 :
223 0 : bool NS_IsAscii(const PRUnichar *aString)
224 : {
225 0 : while(*aString) {
226 0 : if( 0x0080 <= *aString)
227 0 : return false;
228 0 : aString++;
229 : }
230 0 : return true;
231 : }
232 :
233 0 : bool NS_IsAscii(const char *aString)
234 : {
235 0 : while(*aString) {
236 0 : if( 0x80 & *aString)
237 0 : return false;
238 0 : aString++;
239 : }
240 0 : return true;
241 : }
242 :
243 0 : bool NS_IsAscii(const char* aString, PRUint32 aLength)
244 : {
245 0 : const char* end = aString + aLength;
246 0 : while (aString < end) {
247 0 : if (0x80 & *aString)
248 0 : return false;
249 0 : ++aString;
250 : }
251 0 : return true;
252 : }
253 :
254 0 : bool NS_IsAsciiAlpha(PRUnichar aChar)
255 : {
256 : return ((aChar >= 'A') && (aChar <= 'Z')) ||
257 0 : ((aChar >= 'a') && (aChar <= 'z'));
258 : }
259 :
260 115 : bool NS_IsAsciiWhitespace(PRUnichar aChar)
261 : {
262 : return aChar == ' ' ||
263 : aChar == '\r' ||
264 : aChar == '\n' ||
265 115 : aChar == '\t';
266 : }
267 :
268 0 : bool NS_IsAsciiDigit(PRUnichar aChar)
269 : {
270 0 : return aChar >= '0' && aChar <= '9';
271 : }
272 :
273 : #if defined(XP_WIN)
274 : void
275 : printf_stderr(const char *fmt, ...)
276 : {
277 : FILE *fp = _fdopen(_dup(2), "a");
278 : if (!fp)
279 : return;
280 :
281 : va_list args;
282 : va_start(args, fmt);
283 : vfprintf(fp, fmt, args);
284 : va_end(args);
285 :
286 : fclose(fp);
287 : }
288 : #elif defined(ANDROID)
289 : void
290 : printf_stderr(const char *fmt, ...)
291 : {
292 : va_list args;
293 : va_start(args, fmt);
294 : __android_log_vprint(ANDROID_LOG_INFO, "Gecko", fmt, args);
295 : va_end(args);
296 : }
297 : #else
298 : void
299 0 : printf_stderr(const char *fmt, ...)
300 : {
301 : va_list args;
302 0 : va_start(args, fmt);
303 0 : vfprintf(stderr, fmt, args);
304 0 : va_end(args);
305 0 : }
306 : #endif
|