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