blob: 30df53ea7292ee4a5010fd070179d7301a2ceac9 [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"
Dan Sinclair10cfea12015-11-16 13:09:00 -050016#include "core/include/fxcrt/fx_ext.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080017#include "fpdfsdk/include/javascript/IJavaScript.h"
Tom Sepez37458412015-10-06 11:33:46 -070018#include "resource.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020#if _FX_OS_ == _FX_ANDROID_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021#include <ctype.h>
22#endif
23
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024BEGIN_JS_STATIC_CONST(CJS_Util)
25END_JS_STATIC_CONST()
26
27BEGIN_JS_STATIC_PROP(CJS_Util)
28END_JS_STATIC_PROP()
29
30BEGIN_JS_STATIC_METHOD(CJS_Util)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031JS_STATIC_METHOD_ENTRY(printd)
32JS_STATIC_METHOD_ENTRY(printf)
33JS_STATIC_METHOD_ENTRY(printx)
34JS_STATIC_METHOD_ENTRY(scand)
35JS_STATIC_METHOD_ENTRY(byteToChar)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036END_JS_STATIC_METHOD()
37
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038IMPLEMENT_JS_CLASS(CJS_Util, util)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
Lei Zhang2b1a2d52015-08-14 22:16:22 -070042util::~util() {
43}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045struct stru_TbConvert {
46 const FX_WCHAR* lpszJSMark;
47 const FX_WCHAR* lpszCppMark;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048};
49
50const stru_TbConvert fcTable[] = {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 {L"mmmm", L"%B"},
52 {L"mmm", L"%b"},
53 {L"mm", L"%m"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070054 //"m"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 {L"dddd", L"%A"},
56 {L"ddd", L"%a"},
57 {L"dd", L"%d"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070058 //"d", "%w",
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 {L"yyyy", L"%Y"},
60 {L"yy", L"%y"},
61 {L"HH", L"%H"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070062 //"H"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 {L"hh", L"%I"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070064 //"h"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 {L"MM", L"%M"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070066 //"M"
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 {L"ss", L"%S"},
Tom Sepez2f2ffec2015-07-23 14:42:09 -070068 //"s
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 {L"TT", L"%p"},
70//"t"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070071#if defined(_WIN32)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 {L"tt", L"%p"},
73 {L"h", L"%#I"},
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 {L"tt", L"%P"},
76 {L"h", L"%l"},
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077#endif
78};
79
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080#define UTIL_INT 0
81#define UTIL_DOUBLE 1
82#define UTIL_STRING 2
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084int util::ParstDataType(std::wstring* sFormat) {
85 bool bPercent = FALSE;
86 for (size_t i = 0; i < sFormat->length(); ++i) {
87 wchar_t c = (*sFormat)[i];
88 if (c == L'%') {
89 bPercent = true;
90 continue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070091 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 if (bPercent) {
94 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' ||
95 c == L'u' || c == L'x' || c == L'X') {
96 return UTIL_INT;
97 }
98 if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') {
99 return UTIL_DOUBLE;
100 }
101 if (c == L's' || c == L'S') {
102 // Map s to S since we always deal internally
103 // with wchar_t strings.
104 (*sFormat)[i] = L'S';
105 return UTIL_STRING;
106 }
107 if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' ||
108 CJS_PublicMethods::IsDigit(c)) {
109 continue;
110 }
111 break;
112 }
113 }
114
115 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
117
Tom Sepezba038bc2015-10-08 12:03:00 -0700118FX_BOOL util::printf(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800119 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 CJS_Value& vRet,
121 CFX_WideString& sError) {
122 int iSize = params.size();
123 if (iSize < 1)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700124 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 std::wstring c_ConvChar(params[0].ToCFXWideString().c_str());
126 std::vector<std::wstring> c_strConvers;
127 int iOffset = 0;
128 int iOffend = 0;
129 c_ConvChar.insert(c_ConvChar.begin(), L'S');
130 while (iOffset != -1) {
131 iOffend = c_ConvChar.find(L"%", iOffset + 1);
132 std::wstring strSub;
133 if (iOffend == -1)
134 strSub = c_ConvChar.substr(iOffset);
135 else
136 strSub = c_ConvChar.substr(iOffset, iOffend - iOffset);
137 c_strConvers.push_back(strSub);
138 iOffset = iOffend;
139 }
140
141 std::wstring c_strResult;
142
143 // for(int iIndex = 1;iIndex < params.size();iIndex++)
144 std::wstring c_strFormat;
145 for (int iIndex = 0; iIndex < (int)c_strConvers.size(); iIndex++) {
146 c_strFormat = c_strConvers[iIndex];
147 if (iIndex == 0) {
148 c_strResult = c_strFormat;
149 continue;
150 }
151
152 CFX_WideString strSegment;
153 if (iIndex >= iSize) {
154 c_strResult += c_strFormat;
155 continue;
156 }
157
158 switch (ParstDataType(&c_strFormat)) {
159 case UTIL_INT:
160 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt());
161 break;
162 case UTIL_DOUBLE:
163 strSegment.Format(c_strFormat.c_str(), params[iIndex].ToDouble());
164 break;
165 case UTIL_STRING:
166 strSegment.Format(c_strFormat.c_str(),
167 params[iIndex].ToCFXWideString().c_str());
168 break;
169 default:
170 strSegment.Format(L"%S", c_strFormat.c_str());
171 break;
172 }
173 c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1);
174 }
175
176 c_strResult.erase(c_strResult.begin());
177 vRet = c_strResult.c_str();
178 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179}
180
Tom Sepezba038bc2015-10-08 12:03:00 -0700181FX_BOOL util::printd(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800182 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 CJS_Value& vRet,
184 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 int iSize = params.size();
186 if (iSize < 2)
187 return FALSE;
188
Tom Sepez67fd5df2015-10-08 12:24:19 -0700189 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
190 CJS_Value p1(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 p1 = params[0];
192
193 CJS_Value p2 = params[1];
Tom Sepez67fd5df2015-10-08 12:24:19 -0700194 CJS_Date jsDate(pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 if (!p2.ConvertToDate(jsDate)) {
196 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1);
197 return FALSE;
198 }
199
200 if (!jsDate.IsValidDate()) {
201 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2);
202 return FALSE;
203 }
204
Tom Sepez39bfe122015-09-17 15:25:23 -0700205 if (p1.GetType() == CJS_Value::VT_number) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 int nFormat = p1.ToInt();
207 CFX_WideString swResult;
208
209 switch (nFormat) {
210 case 0:
211 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(),
212 jsDate.GetMonth() + 1, jsDate.GetDay(),
213 jsDate.GetHours(), jsDate.GetMinutes(),
214 jsDate.GetSeconds());
215 break;
216 case 1:
217 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", jsDate.GetYear(),
218 jsDate.GetMonth() + 1, jsDate.GetDay(),
219 jsDate.GetHours(), jsDate.GetMinutes(),
220 jsDate.GetSeconds());
221 break;
222 case 2:
223 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", jsDate.GetYear(),
224 jsDate.GetMonth() + 1, jsDate.GetDay(),
225 jsDate.GetHours(), jsDate.GetMinutes(),
226 jsDate.GetSeconds());
227 break;
228 default:
229 return FALSE;
230 }
231
232 vRet = swResult.c_str();
233 return TRUE;
234 }
Tom Sepez39bfe122015-09-17 15:25:23 -0700235 if (p1.GetType() == CJS_Value::VT_string) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 std::basic_string<wchar_t> cFormat = p1.ToCFXWideString().c_str();
237
238 bool bXFAPicture = false;
239 if (iSize > 2) {
240 bXFAPicture = params[2].ToBool();
241 }
242
243 if (bXFAPicture) {
244 return FALSE; // currently, it doesn't support XFAPicture.
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700245 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246
Lei Zhangb9c31972015-08-11 14:09:35 -0700247 for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 int iStart = 0;
249 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700250 while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) {
251 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark),
252 fcTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 iStart = iEnd;
254 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700255 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 int iYear, iMonth, iDay, iHour, iMin, iSec;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700258 iYear = jsDate.GetYear();
259 iMonth = jsDate.GetMonth();
260 iDay = jsDate.GetDay();
261 iHour = jsDate.GetHours();
262 iMin = jsDate.GetMinutes();
263 iSec = jsDate.GetSeconds();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700265 struct tm time = {};
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 time.tm_year = iYear - 1900;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700267 time.tm_mon = iMonth;
268 time.tm_mday = iDay;
269 time.tm_hour = iHour;
270 time.tm_min = iMin;
271 time.tm_sec = iSec;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 struct stru_TbConvertAd {
274 const FX_WCHAR* lpszJSMark;
275 int iValue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700276 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 stru_TbConvertAd cTableAd[] = {
279 {L"m", iMonth + 1}, {L"d", iDay},
280 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
281 {L"M", iMin}, {L"s", iSec},
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700282 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283
Lei Zhangb9c31972015-08-11 14:09:35 -0700284 for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 wchar_t tszValue[10];
286 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700287 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
289 (sValue.GetLength() + 1) * sizeof(wchar_t));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700290
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 int iStart = 0;
292 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700293 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 if (iEnd > 0) {
295 if (cFormat[iEnd - 1] == L'%') {
296 iStart = iEnd + 1;
297 continue;
298 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700299 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700300 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 iStart = iEnd;
302 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700303 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700304
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700305 CFX_WideString strFormat;
306 wchar_t buf[64] = {};
307 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
308 cFormat = buf;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309 vRet = cFormat.c_str();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700310 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 }
312 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700313}
314
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315void util::printd(const std::wstring& cFormat2,
316 CJS_Date jsDate,
317 bool bXFAPicture,
318 std::wstring& cPurpose) {
319 std::wstring cFormat = cFormat2;
320
321 if (bXFAPicture) {
322 return; // currently, it doesn't support XFAPicture.
323 }
324
Lei Zhangb9c31972015-08-11 14:09:35 -0700325 for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 int iStart = 0;
327 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700328 while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) {
329 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark),
330 fcTable[i].lpszCppMark);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 iStart = iEnd;
332 }
333 }
334
335 int iYear, iMonth, iDay, iHour, iMin, iSec;
336 iYear = jsDate.GetYear();
337 iMonth = jsDate.GetMonth();
338 iDay = jsDate.GetDay();
339 iHour = jsDate.GetHours();
340 iMin = jsDate.GetMinutes();
341 iSec = jsDate.GetSeconds();
342
343 struct tm time = {};
344 time.tm_year = iYear - 1900;
345 time.tm_mon = iMonth;
346 time.tm_mday = iDay;
347 time.tm_hour = iHour;
348 time.tm_min = iMin;
349 time.tm_sec = iSec;
350 // COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec);
351 // CString strFormat = cppTm.Format(cFormat.c_str());
352
353 struct stru_TbConvertAd {
354 const FX_WCHAR* lpszJSMark;
355 int iValue;
356 };
357
358 stru_TbConvertAd cTableAd[] = {
359 {L"m", iMonth + 1}, {L"d", iDay},
360 {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour},
361 {L"M", iMin}, {L"s", iSec},
362 };
363
364 // cFormat = strFormat.GetBuffer(strFormat.GetLength()+1);
Lei Zhangb9c31972015-08-11 14:09:35 -0700365 for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 wchar_t tszValue[10];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 CFX_WideString sValue;
Lei Zhangb9c31972015-08-11 14:09:35 -0700368 sValue.Format(L"%d", cTableAd[i].iValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1),
370 sValue.GetLength() * sizeof(wchar_t));
371
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372 int iStart = 0;
373 int iEnd;
Lei Zhangb9c31972015-08-11 14:09:35 -0700374 while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 if (iEnd > 0) {
376 if (cFormat[iEnd - 1] == L'%') {
377 iStart = iEnd + 1;
378 continue;
379 }
380 }
Lei Zhangb9c31972015-08-11 14:09:35 -0700381 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 iStart = iEnd;
383 }
384 }
385
386 CFX_WideString strFormat;
387 wchar_t buf[64] = {};
388 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
389 cFormat = buf;
390 cPurpose = cFormat;
391}
392
Tom Sepezba038bc2015-10-08 12:03:00 -0700393FX_BOOL util::printx(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800394 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 CJS_Value& vRet,
396 CFX_WideString& sError) {
397 int iSize = params.size();
398 if (iSize < 2)
399 return FALSE;
400 CFX_WideString sFormat = params[0].ToCFXWideString();
401 CFX_WideString sSource = params[1].ToCFXWideString();
402 std::string cFormat = CFX_ByteString::FromUnicode(sFormat).c_str();
403 std::string cSource = CFX_ByteString::FromUnicode(sSource).c_str();
404 std::string cDest;
405 printx(cFormat, cSource, cDest);
406 vRet = cDest.c_str();
407 return TRUE;
408}
409
410void util::printx(const std::string& cFormat,
411 const std::string& cSource2,
412 std::string& cPurpose) {
413 std::string cSource(cSource2);
414 if (!cPurpose.empty())
415 // cPurpose.clear();
416 cPurpose.erase();
417 int itSource = 0;
418 int iSize = cSource.size();
419 for (int iIndex = 0; iIndex < (int)cFormat.size() && itSource < iSize;
420 iIndex++) {
421 char letter = cFormat[iIndex];
422 switch (letter) {
423 case '?':
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424 cPurpose += cSource[itSource];
425 itSource++;
426 break;
427 case 'X': {
428 while (itSource < iSize) {
Dan Sinclair10cfea12015-11-16 13:09:00 -0500429 if (std::isdigit(cSource[itSource]) ||
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430 (cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
431 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700432 cPurpose += cSource[itSource];
433 itSource++;
434 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 }
436 itSource++;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700437 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438 break;
439 } break;
440 case 'A': {
441 while (itSource < iSize) {
442 if ((cSource[itSource] >= 'a' && cSource[itSource] <= 'z') ||
443 (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 cPurpose += cSource[itSource];
445 itSource++;
446 break;
447 }
448 itSource++;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700449 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 break;
451 } break;
452 case '9': {
453 while (itSource < iSize) {
Dan Sinclair10cfea12015-11-16 13:09:00 -0500454 if (std::isdigit(cSource[itSource])) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 cPurpose += cSource[itSource];
456 itSource++;
457 break;
458 }
459 itSource++;
460 }
461 break;
462 }
463 case '*': {
464 cPurpose.append(cSource, itSource, iSize - itSource);
465 itSource = iSize - 1;
466 break;
467 }
468 case '\\':
469 break;
470 case '>': {
471 for (std::string::iterator it = cSource.begin(); it != cSource.end();
472 it++) {
473 *it = toupper(*it);
474 }
475 break;
476 }
477 case '<': {
478 for (std::string::iterator it = cSource.begin(); it != cSource.end();
479 it++) {
480 *it = tolower(*it);
481 }
482 break;
483 }
484 case '=':
485 break;
486 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487 cPurpose += letter;
488 break;
489 }
490 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700491}
492
Tom Sepezba038bc2015-10-08 12:03:00 -0700493FX_BOOL util::scand(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800494 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 CJS_Value& vRet,
496 CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497 int iSize = params.size();
498 if (iSize < 2)
499 return FALSE;
500
501 CFX_WideString sFormat = params[0].ToCFXWideString();
502 CFX_WideString sDate = params[1].ToCFXWideString();
503 double dDate = JS_GetDateTime();
504 if (sDate.GetLength() > 0) {
505 FX_BOOL bWrongFormat = FALSE;
506 dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, bWrongFormat);
507 }
508
509 if (!JS_PortIsNan(dDate)) {
Tom Sepez67fd5df2015-10-08 12:24:19 -0700510 vRet = CJS_Date(CJS_Runtime::FromContext(cc), dDate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700511 } else {
512 vRet.SetNull();
513 }
514
515 return TRUE;
516}
517
518int64_t FX_atoi64(const char* nptr) {
519 int c; /* current char */
520 int64_t total; /* current total */
521 int sign; /* if '-', then negative, otherwise positive */
522
523 /* skip whitespace */
524 while (isspace((int)(unsigned char)*nptr))
525 ++nptr;
526
527 c = (int)(unsigned char)*nptr++;
528 sign = c; /* save sign indication */
529 if (c == '-' || c == '+')
530 c = (int)(unsigned char)*nptr++; /* skip sign */
531
532 total = 0;
533
534 while (isdigit(c)) {
Dan Sinclair10cfea12015-11-16 13:09:00 -0500535 total = 10 * total + FXSYS_toDecimalDigit(c); /* accumulate digit */
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536 c = (int)(unsigned char)*nptr++; /* get next char */
537 }
538
539 return sign == '-' ? -total : total;
540}
541
Tom Sepezba038bc2015-10-08 12:03:00 -0700542FX_BOOL util::byteToChar(IJS_Context* cc,
Lei Zhang945fdb72015-11-11 10:18:16 -0800543 const std::vector<CJS_Value>& params,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700544 CJS_Value& vRet,
545 CFX_WideString& sError) {
546 int iSize = params.size();
547 if (iSize == 0)
548 return FALSE;
549 int nByte = params[0].ToInt();
550 unsigned char cByte = (unsigned char)nByte;
551 CFX_WideString csValue;
552 csValue.Format(L"%c", cByte);
553 vRet = csValue.c_str();
554 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700555}