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