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