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