Tidy up JS_Defines.h
[pdfium.git] / fpdfsdk / src / javascript / util.cpp
index b7303a7..a53bb17 100644 (file)
-// Copyright 2014 PDFium Authors. All rights reserved.\r
-// Use of this source code is governed by a BSD-style license that can be\r
-// found in the LICENSE file.\r
\r
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
-\r
-#include "../../include/javascript/JavaScript.h"\r
-#include "../../include/javascript/IJavaScript.h"\r
-#include "../../include/javascript/JS_Define.h"\r
-#include "../../include/javascript/JS_Object.h"\r
-#include "../../include/javascript/JS_Value.h"\r
-#include "../../include/javascript/util.h"\r
-#include "../../include/javascript/PublicMethods.h"\r
-#include "../../include/javascript/resource.h"\r
-#include "../../include/javascript/JS_Context.h"\r
-#include "../../include/javascript/JS_EventHandler.h"\r
-#include "../../include/javascript/JS_Runtime.h"\r
-\r
-#if _FX_OS_  == _FX_ANDROID_\r
-#include <ctype.h>\r
-#endif\r
-\r
-static v8::Isolate* GetIsolate(IFXJS_Context* cc)\r
-{\r
-       CJS_Context* pContext = (CJS_Context *)cc;\r
-       ASSERT(pContext != NULL);\r
-\r
-       CJS_Runtime* pRuntime = pContext->GetJSRuntime();\r
-       ASSERT(pRuntime != NULL);\r
-\r
-       return pRuntime->GetIsolate();\r
-}\r
-\r
-BEGIN_JS_STATIC_CONST(CJS_Util)\r
-END_JS_STATIC_CONST()\r
-\r
-BEGIN_JS_STATIC_PROP(CJS_Util)\r
-END_JS_STATIC_PROP()\r
-\r
-BEGIN_JS_STATIC_METHOD(CJS_Util)\r
-       JS_STATIC_METHOD_ENTRY(printd, 3)\r
-       JS_STATIC_METHOD_ENTRY(printf, 20)\r
-       JS_STATIC_METHOD_ENTRY(printx, 2)\r
-       JS_STATIC_METHOD_ENTRY(scand, 2)\r
-       JS_STATIC_METHOD_ENTRY(byteToChar, 1)\r
-END_JS_STATIC_METHOD()\r
-\r
-IMPLEMENT_JS_CLASS(CJS_Util,util)\r
-\r
-util::util(CJS_Object *pJSObject) : CJS_EmbedObj(pJSObject)\r
-{\r
-}\r
-\r
-util::~util(void)\r
-{\r
-}\r
-\r
-\r
-struct stru_TbConvert\r
-{\r
-       FX_LPCWSTR lpszJSMark;\r
-       FX_LPCWSTR lpszCppMark;\r
-};\r
-\r
-const stru_TbConvert fcTable[] = {\r
-       (FX_LPCWSTR)L"mmmm", (FX_LPCWSTR)L"%B",\r
-       (FX_LPCWSTR)L"mmm", (FX_LPCWSTR)L"%b",\r
-       (FX_LPCWSTR)L"mm",  (FX_LPCWSTR)L"%m",\r
-       //"m"\r
-       (FX_LPCWSTR)L"dddd", (FX_LPCWSTR)L"%A",\r
-       (FX_LPCWSTR)L"ddd", (FX_LPCWSTR)L"%a",\r
-       (FX_LPCWSTR)L"dd",  (FX_LPCWSTR)L"%d",\r
-       //"d",   "%w",\r
-       (FX_LPCWSTR)L"yyyy", (FX_LPCWSTR)L"%Y",\r
-       (FX_LPCWSTR)L"yy",  (FX_LPCWSTR)L"%y",\r
-       (FX_LPCWSTR)L"HH",  (FX_LPCWSTR)L"%H",\r
-       //"H"\r
-       (FX_LPCWSTR)L"hh",  (FX_LPCWSTR)L"%I",\r
-       //"h"\r
-       (FX_LPCWSTR)L"MM",  (FX_LPCWSTR)L"%M",\r
-       //"M"\r
-       (FX_LPCWSTR)L"ss",  (FX_LPCWSTR)L"%S",\r
-       //"s\r
-       (FX_LPCWSTR)L"TT",  (FX_LPCWSTR)L"%p",\r
-       //"t"\r
-#if defined(_WIN32)\r
-       (FX_LPCWSTR)L"tt",  (FX_LPCWSTR)L"%p",\r
-       (FX_LPCWSTR)L"h",  (FX_LPCWSTR)L"%#I",\r
-#else\r
-       (FX_LPCWSTR)L"tt",  (FX_LPCWSTR)L"%P",\r
-       (FX_LPCWSTR)L"h",  (FX_LPCWSTR)L"%l",\r
-#endif\r
-};\r
-\r
-#define UTIL_INT                       0\r
-#define UTIL_DOUBLE                    1\r
-#define UTIL_STRING                    2\r
-\r
-int util::ParstDataType(std::wstring* sFormat)\r
-{\r
-        size_t i = 0;\r
-       bool bPercent = FALSE;\r
-       for (i=0; i<sFormat->length(); ++i)\r
-       {\r
-               wchar_t c = (*sFormat)[i];\r
-               if (c == L'%')\r
-               {\r
-                       bPercent = true;\r
-                       continue;\r
-               }\r
-\r
-               if (bPercent)\r
-               {\r
-                       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')\r
-                       {\r
-                               return UTIL_INT;\r
-                       }\r
-                       else if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G')\r
-                       {\r
-                               return UTIL_DOUBLE;\r
-                       }\r
-                       else if (c == L's' || c == L'S')\r
-                       {\r
-                               // Map s to S since we always deal internally\r
-                               // with wchar_t strings.\r
-                               (*sFormat)[i] = L'S';\r
-                               return UTIL_STRING;\r
-                       }\r
-                       else if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' || CJS_PublicMethods::IsDigit(c))\r
-                       {\r
-                               continue;\r
-                       }\r
-                       else break;\r
-               }\r
-       }\r
-\r
-       return -1;\r
-}\r
-\r
-FX_BOOL util::printf(OBJ_METHOD_PARAMS)\r
-{\r
-       int iSize = params.size();\r
-       if (iSize < 1)\r
-               return FALSE;\r
-       std::wstring  c_ConvChar((const wchar_t*)(FX_LPCWSTR)params[0].operator CFX_WideString());\r
-       std::vector<std::wstring> c_strConvers;\r
-       int iOffset = 0;\r
-       int iOffend = 0;\r
-       c_ConvChar.insert(c_ConvChar.begin(),L'S');\r
-       while(iOffset != -1)\r
-       {\r
-               iOffend = c_ConvChar.find(L"%",iOffset+1);\r
-               std::wstring strSub;\r
-               if (iOffend == -1)\r
-                       strSub = c_ConvChar.substr(iOffset);                    \r
-               else\r
-                       strSub = c_ConvChar.substr(iOffset ,iOffend - iOffset);\r
-               c_strConvers.push_back(strSub);\r
-               iOffset = iOffend ;\r
-       }\r
-\r
-       std::wstring c_strResult;\r
-\r
-       //for(int iIndex = 1;iIndex < params.size();iIndex++)\r
-       std::wstring c_strFormat;\r
-       for(int iIndex = 0;iIndex < (int)c_strConvers.size();iIndex++)\r
-       {\r
-               c_strFormat = c_strConvers[iIndex];\r
-               if (iIndex == 0)\r
-               {\r
-                       c_strResult = c_strFormat;\r
-                       continue;\r
-               }\r
-\r
-\r
-               CFX_WideString strSegment;\r
-               if (iIndex >= iSize) {\r
-                       c_strResult += c_strFormat;\r
-                       continue;\r
-               }\r
-\r
-               switch (ParstDataType(&c_strFormat))\r
-               {\r
-                       case UTIL_INT:\r
-                               strSegment.Format((FX_LPCWSTR)c_strFormat.c_str(),(int)params[iIndex]);\r
-                               break;\r
-                       case UTIL_DOUBLE:\r
-                               strSegment.Format((FX_LPCWSTR)c_strFormat.c_str(),(double)params[iIndex]);\r
-                               break;\r
-                       case UTIL_STRING:\r
-                               strSegment.Format((FX_LPCWSTR)c_strFormat.c_str(),(FX_LPCWSTR)params[iIndex].operator CFX_WideString());\r
-                               break;\r
-                       default:\r
-                               strSegment.Format((FX_LPCWSTR)L"%S", (FX_LPCWSTR)c_strFormat.c_str());\r
-                               break;\r
-               }\r
-               c_strResult += (wchar_t*)strSegment.GetBuffer(strSegment.GetLength()+1);\r
-       }\r
-\r
-       c_strResult.erase(c_strResult.begin());\r
-       vRet = (FX_LPCWSTR)c_strResult.c_str();\r
-       return TRUE;\r
-}\r
-\r
-FX_BOOL util::printd(OBJ_METHOD_PARAMS)\r
-{\r
-       v8::Isolate* isolate = GetIsolate(cc);\r
-\r
-       int iSize = params.size();\r
-       if (iSize < 2)\r
-               return FALSE;\r
-\r
-       CJS_Value p1(isolate);\r
-       p1 = params[0];\r
-\r
-       CJS_Value p2 = params[1];\r
-       CJS_Date jsDate(isolate);\r
-       if (!p2.ConvertToDate(jsDate))\r
-       {\r
-               sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1);\r
-               return FALSE;\r
-       }\r
-\r
-       if (!jsDate.IsValidDate())\r
-       {\r
-               sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2);\r
-               return FALSE;\r
-       }\r
-\r
-       if (p1.GetType() == VT_number)\r
-       {\r
-               int nFormat = p1;\r
-\r
-               CFX_WideString swResult;\r
-\r
-               switch (nFormat)\r
-               {\r
-               case 0:\r
-                       swResult.Format((FX_LPCWSTR)L"D:%04d%02d%02d%02d%02d%02d", \r
-                               jsDate.GetYear(),\r
-                               jsDate.GetMonth() + 1,\r
-                               jsDate.GetDay(),\r
-                               jsDate.GetHours(),\r
-                               jsDate.GetMinutes(),\r
-                               jsDate.GetSeconds());\r
-                       break;\r
-               case 1:\r
-                       swResult.Format((FX_LPCWSTR)L"%04d.%02d.%02d %02d:%02d:%02d", \r
-                               jsDate.GetYear(),\r
-                               jsDate.GetMonth() + 1,\r
-                               jsDate.GetDay(),\r
-                               jsDate.GetHours(),\r
-                               jsDate.GetMinutes(),\r
-                               jsDate.GetSeconds());\r
-                       break;\r
-               case 2:\r
-                       swResult.Format((FX_LPCWSTR)L"%04d/%02d/%02d %02d:%02d:%02d", \r
-                               jsDate.GetYear(),\r
-                               jsDate.GetMonth() + 1,\r
-                               jsDate.GetDay(),\r
-                               jsDate.GetHours(),\r
-                               jsDate.GetMinutes(),\r
-                               jsDate.GetSeconds());\r
-                       break;\r
-               default:\r
-                       return FALSE;\r
-               }\r
-\r
-               vRet = swResult;\r
-               return TRUE;\r
-       }\r
-       else if (p1.GetType() == VT_string)\r
-       {\r
-               std::basic_string<wchar_t> cFormat = (wchar_t*)(FX_LPCWSTR)p1.operator CFX_WideString();                \r
-\r
-               bool bXFAPicture = false;\r
-               if (iSize > 2)\r
-               {\r
-                       //CJS_Value value;\r
-                       bXFAPicture = params[2];\r
-               }\r
-\r
-               if (bXFAPicture)\r
-               {\r
-                       return FALSE; //currently, it doesn't support XFAPicture.\r
-               }\r
-\r
-        int iIndex;\r
-               for(iIndex = 0;iIndex<sizeof(fcTable)/sizeof(stru_TbConvert);iIndex++)\r
-               {\r
-                       int iStart = 0;\r
-                       int iEnd;\r
-                       while((iEnd = cFormat.find((CFX_WideString)fcTable[iIndex].lpszJSMark, iStart)) != -1)\r
-                       {\r
-                               cFormat.replace(iEnd, FXSYS_wcslen(fcTable[iIndex].lpszJSMark), (CFX_WideString)fcTable[iIndex].lpszCppMark);\r
-                               iStart = iEnd;\r
-                       }\r
-               }\r
-\r
-               int iYear,iMonth,iDay,iHour,iMin,iSec;\r
-               iYear = jsDate.GetYear();\r
-               iMonth = jsDate.GetMonth();\r
-               iDay = jsDate.GetDay();\r
-               iHour = jsDate.GetHours();\r
-               iMin = jsDate.GetMinutes();\r
-               iSec = jsDate.GetSeconds();\r
-\r
-               struct tm time = {0};\r
-               time.tm_year = iYear-1900;\r
-               time.tm_mon = iMonth;\r
-               time.tm_mday = iDay;\r
-               time.tm_hour = iHour;\r
-               time.tm_min = iMin;\r
-               time.tm_sec = iSec;\r
-               //COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec);\r
-               //CString strFormat = cppTm.Format(cFormat.c_str());\r
-\r
-               struct stru_TbConvertAd\r
-               {\r
-                       FX_LPCWSTR lpszJSMark;\r
-                       int     iValue;\r
-               };\r
-\r
-               stru_TbConvertAd cTableAd[] ={\r
-                       (FX_LPCWSTR)L"m", iMonth+1,\r
-                               (FX_LPCWSTR)L"d", iDay,\r
-                               (FX_LPCWSTR)L"H", iHour,\r
-                               (FX_LPCWSTR)L"h", iHour>12?iHour-12:iHour,\r
-                               (FX_LPCWSTR)L"M", iMin,\r
-                               (FX_LPCWSTR)L"s", iSec\r
-               };\r
-\r
-               //cFormat = strFormat.GetBuffer(strFormat.GetLength()+1);\r
-               for(iIndex = 0;iIndex<sizeof(cTableAd)/sizeof(stru_TbConvertAd);iIndex++)\r
-               {\r
-                       wchar_t tszValue[10];\r
-                       //_itot(cTableAd[iIndex].iValue,tszValue,10);\r
-                       CFX_WideString sValue;\r
-                       sValue.Format((FX_LPCWSTR)L"%d",cTableAd[iIndex].iValue);\r
-                       memcpy(tszValue, (wchar_t *)sValue.GetBuffer(sValue.GetLength()+1),\r
-                               (sValue.GetLength()+1)*sizeof(wchar_t));\r
-\r
-                       //strFormat.Replace(cTableAd[iIndex].lpszJSMark,"%d");\r
-                       //strFormat.Format(strFormat,cTableAd[iIndex].iValue);\r
-                       int iStart = 0;\r
-                       int iEnd;\r
-                       while((iEnd = cFormat.find((CFX_WideString)cTableAd[iIndex].lpszJSMark,iStart)) != -1)\r
-                       {\r
-                               if (iEnd > 0)\r
-                               {\r
-                                       if (cFormat[iEnd-1] == L'%')\r
-                                       {\r
-                                               iStart = iEnd+1;\r
-                                               continue;\r
-                                       }\r
-                               }\r
-                               cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[iIndex].lpszJSMark), tszValue);\r
-                               iStart = iEnd;\r
-                       }\r
-               }\r
-\r
-               CFX_WideString strFormat;\r
-//             strFormat.Format((FX_LPCWSTR)L"%d,%d,%d,%d,%d,%d",iYear, iMonth, iDay, iHour, iMin, iSec);\r
-//             CString strFormat = cppTm.Format(cFormat.c_str());\r
-               wchar_t buf[64] = {0};\r
-               strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);\r
-               cFormat = buf;\r
-               vRet = (FX_LPCWSTR)cFormat.c_str();\r
-               //rtRet = strFormat.GetBuffer(strFormat.GetLength()+1);\r
-               return TRUE;\r
-       }\r
-       return FALSE;\r
-}\r
-\r
-void util::printd(const std::wstring &cFormat2, CJS_Date jsDate, bool bXFAPicture, std::wstring &cPurpose)\r
-{\r
-       std::wstring cFormat = cFormat2;\r
-           \r
-       if (bXFAPicture)\r
-       {\r
-               return ; //currently, it doesn't support XFAPicture.\r
-       }\r
-\r
-    int iIndex;\r
-       for(iIndex = 0;iIndex<sizeof(fcTable)/sizeof(stru_TbConvert);iIndex++)\r
-       {\r
-               int iStart = 0;\r
-               int iEnd;\r
-               while((iEnd = cFormat.find((CFX_WideString)fcTable[iIndex].lpszJSMark,iStart)) != -1)\r
-               {\r
-                       cFormat.replace(iEnd,FXSYS_wcslen(fcTable[iIndex].lpszJSMark), (CFX_WideString)fcTable[iIndex].lpszCppMark);\r
-                       iStart = iEnd;\r
-               }\r
-       }\r
-\r
-       int iYear,iMonth,iDay,iHour,iMin,iSec;\r
-       iYear = jsDate.GetYear();\r
-       iMonth = jsDate.GetMonth();\r
-       iDay = jsDate.GetDay();\r
-       iHour = jsDate.GetHours();\r
-       iMin = jsDate.GetMinutes();\r
-       iSec = jsDate.GetSeconds();\r
-\r
-       struct tm time = {0};\r
-       time.tm_year = iYear-1900;\r
-       time.tm_mon = iMonth;\r
-       time.tm_mday = iDay;\r
-       time.tm_hour = iHour;\r
-       time.tm_min = iMin;\r
-       time.tm_sec = iSec;\r
-//     COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec);\r
-       //CString strFormat = cppTm.Format(cFormat.c_str());\r
-\r
-       struct stru_TbConvertAd\r
-       {\r
-               FX_LPCWSTR lpszJSMark;\r
-               int     iValue;\r
-       };\r
-\r
-       stru_TbConvertAd cTableAd[] ={\r
-               (FX_LPCWSTR)L"m", iMonth+1,\r
-                       (FX_LPCWSTR)L"d", iDay,\r
-                       (FX_LPCWSTR)L"H", iHour,\r
-                       (FX_LPCWSTR)L"h", iHour>12?iHour-12:iHour,\r
-                       (FX_LPCWSTR)L"M", iMin,\r
-                       (FX_LPCWSTR)L"s", iSec\r
-       };\r
-\r
-       //cFormat = strFormat.GetBuffer(strFormat.GetLength()+1);\r
-       for(iIndex = 0;iIndex<sizeof(cTableAd)/sizeof(stru_TbConvertAd);iIndex++)\r
-       {\r
-               wchar_t tszValue[10];\r
-               //_itot(cTableAd[iIndex].iValue,tszValue,10);\r
-               CFX_WideString sValue;\r
-               sValue.Format((FX_LPCWSTR)L"%d",cTableAd[iIndex].iValue);\r
-               memcpy(tszValue, (wchar_t *)sValue.GetBuffer(sValue.GetLength()+1),sValue.GetLength()*sizeof(wchar_t));\r
-\r
-\r
-               //strFormat.Replace(cTableAd[iIndex].lpszJSMark,"%d");\r
-               //strFormat.Format(strFormat,cTableAd[iIndex].iValue);\r
-               int iStart = 0;\r
-               int iEnd;\r
-               while((iEnd = cFormat.find((CFX_WideString)cTableAd[iIndex].lpszJSMark,iStart)) != -1)\r
-               {\r
-                       if (iEnd > 0)\r
-                       {\r
-                               if (cFormat[iEnd-1] == L'%')\r
-                               {\r
-                                       iStart = iEnd+1;\r
-                                       continue;\r
-                               }\r
-                       }\r
-                       cFormat.replace(iEnd,FXSYS_wcslen(cTableAd[iIndex].lpszJSMark),tszValue);\r
-                       iStart = iEnd;\r
-               }\r
-       }\r
-\r
-               CFX_WideString strFormat;\r
-//             strFormat.Format((FX_LPCWSTR)L"%d,%d,%d,%d,%d,%d",iYear, iMonth, iDay, iHour, iMin, iSec);\r
-//             CString strFormat = cppTm.Format(cFormat.c_str());\r
-               wchar_t buf[64] = {0};\r
-               strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);\r
-               cFormat = buf;\r
-               cPurpose = cFormat;\r
-}\r
-\r
-FX_BOOL util::printx(OBJ_METHOD_PARAMS)\r
-{\r
-       int iSize = params.size();\r
-       if (iSize<2)\r
-               return FALSE;\r
-       CFX_WideString sFormat = params[0].operator CFX_WideString();\r
-       CFX_WideString sSource = params[1].operator CFX_WideString();\r
-       std::string cFormat = (FX_LPCSTR)CFX_ByteString::FromUnicode(sFormat);\r
-       std::string cSource = (FX_LPCSTR)CFX_ByteString::FromUnicode(sSource);\r
-       std::string cDest;\r
-       printx(cFormat,cSource,cDest);\r
-       vRet = cDest.c_str();\r
-       return TRUE;\r
-}\r
-\r
-void util::printx(const std::string &cFormat,const std::string &cSource2,std::string &cPurpose)\r
-{\r
-       std::string cSource(cSource2);\r
-       if (!cPurpose.empty())\r
-               //cPurpose.clear();\r
-               cPurpose.erase();\r
-       int itSource = 0;\r
-       int iSize = cSource.size();\r
-       for(int iIndex = 0; iIndex < (int)cFormat.size() && itSource<iSize; iIndex++)\r
-       {\r
-               char letter = cFormat[iIndex];\r
-               switch(letter)\r
-               {\r
-               case '?':\r
-                       //cPurpose.push_back(cSource[itSource]);\r
-                       cPurpose += cSource[itSource];\r
-                       itSource++;\r
-                       break;\r
-               case 'X':\r
-                       {\r
-                               while(itSource < iSize)\r
-                               {\r
-                                       if ((cSource[itSource]>='0'&&cSource[itSource]<='9') || (cSource[itSource]>='a' && cSource[itSource]<='z') || (cSource[itSource]>='A' && cSource[itSource]<='Z'))\r
-                                       {\r
-                                               //cPurpose.push_back(cSource[itSource]);\r
-                                               cPurpose += cSource[itSource];\r
-                                               itSource++;\r
-                                               break;\r
-                                       }\r
-                                       itSource++;\r
-                               }\r
-                               break;\r
-                       }\r
-                       break;\r
-               case 'A':\r
-                       {\r
-                               while(itSource < iSize)\r
-                               {\r
-                                       if ((cSource[itSource]>='a' && cSource[itSource]<='z') || (cSource[itSource]>='A' && cSource[itSource]<='Z'))\r
-                                       {\r
-                                               //cPurpose.push_back(cSource[itSource]);\r
-                                               cPurpose += cSource[itSource];\r
-                                               itSource++;\r
-                                               break;\r
-                                       }\r
-                                       itSource++;\r
-                               }\r
-                               break;\r
-                       }\r
-                       break;\r
-               case '9':\r
-                       {\r
-                               while(itSource < iSize)\r
-                               {\r
-                                       if (cSource[itSource]>='0'&&cSource[itSource]<='9')\r
-                                       {\r
-                                               //cPurpose.push_back(cSource[itSource]);\r
-                                               cPurpose += cSource[itSource];\r
-                                               itSource++;\r
-                                               break;\r
-                                       }\r
-                                       itSource++;\r
-                               }\r
-                               break;\r
-                       }\r
-               case '*':\r
-                       {\r
-                               cPurpose.append(cSource,itSource,iSize-itSource);\r
-                               itSource = iSize-1;\r
-                               break;\r
-                       }\r
-               case '\\':\r
-                       break;\r
-               case '>':\r
-                       {\r
-                               for(std::string::iterator it = cSource.begin();it != cSource.end(); it++)\r
-                               {\r
-                                       *it = toupper(*it);\r
-                               }\r
-                               break;\r
-                       }\r
-               case '<':\r
-                       {\r
-                               for(std::string::iterator it = cSource.begin();it != cSource.end(); it++)\r
-                               {\r
-                                       *it = tolower(*it);\r
-                               }\r
-                               break;\r
-                       }\r
-               case '=':\r
-                       break;\r
-               default:\r
-                       //cPurpose.push_back(letter);\r
-                       cPurpose += letter;\r
-                       break;\r
-               }\r
-       }\r
-}\r
-\r
-FX_BOOL util::scand(OBJ_METHOD_PARAMS)\r
-{\r
-       v8::Isolate* isolate = GetIsolate(cc);\r
-       int iSize = params.size();\r
-       if (iSize < 2)\r
-               return FALSE;\r
-       CFX_WideString sFormat = params[0].operator CFX_WideString();\r
-       CFX_WideString sDate = params[1].operator CFX_WideString();\r
-\r
-       double dDate = JS_GetDateTime();\r
-       if (sDate.GetLength() > 0)\r
-       {\r
-               FX_BOOL bWrongFormat = FALSE;\r
-               dDate = CJS_PublicMethods::MakeRegularDate(sDate,sFormat,bWrongFormat);\r
-       }       \r
-       \r
-       if (!JS_PortIsNan(dDate))\r
-       {\r
-               CJS_Date date(isolate,dDate);\r
-               vRet = date;\r
-       }\r
-       else\r
-       {\r
-               vRet.SetNull();\r
-       }\r
-\r
-       return TRUE;\r
-}\r
-\r
-FX_INT64 FX_atoi64(const char *nptr)\r
-{\r
-        int c;              /* current char */\r
-        FX_INT64 total;      /* current total */\r
-        int sign;           /* if '-', then negative, otherwise positive */\r
-\r
-        /* skip whitespace */\r
-        while ( isspace((int)(unsigned char)*nptr) )\r
-            ++nptr;\r
-\r
-        c = (int)(unsigned char)*nptr++;\r
-        sign = c;           /* save sign indication */\r
-        if (c == '-' || c == '+')\r
-            c = (int)(unsigned char)*nptr++;    /* skip sign */\r
-\r
-        total = 0;\r
-\r
-        while (isdigit(c)) {\r
-            total = 10 * total + (c - '0');     /* accumulate digit */\r
-            c = (int)(unsigned char)*nptr++;    /* get next char */\r
-        }\r
-\r
-        if (sign == '-')\r
-            return -total;\r
-        else\r
-            return total;   /* return result, negated if necessary */\r
-}\r
-\r
-FX_BOOL util::byteToChar(OBJ_METHOD_PARAMS)\r
-{\r
-       int iSize = params.size();\r
-       if (iSize == 0)\r
-               return FALSE;\r
-       int nByte = (int)params[0];\r
-       unsigned char cByte = (unsigned char)nByte;\r
-       CFX_WideString csValue;\r
-       csValue.Format((FX_LPCWSTR)L"%c", cByte);\r
-       vRet = csValue; \r
-       return TRUE;\r
-}\r
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../../include/javascript/JavaScript.h"
+#include "../../include/javascript/IJavaScript.h"
+#include "../../include/javascript/JS_Define.h"
+#include "../../include/javascript/JS_Object.h"
+#include "../../include/javascript/JS_Value.h"
+#include "../../include/javascript/util.h"
+#include "../../include/javascript/PublicMethods.h"
+#include "../../include/javascript/resource.h"
+#include "../../include/javascript/JS_Context.h"
+#include "../../include/javascript/JS_EventHandler.h"
+#include "../../include/javascript/JS_Runtime.h"
+
+#if _FX_OS_  == _FX_ANDROID_
+#include <ctype.h>
+#endif
+
+static v8::Isolate* GetIsolate(IFXJS_Context* cc)
+{
+       CJS_Context* pContext = (CJS_Context *)cc;
+       ASSERT(pContext != NULL);
+
+       CJS_Runtime* pRuntime = pContext->GetJSRuntime();
+       ASSERT(pRuntime != NULL);
+
+       return pRuntime->GetIsolate();
+}
+
+BEGIN_JS_STATIC_CONST(CJS_Util)
+END_JS_STATIC_CONST()
+
+BEGIN_JS_STATIC_PROP(CJS_Util)
+END_JS_STATIC_PROP()
+
+BEGIN_JS_STATIC_METHOD(CJS_Util)
+       JS_STATIC_METHOD_ENTRY(printd, 3)
+       JS_STATIC_METHOD_ENTRY(printf, 20)
+       JS_STATIC_METHOD_ENTRY(printx, 2)
+       JS_STATIC_METHOD_ENTRY(scand, 2)
+       JS_STATIC_METHOD_ENTRY(byteToChar, 1)
+END_JS_STATIC_METHOD()
+
+IMPLEMENT_JS_CLASS(CJS_Util,util)
+
+util::util(CJS_Object *pJSObject) : CJS_EmbedObj(pJSObject)
+{
+}
+
+util::~util(void)
+{
+}
+
+
+struct stru_TbConvert
+{
+       FX_LPCWSTR lpszJSMark;
+       FX_LPCWSTR lpszCppMark;
+};
+
+const stru_TbConvert fcTable[] = {
+       { L"mmmm", L"%B" },
+       { L"mmm", L"%b" },
+       { L"mm",  L"%m" },
+       //"m"
+       { L"dddd", L"%A" },
+       { L"ddd", L"%a" },
+       { L"dd",  L"%d" },
+       //"d",   "%w",
+       { L"yyyy", L"%Y" },
+       { L"yy",  L"%y" },
+       { L"HH",  L"%H" },
+       //"H"
+       { L"hh",  L"%I" },
+       //"h"
+       { L"MM",  L"%M" },
+       //"M"
+       { L"ss",  L"%S" },
+       //"s
+       { L"TT",  L"%p" },
+       //"t"
+#if defined(_WIN32)
+       { L"tt",  L"%p" },
+       { L"h",  L"%#I" },
+#else
+       { L"tt",  L"%P" },
+       { L"h",  L"%l" },
+#endif
+};
+
+#define UTIL_INT                       0
+#define UTIL_DOUBLE                    1
+#define UTIL_STRING                    2
+
+int util::ParstDataType(std::wstring* sFormat)
+{
+        size_t i = 0;
+       bool bPercent = FALSE;
+       for (i=0; i<sFormat->length(); ++i)
+       {
+               wchar_t c = (*sFormat)[i];
+               if (c == L'%')
+               {
+                       bPercent = true;
+                       continue;
+               }
+
+               if (bPercent)
+               {
+                       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')
+                       {
+                               return UTIL_INT;
+                       }
+                       else if (c == L'e' || c == L'E' || c == L'f' || c == L'g' || c == L'G')
+                       {
+                               return UTIL_DOUBLE;
+                       }
+                       else if (c == L's' || c == L'S')
+                       {
+                               // Map s to S since we always deal internally
+                               // with wchar_t strings.
+                               (*sFormat)[i] = L'S';
+                               return UTIL_STRING;
+                       }
+                       else if (c == L'.' || c == L'+' || c == L'-' || c == L'#' || c == L' ' || CJS_PublicMethods::IsDigit(c))
+                       {
+                               continue;
+                       }
+                       else break;
+               }
+       }
+
+       return -1;
+}
+
+FX_BOOL util::printf(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
+{
+       int iSize = params.size();
+       if (iSize < 1)
+               return FALSE;
+       std::wstring  c_ConvChar((const wchar_t*)(FX_LPCWSTR)params[0].operator CFX_WideString());
+       std::vector<std::wstring> c_strConvers;
+       int iOffset = 0;
+       int iOffend = 0;
+       c_ConvChar.insert(c_ConvChar.begin(),L'S');
+       while(iOffset != -1)
+       {
+               iOffend = c_ConvChar.find(L"%",iOffset+1);
+               std::wstring strSub;
+               if (iOffend == -1)
+                       strSub = c_ConvChar.substr(iOffset);                    
+               else
+                       strSub = c_ConvChar.substr(iOffset ,iOffend - iOffset);
+               c_strConvers.push_back(strSub);
+               iOffset = iOffend ;
+       }
+
+       std::wstring c_strResult;
+
+       //for(int iIndex = 1;iIndex < params.size();iIndex++)
+       std::wstring c_strFormat;
+       for(int iIndex = 0;iIndex < (int)c_strConvers.size();iIndex++)
+       {
+               c_strFormat = c_strConvers[iIndex];
+               if (iIndex == 0)
+               {
+                       c_strResult = c_strFormat;
+                       continue;
+               }
+
+
+               CFX_WideString strSegment;
+               if (iIndex >= iSize) {
+                       c_strResult += c_strFormat;
+                       continue;
+               }
+
+               switch (ParstDataType(&c_strFormat))
+               {
+                       case UTIL_INT:
+                               strSegment.Format(c_strFormat.c_str(),(int)params[iIndex]);
+                               break;
+                       case UTIL_DOUBLE:
+                               strSegment.Format(c_strFormat.c_str(),(double)params[iIndex]);
+                               break;
+                       case UTIL_STRING:
+                               strSegment.Format(c_strFormat.c_str(),(FX_LPCWSTR)params[iIndex].operator CFX_WideString());
+                               break;
+                       default:
+                               strSegment.Format(L"%S", c_strFormat.c_str());
+                               break;
+               }
+               c_strResult += strSegment.GetBuffer(strSegment.GetLength()+1);
+       }
+
+       c_strResult.erase(c_strResult.begin());
+       vRet = c_strResult.c_str();
+       return TRUE;
+}
+
+FX_BOOL util::printd(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
+{
+       v8::Isolate* isolate = GetIsolate(cc);
+
+       int iSize = params.size();
+       if (iSize < 2)
+               return FALSE;
+
+       CJS_Value p1(isolate);
+       p1 = params[0];
+
+       CJS_Value p2 = params[1];
+       CJS_Date jsDate(isolate);
+       if (!p2.ConvertToDate(jsDate))
+       {
+               sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT1);
+               return FALSE;
+       }
+
+       if (!jsDate.IsValidDate())
+       {
+               sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPRINT2);
+               return FALSE;
+       }
+
+       if (p1.GetType() == VT_number)
+       {
+               int nFormat = p1;
+
+               CFX_WideString swResult;
+
+               switch (nFormat)
+               {
+               case 0:
+                       swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", 
+                               jsDate.GetYear(),
+                               jsDate.GetMonth() + 1,
+                               jsDate.GetDay(),
+                               jsDate.GetHours(),
+                               jsDate.GetMinutes(),
+                               jsDate.GetSeconds());
+                       break;
+               case 1:
+                       swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", 
+                               jsDate.GetYear(),
+                               jsDate.GetMonth() + 1,
+                               jsDate.GetDay(),
+                               jsDate.GetHours(),
+                               jsDate.GetMinutes(),
+                               jsDate.GetSeconds());
+                       break;
+               case 2:
+                       swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", 
+                               jsDate.GetYear(),
+                               jsDate.GetMonth() + 1,
+                               jsDate.GetDay(),
+                               jsDate.GetHours(),
+                               jsDate.GetMinutes(),
+                               jsDate.GetSeconds());
+                       break;
+               default:
+                       return FALSE;
+               }
+
+               vRet = swResult;
+               return TRUE;
+       }
+       else if (p1.GetType() == VT_string)
+       {
+               std::basic_string<wchar_t> cFormat = (FX_LPCWSTR)p1.operator CFX_WideString();          
+
+               bool bXFAPicture = false;
+               if (iSize > 2)
+               {
+                       //CJS_Value value;
+                       bXFAPicture = params[2];
+               }
+
+               if (bXFAPicture)
+               {
+                       return FALSE; //currently, it doesn't support XFAPicture.
+               }
+
+        int iIndex;
+               for(iIndex = 0;iIndex<sizeof(fcTable)/sizeof(stru_TbConvert);iIndex++)
+               {
+                       int iStart = 0;
+                       int iEnd;
+                       while((iEnd = cFormat.find((CFX_WideString)fcTable[iIndex].lpszJSMark, iStart)) != -1)
+                       {
+                               cFormat.replace(iEnd, FXSYS_wcslen(fcTable[iIndex].lpszJSMark), (CFX_WideString)fcTable[iIndex].lpszCppMark);
+                               iStart = iEnd;
+                       }
+               }
+
+               int iYear,iMonth,iDay,iHour,iMin,iSec;
+               iYear = jsDate.GetYear();
+               iMonth = jsDate.GetMonth();
+               iDay = jsDate.GetDay();
+               iHour = jsDate.GetHours();
+               iMin = jsDate.GetMinutes();
+               iSec = jsDate.GetSeconds();
+
+               struct tm time = {0};
+               time.tm_year = iYear-1900;
+               time.tm_mon = iMonth;
+               time.tm_mday = iDay;
+               time.tm_hour = iHour;
+               time.tm_min = iMin;
+               time.tm_sec = iSec;
+               //COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec);
+               //CString strFormat = cppTm.Format(cFormat.c_str());
+
+               struct stru_TbConvertAd
+               {
+                       FX_LPCWSTR lpszJSMark;
+                       int     iValue;
+               };
+
+               stru_TbConvertAd cTableAd[] ={
+                       { L"m", iMonth+1 },
+                       { L"d", iDay },
+                       { L"H", iHour },
+                       { L"h", iHour>12?iHour-12:iHour },
+                       { L"M", iMin },
+                       { L"s", iSec },
+               };
+
+               //cFormat = strFormat.GetBuffer(strFormat.GetLength()+1);
+               for(iIndex = 0;iIndex<sizeof(cTableAd)/sizeof(stru_TbConvertAd);iIndex++)
+               {
+                       wchar_t tszValue[10];
+                       //_itot(cTableAd[iIndex].iValue,tszValue,10);
+                       CFX_WideString sValue;
+                       sValue.Format(L"%d",cTableAd[iIndex].iValue);
+                       memcpy(tszValue, (wchar_t *)sValue.GetBuffer(sValue.GetLength()+1),
+                               (sValue.GetLength()+1)*sizeof(wchar_t));
+
+                       //strFormat.Replace(cTableAd[iIndex].lpszJSMark,"%d");
+                       //strFormat.Format(strFormat,cTableAd[iIndex].iValue);
+                       int iStart = 0;
+                       int iEnd;
+                       while((iEnd = cFormat.find((CFX_WideString)cTableAd[iIndex].lpszJSMark,iStart)) != -1)
+                       {
+                               if (iEnd > 0)
+                               {
+                                       if (cFormat[iEnd-1] == L'%')
+                                       {
+                                               iStart = iEnd+1;
+                                               continue;
+                                       }
+                               }
+                               cFormat.replace(iEnd, FXSYS_wcslen(cTableAd[iIndex].lpszJSMark), tszValue);
+                               iStart = iEnd;
+                       }
+               }
+
+               CFX_WideString strFormat;
+//             strFormat.Format(L"%d,%d,%d,%d,%d,%d",iYear, iMonth, iDay, iHour, iMin, iSec);
+//             CString strFormat = cppTm.Format(cFormat.c_str());
+               wchar_t buf[64] = {0};
+               strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
+               cFormat = buf;
+               vRet = cFormat.c_str();
+               //rtRet = strFormat.GetBuffer(strFormat.GetLength()+1);
+               return TRUE;
+       }
+       return FALSE;
+}
+
+void util::printd(const std::wstring &cFormat2, CJS_Date jsDate, bool bXFAPicture, std::wstring &cPurpose)
+{
+       std::wstring cFormat = cFormat2;
+           
+       if (bXFAPicture)
+       {
+               return ; //currently, it doesn't support XFAPicture.
+       }
+
+    int iIndex;
+       for(iIndex = 0;iIndex<sizeof(fcTable)/sizeof(stru_TbConvert);iIndex++)
+       {
+               int iStart = 0;
+               int iEnd;
+               while((iEnd = cFormat.find((CFX_WideString)fcTable[iIndex].lpszJSMark,iStart)) != -1)
+               {
+                       cFormat.replace(iEnd,FXSYS_wcslen(fcTable[iIndex].lpszJSMark), (CFX_WideString)fcTable[iIndex].lpszCppMark);
+                       iStart = iEnd;
+               }
+       }
+
+       int iYear,iMonth,iDay,iHour,iMin,iSec;
+       iYear = jsDate.GetYear();
+       iMonth = jsDate.GetMonth();
+       iDay = jsDate.GetDay();
+       iHour = jsDate.GetHours();
+       iMin = jsDate.GetMinutes();
+       iSec = jsDate.GetSeconds();
+
+       struct tm time = {0};
+       time.tm_year = iYear-1900;
+       time.tm_mon = iMonth;
+       time.tm_mday = iDay;
+       time.tm_hour = iHour;
+       time.tm_min = iMin;
+       time.tm_sec = iSec;
+//     COleDateTime cppTm(iYear,iMonth+1,iDay,iHour,iMin,iSec);
+       //CString strFormat = cppTm.Format(cFormat.c_str());
+
+       struct stru_TbConvertAd
+       {
+               FX_LPCWSTR lpszJSMark;
+               int     iValue;
+       };
+
+       stru_TbConvertAd cTableAd[] ={
+               { L"m", iMonth+1 },
+               { L"d", iDay },
+               { L"H", iHour },
+               { L"h", iHour>12?iHour-12:iHour },
+               { L"M", iMin },
+               { L"s", iSec },
+       };
+
+       //cFormat = strFormat.GetBuffer(strFormat.GetLength()+1);
+       for(iIndex = 0;iIndex<sizeof(cTableAd)/sizeof(stru_TbConvertAd);iIndex++)
+       {
+               wchar_t tszValue[10];
+               //_itot(cTableAd[iIndex].iValue,tszValue,10);
+               CFX_WideString sValue;
+               sValue.Format(L"%d",cTableAd[iIndex].iValue);
+               memcpy(tszValue, (wchar_t *)sValue.GetBuffer(sValue.GetLength()+1),sValue.GetLength()*sizeof(wchar_t));
+
+
+               //strFormat.Replace(cTableAd[iIndex].lpszJSMark,"%d");
+               //strFormat.Format(strFormat,cTableAd[iIndex].iValue);
+               int iStart = 0;
+               int iEnd;
+               while((iEnd = cFormat.find((CFX_WideString)cTableAd[iIndex].lpszJSMark,iStart)) != -1)
+               {
+                       if (iEnd > 0)
+                       {
+                               if (cFormat[iEnd-1] == L'%')
+                               {
+                                       iStart = iEnd+1;
+                                       continue;
+                               }
+                       }
+                       cFormat.replace(iEnd,FXSYS_wcslen(cTableAd[iIndex].lpszJSMark),tszValue);
+                       iStart = iEnd;
+               }
+       }
+
+               CFX_WideString strFormat;
+//             strFormat.Format(L"%d,%d,%d,%d,%d,%d",iYear, iMonth, iDay, iHour, iMin, iSec);
+//             CString strFormat = cppTm.Format(cFormat.c_str());
+               wchar_t buf[64] = {0};
+               strFormat = wcsftime(buf, 64, cFormat.c_str(), &time);
+               cFormat = buf;
+               cPurpose = cFormat;
+}
+
+FX_BOOL util::printx(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
+{
+       int iSize = params.size();
+       if (iSize<2)
+               return FALSE;
+       CFX_WideString sFormat = params[0].operator CFX_WideString();
+       CFX_WideString sSource = params[1].operator CFX_WideString();
+       std::string cFormat = CFX_ByteString::FromUnicode(sFormat).c_str();
+       std::string cSource = CFX_ByteString::FromUnicode(sSource).c_str();
+       std::string cDest;
+       printx(cFormat,cSource,cDest);
+       vRet = cDest.c_str();
+       return TRUE;
+}
+
+void util::printx(const std::string &cFormat,const std::string &cSource2,std::string &cPurpose)
+{
+       std::string cSource(cSource2);
+       if (!cPurpose.empty())
+               //cPurpose.clear();
+               cPurpose.erase();
+       int itSource = 0;
+       int iSize = cSource.size();
+       for(int iIndex = 0; iIndex < (int)cFormat.size() && itSource<iSize; iIndex++)
+       {
+               char letter = cFormat[iIndex];
+               switch(letter)
+               {
+               case '?':
+                       //cPurpose.push_back(cSource[itSource]);
+                       cPurpose += cSource[itSource];
+                       itSource++;
+                       break;
+               case 'X':
+                       {
+                               while(itSource < iSize)
+                               {
+                                       if ((cSource[itSource]>='0'&&cSource[itSource]<='9') || (cSource[itSource]>='a' && cSource[itSource]<='z') || (cSource[itSource]>='A' && cSource[itSource]<='Z'))
+                                       {
+                                               //cPurpose.push_back(cSource[itSource]);
+                                               cPurpose += cSource[itSource];
+                                               itSource++;
+                                               break;
+                                       }
+                                       itSource++;
+                               }
+                               break;
+                       }
+                       break;
+               case 'A':
+                       {
+                               while(itSource < iSize)
+                               {
+                                       if ((cSource[itSource]>='a' && cSource[itSource]<='z') || (cSource[itSource]>='A' && cSource[itSource]<='Z'))
+                                       {
+                                               //cPurpose.push_back(cSource[itSource]);
+                                               cPurpose += cSource[itSource];
+                                               itSource++;
+                                               break;
+                                       }
+                                       itSource++;
+                               }
+                               break;
+                       }
+                       break;
+               case '9':
+                       {
+                               while(itSource < iSize)
+                               {
+                                       if (cSource[itSource]>='0'&&cSource[itSource]<='9')
+                                       {
+                                               //cPurpose.push_back(cSource[itSource]);
+                                               cPurpose += cSource[itSource];
+                                               itSource++;
+                                               break;
+                                       }
+                                       itSource++;
+                               }
+                               break;
+                       }
+               case '*':
+                       {
+                               cPurpose.append(cSource,itSource,iSize-itSource);
+                               itSource = iSize-1;
+                               break;
+                       }
+               case '\\':
+                       break;
+               case '>':
+                       {
+                               for(std::string::iterator it = cSource.begin();it != cSource.end(); it++)
+                               {
+                                       *it = toupper(*it);
+                               }
+                               break;
+                       }
+               case '<':
+                       {
+                               for(std::string::iterator it = cSource.begin();it != cSource.end(); it++)
+                               {
+                                       *it = tolower(*it);
+                               }
+                               break;
+                       }
+               case '=':
+                       break;
+               default:
+                       //cPurpose.push_back(letter);
+                       cPurpose += letter;
+                       break;
+               }
+       }
+}
+
+FX_BOOL util::scand(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
+{
+       v8::Isolate* isolate = GetIsolate(cc);
+       int iSize = params.size();
+       if (iSize < 2)
+               return FALSE;
+       CFX_WideString sFormat = params[0].operator CFX_WideString();
+       CFX_WideString sDate = params[1].operator CFX_WideString();
+
+       double dDate = JS_GetDateTime();
+       if (sDate.GetLength() > 0)
+       {
+               FX_BOOL bWrongFormat = FALSE;
+               dDate = CJS_PublicMethods::MakeRegularDate(sDate,sFormat,bWrongFormat);
+       }       
+       
+       if (!JS_PortIsNan(dDate))
+       {
+               CJS_Date date(isolate,dDate);
+               vRet = date;
+       }
+       else
+       {
+               vRet.SetNull();
+       }
+
+       return TRUE;
+}
+
+FX_INT64 FX_atoi64(const char *nptr)
+{
+        int c;              /* current char */
+        FX_INT64 total;      /* current total */
+        int sign;           /* if '-', then negative, otherwise positive */
+
+        /* skip whitespace */
+        while ( isspace((int)(unsigned char)*nptr) )
+            ++nptr;
+
+        c = (int)(unsigned char)*nptr++;
+        sign = c;           /* save sign indication */
+        if (c == '-' || c == '+')
+            c = (int)(unsigned char)*nptr++;    /* skip sign */
+
+        total = 0;
+
+        while (isdigit(c)) {
+            total = 10 * total + (c - '0');     /* accumulate digit */
+            c = (int)(unsigned char)*nptr++;    /* get next char */
+        }
+
+        if (sign == '-')
+            return -total;
+        else
+            return total;   /* return result, negated if necessary */
+}
+
+FX_BOOL util::byteToChar(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, JS_ErrorString& sError)
+{
+       int iSize = params.size();
+       if (iSize == 0)
+               return FALSE;
+       int nByte = (int)params[0];
+       unsigned char cByte = (unsigned char)nByte;
+       CFX_WideString csValue;
+       csValue.Format(L"%c", cByte);
+       vRet = csValue; 
+       return TRUE;
+}