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 | |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 7 | #include "util.h" |
| 8 | |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 9 | #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 Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 16 | #include "core/include/fxcrt/fx_ext.h" |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 17 | #include "fpdfsdk/include/javascript/IJavaScript.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 18 | #include "resource.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 19 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 20 | #if _FX_OS_ == _FX_ANDROID_ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 21 | #include <ctype.h> |
| 22 | #endif |
| 23 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 24 | BEGIN_JS_STATIC_CONST(CJS_Util) |
| 25 | END_JS_STATIC_CONST() |
| 26 | |
| 27 | BEGIN_JS_STATIC_PROP(CJS_Util) |
| 28 | END_JS_STATIC_PROP() |
| 29 | |
| 30 | BEGIN_JS_STATIC_METHOD(CJS_Util) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 31 | JS_STATIC_METHOD_ENTRY(printd) |
| 32 | JS_STATIC_METHOD_ENTRY(printf) |
| 33 | JS_STATIC_METHOD_ENTRY(printx) |
| 34 | JS_STATIC_METHOD_ENTRY(scand) |
| 35 | JS_STATIC_METHOD_ENTRY(byteToChar) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 36 | END_JS_STATIC_METHOD() |
| 37 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 38 | IMPLEMENT_JS_CLASS(CJS_Util, util) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 39 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 40 | util::util(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 41 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 42 | util::~util() { |
| 43 | } |
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 | struct stru_TbConvert { |
| 46 | const FX_WCHAR* lpszJSMark; |
| 47 | const FX_WCHAR* lpszCppMark; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | const stru_TbConvert fcTable[] = { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 51 | {L"mmmm", L"%B"}, |
| 52 | {L"mmm", L"%b"}, |
| 53 | {L"mm", L"%m"}, |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 54 | //"m" |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 55 | {L"dddd", L"%A"}, |
| 56 | {L"ddd", L"%a"}, |
| 57 | {L"dd", L"%d"}, |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 58 | //"d", "%w", |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 59 | {L"yyyy", L"%Y"}, |
| 60 | {L"yy", L"%y"}, |
| 61 | {L"HH", L"%H"}, |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 62 | //"H" |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 63 | {L"hh", L"%I"}, |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 64 | //"h" |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 65 | {L"MM", L"%M"}, |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 66 | //"M" |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 67 | {L"ss", L"%S"}, |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 68 | //"s |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 69 | {L"TT", L"%p"}, |
| 70 | //"t" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 71 | #if defined(_WIN32) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 72 | {L"tt", L"%p"}, |
| 73 | {L"h", L"%#I"}, |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 74 | #else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | {L"tt", L"%P"}, |
| 76 | {L"h", L"%l"}, |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 77 | #endif |
| 78 | }; |
| 79 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 80 | #define UTIL_INT 0 |
| 81 | #define UTIL_DOUBLE 1 |
| 82 | #define UTIL_STRING 2 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 83 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 84 | int 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 Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 91 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 92 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 93 | 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' ' || |
Lei Zhang | 9559b7a | 2015-12-21 11:12:20 -0800 | [diff] [blame] | 108 | FXSYS_iswdigit(c)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 109 | continue; |
| 110 | } |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 118 | FX_BOOL util::printf(IJS_Context* cc, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 119 | const std::vector<CJS_Value>& params, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | CJS_Value& vRet, |
| 121 | CFX_WideString& sError) { |
| 122 | int iSize = params.size(); |
| 123 | if (iSize < 1) |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 124 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 125 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 181 | FX_BOOL util::printd(IJS_Context* cc, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 182 | const std::vector<CJS_Value>& params, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 183 | CJS_Value& vRet, |
| 184 | CFX_WideString& sError) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 185 | int iSize = params.size(); |
| 186 | if (iSize < 2) |
| 187 | return FALSE; |
| 188 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 189 | CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
| 190 | CJS_Value p1(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 191 | p1 = params[0]; |
| 192 | |
| 193 | CJS_Value p2 = params[1]; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 194 | CJS_Date jsDate(pRuntime); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 195 | 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 Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 205 | if (p1.GetType() == CJS_Value::VT_number) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 206 | 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 Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 235 | if (p1.GetType() == CJS_Value::VT_string) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 236 | 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 Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 245 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 246 | |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 247 | for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 248 | int iStart = 0; |
| 249 | int iEnd; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 250 | while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) { |
| 251 | cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark), |
| 252 | fcTable[i].lpszCppMark); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 253 | iStart = iEnd; |
| 254 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 255 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 256 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | int iYear, iMonth, iDay, iHour, iMin, iSec; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 258 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 264 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 265 | struct tm time = {}; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 266 | time.tm_year = iYear - 1900; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 267 | 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-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 | struct stru_TbConvertAd { |
| 274 | const FX_WCHAR* lpszJSMark; |
| 275 | int iValue; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 276 | }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 277 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 278 | 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 Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 282 | }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 283 | |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 284 | for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 285 | wchar_t tszValue[10]; |
| 286 | CFX_WideString sValue; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 287 | sValue.Format(L"%d", cTableAd[i].iValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 288 | memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1), |
| 289 | (sValue.GetLength() + 1) * sizeof(wchar_t)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 290 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 291 | int iStart = 0; |
| 292 | int iEnd; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 293 | while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 294 | if (iEnd > 0) { |
| 295 | if (cFormat[iEnd - 1] == L'%') { |
| 296 | iStart = iEnd + 1; |
| 297 | continue; |
| 298 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 299 | } |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 300 | cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 301 | iStart = iEnd; |
| 302 | } |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 303 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 304 | |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 305 | CFX_WideString strFormat; |
| 306 | wchar_t buf[64] = {}; |
| 307 | strFormat = wcsftime(buf, 64, cFormat.c_str(), &time); |
| 308 | cFormat = buf; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 309 | vRet = cFormat.c_str(); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 310 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 311 | } |
| 312 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 315 | void 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 Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 325 | for (size_t i = 0; i < sizeof(fcTable) / sizeof(stru_TbConvert); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 326 | int iStart = 0; |
| 327 | int iEnd; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 328 | while ((iEnd = cFormat.find(fcTable[i].lpszJSMark, iStart)) != -1) { |
| 329 | cFormat.replace(iEnd, FXSYS_wcslen(fcTable[i].lpszJSMark), |
| 330 | fcTable[i].lpszCppMark); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 331 | 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 Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 365 | for (size_t i = 0; i < sizeof(cTableAd) / sizeof(stru_TbConvertAd); ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 366 | wchar_t tszValue[10]; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 367 | CFX_WideString sValue; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 368 | sValue.Format(L"%d", cTableAd[i].iValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 369 | memcpy(tszValue, (wchar_t*)sValue.GetBuffer(sValue.GetLength() + 1), |
| 370 | sValue.GetLength() * sizeof(wchar_t)); |
| 371 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 372 | int iStart = 0; |
| 373 | int iEnd; |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 374 | while ((iEnd = cFormat.find(cTableAd[i].lpszJSMark, iStart)) != -1) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 375 | if (iEnd > 0) { |
| 376 | if (cFormat[iEnd - 1] == L'%') { |
| 377 | iStart = iEnd + 1; |
| 378 | continue; |
| 379 | } |
| 380 | } |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 381 | cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[i].lpszJSMark), tszValue); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 382 | 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 Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 393 | FX_BOOL util::printx(IJS_Context* cc, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 394 | const std::vector<CJS_Value>& params, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 395 | 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 | |
| 410 | void 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 Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 424 | cPurpose += cSource[itSource]; |
| 425 | itSource++; |
| 426 | break; |
| 427 | case 'X': { |
| 428 | while (itSource < iSize) { |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 429 | if (std::isdigit(cSource[itSource]) || |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 430 | (cSource[itSource] >= 'a' && cSource[itSource] <= 'z') || |
| 431 | (cSource[itSource] >= 'A' && cSource[itSource] <= 'Z')) { |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 432 | cPurpose += cSource[itSource]; |
| 433 | itSource++; |
| 434 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 435 | } |
| 436 | itSource++; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 437 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 438 | 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 Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 444 | cPurpose += cSource[itSource]; |
| 445 | itSource++; |
| 446 | break; |
| 447 | } |
| 448 | itSource++; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 449 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 450 | break; |
| 451 | } break; |
| 452 | case '9': { |
| 453 | while (itSource < iSize) { |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 454 | if (std::isdigit(cSource[itSource])) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 455 | 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 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 | |
| 517 | int64_t FX_atoi64(const char* nptr) { |
| 518 | int c; /* current char */ |
| 519 | int64_t total; /* current total */ |
| 520 | int sign; /* if '-', then negative, otherwise positive */ |
| 521 | |
| 522 | /* skip whitespace */ |
| 523 | while (isspace((int)(unsigned char)*nptr)) |
| 524 | ++nptr; |
| 525 | |
| 526 | c = (int)(unsigned char)*nptr++; |
| 527 | sign = c; /* save sign indication */ |
| 528 | if (c == '-' || c == '+') |
| 529 | c = (int)(unsigned char)*nptr++; /* skip sign */ |
| 530 | |
| 531 | total = 0; |
| 532 | |
| 533 | while (isdigit(c)) { |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 534 | total = 10 * total + FXSYS_toDecimalDigit(c); /* accumulate digit */ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 535 | c = (int)(unsigned char)*nptr++; /* get next char */ |
| 536 | } |
| 537 | |
| 538 | return sign == '-' ? -total : total; |
| 539 | } |
| 540 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 541 | FX_BOOL util::byteToChar(IJS_Context* cc, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 542 | const std::vector<CJS_Value>& params, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 543 | CJS_Value& vRet, |
| 544 | CFX_WideString& sError) { |
| 545 | int iSize = params.size(); |
| 546 | if (iSize == 0) |
| 547 | return FALSE; |
| 548 | int nByte = params[0].ToInt(); |
| 549 | unsigned char cByte = (unsigned char)nByte; |
| 550 | CFX_WideString csValue; |
| 551 | csValue.Format(L"%c", cByte); |
| 552 | vRet = csValue.c_str(); |
| 553 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 554 | } |