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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
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"
19 #if _FX_OS_ == _FX_ANDROID_
23 static v8::Isolate* GetIsolate(IFXJS_Context* cc)
25 CJS_Context* pContext = (CJS_Context *)cc;
26 ASSERT(pContext != NULL);
28 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
29 ASSERT(pRuntime != NULL);
31 return pRuntime->GetIsolate();
34 BEGIN_JS_STATIC_CONST(CJS_Util)
37 BEGIN_JS_STATIC_PROP(CJS_Util)
40 BEGIN_JS_STATIC_METHOD(CJS_Util)
41 JS_STATIC_METHOD_ENTRY(printd, 3)
42 JS_STATIC_METHOD_ENTRY(printf, 20)
43 JS_STATIC_METHOD_ENTRY(printx, 2)
44 JS_STATIC_METHOD_ENTRY(scand, 2)
45 JS_STATIC_METHOD_ENTRY(byteToChar, 1)
46 END_JS_STATIC_METHOD()
48 IMPLEMENT_JS_CLASS(CJS_Util,util)
50 util::util(CJS_Object *pJSObject) : CJS_EmbedObj(pJSObject)
61 FX_LPCWSTR lpszJSMark;
62 FX_LPCWSTR lpszCppMark;
65 const stru_TbConvert fcTable[] = {
99 int util::ParstDataType(std::wstring* sFormat)
102 bool bPercent = FALSE;
103 for (i=0; i<sFormat->length(); ++i)
105 wchar_t c = (*sFormat)[i];
114 if (c == L'c' || c == L'C' || c == L'd' || c == L'i' || c == L'o' || c == L'u' || c == L'x' || c == L'X')
118 else if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G')
122 else if (c == L's' || c == L'S')
124 // Map s to S since we always deal internally
125 // with wchar_t strings.
126 (*sFormat)[i] = L'S';
129 else if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' || CJS_PublicMethods::IsDigit(c))
140 FX_BOOL util::printf(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
142 int iSize = params.size();
145 std::wstring c_ConvChar((const wchar_t*)(FX_LPCWSTR)params[0].operator CFX_WideString());
146 std::vector<std::wstring> c_strConvers;
149 c_ConvChar.insert(c_ConvChar.begin(),L'S');
152 iOffend = c_ConvChar.find(L"%",iOffset+1);
155 strSub = c_ConvChar.substr(iOffset);
157 strSub = c_ConvChar.substr(iOffset ,iOffend - iOffset);
158 c_strConvers.push_back(strSub);
162 std::wstring c_strResult;
164 //for(int iIndex = 1;iIndex < params.size();iIndex++)
165 std::wstring c_strFormat;
166 for(int iIndex = 0;iIndex < (int)c_strConvers.size();iIndex++)
168 c_strFormat = c_strConvers[iIndex];
171 c_strResult = c_strFormat;
176 CFX_WideString strSegment;
177 if (iIndex >= iSize) {
178 c_strResult += c_strFormat;
182 switch (ParstDataType(&c_strFormat))
185 strSegment.Format(c_strFormat.c_str(),(int)params[iIndex]);
188 strSegment.Format(c_strFormat.c_str(),(double)params[iIndex]);
191 strSegment.Format(c_strFormat.c_str(),(FX_LPCWSTR)params[iIndex].operator CFX_WideString());
194 strSegment.Format(L"%S", c_strFormat.c_str());
197 c_strResult += strSegment.GetBuffer(strSegment.GetLength()+1);
200 c_strResult.erase(c_strResult.begin());
201 vRet = c_strResult.c_str();
205 FX_BOOL util::printd(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
207 v8::Isolate* isolate = GetIsolate(cc);
209 int iSize = params.size();
213 CJS_Value p1(isolate);
216 CJS_Value p2 = params[1];
217 CJS_Date jsDate(isolate);
218 if (!p2.ConvertToDate(jsDate))
220 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1);
224 if (!jsDate.IsValidDate())
226 sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2);
230 if (p1.GetType() == VT_number)
234 CFX_WideString swResult;
239 swResult.Format(L"D:%04d%02d%02d%02d%02d%02d",
241 jsDate.GetMonth() + 1,
245 jsDate.GetSeconds());
248 swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d",
250 jsDate.GetMonth() + 1,
254 jsDate.GetSeconds());
257 swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d",
259 jsDate.GetMonth() + 1,
263 jsDate.GetSeconds());
272 else if (p1.GetType() == VT_string)
274 std::basic_string<wchar_t> cFormat = (FX_LPCWSTR)p1.operator CFX_WideString();
276 bool bXFAPicture = false;
280 bXFAPicture = params[2];
285 return FALSE; //currently, it doesn't support XFAPicture.
289 for(iIndex = 0;iIndex<sizeof(fcTable)/sizeof(stru_TbConvert);iIndex++)
293 while((iEnd = cFormat.find((CFX_WideString)fcTable[iIndex].lpszJSMark, iStart)) != -1)
295 cFormat.replace(iEnd, FXSYS_wcslen(fcTable[iIndex].lpszJSMark), (CFX_WideString)fcTable[iIndex].lpszCppMark);
300 int iYear,iMonth,iDay,iHour,iMin,iSec;
301 iYear = jsDate.GetYear();
302 iMonth = jsDate.GetMonth();
303 iDay = jsDate.GetDay();
304 iHour = jsDate.GetHours();
305 iMin = jsDate.GetMinutes();
306 iSec = jsDate.GetSeconds();
308 struct tm time = {0};
309 time.tm_year = iYear-1900;
310 time.tm_mon = iMonth;
312 time.tm_hour = iHour;
315 //COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec);
316 //CString strFormat = cppTm.Format(cFormat.c_str());
318 struct stru_TbConvertAd
320 FX_LPCWSTR lpszJSMark;
324 stru_TbConvertAd cTableAd[] ={
328 { L"h", iHour>12?iHour-12:iHour },
333 //cFormat = strFormat.GetBuffer(strFormat.GetLength()+1);
334 for(iIndex = 0;iIndex<sizeof(cTableAd)/sizeof(stru_TbConvertAd);iIndex++)
336 wchar_t tszValue[10];
337 //_itot(cTableAd[iIndex].iValue,tszValue,10);
338 CFX_WideString sValue;
339 sValue.Format(L"%d",cTableAd[iIndex].iValue);
340 memcpy(tszValue, (wchar_t *)sValue.GetBuffer(sValue.GetLength()+1),
341 (sValue.GetLength()+1)*sizeof(wchar_t));
343 //strFormat.Replace(cTableAd[iIndex].lpszJSMark,"%d");
344 //strFormat.Format(strFormat,cTableAd[iIndex].iValue);
347 while((iEnd = cFormat.find((CFX_WideString)cTableAd[iIndex].lpszJSMark,iStart)) != -1)
351 if (cFormat[iEnd-1] == L'%')
357 cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[iIndex].lpszJSMark), tszValue);
362 CFX_WideString strFormat;
363 // strFormat.Format(L"%d,%d,%d,%d,%d,%d",iYear, iMonth, iDay, iHour, iMin, iSec);
364 // CString strFormat = cppTm.Format(cFormat.c_str());
365 wchar_t buf[64] = {0};
366 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
368 vRet = cFormat.c_str();
369 //rtRet = strFormat.GetBuffer(strFormat.GetLength()+1);
375 void util::printd(const std::wstring &cFormat2, CJS_Date jsDate, bool bXFAPicture, std::wstring &cPurpose)
377 std::wstring cFormat = cFormat2;
381 return ; //currently, it doesn't support XFAPicture.
385 for(iIndex = 0;iIndex<sizeof(fcTable)/sizeof(stru_TbConvert);iIndex++)
389 while((iEnd = cFormat.find((CFX_WideString)fcTable[iIndex].lpszJSMark,iStart)) != -1)
391 cFormat.replace(iEnd,FXSYS_wcslen(fcTable[iIndex].lpszJSMark), (CFX_WideString)fcTable[iIndex].lpszCppMark);
396 int iYear,iMonth,iDay,iHour,iMin,iSec;
397 iYear = jsDate.GetYear();
398 iMonth = jsDate.GetMonth();
399 iDay = jsDate.GetDay();
400 iHour = jsDate.GetHours();
401 iMin = jsDate.GetMinutes();
402 iSec = jsDate.GetSeconds();
404 struct tm time = {0};
405 time.tm_year = iYear-1900;
406 time.tm_mon = iMonth;
408 time.tm_hour = iHour;
411 // COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec);
412 //CString strFormat = cppTm.Format(cFormat.c_str());
414 struct stru_TbConvertAd
416 FX_LPCWSTR lpszJSMark;
420 stru_TbConvertAd cTableAd[] ={
424 { L"h", iHour>12?iHour-12:iHour },
429 //cFormat = strFormat.GetBuffer(strFormat.GetLength()+1);
430 for(iIndex = 0;iIndex<sizeof(cTableAd)/sizeof(stru_TbConvertAd);iIndex++)
432 wchar_t tszValue[10];
433 //_itot(cTableAd[iIndex].iValue,tszValue,10);
434 CFX_WideString sValue;
435 sValue.Format(L"%d",cTableAd[iIndex].iValue);
436 memcpy(tszValue, (wchar_t *)sValue.GetBuffer(sValue.GetLength()+1),sValue.GetLength()*sizeof(wchar_t));
439 //strFormat.Replace(cTableAd[iIndex].lpszJSMark,"%d");
440 //strFormat.Format(strFormat,cTableAd[iIndex].iValue);
443 while((iEnd = cFormat.find((CFX_WideString)cTableAd[iIndex].lpszJSMark,iStart)) != -1)
447 if (cFormat[iEnd-1] == L'%')
453 cFormat.replace(iEnd,FXSYS_wcslen(cTableAd[iIndex].lpszJSMark),tszValue);
458 CFX_WideString strFormat;
459 // strFormat.Format(L"%d,%d,%d,%d,%d,%d",iYear, iMonth, iDay, iHour, iMin, iSec);
460 // CString strFormat = cppTm.Format(cFormat.c_str());
461 wchar_t buf[64] = {0};
462 strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
467 FX_BOOL util::printx(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
469 int iSize = params.size();
472 CFX_WideString sFormat = params[0].operator CFX_WideString();
473 CFX_WideString sSource = params[1].operator CFX_WideString();
474 std::string cFormat = CFX_ByteString::FromUnicode(sFormat).c_str();
475 std::string cSource = CFX_ByteString::FromUnicode(sSource).c_str();
477 printx(cFormat,cSource,cDest);
478 vRet = cDest.c_str();
482 void util::printx(const std::string &cFormat,const std::string &cSource2,std::string &cPurpose)
484 std::string cSource(cSource2);
485 if (!cPurpose.empty())
489 int iSize = cSource.size();
490 for(int iIndex = 0; iIndex < (int)cFormat.size() && itSource<iSize; iIndex++)
492 char letter = cFormat[iIndex];
496 //cPurpose.push_back(cSource[itSource]);
497 cPurpose += cSource[itSource];
502 while(itSource < iSize)
504 if ((cSource[itSource]>='0'&&cSource[itSource]<='9') || (cSource[itSource]>='a' && cSource[itSource]<='z') || (cSource[itSource]>='A' && cSource[itSource]<='Z'))
506 //cPurpose.push_back(cSource[itSource]);
507 cPurpose += cSource[itSource];
518 while(itSource < iSize)
520 if ((cSource[itSource]>='a' && cSource[itSource]<='z') || (cSource[itSource]>='A' && cSource[itSource]<='Z'))
522 //cPurpose.push_back(cSource[itSource]);
523 cPurpose += cSource[itSource];
534 while(itSource < iSize)
536 if (cSource[itSource]>='0'&&cSource[itSource]<='9')
538 //cPurpose.push_back(cSource[itSource]);
539 cPurpose += cSource[itSource];
549 cPurpose.append(cSource,itSource,iSize-itSource);
557 for(std::string::iterator it = cSource.begin();it != cSource.end(); it++)
565 for(std::string::iterator it = cSource.begin();it != cSource.end(); it++)
574 //cPurpose.push_back(letter);
581 FX_BOOL util::scand(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
583 v8::Isolate* isolate = GetIsolate(cc);
584 int iSize = params.size();
587 CFX_WideString sFormat = params[0].operator CFX_WideString();
588 CFX_WideString sDate = params[1].operator CFX_WideString();
590 double dDate = JS_GetDateTime();
591 if (sDate.GetLength() > 0)
593 FX_BOOL bWrongFormat = FALSE;
594 dDate = CJS_PublicMethods::MakeRegularDate(sDate,sFormat,bWrongFormat);
597 if (!JS_PortIsNan(dDate))
599 CJS_Date date(isolate,dDate);
610 FX_INT64 FX_atoi64(const char *nptr)
612 int c; /* current char */
613 FX_INT64 total; /* current total */
614 int sign; /* if '-', then negative, otherwise positive */
616 /* skip whitespace */
617 while ( isspace((int)(unsigned char)*nptr) )
620 c = (int)(unsigned char)*nptr++;
621 sign = c; /* save sign indication */
622 if (c == '-' || c == '+')
623 c = (int)(unsigned char)*nptr++; /* skip sign */
628 total = 10 * total + (c - '0'); /* accumulate digit */
629 c = (int)(unsigned char)*nptr++; /* get next char */
635 return total; /* return result, negated if necessary */
638 FX_BOOL util::byteToChar(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
640 int iSize = params.size();
643 int nByte = (int)params[0];
644 unsigned char cByte = (unsigned char)nByte;
645 CFX_WideString csValue;
646 csValue.Format(L"%c", cByte);