John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // 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 Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 7 | #include "fpdfsdk/javascript/util.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 8 | |
Lei Zhang | 6b37a5f | 2015-12-25 00:20:59 -0800 | [diff] [blame] | 9 | #include <time.h> |
| 10 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 11 | #include <algorithm> |
Lei Zhang | e247ec4 | 2017-04-20 21:41:36 -0700 | [diff] [blame] | 12 | #include <cwctype> |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
Dan Sinclair | cfb1944 | 2017-04-20 13:13:04 -0400 | [diff] [blame] | 16 | #include "core/fxcrt/fx_extension.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 17 | #include "fpdfsdk/javascript/JS_Define.h" |
| 18 | #include "fpdfsdk/javascript/JS_EventHandler.h" |
| 19 | #include "fpdfsdk/javascript/JS_Object.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 20 | #include "fpdfsdk/javascript/JS_Value.h" |
| 21 | #include "fpdfsdk/javascript/PublicMethods.h" |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 22 | #include "fpdfsdk/javascript/cjs_event_context.h" |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame] | 23 | #include "fpdfsdk/javascript/cjs_runtime.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 24 | #include "fpdfsdk/javascript/resource.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 25 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 26 | #if _FX_OS_ == _FX_ANDROID_ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 27 | #include <ctype.h> |
| 28 | #endif |
| 29 | |
Tom Sepez | 04557b8 | 2017-02-16 09:43:10 -0800 | [diff] [blame] | 30 | JSConstSpec CJS_Util::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 31 | |
Tom Sepez | 04557b8 | 2017-02-16 09:43:10 -0800 | [diff] [blame] | 32 | JSPropertySpec CJS_Util::PropertySpecs[] = {{0, 0, 0}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 33 | |
Tom Sepez | 04557b8 | 2017-02-16 09:43:10 -0800 | [diff] [blame] | 34 | JSMethodSpec CJS_Util::MethodSpecs[] = { |
Tom Sepez | 9b99b63 | 2017-02-21 15:05:57 -0800 | [diff] [blame] | 35 | {"printd", printd_static}, {"printf", printf_static}, |
| 36 | {"printx", printx_static}, {"scand", scand_static}, |
| 37 | {"byteToChar", byteToChar_static}, {0, 0}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 38 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 39 | IMPLEMENT_JS_CLASS(CJS_Util, util) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 40 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 41 | #define UTIL_INT 0 |
| 42 | #define UTIL_DOUBLE 1 |
| 43 | #define UTIL_STRING 2 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 44 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 45 | namespace { |
| 46 | |
| 47 | // Map PDF-style directives to equivalent wcsftime directives. Not |
| 48 | // all have direct equivalents, though. |
| 49 | struct TbConvert { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 50 | const wchar_t* lpszJSMark; |
| 51 | const wchar_t* lpszCppMark; |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // Map PDF-style directives lacking direct wcsftime directives to |
| 55 | // the value with which they will be replaced. |
| 56 | struct TbConvertAdditional { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 57 | const wchar_t* lpszJSMark; |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 58 | int iValue; |
| 59 | }; |
| 60 | |
| 61 | const TbConvert TbConvertTable[] = { |
| 62 | {L"mmmm", L"%B"}, {L"mmm", L"%b"}, {L"mm", L"%m"}, {L"dddd", L"%A"}, |
| 63 | {L"ddd", L"%a"}, {L"dd", L"%d"}, {L"yyyy", L"%Y"}, {L"yy", L"%y"}, |
| 64 | {L"HH", L"%H"}, {L"hh", L"%I"}, {L"MM", L"%M"}, {L"ss", L"%S"}, |
| 65 | {L"TT", L"%p"}, |
| 66 | #if defined(_WIN32) |
| 67 | {L"tt", L"%p"}, {L"h", L"%#I"}, |
| 68 | #else |
| 69 | {L"tt", L"%P"}, {L"h", L"%l"}, |
| 70 | #endif |
| 71 | }; |
| 72 | |
| 73 | int ParseDataType(std::wstring* sFormat) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 74 | bool bPercent = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | for (size_t i = 0; i < sFormat->length(); ++i) { |
| 76 | wchar_t c = (*sFormat)[i]; |
| 77 | if (c == L'%') { |
| 78 | bPercent = true; |
| 79 | continue; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 80 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 81 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 82 | if (bPercent) { |
| 83 | if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' || |
| 84 | c == L'u' || c == L'x' || c == L'X') { |
| 85 | return UTIL_INT; |
| 86 | } |
| 87 | if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') { |
| 88 | return UTIL_DOUBLE; |
| 89 | } |
| 90 | if (c == L's' || c == L'S') { |
| 91 | // Map s to S since we always deal internally |
| 92 | // with wchar_t strings. |
| 93 | (*sFormat)[i] = L'S'; |
| 94 | return UTIL_STRING; |
| 95 | } |
| 96 | if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' || |
Lei Zhang | e247ec4 | 2017-04-20 21:41:36 -0700 | [diff] [blame] | 97 | std::iswdigit(c)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | continue; |
| 99 | } |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 105 | } |
| 106 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 107 | } // namespace |
| 108 | |
| 109 | util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {} |
| 110 | |
| 111 | util::~util() {} |
| 112 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 113 | bool util::printf(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 114 | const std::vector<CJS_Value>& params, |
| 115 | CJS_Value& vRet, |
| 116 | CFX_WideString& sError) { |
Lei Zhang | 574b574 | 2017-03-30 12:41:55 -0700 | [diff] [blame] | 117 | const size_t iSize = params.size(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 118 | if (iSize < 1) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 119 | return false; |
Lei Zhang | 574b574 | 2017-03-30 12:41:55 -0700 | [diff] [blame] | 120 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 121 | std::wstring c_ConvChar(params[0].ToCFXWideString(pRuntime).c_str()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 122 | std::vector<std::wstring> c_strConvers; |
| 123 | int iOffset = 0; |
| 124 | int iOffend = 0; |
| 125 | c_ConvChar.insert(c_ConvChar.begin(), L'S'); |
| 126 | while (iOffset != -1) { |
| 127 | iOffend = c_ConvChar.find(L"%", iOffset + 1); |
| 128 | std::wstring strSub; |
| 129 | if (iOffend == -1) |
| 130 | strSub = c_ConvChar.substr(iOffset); |
| 131 | else |
| 132 | strSub = c_ConvChar.substr(iOffset, iOffend - iOffset); |
| 133 | c_strConvers.push_back(strSub); |
| 134 | iOffset = iOffend; |
| 135 | } |
| 136 | |
| 137 | std::wstring c_strResult; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 138 | std::wstring c_strFormat; |
Lei Zhang | 574b574 | 2017-03-30 12:41:55 -0700 | [diff] [blame] | 139 | for (size_t iIndex = 0; iIndex < c_strConvers.size(); ++iIndex) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 140 | c_strFormat = c_strConvers[iIndex]; |
| 141 | if (iIndex == 0) { |
| 142 | c_strResult = c_strFormat; |
| 143 | continue; |
| 144 | } |
| 145 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 146 | if (iIndex >= iSize) { |
| 147 | c_strResult += c_strFormat; |
| 148 | continue; |
| 149 | } |
| 150 | |
Lei Zhang | 574b574 | 2017-03-30 12:41:55 -0700 | [diff] [blame] | 151 | CFX_WideString strSegment; |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 152 | switch (ParseDataType(&c_strFormat)) { |
Dan Sinclair | 0c99829 | 2017-07-13 09:58:52 -0400 | [diff] [blame^] | 153 | case UTIL_INT: { |
| 154 | int dot = c_strFormat.find(L".", 0); |
| 155 | if (dot != -1) { |
| 156 | size_t len = 0; |
| 157 | for (size_t i = dot + 1; i < c_strFormat.length(); ++i) { |
| 158 | wchar_t c = c_strFormat[i]; |
| 159 | if (std::iswdigit(c)) { |
| 160 | ++len; |
| 161 | continue; |
| 162 | } |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | // Windows has a max of ~261 characters in the format string of |
| 167 | // the form %0.261x. We're just going to bail out if the format |
| 168 | // would be over 3 or more characters long. |
| 169 | if (len > 2) |
| 170 | return false; |
| 171 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 172 | strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt(pRuntime)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 173 | break; |
Dan Sinclair | 0c99829 | 2017-07-13 09:58:52 -0400 | [diff] [blame^] | 174 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 175 | case UTIL_DOUBLE: |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 176 | strSegment.Format(c_strFormat.c_str(), |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 177 | params[iIndex].ToDouble(pRuntime)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | break; |
| 179 | case UTIL_STRING: |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 180 | strSegment.Format(c_strFormat.c_str(), |
| 181 | params[iIndex].ToCFXWideString(pRuntime).c_str()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 182 | break; |
| 183 | default: |
| 184 | strSegment.Format(L"%S", c_strFormat.c_str()); |
| 185 | break; |
| 186 | } |
Lei Zhang | 1e25e12 | 2017-06-16 02:14:40 -0700 | [diff] [blame] | 187 | c_strResult += strSegment.c_str(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | c_strResult.erase(c_strResult.begin()); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 191 | vRet = CJS_Value(pRuntime, c_strResult.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 192 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 195 | bool util::printd(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 196 | const std::vector<CJS_Value>& params, |
| 197 | CJS_Value& vRet, |
| 198 | CFX_WideString& sError) { |
Lei Zhang | 574b574 | 2017-03-30 12:41:55 -0700 | [diff] [blame] | 199 | const size_t iSize = params.size(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 200 | if (iSize < 2) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 201 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 202 | |
Lei Zhang | 574b574 | 2017-03-30 12:41:55 -0700 | [diff] [blame] | 203 | const CJS_Value& p1 = params[0]; |
| 204 | const CJS_Value& p2 = params[1]; |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 205 | CJS_Date jsDate; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 206 | if (!p2.ConvertToDate(pRuntime, jsDate)) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 207 | sError = JSGetStringFromID(IDS_STRING_JSPRINT1); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 208 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 209 | } |
| 210 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 211 | if (!jsDate.IsValidDate(pRuntime)) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 212 | sError = JSGetStringFromID(IDS_STRING_JSPRINT2); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 213 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 216 | if (p1.GetType() == CJS_Value::VT_number) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 217 | CFX_WideString swResult; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 218 | switch (p1.ToInt(pRuntime)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 219 | case 0: |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 220 | swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(pRuntime), |
| 221 | jsDate.GetMonth(pRuntime) + 1, jsDate.GetDay(pRuntime), |
| 222 | jsDate.GetHours(pRuntime), jsDate.GetMinutes(pRuntime), |
| 223 | jsDate.GetSeconds(pRuntime)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 224 | break; |
| 225 | case 1: |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 226 | swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 227 | jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1, |
| 228 | jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime), |
| 229 | jsDate.GetMinutes(pRuntime), |
| 230 | jsDate.GetSeconds(pRuntime)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 231 | break; |
| 232 | case 2: |
tsepez | f3c8832 | 2016-08-09 07:30:38 -0700 | [diff] [blame] | 233 | swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 234 | jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1, |
| 235 | jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime), |
| 236 | jsDate.GetMinutes(pRuntime), |
| 237 | jsDate.GetSeconds(pRuntime)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 238 | break; |
| 239 | default: |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 240 | sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 241 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 242 | } |
| 243 | |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 244 | vRet = CJS_Value(pRuntime, swResult.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 245 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 246 | } |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 247 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 248 | if (p1.GetType() == CJS_Value::VT_string) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 249 | if (iSize > 2 && params[2].ToBool(pRuntime)) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 250 | sError = JSGetStringFromID(IDS_STRING_JSNOTSUPPORT); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 251 | return false; // currently, it doesn't support XFAPicture. |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 252 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 253 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 254 | // Convert PDF-style format specifiers to wcsftime specifiers. Remove any |
| 255 | // pre-existing %-directives before inserting our own. |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 256 | std::basic_string<wchar_t> cFormat = p1.ToCFXWideString(pRuntime).c_str(); |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 257 | cFormat.erase(std::remove(cFormat.begin(), cFormat.end(), '%'), |
| 258 | cFormat.end()); |
| 259 | |
| 260 | for (size_t i = 0; i < FX_ArraySize(TbConvertTable); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 261 | int iStart = 0; |
| 262 | int iEnd; |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 263 | while ((iEnd = cFormat.find(TbConvertTable[i].lpszJSMark, iStart)) != |
| 264 | -1) { |
| 265 | cFormat.replace(iEnd, FXSYS_wcslen(TbConvertTable[i].lpszJSMark), |
| 266 | TbConvertTable[i].lpszCppMark); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 267 | iStart = iEnd; |
| 268 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 269 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 270 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 271 | int iYear = jsDate.GetYear(pRuntime); |
Lei Zhang | 2bf942d | 2017-06-16 13:48:19 -0700 | [diff] [blame] | 272 | if (iYear < 0) { |
| 273 | sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR); |
| 274 | return false; |
| 275 | } |
| 276 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 277 | int iMonth = jsDate.GetMonth(pRuntime); |
| 278 | int iDay = jsDate.GetDay(pRuntime); |
| 279 | int iHour = jsDate.GetHours(pRuntime); |
| 280 | int iMin = jsDate.GetMinutes(pRuntime); |
| 281 | int iSec = jsDate.GetSeconds(pRuntime); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 282 | |
Lei Zhang | 1e25e12 | 2017-06-16 02:14:40 -0700 | [diff] [blame] | 283 | static const TbConvertAdditional cTableAd[] = { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 284 | {L"m", iMonth + 1}, {L"d", iDay}, |
| 285 | {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour}, |
| 286 | {L"M", iMin}, {L"s", iSec}, |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 287 | }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 288 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 289 | for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 290 | CFX_WideString sValue; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 291 | sValue.Format(L"%d", cTableAd[i].iValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 292 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 293 | int iStart = 0; |
| 294 | int iEnd; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 295 | while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 296 | if (iEnd > 0) { |
| 297 | if (cFormat[iEnd - 1] == L'%') { |
| 298 | iStart = iEnd + 1; |
| 299 | continue; |
| 300 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 301 | } |
Lei Zhang | 1e25e12 | 2017-06-16 02:14:40 -0700 | [diff] [blame] | 302 | cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), |
| 303 | sValue.c_str()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 304 | iStart = iEnd; |
| 305 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 306 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 307 | |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 308 | struct tm time = {}; |
| 309 | time.tm_year = iYear - 1900; |
| 310 | time.tm_mon = iMonth; |
| 311 | time.tm_mday = iDay; |
| 312 | time.tm_hour = iHour; |
| 313 | time.tm_min = iMin; |
| 314 | time.tm_sec = iSec; |
| 315 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 316 | wchar_t buf[64] = {}; |
Lei Zhang | 2bf942d | 2017-06-16 13:48:19 -0700 | [diff] [blame] | 317 | FXSYS_wcsftime(buf, 64, cFormat.c_str(), &time); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 318 | cFormat = buf; |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 319 | vRet = CJS_Value(pRuntime, cFormat.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 320 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 321 | } |
tsepez | 86a61dc | 2016-03-25 10:00:11 -0700 | [diff] [blame] | 322 | |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 323 | sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 324 | return false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 327 | bool util::printx(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 328 | const std::vector<CJS_Value>& params, |
| 329 | CJS_Value& vRet, |
| 330 | CFX_WideString& sError) { |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 331 | if (params.size() < 2) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 332 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 333 | return false; |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 334 | } |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 335 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 336 | vRet = CJS_Value(pRuntime, printx(params[0].ToCFXWideString(pRuntime), |
| 337 | params[1].ToCFXWideString(pRuntime)) |
| 338 | .c_str()); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 339 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 340 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 341 | } |
| 342 | |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 343 | enum CaseMode { kPreserveCase, kUpperCase, kLowerCase }; |
| 344 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 345 | static wchar_t TranslateCase(wchar_t input, CaseMode eMode) { |
Lei Zhang | ef002c8 | 2017-04-24 16:28:07 -0700 | [diff] [blame] | 346 | if (eMode == kLowerCase && FXSYS_isupper(input)) |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 347 | return input | 0x20; |
Lei Zhang | ef002c8 | 2017-04-24 16:28:07 -0700 | [diff] [blame] | 348 | if (eMode == kUpperCase && FXSYS_islower(input)) |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 349 | return input & ~0x20; |
| 350 | return input; |
| 351 | } |
| 352 | |
| 353 | CFX_WideString util::printx(const CFX_WideString& wsFormat, |
| 354 | const CFX_WideString& wsSource) { |
| 355 | CFX_WideString wsResult; |
| 356 | FX_STRSIZE iSourceIdx = 0; |
| 357 | FX_STRSIZE iFormatIdx = 0; |
| 358 | CaseMode eCaseMode = kPreserveCase; |
| 359 | bool bEscaped = false; |
| 360 | while (iFormatIdx < wsFormat.GetLength()) { |
| 361 | if (bEscaped) { |
| 362 | bEscaped = false; |
| 363 | wsResult += wsFormat[iFormatIdx]; |
| 364 | ++iFormatIdx; |
| 365 | continue; |
| 366 | } |
| 367 | switch (wsFormat[iFormatIdx]) { |
| 368 | case '\\': { |
| 369 | bEscaped = true; |
| 370 | ++iFormatIdx; |
| 371 | } break; |
| 372 | case '<': { |
| 373 | eCaseMode = kLowerCase; |
| 374 | ++iFormatIdx; |
| 375 | } break; |
| 376 | case '>': { |
| 377 | eCaseMode = kUpperCase; |
| 378 | ++iFormatIdx; |
| 379 | } break; |
| 380 | case '=': { |
| 381 | eCaseMode = kPreserveCase; |
| 382 | ++iFormatIdx; |
| 383 | } break; |
| 384 | case '?': { |
| 385 | if (iSourceIdx < wsSource.GetLength()) { |
| 386 | wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); |
| 387 | ++iSourceIdx; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 388 | } |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 389 | ++iFormatIdx; |
| 390 | } break; |
| 391 | case 'X': { |
| 392 | if (iSourceIdx < wsSource.GetLength()) { |
Lei Zhang | ef002c8 | 2017-04-24 16:28:07 -0700 | [diff] [blame] | 393 | if (FXSYS_iswalnum(wsSource[iSourceIdx])) { |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 394 | wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); |
| 395 | ++iFormatIdx; |
| 396 | } |
| 397 | ++iSourceIdx; |
| 398 | } else { |
| 399 | ++iFormatIdx; |
| 400 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 401 | } break; |
| 402 | case 'A': { |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 403 | if (iSourceIdx < wsSource.GetLength()) { |
Lei Zhang | ef002c8 | 2017-04-24 16:28:07 -0700 | [diff] [blame] | 404 | if (FXSYS_iswalpha(wsSource[iSourceIdx])) { |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 405 | wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); |
| 406 | ++iFormatIdx; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 407 | } |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 408 | ++iSourceIdx; |
| 409 | } else { |
| 410 | ++iFormatIdx; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 411 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 412 | } break; |
| 413 | case '9': { |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 414 | if (iSourceIdx < wsSource.GetLength()) { |
Lei Zhang | ef002c8 | 2017-04-24 16:28:07 -0700 | [diff] [blame] | 415 | if (std::iswdigit(wsSource[iSourceIdx])) { |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 416 | wsResult += wsSource[iSourceIdx]; |
| 417 | ++iFormatIdx; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 418 | } |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 419 | ++iSourceIdx; |
| 420 | } else { |
| 421 | ++iFormatIdx; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 422 | } |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 423 | } break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 424 | case '*': { |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 425 | if (iSourceIdx < wsSource.GetLength()) { |
| 426 | wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode); |
| 427 | ++iSourceIdx; |
| 428 | } else { |
| 429 | ++iFormatIdx; |
| 430 | } |
| 431 | } break; |
| 432 | default: { |
| 433 | wsResult += wsFormat[iFormatIdx]; |
| 434 | ++iFormatIdx; |
| 435 | } break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
tsepez | 4f1f41f | 2016-03-28 14:13:16 -0700 | [diff] [blame] | 438 | return wsResult; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 441 | bool util::scand(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 442 | const std::vector<CJS_Value>& params, |
| 443 | CJS_Value& vRet, |
| 444 | CFX_WideString& sError) { |
Lei Zhang | 574b574 | 2017-03-30 12:41:55 -0700 | [diff] [blame] | 445 | if (params.size() < 2) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 446 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 447 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 448 | CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime); |
| 449 | CFX_WideString sDate = params[1].ToCFXWideString(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 450 | double dDate = JS_GetDateTime(); |
| 451 | if (sDate.GetLength() > 0) { |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 452 | dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | if (!JS_PortIsNan(dDate)) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 456 | vRet = CJS_Value(pRuntime, CJS_Date(pRuntime, dDate)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 457 | } else { |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 458 | vRet.SetNull(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 459 | } |
| 460 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 461 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 464 | bool util::byteToChar(CJS_Runtime* pRuntime, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 465 | const std::vector<CJS_Value>& params, |
| 466 | CJS_Value& vRet, |
| 467 | CFX_WideString& sError) { |
tsepez | 90d8779 | 2016-03-29 09:21:54 -0700 | [diff] [blame] | 468 | if (params.size() < 1) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 469 | sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 470 | return false; |
tsepez | 90d8779 | 2016-03-29 09:21:54 -0700 | [diff] [blame] | 471 | } |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 472 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 473 | int arg = params[0].ToInt(pRuntime); |
tsepez | 90d8779 | 2016-03-29 09:21:54 -0700 | [diff] [blame] | 474 | if (arg < 0 || arg > 255) { |
tsepez | cd5dc85 | 2016-09-08 11:23:24 -0700 | [diff] [blame] | 475 | sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 476 | return false; |
tsepez | 90d8779 | 2016-03-29 09:21:54 -0700 | [diff] [blame] | 477 | } |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 478 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 479 | CFX_WideString wStr(static_cast<wchar_t>(arg)); |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 480 | vRet = CJS_Value(pRuntime, wStr.c_str()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 481 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 482 | } |