blob: c735b971dce57dd71d232663a8f74b789e44240d [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "../../include/javascript/JavaScript.h"
8#include "../../include/javascript/IJavaScript.h"
9#include "../../include/javascript/JS_Define.h"
10#include "../../include/javascript/JS_Object.h"
11#include "../../include/javascript/JS_Value.h"
12#include "../../include/javascript/util.h"
13#include "../../include/javascript/PublicMethods.h"
14#include "../../include/javascript/resource.h"
15#include "../../include/javascript/JS_Context.h"
16#include "../../include/javascript/JS_EventHandler.h"
17#include "../../include/javascript/JS_Runtime.h"
18
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019#if _FX_OS_ == _FX_ANDROID_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020#include <ctype.h>
21#endif
22
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023static v8::Isolate* GetIsolate(IFXJS_Context* cc) {
24 CJS_Context* pContext = (CJS_Context*)cc;
25 ASSERT(pContext != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
28 ASSERT(pRuntime != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030 return pRuntime->GetIsolate();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031}
32
33BEGIN_JS_STATIC_CONST(CJS_Util)
34END_JS_STATIC_CONST()
35
36BEGIN_JS_STATIC_PROP(CJS_Util)
37END_JS_STATIC_PROP()
38
39BEGIN_JS_STATIC_METHOD(CJS_Util)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040JS_STATIC_METHOD_ENTRY(printd)
41JS_STATIC_METHOD_ENTRY(printf)
42JS_STATIC_METHOD_ENTRY(printx)
43JS_STATIC_METHOD_ENTRY(scand)
44JS_STATIC_METHOD_ENTRY(byteToChar)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045END_JS_STATIC_METHOD()
46
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047IMPLEMENT_JS_CLASS(CJS_Util, util)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051util::~util(void) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053struct stru_TbConvert {
54 const FX_WCHAR* lpszJSMark;
55 const FX_WCHAR* lpszCppMark;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056};
57
58const stru_TbConvert fcTable[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 {L"mmmm", L"%B"},
60 {L"mmm", L"%b"},
61 {L"mm", L"%m"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070062 //"m"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 {L"dddd", L"%A"},
64 {L"ddd", L"%a"},
65 {L"dd", L"%d"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070066 //"d", "%w",
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 {L"yyyy", L"%Y"},
68 {L"yy", L"%y"},
69 {L"HH", L"%H"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070070 //"H"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 {L"hh", L"%I"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070072 //"h"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 {L"MM", L"%M"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070074 //"M"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 {L"ss", L"%S"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070076 //"s
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 {L"TT", L"%p"},
78//"t"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 {L"tt", L"%p"},
81 {L"h", L"%#I"},
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 {L"tt", L"%P"},
84 {L"h", L"%l"},
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085#endif
86};
87
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088#define UTIL_INT 0
89#define UTIL_DOUBLE 1
90#define UTIL_STRING 2
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092int util::ParstDataType(std::wstring* sFormat) {
93 bool bPercent = FALSE;
94 for (size_t i = 0; i < sFormat->length(); ++i) {
95 wchar_t c = (*sFormat)[i];
96 if (c == L'%') {
97 bPercent = true;
98 continue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070099 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 if (bPercent) {
102 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
103 c == L'u' || c == L'x' || c == L'X') {
104 return UTIL_INT;
105 }
106 if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') {
107 return UTIL_DOUBLE;
108 }
109 if (c == L's' || c == L'S') {
110 // Map s to S since we always deal internally
111 // with wchar_t strings.
112 (*sFormat)[i] = L'S';
113 return UTIL_STRING;
114 }
115 if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' ||
116 CJS_PublicMethods::IsDigit(c)) {
117 continue;
118 }
119 break;
120 }
121 }
122
123 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126FX_BOOL util::printf(IFXJS_Context* cc,
127 const CJS_Parameters& params,
128 CJS_Value& vRet,
129 CFX_WideString& sError) {
130 int iSize = params.size();
131 if (iSize < 1)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700132 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 std::wstring c_ConvChar(params[0].ToCFXWideString().c_str());
134 std::vector<std::wstring> c_strConvers;
135 int iOffset = 0;
136 int iOffend = 0;
137 c_ConvChar.insert(c_ConvChar.begin(), L'S');
138 while (iOffset != -1) {
139 iOffend = c_ConvChar.find(L"%", iOffset + 1);
140 std::wstring strSub;
141 if (iOffend == -1)
142 strSub = c_ConvChar.substr(iOffset);
143 else
144 strSub = c_ConvChar.substr(iOffset, iOffend - iOffset);
145 c_strConvers.push_back(strSub);
146 iOffset = iOffend;
147 }
148
149 std::wstring c_strResult;
150
151 // for(int iIndex = 1;iIndex < params.size();iIndex++)
152 std::wstring c_strFormat;
153 for (int iIndex = 0; iIndex < (int)c_strConvers.size(); iIndex++) {
154 c_strFormat = c_strConvers[iIndex];
155 if (iIndex == 0) {
156 c_strResult = c_strFormat;
157 continue;
158 }
159
160 CFX_WideString strSegment;
161 if (iIndex >= iSize) {
162 c_strResult += c_strFormat;
163 continue;
164 }
165
166 switch (ParstDataType(&c_strFormat)) {
167 case UTIL_INT:
168 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt());
169 break;
170 case UTIL_DOUBLE:
171 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToDouble());
172 break;
173 case UTIL_STRING:
174 strSegment.Format(c_strFormat.c_str(),
175 params[iIndex].ToCFXWideString().c_str());
176 break;
177 default:
178 strSegment.Format(L"%S", c_strFormat.c_str());
179 break;
180 }
181 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1);
182 }
183
184 c_strResult.erase(c_strResult.begin());
185 vRet = c_strResult.c_str();
186 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700187}
188
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189FX_BOOL util::printd(IFXJS_Context* cc,
190 const CJS_Parameters& params,
191 CJS_Value& vRet,
192 CFX_WideString& sError) {
193 v8::Isolate* isolate = GetIsolate(cc);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 int iSize = params.size();
196 if (iSize < 2)
197 return FALSE;
198
199 CJS_Value p1(isolate);
200 p1 = params[0];
201
202 CJS_Value p2 = params[1];
203 CJS_Date jsDate(isolate);
204 if (!p2.ConvertToDate(jsDate)) {
205 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1);
206 return FALSE;
207 }
208
209 if (!jsDate.IsValidDate()) {
210 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2);
211 return FALSE;
212 }
213
214 if (p1.GetType() == VT_number) {
215 int nFormat = p1.ToInt();
216 CFX_WideString swResult;
217
218 switch (nFormat) {
219 case 0:
220 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(),
221 jsDate.GetMonth() + 1, jsDate.GetDay(),
222 jsDate.GetHours(), jsDate.GetMinutes(),
223 jsDate.GetSeconds());
224 break;
225 case 1:
226 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", jsDate.GetYear(),
227 jsDate.GetMonth() + 1, jsDate.GetDay(),
228 jsDate.GetHours(), jsDate.GetMinutes(),
229 jsDate.GetSeconds());
230 break;
231 case 2:
232 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", jsDate.GetYear(),
233 jsDate.GetMonth() + 1, jsDate.GetDay(),
234 jsDate.GetHours(), jsDate.GetMinutes(),
235 jsDate.GetSeconds());
236 break;
237 default:
238 return FALSE;
239 }
240
241 vRet = swResult.c_str();
242 return TRUE;
243 }
244 if (p1.GetType() == VT_string) {
245 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString().c_str();
246
247 bool bXFAPicture = false;
248 if (iSize > 2) {
249 bXFAPicture = params[2].ToBool();
250 }
251
252 if (bXFAPicture) {
253 return FALSE; // currently, it doesn't support XFAPicture.
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700254 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255
Lei Zhangb9c31972015-08-11 14:09:35 -0700256 for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 int iStart = 0;
258 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700259 while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) {
260 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark),
261 fcTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 iStart = iEnd;
263 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700264 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 int iYear, iMonth, iDay, iHour, iMin, iSec;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700267 iYear = jsDate.GetYear();
268 iMonth = jsDate.GetMonth();
269 iDay = jsDate.GetDay();
270 iHour = jsDate.GetHours();
271 iMin = jsDate.GetMinutes();
272 iSec = jsDate.GetSeconds();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700274 struct tm time = {};
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 time.tm_year = iYear - 1900;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700276 time.tm_mon = iMonth;
277 time.tm_mday = iDay;
278 time.tm_hour = iHour;
279 time.tm_min = iMin;
280 time.tm_sec = iSec;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 struct stru_TbConvertAd {
283 const FX_WCHAR* lpszJSMark;
284 int iValue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700285 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 stru_TbConvertAd cTableAd[] = {
288 {L"m", iMonth + 1}, {L"d", iDay},
289 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
290 {L"M", iMin}, {L"s", iSec},
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700291 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292
Lei Zhangb9c31972015-08-11 14:09:35 -0700293 for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 wchar_t tszValue[10];
295 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700296 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
298 (sValue.GetLength() + 1) * sizeof(wchar_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700299
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 int iStart = 0;
301 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700302 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 if (iEnd > 0) {
304 if (cFormat[iEnd - 1] == L'%') {
305 iStart = iEnd + 1;
306 continue;
307 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700308 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700309 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 iStart = iEnd;
311 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700312 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700313
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700314 CFX_WideString strFormat;
315 wchar_t buf[64] = {};
316 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
317 cFormat = buf;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 vRet = cFormat.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700319 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 }
321 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700322}
323
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324void util::printd(const std::wstring& cFormat2,
325 CJS_Date jsDate,
326 bool bXFAPicture,
327 std::wstring& cPurpose) {
328 std::wstring cFormat = cFormat2;
329
330 if (bXFAPicture) {
331 return; // currently, it doesn't support XFAPicture.
332 }
333
Lei Zhangb9c31972015-08-11 14:09:35 -0700334 for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 int iStart = 0;
336 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700337 while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) {
338 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark),
339 fcTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 iStart = iEnd;
341 }
342 }
343
344 int iYear, iMonth, iDay, iHour, iMin, iSec;
345 iYear = jsDate.GetYear();
346 iMonth = jsDate.GetMonth();
347 iDay = jsDate.GetDay();
348 iHour = jsDate.GetHours();
349 iMin = jsDate.GetMinutes();
350 iSec = jsDate.GetSeconds();
351
352 struct tm time = {};
353 time.tm_year = iYear - 1900;
354 time.tm_mon = iMonth;
355 time.tm_mday = iDay;
356 time.tm_hour = iHour;
357 time.tm_min = iMin;
358 time.tm_sec = iSec;
359 // COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec);
360 // CString strFormat = cppTm.Format(cFormat.c_str());
361
362 struct stru_TbConvertAd {
363 const FX_WCHAR* lpszJSMark;
364 int iValue;
365 };
366
367 stru_TbConvertAd cTableAd[] = {
368 {L"m", iMonth + 1}, {L"d", iDay},
369 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
370 {L"M", iMin}, {L"s", iSec},
371 };
372
373 // cFormat = strFormat.GetBuffer(strFormat.GetLength()+1);
Lei Zhangb9c31972015-08-11 14:09:35 -0700374 for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 wchar_t tszValue[10];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700377 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
379 sValue.GetLength() * sizeof(wchar_t));
380
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 int iStart = 0;
382 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700383 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 if (iEnd > 0) {
385 if (cFormat[iEnd - 1] == L'%') {
386 iStart = iEnd + 1;
387 continue;
388 }
389 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700390 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391 iStart = iEnd;
392 }
393 }
394
395 CFX_WideString strFormat;
396 wchar_t buf[64] = {};
397 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
398 cFormat = buf;
399 cPurpose = cFormat;
400}
401
402FX_BOOL util::printx(IFXJS_Context* cc,
403 const CJS_Parameters& params,
404 CJS_Value& vRet,
405 CFX_WideString& sError) {
406 int iSize = params.size();
407 if (iSize < 2)
408 return FALSE;
409 CFX_WideString sFormat = params[0].ToCFXWideString();
410 CFX_WideString sSource = params[1].ToCFXWideString();
411 std::string cFormat = CFX_ByteString::FromUnicode(sFormat).c_str();
412 std::string cSource = CFX_ByteString::FromUnicode(sSource).c_str();
413 std::string cDest;
414 printx(cFormat, cSource, cDest);
415 vRet = cDest.c_str();
416 return TRUE;
417}
418
419void util::printx(const std::string& cFormat,
420 const std::string& cSource2,
421 std::string& cPurpose) {
422 std::string cSource(cSource2);
423 if (!cPurpose.empty())
424 // cPurpose.clear();
425 cPurpose.erase();
426 int itSource = 0;
427 int iSize = cSource.size();
428 for (int iIndex = 0; iIndex < (int)cFormat.size() && itSource < iSize;
429 iIndex++) {
430 char letter = cFormat[iIndex];
431 switch (letter) {
432 case '?':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 cPurpose += cSource[itSource];
434 itSource++;
435 break;
436 case 'X': {
437 while (itSource < iSize) {
438 if ((cSource[itSource] >= '0' && cSource[itSource] <= '9') ||
439 (cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
440 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700441 cPurpose += cSource[itSource];
442 itSource++;
443 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 }
445 itSource++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700446 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 break;
448 } break;
449 case 'A': {
450 while (itSource < iSize) {
451 if ((cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
452 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 cPurpose += cSource[itSource];
454 itSource++;
455 break;
456 }
457 itSource++;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700458 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 break;
460 } break;
461 case '9': {
462 while (itSource < iSize) {
463 if (cSource[itSource] >= '0' && cSource[itSource] <= '9') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464 cPurpose += cSource[itSource];
465 itSource++;
466 break;
467 }
468 itSource++;
469 }
470 break;
471 }
472 case '*': {
473 cPurpose.append(cSource, itSource, iSize - itSource);
474 itSource = iSize - 1;
475 break;
476 }
477 case '\\':
478 break;
479 case '>': {
480 for (std::string::iterator it = cSource.begin(); it != cSource.end();
481 it++) {
482 *it = toupper(*it);
483 }
484 break;
485 }
486 case '<': {
487 for (std::string::iterator it = cSource.begin(); it != cSource.end();
488 it++) {
489 *it = tolower(*it);
490 }
491 break;
492 }
493 case '=':
494 break;
495 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 cPurpose += letter;
497 break;
498 }
499 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700500}
501
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502FX_BOOL util::scand(IFXJS_Context* cc,
503 const CJS_Parameters& params,
504 CJS_Value& vRet,
505 CFX_WideString& sError) {
506 v8::Isolate* isolate = GetIsolate(cc);
507 int iSize = params.size();
508 if (iSize < 2)
509 return FALSE;
510
511 CFX_WideString sFormat = params[0].ToCFXWideString();
512 CFX_WideString sDate = params[1].ToCFXWideString();
513 double dDate = JS_GetDateTime();
514 if (sDate.GetLength() > 0) {
515 FX_BOOL bWrongFormat = FALSE;
516 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, bWrongFormat);
517 }
518
519 if (!JS_PortIsNan(dDate)) {
520 CJS_Date date(isolate, dDate);
521 vRet = date;
522 } else {
523 vRet.SetNull();
524 }
525
526 return TRUE;
527}
528
529int64_t FX_atoi64(const char* nptr) {
530 int c; /* current char */
531 int64_t total; /* current total */
532 int sign; /* if '-', then negative, otherwise positive */
533
534 /* skip whitespace */
535 while (isspace((int)(unsigned char)*nptr))
536 ++nptr;
537
538 c = (int)(unsigned char)*nptr++;
539 sign = c; /* save sign indication */
540 if (c == '-' || c == '+')
541 c = (int)(unsigned char)*nptr++; /* skip sign */
542
543 total = 0;
544
545 while (isdigit(c)) {
546 total = 10 * total + (c - '0'); /* accumulate digit */
547 c = (int)(unsigned char)*nptr++; /* get next char */
548 }
549
550 return sign == '-' ? -total : total;
551}
552
553FX_BOOL util::byteToChar(IFXJS_Context* cc,
554 const CJS_Parameters& params,
555 CJS_Value& vRet,
556 CFX_WideString& sError) {
557 int iSize = params.size();
558 if (iSize == 0)
559 return FALSE;
560 int nByte = params[0].ToInt();
561 unsigned char cByte = (unsigned char)nByte;
562 CFX_WideString csValue;
563 csValue.Format(L"%c", cByte);
564 vRet = csValue.c_str();
565 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700566}