blob: c021ec9e9552ea8a5854bfde47b893a6c00a3da3 [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
Tom Sepez37458412015-10-06 11:33:46 -07007#include "util.h"
8
Tom Sepez37458412015-10-06 11:33:46 -07009#include "JS_Context.h"
10#include "JS_Define.h"
11#include "JS_EventHandler.h"
12#include "JS_Object.h"
13#include "JS_Runtime.h"
14#include "JS_Value.h"
15#include "PublicMethods.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080016#include "fpdfsdk/include/javascript/IJavaScript.h"
Tom Sepez37458412015-10-06 11:33:46 -070017#include "resource.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018
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
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023BEGIN_JS_STATIC_CONST(CJS_Util)
24END_JS_STATIC_CONST()
25
26BEGIN_JS_STATIC_PROP(CJS_Util)
27END_JS_STATIC_PROP()
28
29BEGIN_JS_STATIC_METHOD(CJS_Util)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030JS_STATIC_METHOD_ENTRY(printd)
31JS_STATIC_METHOD_ENTRY(printf)
32JS_STATIC_METHOD_ENTRY(printx)
33JS_STATIC_METHOD_ENTRY(scand)
34JS_STATIC_METHOD_ENTRY(byteToChar)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035END_JS_STATIC_METHOD()
36
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037IMPLEMENT_JS_CLASS(CJS_Util, util)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040
Lei Zhang2b1a2d52015-08-14 22:16:22 -070041util::~util() {
42}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044struct stru_TbConvert {
45 const FX_WCHAR* lpszJSMark;
46 const FX_WCHAR* lpszCppMark;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047};
48
49const stru_TbConvert fcTable[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 {L"mmmm", L"%B"},
51 {L"mmm", L"%b"},
52 {L"mm", L"%m"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070053 //"m"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 {L"dddd", L"%A"},
55 {L"ddd", L"%a"},
56 {L"dd", L"%d"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070057 //"d", "%w",
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 {L"yyyy", L"%Y"},
59 {L"yy", L"%y"},
60 {L"HH", L"%H"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070061 //"H"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 {L"hh", L"%I"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070063 //"h"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 {L"MM", L"%M"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070065 //"M"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 {L"ss", L"%S"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070067 //"s
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 {L"TT", L"%p"},
69//"t"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 {L"tt", L"%p"},
72 {L"h", L"%#I"},
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070073#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 {L"tt", L"%P"},
75 {L"h", L"%l"},
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076#endif
77};
78
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079#define UTIL_INT 0
80#define UTIL_DOUBLE 1
81#define UTIL_STRING 2
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083int util::ParstDataType(std::wstring* sFormat) {
84 bool bPercent = FALSE;
85 for (size_t i = 0; i < sFormat->length(); ++i) {
86 wchar_t c = (*sFormat)[i];
87 if (c == L'%') {
88 bPercent = true;
89 continue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070090 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 if (bPercent) {
93 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
94 c == L'u' || c == L'x' || c == L'X') {
95 return UTIL_INT;
96 }
97 if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') {
98 return UTIL_DOUBLE;
99 }
100 if (c == L's' || c == L'S') {
101 // Map s to S since we always deal internally
102 // with wchar_t strings.
103 (*sFormat)[i] = L'S';
104 return UTIL_STRING;
105 }
106 if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' ||
107 CJS_PublicMethods::IsDigit(c)) {
108 continue;
109 }
110 break;
111 }
112 }
113
114 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115}
116
Tom Sepezba038bc2015-10-08 12:03:00 -0700117FX_BOOL util::printf(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800118 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 CJS_Value& vRet,
120 CFX_WideString& sError) {
121 int iSize = params.size();
122 if (iSize < 1)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700123 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 std::wstring c_ConvChar(params[0].ToCFXWideString().c_str());
125 std::vector<std::wstring> c_strConvers;
126 int iOffset = 0;
127 int iOffend = 0;
128 c_ConvChar.insert(c_ConvChar.begin(), L'S');
129 while (iOffset != -1) {
130 iOffend = c_ConvChar.find(L"%", iOffset + 1);
131 std::wstring strSub;
132 if (iOffend == -1)
133 strSub = c_ConvChar.substr(iOffset);
134 else
135 strSub = c_ConvChar.substr(iOffset, iOffend - iOffset);
136 c_strConvers.push_back(strSub);
137 iOffset = iOffend;
138 }
139
140 std::wstring c_strResult;
141
142 // for(int iIndex = 1;iIndex < params.size();iIndex++)
143 std::wstring c_strFormat;
144 for (int iIndex = 0; iIndex < (int)c_strConvers.size(); iIndex++) {
145 c_strFormat = c_strConvers[iIndex];
146 if (iIndex == 0) {
147 c_strResult = c_strFormat;
148 continue;
149 }
150
151 CFX_WideString strSegment;
152 if (iIndex >= iSize) {
153 c_strResult += c_strFormat;
154 continue;
155 }
156
157 switch (ParstDataType(&c_strFormat)) {
158 case UTIL_INT:
159 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt());
160 break;
161 case UTIL_DOUBLE:
162 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToDouble());
163 break;
164 case UTIL_STRING:
165 strSegment.Format(c_strFormat.c_str(),
166 params[iIndex].ToCFXWideString().c_str());
167 break;
168 default:
169 strSegment.Format(L"%S", c_strFormat.c_str());
170 break;
171 }
172 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1);
173 }
174
175 c_strResult.erase(c_strResult.begin());
176 vRet = c_strResult.c_str();
177 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178}
179
Tom Sepezba038bc2015-10-08 12:03:00 -0700180FX_BOOL util::printd(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800181 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 CJS_Value& vRet,
183 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 int iSize = params.size();
185 if (iSize < 2)
186 return FALSE;
187
Tom Sepez67fd5df2015-10-08 12:24:19 -0700188 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
189 CJS_Value p1(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 p1 = params[0];
191
192 CJS_Value p2 = params[1];
Tom Sepez67fd5df2015-10-08 12:24:19 -0700193 CJS_Date jsDate(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194 if (!p2.ConvertToDate(jsDate)) {
195 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1);
196 return FALSE;
197 }
198
199 if (!jsDate.IsValidDate()) {
200 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2);
201 return FALSE;
202 }
203
Tom Sepez39bfe122015-09-17 15:25:23 -0700204 if (p1.GetType() == CJS_Value::VT_number) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 int nFormat = p1.ToInt();
206 CFX_WideString swResult;
207
208 switch (nFormat) {
209 case 0:
210 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(),
211 jsDate.GetMonth() + 1, jsDate.GetDay(),
212 jsDate.GetHours(), jsDate.GetMinutes(),
213 jsDate.GetSeconds());
214 break;
215 case 1:
216 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", jsDate.GetYear(),
217 jsDate.GetMonth() + 1, jsDate.GetDay(),
218 jsDate.GetHours(), jsDate.GetMinutes(),
219 jsDate.GetSeconds());
220 break;
221 case 2:
222 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", jsDate.GetYear(),
223 jsDate.GetMonth() + 1, jsDate.GetDay(),
224 jsDate.GetHours(), jsDate.GetMinutes(),
225 jsDate.GetSeconds());
226 break;
227 default:
228 return FALSE;
229 }
230
231 vRet = swResult.c_str();
232 return TRUE;
233 }
Tom Sepez39bfe122015-09-17 15:25:23 -0700234 if (p1.GetType() == CJS_Value::VT_string) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString().c_str();
236
237 bool bXFAPicture = false;
238 if (iSize > 2) {
239 bXFAPicture = params[2].ToBool();
240 }
241
242 if (bXFAPicture) {
243 return FALSE; // currently, it doesn't support XFAPicture.
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700244 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700245
Lei Zhangb9c31972015-08-11 14:09:35 -0700246 for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 int iStart = 0;
248 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700249 while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) {
250 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark),
251 fcTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 iStart = iEnd;
253 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700254 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 int iYear, iMonth, iDay, iHour, iMin, iSec;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700257 iYear = jsDate.GetYear();
258 iMonth = jsDate.GetMonth();
259 iDay = jsDate.GetDay();
260 iHour = jsDate.GetHours();
261 iMin = jsDate.GetMinutes();
262 iSec = jsDate.GetSeconds();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700264 struct tm time = {};
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 time.tm_year = iYear - 1900;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700266 time.tm_mon = iMonth;
267 time.tm_mday = iDay;
268 time.tm_hour = iHour;
269 time.tm_min = iMin;
270 time.tm_sec = iSec;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 struct stru_TbConvertAd {
273 const FX_WCHAR* lpszJSMark;
274 int iValue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700275 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700276
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 stru_TbConvertAd cTableAd[] = {
278 {L"m", iMonth + 1}, {L"d", iDay},
279 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
280 {L"M", iMin}, {L"s", iSec},
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700281 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282
Lei Zhangb9c31972015-08-11 14:09:35 -0700283 for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 wchar_t tszValue[10];
285 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700286 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
288 (sValue.GetLength() + 1) * sizeof(wchar_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700289
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 int iStart = 0;
291 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700292 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 if (iEnd > 0) {
294 if (cFormat[iEnd - 1] == L'%') {
295 iStart = iEnd + 1;
296 continue;
297 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700298 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700299 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 iStart = iEnd;
301 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700302 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700303
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700304 CFX_WideString strFormat;
305 wchar_t buf[64] = {};
306 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
307 cFormat = buf;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 vRet = cFormat.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700309 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 }
311 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700312}
313
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314void util::printd(const std::wstring& cFormat2,
315 CJS_Date jsDate,
316 bool bXFAPicture,
317 std::wstring& cPurpose) {
318 std::wstring cFormat = cFormat2;
319
320 if (bXFAPicture) {
321 return; // currently, it doesn't support XFAPicture.
322 }
323
Lei Zhangb9c31972015-08-11 14:09:35 -0700324 for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 int iStart = 0;
326 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700327 while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) {
328 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark),
329 fcTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 iStart = iEnd;
331 }
332 }
333
334 int iYear, iMonth, iDay, iHour, iMin, iSec;
335 iYear = jsDate.GetYear();
336 iMonth = jsDate.GetMonth();
337 iDay = jsDate.GetDay();
338 iHour = jsDate.GetHours();
339 iMin = jsDate.GetMinutes();
340 iSec = jsDate.GetSeconds();
341
342 struct tm time = {};
343 time.tm_year = iYear - 1900;
344 time.tm_mon = iMonth;
345 time.tm_mday = iDay;
346 time.tm_hour = iHour;
347 time.tm_min = iMin;
348 time.tm_sec = iSec;
349 // COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec);
350 // CString strFormat = cppTm.Format(cFormat.c_str());
351
352 struct stru_TbConvertAd {
353 const FX_WCHAR* lpszJSMark;
354 int iValue;
355 };
356
357 stru_TbConvertAd cTableAd[] = {
358 {L"m", iMonth + 1}, {L"d", iDay},
359 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
360 {L"M", iMin}, {L"s", iSec},
361 };
362
363 // cFormat = strFormat.GetBuffer(strFormat.GetLength()+1);
Lei Zhangb9c31972015-08-11 14:09:35 -0700364 for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 wchar_t tszValue[10];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700367 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
369 sValue.GetLength() * sizeof(wchar_t));
370
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 int iStart = 0;
372 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700373 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 if (iEnd > 0) {
375 if (cFormat[iEnd - 1] == L'%') {
376 iStart = iEnd + 1;
377 continue;
378 }
379 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700380 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 iStart = iEnd;
382 }
383 }
384
385 CFX_WideString strFormat;
386 wchar_t buf[64] = {};
387 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
388 cFormat = buf;
389 cPurpose = cFormat;
390}
391
Tom Sepezba038bc2015-10-08 12:03:00 -0700392FX_BOOL util::printx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800393 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 CJS_Value& vRet,
395 CFX_WideString& sError) {
396 int iSize = params.size();
397 if (iSize < 2)
398 return FALSE;
399 CFX_WideString sFormat = params[0].ToCFXWideString();
400 CFX_WideString sSource = params[1].ToCFXWideString();
401 std::string cFormat = CFX_ByteString::FromUnicode(sFormat).c_str();
402 std::string cSource = CFX_ByteString::FromUnicode(sSource).c_str();
403 std::string cDest;
404 printx(cFormat, cSource, cDest);
405 vRet = cDest.c_str();
406 return TRUE;
407}
408
409void util::printx(const std::string& cFormat,
410 const std::string& cSource2,
411 std::string& cPurpose) {
412 std::string cSource(cSource2);
413 if (!cPurpose.empty())
414 // cPurpose.clear();
415 cPurpose.erase();
416 int itSource = 0;
417 int iSize = cSource.size();
418 for (int iIndex = 0; iIndex < (int)cFormat.size() && itSource < iSize;
419 iIndex++) {
420 char letter = cFormat[iIndex];
421 switch (letter) {
422 case '?':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423 cPurpose += cSource[itSource];
424 itSource++;
425 break;
426 case 'X': {
427 while (itSource < iSize) {
428 if ((cSource[itSource] >= '0' && cSource[itSource] <= '9') ||
429 (cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
430 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700431 cPurpose += cSource[itSource];
432 itSource++;
433 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434 }
435 itSource++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700436 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 break;
438 } break;
439 case 'A': {
440 while (itSource < iSize) {
441 if ((cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
442 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443 cPurpose += cSource[itSource];
444 itSource++;
445 break;
446 }
447 itSource++;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700448 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449 break;
450 } break;
451 case '9': {
452 while (itSource < iSize) {
453 if (cSource[itSource] >= '0' && cSource[itSource] <= '9') {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 cPurpose += cSource[itSource];
455 itSource++;
456 break;
457 }
458 itSource++;
459 }
460 break;
461 }
462 case '*': {
463 cPurpose.append(cSource, itSource, iSize - itSource);
464 itSource = iSize - 1;
465 break;
466 }
467 case '\\':
468 break;
469 case '>': {
470 for (std::string::iterator it = cSource.begin(); it != cSource.end();
471 it++) {
472 *it = toupper(*it);
473 }
474 break;
475 }
476 case '<': {
477 for (std::string::iterator it = cSource.begin(); it != cSource.end();
478 it++) {
479 *it = tolower(*it);
480 }
481 break;
482 }
483 case '=':
484 break;
485 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486 cPurpose += letter;
487 break;
488 }
489 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700490}
491
Tom Sepezba038bc2015-10-08 12:03:00 -0700492FX_BOOL util::scand(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800493 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494 CJS_Value& vRet,
495 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 int iSize = params.size();
497 if (iSize < 2)
498 return FALSE;
499
500 CFX_WideString sFormat = params[0].ToCFXWideString();
501 CFX_WideString sDate = params[1].ToCFXWideString();
502 double dDate = JS_GetDateTime();
503 if (sDate.GetLength() > 0) {
504 FX_BOOL bWrongFormat = FALSE;
505 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, bWrongFormat);
506 }
507
508 if (!JS_PortIsNan(dDate)) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700509 vRet = CJS_Date(CJS_Runtime::FromContext(cc), dDate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510 } else {
511 vRet.SetNull();
512 }
513
514 return TRUE;
515}
516
517int64_t FX_atoi64(const char* nptr) {
518 int c; /* current char */
519 int64_t total; /* current total */
520 int sign; /* if '-', then negative, otherwise positive */
521
522 /* skip whitespace */
523 while (isspace((int)(unsigned char)*nptr))
524 ++nptr;
525
526 c = (int)(unsigned char)*nptr++;
527 sign = c; /* save sign indication */
528 if (c == '-' || c == '+')
529 c = (int)(unsigned char)*nptr++; /* skip sign */
530
531 total = 0;
532
533 while (isdigit(c)) {
534 total = 10 * total + (c - '0'); /* accumulate digit */
535 c = (int)(unsigned char)*nptr++; /* get next char */
536 }
537
538 return sign == '-' ? -total : total;
539}
540
Tom Sepezba038bc2015-10-08 12:03:00 -0700541FX_BOOL util::byteToChar(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800542 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543 CJS_Value& vRet,
544 CFX_WideString& sError) {
545 int iSize = params.size();
546 if (iSize == 0)
547 return FALSE;
548 int nByte = params[0].ToInt();
549 unsigned char cByte = (unsigned char)nByte;
550 CFX_WideString csValue;
551 csValue.Format(L"%c", cByte);
552 vRet = csValue.c_str();
553 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700554}