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 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
Dan Sinclair | a8a28e0 | 2016-03-23 15:41:39 -0400 | [diff] [blame^] | 14 | #include "core/fxcrt/include/fx_ext.h" |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 15 | #include "fpdfsdk/include/javascript/IJavaScript.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 16 | #include "fpdfsdk/javascript/JS_Context.h" |
| 17 | #include "fpdfsdk/javascript/JS_Define.h" |
| 18 | #include "fpdfsdk/javascript/JS_EventHandler.h" |
| 19 | #include "fpdfsdk/javascript/JS_Object.h" |
| 20 | #include "fpdfsdk/javascript/JS_Runtime.h" |
| 21 | #include "fpdfsdk/javascript/JS_Value.h" |
| 22 | #include "fpdfsdk/javascript/PublicMethods.h" |
| 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 | util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 46 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 47 | util::~util() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 48 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 49 | struct stru_TbConvert { |
| 50 | const FX_WCHAR* lpszJSMark; |
| 51 | const FX_WCHAR* lpszCppMark; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | const stru_TbConvert fcTable[] = { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 55 | {L"mmmm", L"%B"}, |
| 56 | {L"mmm", L"%b"}, |
| 57 | {L"mm", L"%m"}, |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 58 | // "m" |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 59 | {L"dddd", L"%A"}, |
| 60 | {L"ddd", L"%a"}, |
| 61 | {L"dd", L"%d"}, |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 62 | // "d", "%w", |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 63 | {L"yyyy", L"%Y"}, |
| 64 | {L"yy", L"%y"}, |
| 65 | {L"HH", L"%H"}, |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 66 | // "H" |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 67 | {L"hh", L"%I"}, |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 68 | // "h" |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 69 | {L"MM", L"%M"}, |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 70 | // "M" |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 71 | {L"ss", L"%S"}, |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 72 | // "s |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 73 | {L"TT", L"%p"}, |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 74 | // "t" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 75 | #if defined(_WIN32) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 76 | {L"tt", L"%p"}, |
| 77 | {L"h", L"%#I"}, |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 78 | #else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | {L"tt", L"%P"}, |
| 80 | {L"h", L"%l"}, |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 81 | #endif |
| 82 | }; |
| 83 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 84 | #define UTIL_INT 0 |
| 85 | #define UTIL_DOUBLE 1 |
| 86 | #define UTIL_STRING 2 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 87 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 88 | int util::ParstDataType(std::wstring* sFormat) { |
| 89 | bool bPercent = FALSE; |
| 90 | for (size_t i = 0; i < sFormat->length(); ++i) { |
| 91 | wchar_t c = (*sFormat)[i]; |
| 92 | if (c == L'%') { |
| 93 | bPercent = true; |
| 94 | continue; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 95 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 96 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 97 | if (bPercent) { |
| 98 | if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' || |
| 99 | c == L'u' || c == L'x' || c == L'X') { |
| 100 | return UTIL_INT; |
| 101 | } |
| 102 | if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G') { |
| 103 | return UTIL_DOUBLE; |
| 104 | } |
| 105 | if (c == L's' || c == L'S') { |
| 106 | // Map s to S since we always deal internally |
| 107 | // with wchar_t strings. |
| 108 | (*sFormat)[i] = L'S'; |
| 109 | return UTIL_STRING; |
| 110 | } |
| 111 | if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' || |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 112 | FXSYS_iswdigit(c)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 113 | continue; |
| 114 | } |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 122 | FX_BOOL util::printf(IJS_Context* cc, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 123 | const std::vector<CJS_Value>& params, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 124 | CJS_Value& vRet, |
| 125 | CFX_WideString& sError) { |
| 126 | int iSize = params.size(); |
| 127 | if (iSize < 1) |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 128 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 129 | std::wstring c_ConvChar(params[0].ToCFXWideString().c_str()); |
| 130 | std::vector<std::wstring> c_strConvers; |
| 131 | int iOffset = 0; |
| 132 | int iOffend = 0; |
| 133 | c_ConvChar.insert(c_ConvChar.begin(), L'S'); |
| 134 | while (iOffset != -1) { |
| 135 | iOffend = c_ConvChar.find(L"%", iOffset + 1); |
| 136 | std::wstring strSub; |
| 137 | if (iOffend == -1) |
| 138 | strSub = c_ConvChar.substr(iOffset); |
| 139 | else |
| 140 | strSub = c_ConvChar.substr(iOffset, iOffend - iOffset); |
| 141 | c_strConvers.push_back(strSub); |
| 142 | iOffset = iOffend; |
| 143 | } |
| 144 | |
| 145 | std::wstring c_strResult; |
| 146 | |
| 147 | // for(int iIndex = 1;iIndex < params.size();iIndex++) |
| 148 | std::wstring c_strFormat; |
| 149 | for (int iIndex = 0; iIndex < (int)c_strConvers.size(); iIndex++) { |
| 150 | c_strFormat = c_strConvers[iIndex]; |
| 151 | if (iIndex == 0) { |
| 152 | c_strResult = c_strFormat; |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | CFX_WideString strSegment; |
| 157 | if (iIndex >= iSize) { |
| 158 | c_strResult += c_strFormat; |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | switch (ParstDataType(&c_strFormat)) { |
| 163 | case UTIL_INT: |
| 164 | strSegment.Format(c_strFormat.c_str(), params[iIndex].ToInt()); |
| 165 | break; |
| 166 | case UTIL_DOUBLE: |
| 167 | strSegment.Format(c_strFormat.c_str(), params[iIndex].ToDouble()); |
| 168 | break; |
| 169 | case UTIL_STRING: |
| 170 | strSegment.Format(c_strFormat.c_str(), |
| 171 | params[iIndex].ToCFXWideString().c_str()); |
| 172 | break; |
| 173 | default: |
| 174 | strSegment.Format(L"%S", c_strFormat.c_str()); |
| 175 | break; |
| 176 | } |
| 177 | c_strResult += strSegment.GetBuffer(strSegment.GetLength() + 1); |
| 178 | } |
| 179 | |
| 180 | c_strResult.erase(c_strResult.begin()); |
| 181 | vRet = c_strResult.c_str(); |
| 182 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 185 | FX_BOOL util::printd(IJS_Context* cc, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 186 | const std::vector<CJS_Value>& params, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 187 | CJS_Value& vRet, |
| 188 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 189 | int iSize = params.size(); |
| 190 | if (iSize < 2) |
| 191 | return FALSE; |
| 192 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 193 | CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
| 194 | CJS_Value p1(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 195 | p1 = params[0]; |
| 196 | |
| 197 | CJS_Value p2 = params[1]; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 198 | CJS_Date jsDate(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 199 | if (!p2.ConvertToDate(jsDate)) { |
| 200 | sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1); |
| 201 | return FALSE; |
| 202 | } |
| 203 | |
| 204 | if (!jsDate.IsValidDate()) { |
| 205 | sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2); |
| 206 | return FALSE; |
| 207 | } |
| 208 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 209 | if (p1.GetType() == CJS_Value::VT_number) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 210 | int nFormat = p1.ToInt(); |
| 211 | CFX_WideString swResult; |
| 212 | |
| 213 | switch (nFormat) { |
| 214 | case 0: |
| 215 | swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(), |
| 216 | jsDate.GetMonth() + 1, jsDate.GetDay(), |
| 217 | jsDate.GetHours(), jsDate.GetMinutes(), |
| 218 | jsDate.GetSeconds()); |
| 219 | break; |
| 220 | case 1: |
| 221 | swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", jsDate.GetYear(), |
| 222 | jsDate.GetMonth() + 1, jsDate.GetDay(), |
| 223 | jsDate.GetHours(), jsDate.GetMinutes(), |
| 224 | jsDate.GetSeconds()); |
| 225 | break; |
| 226 | case 2: |
| 227 | swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", jsDate.GetYear(), |
| 228 | jsDate.GetMonth() + 1, jsDate.GetDay(), |
| 229 | jsDate.GetHours(), jsDate.GetMinutes(), |
| 230 | jsDate.GetSeconds()); |
| 231 | break; |
| 232 | default: |
| 233 | return FALSE; |
| 234 | } |
| 235 | |
| 236 | vRet = swResult.c_str(); |
| 237 | return TRUE; |
| 238 | } |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 239 | if (p1.GetType() == CJS_Value::VT_string) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 240 | std::basic_string<wchar_t> cFormat = p1.ToCFXWideString().c_str(); |
| 241 | |
| 242 | bool bXFAPicture = false; |
| 243 | if (iSize > 2) { |
| 244 | bXFAPicture = params[2].ToBool(); |
| 245 | } |
| 246 | |
| 247 | if (bXFAPicture) { |
| 248 | return FALSE; // currently, it doesn't support XFAPicture. |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 249 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 250 | |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 251 | for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 252 | int iStart = 0; |
| 253 | int iEnd; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 254 | while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) { |
| 255 | cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark), |
| 256 | fcTable[i].lpszCppMark); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | iStart = iEnd; |
| 258 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 259 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 260 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 261 | int iYear, iMonth, iDay, iHour, iMin, iSec; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 262 | iYear = jsDate.GetYear(); |
| 263 | iMonth = jsDate.GetMonth(); |
| 264 | iDay = jsDate.GetDay(); |
| 265 | iHour = jsDate.GetHours(); |
| 266 | iMin = jsDate.GetMinutes(); |
| 267 | iSec = jsDate.GetSeconds(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 268 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 269 | struct tm time = {}; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 270 | time.tm_year = iYear - 1900; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 271 | time.tm_mon = iMonth; |
| 272 | time.tm_mday = iDay; |
| 273 | time.tm_hour = iHour; |
| 274 | time.tm_min = iMin; |
| 275 | time.tm_sec = iSec; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 276 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 277 | struct stru_TbConvertAd { |
| 278 | const FX_WCHAR* lpszJSMark; |
| 279 | int iValue; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 280 | }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 281 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 282 | stru_TbConvertAd cTableAd[] = { |
| 283 | {L"m", iMonth + 1}, {L"d", iDay}, |
| 284 | {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour}, |
| 285 | {L"M", iMin}, {L"s", iSec}, |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 286 | }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 287 | |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 288 | for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 289 | wchar_t tszValue[10]; |
| 290 | CFX_WideString sValue; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 291 | sValue.Format(L"%d", cTableAd[i].iValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 292 | memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1), |
| 293 | (sValue.GetLength() + 1) * sizeof(wchar_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 294 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 295 | int iStart = 0; |
| 296 | int iEnd; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 297 | while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 298 | if (iEnd > 0) { |
| 299 | if (cFormat[iEnd - 1] == L'%') { |
| 300 | iStart = iEnd + 1; |
| 301 | continue; |
| 302 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 303 | } |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 304 | cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 305 | iStart = iEnd; |
| 306 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 307 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 308 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 309 | CFX_WideString strFormat; |
| 310 | wchar_t buf[64] = {}; |
| 311 | strFormat = wcsftime(buf, 64, cFormat.c_str(), &time); |
| 312 | cFormat = buf; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 313 | vRet = cFormat.c_str(); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 314 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 315 | } |
| 316 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 319 | void util::printd(const std::wstring& cFormat2, |
| 320 | CJS_Date jsDate, |
| 321 | bool bXFAPicture, |
| 322 | std::wstring& cPurpose) { |
| 323 | std::wstring cFormat = cFormat2; |
| 324 | |
| 325 | if (bXFAPicture) { |
| 326 | return; // currently, it doesn't support XFAPicture. |
| 327 | } |
| 328 | |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 329 | for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 330 | int iStart = 0; |
| 331 | int iEnd; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 332 | while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) { |
| 333 | cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark), |
| 334 | fcTable[i].lpszCppMark); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 335 | iStart = iEnd; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | int iYear, iMonth, iDay, iHour, iMin, iSec; |
| 340 | iYear = jsDate.GetYear(); |
| 341 | iMonth = jsDate.GetMonth(); |
| 342 | iDay = jsDate.GetDay(); |
| 343 | iHour = jsDate.GetHours(); |
| 344 | iMin = jsDate.GetMinutes(); |
| 345 | iSec = jsDate.GetSeconds(); |
| 346 | |
| 347 | struct tm time = {}; |
| 348 | time.tm_year = iYear - 1900; |
| 349 | time.tm_mon = iMonth; |
| 350 | time.tm_mday = iDay; |
| 351 | time.tm_hour = iHour; |
| 352 | time.tm_min = iMin; |
| 353 | time.tm_sec = iSec; |
| 354 | // COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec); |
| 355 | // CString strFormat = cppTm.Format(cFormat.c_str()); |
| 356 | |
| 357 | struct stru_TbConvertAd { |
| 358 | const FX_WCHAR* lpszJSMark; |
| 359 | int iValue; |
| 360 | }; |
| 361 | |
| 362 | stru_TbConvertAd cTableAd[] = { |
| 363 | {L"m", iMonth + 1}, {L"d", iDay}, |
| 364 | {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour}, |
| 365 | {L"M", iMin}, {L"s", iSec}, |
| 366 | }; |
| 367 | |
| 368 | // cFormat = strFormat.GetBuffer(strFormat.GetLength()+1); |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 369 | for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 370 | wchar_t tszValue[10]; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 371 | CFX_WideString sValue; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 372 | sValue.Format(L"%d", cTableAd[i].iValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 373 | memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1), |
| 374 | sValue.GetLength() * sizeof(wchar_t)); |
| 375 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 376 | int iStart = 0; |
| 377 | int iEnd; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 378 | while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 379 | if (iEnd > 0) { |
| 380 | if (cFormat[iEnd - 1] == L'%') { |
| 381 | iStart = iEnd + 1; |
| 382 | continue; |
| 383 | } |
| 384 | } |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 385 | cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 386 | iStart = iEnd; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | CFX_WideString strFormat; |
| 391 | wchar_t buf[64] = {}; |
| 392 | strFormat = wcsftime(buf, 64, cFormat.c_str(), &time); |
| 393 | cFormat = buf; |
| 394 | cPurpose = cFormat; |
| 395 | } |
| 396 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 397 | FX_BOOL util::printx(IJS_Context* cc, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 398 | const std::vector<CJS_Value>& params, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 399 | CJS_Value& vRet, |
| 400 | CFX_WideString& sError) { |
| 401 | int iSize = params.size(); |
| 402 | if (iSize < 2) |
| 403 | return FALSE; |
| 404 | CFX_WideString sFormat = params[0].ToCFXWideString(); |
| 405 | CFX_WideString sSource = params[1].ToCFXWideString(); |
| 406 | std::string cFormat = CFX_ByteString::FromUnicode(sFormat).c_str(); |
| 407 | std::string cSource = CFX_ByteString::FromUnicode(sSource).c_str(); |
| 408 | std::string cDest; |
| 409 | printx(cFormat, cSource, cDest); |
| 410 | vRet = cDest.c_str(); |
| 411 | return TRUE; |
| 412 | } |
| 413 | |
| 414 | void util::printx(const std::string& cFormat, |
| 415 | const std::string& cSource2, |
| 416 | std::string& cPurpose) { |
| 417 | std::string cSource(cSource2); |
| 418 | if (!cPurpose.empty()) |
| 419 | // cPurpose.clear(); |
| 420 | cPurpose.erase(); |
| 421 | int itSource = 0; |
| 422 | int iSize = cSource.size(); |
| 423 | for (int iIndex = 0; iIndex < (int)cFormat.size() && itSource < iSize; |
| 424 | iIndex++) { |
| 425 | char letter = cFormat[iIndex]; |
| 426 | switch (letter) { |
| 427 | case '?': |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 428 | cPurpose += cSource[itSource]; |
| 429 | itSource++; |
| 430 | break; |
| 431 | case 'X': { |
| 432 | while (itSource < iSize) { |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 433 | if (std::isdigit(cSource[itSource]) || |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 434 | (cSource[itSource] >= 'a' && cSource[itSource] <= 'z') || |
| 435 | (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 436 | cPurpose += cSource[itSource]; |
| 437 | itSource++; |
| 438 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 439 | } |
| 440 | itSource++; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 441 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 442 | break; |
| 443 | } break; |
| 444 | case 'A': { |
| 445 | while (itSource < iSize) { |
| 446 | if ((cSource[itSource] >= 'a' && cSource[itSource] <= 'z') || |
| 447 | (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 448 | cPurpose += cSource[itSource]; |
| 449 | itSource++; |
| 450 | break; |
| 451 | } |
| 452 | itSource++; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 453 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 454 | break; |
| 455 | } break; |
| 456 | case '9': { |
| 457 | while (itSource < iSize) { |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 458 | if (std::isdigit(cSource[itSource])) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 459 | cPurpose += cSource[itSource]; |
| 460 | itSource++; |
| 461 | break; |
| 462 | } |
| 463 | itSource++; |
| 464 | } |
| 465 | break; |
| 466 | } |
| 467 | case '*': { |
| 468 | cPurpose.append(cSource, itSource, iSize - itSource); |
| 469 | itSource = iSize - 1; |
| 470 | break; |
| 471 | } |
| 472 | case '\\': |
| 473 | break; |
| 474 | case '>': { |
Lei Zhang | cd2bb30 | 2015-12-22 13:49:44 -0800 | [diff] [blame] | 475 | for (char& c : cSource) |
| 476 | c = toupper(c); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 477 | break; |
| 478 | } |
| 479 | case '<': { |
Lei Zhang | cd2bb30 | 2015-12-22 13:49:44 -0800 | [diff] [blame] | 480 | for (char& c : cSource) |
| 481 | c = tolower(c); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 482 | break; |
| 483 | } |
| 484 | case '=': |
| 485 | break; |
| 486 | default: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 487 | cPurpose += letter; |
| 488 | break; |
| 489 | } |
| 490 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 493 | FX_BOOL util::scand(IJS_Context* cc, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 494 | const std::vector<CJS_Value>& params, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 495 | CJS_Value& vRet, |
| 496 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 497 | 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) { |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 505 | dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | if (!JS_PortIsNan(dDate)) { |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 509 | vRet = CJS_Date(CJS_Runtime::FromContext(cc), dDate); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 510 | } else { |
| 511 | vRet.SetNull(); |
| 512 | } |
| 513 | |
| 514 | return TRUE; |
| 515 | } |
| 516 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 517 | FX_BOOL util::byteToChar(IJS_Context* cc, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 518 | const std::vector<CJS_Value>& params, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 519 | CJS_Value& vRet, |
| 520 | CFX_WideString& sError) { |
| 521 | int iSize = params.size(); |
| 522 | if (iSize == 0) |
| 523 | return FALSE; |
| 524 | int nByte = params[0].ToInt(); |
| 525 | unsigned char cByte = (unsigned char)nByte; |
| 526 | CFX_WideString csValue; |
| 527 | csValue.Format(L"%c", cByte); |
| 528 | vRet = csValue.c_str(); |
| 529 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 530 | } |