1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/nsCRTGlue.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,334 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "nsCRTGlue.h" 1.9 +#include "nsXPCOM.h" 1.10 +#include "nsDebug.h" 1.11 +#include "prtime.h" 1.12 + 1.13 +#include <stdlib.h> 1.14 +#include <string.h> 1.15 +#include <stdio.h> 1.16 +#include <stdarg.h> 1.17 + 1.18 +#ifdef XP_WIN 1.19 +#include <io.h> 1.20 +#include <windows.h> 1.21 +#endif 1.22 + 1.23 +#ifdef ANDROID 1.24 +#include <android/log.h> 1.25 +#endif 1.26 + 1.27 +const char* 1.28 +NS_strspnp(const char *delims, const char *str) 1.29 +{ 1.30 + const char *d; 1.31 + do { 1.32 + for (d = delims; *d != '\0'; ++d) { 1.33 + if (*str == *d) { 1.34 + ++str; 1.35 + break; 1.36 + } 1.37 + } 1.38 + } while (*d); 1.39 + 1.40 + return str; 1.41 +} 1.42 + 1.43 +char* 1.44 +NS_strtok(const char *delims, char **str) 1.45 +{ 1.46 + if (!*str) 1.47 + return nullptr; 1.48 + 1.49 + char *ret = (char*) NS_strspnp(delims, *str); 1.50 + 1.51 + if (!*ret) { 1.52 + *str = ret; 1.53 + return nullptr; 1.54 + } 1.55 + 1.56 + char *i = ret; 1.57 + do { 1.58 + for (const char *d = delims; *d != '\0'; ++d) { 1.59 + if (*i == *d) { 1.60 + *i = '\0'; 1.61 + *str = ++i; 1.62 + return ret; 1.63 + } 1.64 + } 1.65 + ++i; 1.66 + } while (*i); 1.67 + 1.68 + *str = nullptr; 1.69 + return ret; 1.70 +} 1.71 + 1.72 +uint32_t 1.73 +NS_strlen(const char16_t *aString) 1.74 +{ 1.75 + const char16_t *end; 1.76 + 1.77 + for (end = aString; *end; ++end) { 1.78 + // empty loop 1.79 + } 1.80 + 1.81 + return end - aString; 1.82 +} 1.83 + 1.84 +int 1.85 +NS_strcmp(const char16_t *a, const char16_t *b) 1.86 +{ 1.87 + while (*b) { 1.88 + int r = *a - *b; 1.89 + if (r) 1.90 + return r; 1.91 + 1.92 + ++a; 1.93 + ++b; 1.94 + } 1.95 + 1.96 + return *a != '\0'; 1.97 +} 1.98 + 1.99 +char16_t* 1.100 +NS_strdup(const char16_t *aString) 1.101 +{ 1.102 + uint32_t len = NS_strlen(aString); 1.103 + return NS_strndup(aString, len); 1.104 +} 1.105 + 1.106 +char16_t* 1.107 +NS_strndup(const char16_t *aString, uint32_t aLen) 1.108 +{ 1.109 + char16_t *newBuf = (char16_t*) NS_Alloc((aLen + 1) * sizeof(char16_t)); 1.110 + if (newBuf) { 1.111 + memcpy(newBuf, aString, aLen * sizeof(char16_t)); 1.112 + newBuf[aLen] = '\0'; 1.113 + } 1.114 + return newBuf; 1.115 +} 1.116 + 1.117 +char* 1.118 +NS_strdup(const char *aString) 1.119 +{ 1.120 + uint32_t len = strlen(aString); 1.121 + char *str = (char*) NS_Alloc(len + 1); 1.122 + if (str) { 1.123 + memcpy(str, aString, len); 1.124 + str[len] = '\0'; 1.125 + } 1.126 + return str; 1.127 +} 1.128 + 1.129 +// This table maps uppercase characters to lower case characters; 1.130 +// characters that are neither upper nor lower case are unaffected. 1.131 +const unsigned char nsLowerUpperUtils::kUpper2Lower[256] = { 1.132 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1.133 + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1.134 + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 1.135 + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 1.136 + 64, 1.137 + 1.138 + // upper band mapped to lower [A-Z] => [a-z] 1.139 + 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, 1.140 + 112,113,114,115,116,117,118,119,120,121,122, 1.141 + 1.142 + 91, 92, 93, 94, 95, 1.143 + 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, 1.144 + 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, 1.145 + 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, 1.146 + 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, 1.147 + 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, 1.148 + 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, 1.149 + 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, 1.150 + 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, 1.151 + 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, 1.152 + 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255 1.153 +}; 1.154 + 1.155 +const unsigned char nsLowerUpperUtils::kLower2Upper[256] = { 1.156 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1.157 + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1.158 + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 1.159 + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 1.160 + 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1.161 + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 1.162 + 96, 1.163 + 1.164 + // lower band mapped to upper [a-z] => [A-Z] 1.165 + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1.166 + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 1.167 + 1.168 + 123,124,125,126,127, 1.169 + 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, 1.170 + 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, 1.171 + 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, 1.172 + 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, 1.173 + 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, 1.174 + 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, 1.175 + 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, 1.176 + 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255 1.177 +}; 1.178 + 1.179 +bool NS_IsUpper(char aChar) 1.180 +{ 1.181 + return aChar != (char)nsLowerUpperUtils::kUpper2Lower[(unsigned char)aChar]; 1.182 +} 1.183 + 1.184 +bool NS_IsLower(char aChar) 1.185 +{ 1.186 + return aChar != (char)nsLowerUpperUtils::kLower2Upper[(unsigned char)aChar]; 1.187 +} 1.188 + 1.189 +bool NS_IsAscii(char16_t aChar) 1.190 +{ 1.191 + return (0x0080 > aChar); 1.192 +} 1.193 + 1.194 +bool NS_IsAscii(const char16_t *aString) 1.195 +{ 1.196 + while(*aString) { 1.197 + if( 0x0080 <= *aString) 1.198 + return false; 1.199 + aString++; 1.200 + } 1.201 + return true; 1.202 +} 1.203 + 1.204 +bool NS_IsAscii(const char *aString) 1.205 +{ 1.206 + while(*aString) { 1.207 + if( 0x80 & *aString) 1.208 + return false; 1.209 + aString++; 1.210 + } 1.211 + return true; 1.212 +} 1.213 + 1.214 +bool NS_IsAscii(const char* aString, uint32_t aLength) 1.215 +{ 1.216 + const char* end = aString + aLength; 1.217 + while (aString < end) { 1.218 + if (0x80 & *aString) 1.219 + return false; 1.220 + ++aString; 1.221 + } 1.222 + return true; 1.223 +} 1.224 + 1.225 +bool NS_IsAsciiAlpha(char16_t aChar) 1.226 +{ 1.227 + return ((aChar >= 'A') && (aChar <= 'Z')) || 1.228 + ((aChar >= 'a') && (aChar <= 'z')); 1.229 +} 1.230 + 1.231 +bool NS_IsAsciiWhitespace(char16_t aChar) 1.232 +{ 1.233 + return aChar == ' ' || 1.234 + aChar == '\r' || 1.235 + aChar == '\n' || 1.236 + aChar == '\t'; 1.237 +} 1.238 + 1.239 +bool NS_IsAsciiDigit(char16_t aChar) 1.240 +{ 1.241 + return aChar >= '0' && aChar <= '9'; 1.242 +} 1.243 + 1.244 + 1.245 +#ifndef XPCOM_GLUE_AVOID_NSPR 1.246 +#define TABLE_SIZE 36 1.247 +static const char table[] = { 1.248 + 'a','b','c','d','e','f','g','h','i','j', 1.249 + 'k','l','m','n','o','p','q','r','s','t', 1.250 + 'u','v','w','x','y','z','0','1','2','3', 1.251 + '4','5','6','7','8','9' 1.252 +}; 1.253 + 1.254 +void NS_MakeRandomString(char *aBuf, int32_t aBufLen) 1.255 +{ 1.256 + // turn PR_Now() into milliseconds since epoch 1.257 + // and salt rand with that. 1.258 + static unsigned int seed = 0; 1.259 + if (seed == 0) { 1.260 + double fpTime = double(PR_Now()); 1.261 + seed = (unsigned int)(fpTime * 1e-6 + 0.5); // use 1e-6, granularity of PR_Now() on the mac is seconds 1.262 + srand(seed); 1.263 + } 1.264 + 1.265 + int32_t i; 1.266 + for (i=0;i<aBufLen;i++) { 1.267 + *aBuf++ = table[rand()%TABLE_SIZE]; 1.268 + } 1.269 + *aBuf = 0; 1.270 +} 1.271 + 1.272 +#endif 1.273 +#if defined(XP_WIN) 1.274 + 1.275 +#define va_copy(dest, src) (dest = src) 1.276 + 1.277 +void 1.278 +vprintf_stderr(const char *fmt, va_list args) 1.279 +{ 1.280 + if (IsDebuggerPresent()) { 1.281 + char buf[2048]; 1.282 + va_list argsCpy; 1.283 + va_copy(argsCpy, args); 1.284 + vsnprintf(buf, sizeof(buf), fmt, argsCpy); 1.285 + buf[sizeof(buf) - 1] = '\0'; 1.286 + va_end(argsCpy); 1.287 + OutputDebugStringA(buf); 1.288 + } 1.289 + 1.290 + FILE *fp = _fdopen(_dup(2), "a"); 1.291 + if (!fp) 1.292 + return; 1.293 + 1.294 + vfprintf(fp, fmt, args); 1.295 + 1.296 + fclose(fp); 1.297 +} 1.298 + 1.299 +#undef va_copy 1.300 + 1.301 +#elif defined(ANDROID) 1.302 +void 1.303 +vprintf_stderr(const char *fmt, va_list args) 1.304 +{ 1.305 + __android_log_vprint(ANDROID_LOG_INFO, "Gecko", fmt, args); 1.306 +} 1.307 +#else 1.308 +void 1.309 +vprintf_stderr(const char *fmt, va_list args) 1.310 +{ 1.311 + vfprintf(stderr, fmt, args); 1.312 +} 1.313 +#endif 1.314 + 1.315 +void 1.316 +printf_stderr(const char *fmt, ...) 1.317 +{ 1.318 + va_list args; 1.319 + va_start(args, fmt); 1.320 + vprintf_stderr(fmt, args); 1.321 + va_end(args); 1.322 +} 1.323 + 1.324 +void 1.325 +fprintf_stderr(FILE* aFile, const char *fmt, ...) 1.326 +{ 1.327 + va_list args; 1.328 + va_start(args, fmt); 1.329 + if (aFile == stderr) { 1.330 + vprintf_stderr(fmt, args); 1.331 + } else { 1.332 + vfprintf(aFile, fmt, args); 1.333 + } 1.334 + va_end(args); 1.335 +} 1.336 + 1.337 +