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