Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: fpdfsdk/src/javascript/PublicMethods.cpp

Issue 1265503005: clang-format all pdfium code. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: sigh Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "../../include/javascript/JavaScript.h" 7 #include "../../include/javascript/JavaScript.h"
8 #include "../../include/javascript/IJavaScript.h" 8 #include "../../include/javascript/IJavaScript.h"
9 #include "../../include/javascript/JS_Define.h" 9 #include "../../include/javascript/JS_Define.h"
10 #include "../../include/javascript/JS_Object.h" 10 #include "../../include/javascript/JS_Object.h"
11 #include "../../include/javascript/JS_Value.h" 11 #include "../../include/javascript/JS_Value.h"
12 #include "../../include/javascript/PublicMethods.h" 12 #include "../../include/javascript/PublicMethods.h"
13 #include "../../include/javascript/JS_EventHandler.h" 13 #include "../../include/javascript/JS_EventHandler.h"
14 #include "../../include/javascript/resource.h" 14 #include "../../include/javascript/resource.h"
15 #include "../../include/javascript/JS_Context.h" 15 #include "../../include/javascript/JS_Context.h"
16 #include "../../include/javascript/JS_Value.h" 16 #include "../../include/javascript/JS_Value.h"
17 #include "../../include/javascript/util.h" 17 #include "../../include/javascript/util.h"
18 #include "../../include/javascript/Field.h" 18 #include "../../include/javascript/Field.h"
19 #include "../../include/javascript/color.h" 19 #include "../../include/javascript/color.h"
20 #include "../../include/javascript/JS_Runtime.h" 20 #include "../../include/javascript/JS_Runtime.h"
21 21
22 static v8::Isolate* GetIsolate(IFXJS_Context* cc) 22 static v8::Isolate* GetIsolate(IFXJS_Context* cc) {
23 { 23 CJS_Context* pContext = (CJS_Context*)cc;
24 CJS_Context* pContext = (CJS_Context *)cc; 24 ASSERT(pContext != NULL);
25 ASSERT(pContext != NULL); 25
26 26 CJS_Runtime* pRuntime = pContext->GetJSRuntime();
27 CJS_Runtime* pRuntime = pContext->GetJSRuntime(); 27 ASSERT(pRuntime != NULL);
28 ASSERT(pRuntime != NULL); 28
29 29 return pRuntime->GetIsolate();
30 return pRuntime->GetIsolate(); 30 }
31 } 31
32 32 /* -------------------------------- CJS_PublicMethods
33 33 * -------------------------------- */
34 /* -------------------------------- CJS_PublicMethods -------------------------- ------ */ 34
35 35 #define DOUBLE_CORRECT 0.000000000000001
36 #define DOUBLE_CORRECT 0.000000000000001
37 36
38 BEGIN_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods) 37 BEGIN_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
39 JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Format) 38 JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Format)
40 JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Keystroke) 39 JS_STATIC_GLOBAL_FUN_ENTRY(AFNumber_Keystroke)
41 JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Format) 40 JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Format)
42 JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Keystroke) 41 JS_STATIC_GLOBAL_FUN_ENTRY(AFPercent_Keystroke)
43 JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_FormatEx) 42 JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_FormatEx)
44 JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_KeystrokeEx) 43 JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_KeystrokeEx)
45 JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Format) 44 JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Format)
46 JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Keystroke) 45 JS_STATIC_GLOBAL_FUN_ENTRY(AFDate_Keystroke)
47 JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_FormatEx) 46 JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_FormatEx)
48 JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_KeystrokeEx) 47 JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_KeystrokeEx)
49 JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Format) 48 JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Format)
50 JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Keystroke) 49 JS_STATIC_GLOBAL_FUN_ENTRY(AFTime_Keystroke)
51 JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Format) 50 JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Format)
52 JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Keystroke) 51 JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_Keystroke)
53 JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_KeystrokeEx) 52 JS_STATIC_GLOBAL_FUN_ENTRY(AFSpecial_KeystrokeEx)
54 JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple) 53 JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple)
55 JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber) 54 JS_STATIC_GLOBAL_FUN_ENTRY(AFMakeNumber)
56 JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate) 55 JS_STATIC_GLOBAL_FUN_ENTRY(AFSimple_Calculate)
57 JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate) 56 JS_STATIC_GLOBAL_FUN_ENTRY(AFRange_Validate)
58 JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange) 57 JS_STATIC_GLOBAL_FUN_ENTRY(AFMergeChange)
59 JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx) 58 JS_STATIC_GLOBAL_FUN_ENTRY(AFParseDateEx)
60 JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums) 59 JS_STATIC_GLOBAL_FUN_ENTRY(AFExtractNums)
61 END_JS_STATIC_GLOBAL_FUN() 60 END_JS_STATIC_GLOBAL_FUN()
62 61
63 IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods) 62 IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
64 63
65 static const FX_WCHAR* months[] = 64 static const FX_WCHAR* months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
66 { 65 L"May", L"Jun", L"Jul", L"Aug",
67 L"Jan", L"Feb", L"Mar", L"Apr", L"May", L"Jun", L"Jul", L"Aug", L"Sep", L"Oc t", L"Nov", L"Dec" 66 L"Sep", L"Oct", L"Nov", L"Dec"};
68 }; 67
69 68 static const FX_WCHAR* fullmonths[] = {L"January", L"February", L"March",
70 static const FX_WCHAR* fullmonths[] = 69 L"April", L"May", L"June",
71 { 70 L"July", L"August", L"September",
72 L"January", L"February", L"March", L"April", L"May", L"June", L"July", L"Aug ust", L"September", L"October", L"November", L"December" 71 L"October", L"November", L"December"};
73 }; 72
74 73 FX_BOOL CJS_PublicMethods::IsNumber(const FX_WCHAR* string) {
75 FX_BOOL CJS_PublicMethods::IsNumber(const FX_WCHAR* string) 74 CFX_WideString sTrim = StrTrim(string);
76 { 75 const FX_WCHAR* pTrim = sTrim.c_str();
77 CFX_WideString sTrim = StrTrim(string); 76 const FX_WCHAR* p = pTrim;
78 const FX_WCHAR* pTrim = sTrim.c_str(); 77
79 const FX_WCHAR* p = pTrim; 78 FX_BOOL bDot = FALSE;
80 79 FX_BOOL bKXJS = FALSE;
81 80
82 FX_BOOL bDot = FALSE; 81 wchar_t c;
83 FX_BOOL bKXJS = FALSE; 82 while ((c = *p)) {
84 83 if (c == '.' || c == ',') {
85 wchar_t c; 84 if (bDot)
86 while ((c = *p)) 85 return FALSE;
87 { 86 bDot = TRUE;
88 if (c == '.' || c == ',') 87 } else if (c == '-' || c == '+') {
89 { 88 if (p != pTrim)
90 if (bDot) return FALSE; 89 return FALSE;
90 } else if (c == 'e' || c == 'E') {
91 if (bKXJS)
92 return FALSE;
93
94 p++;
95 c = *p;
96 if (c == '+' || c == '-') {
97 bKXJS = TRUE;
98 } else {
99 return FALSE;
100 }
101 } else if (!IsDigit(c)) {
102 return FALSE;
103 }
104 p++;
105 }
106
107 return TRUE;
108 }
109
110 FX_BOOL CJS_PublicMethods::IsDigit(wchar_t ch) {
111 return (ch >= L'0' && ch <= L'9');
112 }
113
114 FX_BOOL CJS_PublicMethods::IsDigit(char ch) {
115 return (ch >= '0' && ch <= '9');
116 }
117
118 FX_BOOL CJS_PublicMethods::IsAlphabetic(wchar_t ch) {
119 return ((ch >= L'a' && ch <= L'z') || (ch >= L'A' && ch <= L'Z'));
120 }
121
122 FX_BOOL CJS_PublicMethods::IsAlphaNumeric(wchar_t ch) {
123 return (IsDigit(ch) || IsAlphabetic(ch));
124 }
125
126 FX_BOOL CJS_PublicMethods::maskSatisfied(wchar_t c_Change, wchar_t c_Mask) {
127 switch (c_Mask) {
128 case L'9':
129 return IsDigit(c_Change);
130 case L'A':
131 return IsAlphabetic(c_Change);
132 case L'O':
133 return IsAlphaNumeric(c_Change);
134 case L'X':
135 return TRUE;
136 default:
137 return (c_Change == c_Mask);
138 }
139 }
140
141 FX_BOOL CJS_PublicMethods::isReservedMaskChar(wchar_t ch) {
142 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
143 }
144
145 double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction,
146 double dValue1,
147 double dValue2) {
148 if (FXSYS_wcsicmp(sFuction, L"AVG") == 0 ||
149 FXSYS_wcsicmp(sFuction, L"SUM") == 0) {
150 return dValue1 + dValue2;
151 }
152 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0) {
153 return dValue1 * dValue2;
154 }
155 if (FXSYS_wcsicmp(sFuction, L"MIN") == 0) {
156 return FX_MIN(dValue1, dValue2);
157 }
158 if (FXSYS_wcsicmp(sFuction, L"MAX") == 0) {
159 return FX_MAX(dValue1, dValue2);
160 }
161 return dValue1;
162 }
163
164 CFX_WideString CJS_PublicMethods::StrLTrim(const FX_WCHAR* pStr) {
165 while (*pStr && *pStr == L' ')
166 pStr++;
167
168 return pStr;
169 }
170
171 CFX_WideString CJS_PublicMethods::StrRTrim(const FX_WCHAR* pStr) {
172 const FX_WCHAR* p = pStr;
173 while (*p)
174 p++;
175 while (p > pStr && *(p - 1) == L' ')
176 p--;
177
178 return CFX_WideString(pStr, p - pStr);
179 }
180
181 CFX_WideString CJS_PublicMethods::StrTrim(const FX_WCHAR* pStr) {
182 return StrRTrim(StrLTrim(pStr).c_str());
183 }
184
185 CFX_ByteString CJS_PublicMethods::StrLTrim(const FX_CHAR* pStr) {
186 while (*pStr && *pStr == ' ')
187 pStr++;
188
189 return pStr;
190 }
191
192 CFX_ByteString CJS_PublicMethods::StrRTrim(const FX_CHAR* pStr) {
193 const FX_CHAR* p = pStr;
194 while (*p)
195 p++;
196 while (p > pStr && *(p - 1) == L' ')
197 p--;
198
199 return CFX_ByteString(pStr, p - pStr);
200 }
201
202 CFX_ByteString CJS_PublicMethods::StrTrim(const FX_CHAR* pStr) {
203 return StrRTrim(StrLTrim(pStr));
204 }
205
206 double CJS_PublicMethods::ParseNumber(const FX_WCHAR* swSource,
207 FX_BOOL& bAllDigits,
208 FX_BOOL& bDot,
209 FX_BOOL& bSign,
210 FX_BOOL& bKXJS) {
211 bDot = FALSE;
212 bSign = FALSE;
213 bKXJS = FALSE;
214
215 FX_BOOL bDigitExist = FALSE;
216
217 const FX_WCHAR* p = swSource;
218 wchar_t c;
219
220 const FX_WCHAR* pStart = NULL;
221 const FX_WCHAR* pEnd = NULL;
222
223 while ((c = *p)) {
224 if (!pStart && c != L' ') {
225 pStart = p;
226 }
227
228 pEnd = p;
229 p++;
230 }
231
232 if (!pStart) {
233 bAllDigits = FALSE;
234 return 0;
235 }
236
237 while (pEnd != pStart) {
238 if (*pEnd == L' ')
239 pEnd--;
240 else
241 break;
242 }
243
244 double dRet = 0;
245 p = pStart;
246 bAllDigits = TRUE;
247 CFX_WideString swDigits;
248
249 while (p <= pEnd) {
250 c = *p;
251
252 if (IsDigit(c)) {
253 swDigits += c;
254 bDigitExist = TRUE;
255 } else {
256 switch (c) {
257 case L' ':
258 bAllDigits = FALSE;
259 break;
260 case L'.':
261 case L',':
262 if (!bDot) {
263 if (bDigitExist) {
264 swDigits += L'.';
265 } else {
266 swDigits += L'0';
267 swDigits += L'.';
268 bDigitExist = TRUE;
269 }
270
91 bDot = TRUE; 271 bDot = TRUE;
92 } 272 break;
93 else if (c == '-' || c == '+') 273 }
94 { 274 case 'e':
95 if (p != pTrim) 275 case 'E':
96 return FALSE; 276 if (!bKXJS) {
97 }
98 else if (c == 'e' || c == 'E')
99 {
100 if (bKXJS)
101 return FALSE;
102
103 p++; 277 p++;
104 c = *p; 278 c = *p;
105 if (c == '+' || c == '-') 279 if (c == '+' || c == '-') {
106 { 280 bKXJS = TRUE;
107 bKXJS = TRUE; 281 swDigits += 'e';
282 swDigits += c;
108 } 283 }
109 else 284 break;
110 { 285 }
111 return FALSE; 286 case L'-':
287 if (!bDigitExist && !bSign) {
288 swDigits += c;
289 bSign = TRUE;
290 break;
291 }
292 default:
293 bAllDigits = FALSE;
294
295 if (p != pStart && !bDot && bDigitExist) {
296 swDigits += L'.';
297 bDot = TRUE;
298 } else {
299 bDot = FALSE;
300 bDigitExist = FALSE;
301 swDigits = L"";
302 }
303 break;
304 }
305 }
306
307 p++;
308 }
309
310 if (swDigits.GetLength() > 0 && swDigits.GetLength() < 17) {
311 CFX_ByteString sDigits = swDigits.UTF8Encode();
312
313 if (bKXJS) {
314 dRet = atof(sDigits);
315 } else {
316 if (bDot) {
317 char* pStopString;
318 dRet = ::strtod(sDigits, &pStopString);
319 } else {
320 dRet = atol(sDigits);
321 }
322 }
323 }
324
325 return dRet;
326 }
327
328 double CJS_PublicMethods::ParseStringToNumber(const FX_WCHAR* swSource) {
329 FX_BOOL bAllDigits = FALSE;
330 FX_BOOL bDot = FALSE;
331 FX_BOOL bSign = FALSE;
332 FX_BOOL bKXJS = FALSE;
333
334 return ParseNumber(swSource, bAllDigits, bDot, bSign, bKXJS);
335 }
336
337 FX_BOOL CJS_PublicMethods::ConvertStringToNumber(const FX_WCHAR* swSource,
338 double& dRet,
339 FX_BOOL& bDot) {
340 FX_BOOL bAllDigits = FALSE;
341 FX_BOOL bSign = FALSE;
342 FX_BOOL bKXJS = FALSE;
343
344 dRet = ParseNumber(swSource, bAllDigits, bDot, bSign, bKXJS);
345
346 return bAllDigits;
347 }
348
349 CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(v8::Isolate* isolate,
350 CJS_Value val) {
351 CJS_Array StrArray(isolate);
352 if (val.IsArrayObject()) {
353 val.ConvertToArray(StrArray);
354 return StrArray;
355 }
356 CFX_WideString wsStr = val.ToCFXWideString();
357 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
358 const char* p = (const char*)t;
359
360 int ch = ',';
361 int nIndex = 0;
362
363 while (*p) {
364 const char* pTemp = strchr(p, ch);
365 if (pTemp == NULL) {
366 StrArray.SetElement(nIndex, CJS_Value(isolate, StrTrim(p).c_str()));
367 break;
368 } else {
369 char* pSub = new char[pTemp - p + 1];
370 strncpy(pSub, p, pTemp - p);
371 *(pSub + (pTemp - p)) = '\0';
372
373 StrArray.SetElement(nIndex, CJS_Value(isolate, StrTrim(pSub).c_str()));
374 delete[] pSub;
375
376 nIndex++;
377 p = ++pTemp;
378 }
379 }
380 return StrArray;
381 }
382
383 int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& string,
384 int nStart,
385 int& nSkip,
386 int nMaxStep) {
387 int nRet = 0;
388 nSkip = 0;
389 for (int i = nStart, sz = string.GetLength(); i < sz; i++) {
390 if (i - nStart > 10)
391 break;
392
393 FX_WCHAR c = string.GetAt(i);
394 if (IsDigit((wchar_t)c)) {
395 nRet = nRet * 10 + (c - '0');
396 nSkip = i - nStart + 1;
397 if (nSkip >= nMaxStep)
398 break;
399 } else
400 break;
401 }
402
403 return nRet;
404 }
405
406 CFX_WideString CJS_PublicMethods::ParseStringString(
407 const CFX_WideString& string,
408 int nStart,
409 int& nSkip) {
410 CFX_WideString swRet;
411 nSkip = 0;
412 for (int i = nStart, sz = string.GetLength(); i < sz; i++) {
413 FX_WCHAR c = string.GetAt(i);
414 if ((c >= L'a' && c <= L'z') || (c >= L'A' && c <= L'Z')) {
415 swRet += c;
416 nSkip = i - nStart + 1;
417 } else
418 break;
419 }
420
421 return swRet;
422 }
423
424 double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value,
425 FX_BOOL& bWrongFormat) {
426 double dt = JS_GetDateTime();
427
428 int nYear = JS_GetYearFromTime(dt);
429 int nMonth = JS_GetMonthFromTime(dt) + 1;
430 int nDay = JS_GetDayFromTime(dt);
431 int nHour = JS_GetHourFromTime(dt);
432 int nMin = JS_GetMinFromTime(dt);
433 int nSec = JS_GetSecFromTime(dt);
434
435 int number[3];
436
437 int nSkip = 0;
438 int nLen = value.GetLength();
439 int nIndex = 0;
440 int i = 0;
441 while (i < nLen) {
442 if (nIndex > 2)
443 break;
444
445 FX_WCHAR c = value.GetAt(i);
446 if (IsDigit((wchar_t)c)) {
447 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
448 i += nSkip;
449 } else {
450 i++;
451 }
452 }
453
454 if (nIndex == 2) {
455 // case2: month/day
456 // case3: day/month
457 if ((number[0] >= 1 && number[0] <= 12) &&
458 (number[1] >= 1 && number[1] <= 31)) {
459 nMonth = number[0];
460 nDay = number[1];
461 } else if ((number[0] >= 1 && number[0] <= 31) &&
462 (number[1] >= 1 && number[1] <= 12)) {
463 nDay = number[0];
464 nMonth = number[1];
465 }
466
467 bWrongFormat = FALSE;
468 } else if (nIndex == 3) {
469 // case1: year/month/day
470 // case2: month/day/year
471 // case3: day/month/year
472
473 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) &&
474 (number[2] >= 1 && number[2] <= 31)) {
475 nYear = number[0];
476 nMonth = number[1];
477 nDay = number[2];
478 } else if ((number[0] >= 1 && number[0] <= 12) &&
479 (number[1] >= 1 && number[1] <= 31) && number[2] > 31) {
480 nMonth = number[0];
481 nDay = number[1];
482 nYear = number[2];
483 } else if ((number[0] >= 1 && number[0] <= 31) &&
484 (number[1] >= 1 && number[1] <= 12) && number[2] > 31) {
485 nDay = number[0];
486 nMonth = number[1];
487 nYear = number[2];
488 }
489
490 bWrongFormat = FALSE;
491 } else {
492 bWrongFormat = TRUE;
493 return dt;
494 }
495
496 CFX_WideString swTemp;
497 swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec);
498 return JS_DateParse(swTemp.c_str());
499 }
500
501 double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value,
502 const CFX_WideString& format,
503 FX_BOOL& bWrongFormat) {
504 double dt = JS_GetDateTime();
505
506 if (format.IsEmpty() || value.IsEmpty())
507 return dt;
508
509 int nYear = JS_GetYearFromTime(dt);
510 int nMonth = JS_GetMonthFromTime(dt) + 1;
511 int nDay = JS_GetDayFromTime(dt);
512 int nHour = JS_GetHourFromTime(dt);
513 int nMin = JS_GetMinFromTime(dt);
514 int nSec = JS_GetSecFromTime(dt);
515
516 int nYearSub = 99; // nYear - 2000;
517
518 FX_BOOL bPm = FALSE;
519 FX_BOOL bExit = FALSE;
520 bWrongFormat = FALSE;
521
522 int i = 0;
523 int j = 0;
524
525 while (i < format.GetLength()) {
526 if (bExit)
527 break;
528
529 FX_WCHAR c = format.GetAt(i);
530 switch (c) {
531 case ':':
532 case '.':
533 case '-':
534 case '\\':
535 case '/':
536 i++;
537 j++;
538 break;
539
540 case 'y':
541 case 'm':
542 case 'd':
543 case 'H':
544 case 'h':
545 case 'M':
546 case 's':
547 case 't': {
548 int oldj = j;
549 int nSkip = 0;
550 int remaining = format.GetLength() - i - 1;
551
552 if (remaining == 0 || format.GetAt(i + 1) != c) {
553 switch (c) {
554 case 'y':
555 i++;
556 j++;
557 break;
558 case 'm':
559 nMonth = ParseStringInteger(value, j, nSkip, 2);
560 i++;
561 j += nSkip;
562 break;
563 case 'd':
564 nDay = ParseStringInteger(value, j, nSkip, 2);
565 i++;
566 j += nSkip;
567 break;
568 case 'H':
569 nHour = ParseStringInteger(value, j, nSkip, 2);
570 i++;
571 j += nSkip;
572 break;
573 case 'h':
574 nHour = ParseStringInteger(value, j, nSkip, 2);
575 i++;
576 j += nSkip;
577 break;
578 case 'M':
579 nMin = ParseStringInteger(value, j, nSkip, 2);
580 i++;
581 j += nSkip;
582 break;
583 case 's':
584 nSec = ParseStringInteger(value, j, nSkip, 2);
585 i++;
586 j += nSkip;
587 break;
588 case 't':
589 bPm = (j < value.GetLength() && value.GetAt(j) == 'p');
590 i++;
591 j++;
592 break;
593 }
594 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
595 switch (c) {
596 case 'y':
597 nYear = ParseStringInteger(value, j, nSkip, 4);
598 i += 2;
599 j += nSkip;
600 break;
601 case 'm':
602 nMonth = ParseStringInteger(value, j, nSkip, 2);
603 i += 2;
604 j += nSkip;
605 break;
606 case 'd':
607 nDay = ParseStringInteger(value, j, nSkip, 2);
608 i += 2;
609 j += nSkip;
610 break;
611 case 'H':
612 nHour = ParseStringInteger(value, j, nSkip, 2);
613 i += 2;
614 j += nSkip;
615 break;
616 case 'h':
617 nHour = ParseStringInteger(value, j, nSkip, 2);
618 i += 2;
619 j += nSkip;
620 break;
621 case 'M':
622 nMin = ParseStringInteger(value, j, nSkip, 2);
623 i += 2;
624 j += nSkip;
625 break;
626 case 's':
627 nSec = ParseStringInteger(value, j, nSkip, 2);
628 i += 2;
629 j += nSkip;
630 break;
631 case 't':
632 bPm = (j + 1 < value.GetLength() && value.GetAt(j) == 'p' &&
633 value.GetAt(j + 1) == 'm');
634 i += 2;
635 j += 2;
636 break;
637 }
638 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
639 switch (c) {
640 case 'm': {
641 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
642 FX_BOOL bFind = FALSE;
643 for (int m = 0; m < 12; m++) {
644 if (sMonth.CompareNoCase(months[m]) == 0) {
645 nMonth = m + 1;
646 i += 3;
647 j += nSkip;
648 bFind = TRUE;
649 break;
650 }
651 }
652
653 if (!bFind) {
654 nMonth = ParseStringInteger(value, j, nSkip, 3);
655 i += 3;
656 j += nSkip;
657 }
658 } break;
659 case 'y':
660 break;
661 default:
662 i += 3;
663 j += 3;
664 break;
665 }
666 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
667 switch (c) {
668 case 'y':
669 nYear = ParseStringInteger(value, j, nSkip, 4);
670 j += nSkip;
671 i += 4;
672 break;
673 case 'm': {
674 FX_BOOL bFind = FALSE;
675
676 CFX_WideString sMonth = ParseStringString(value, j, nSkip);
677 sMonth.MakeLower();
678
679 for (int m = 0; m < 12; m++) {
680 CFX_WideString sFullMonths = fullmonths[m];
681 sFullMonths.MakeLower();
682
683 if (sFullMonths.Find(sMonth.c_str(), 0) != -1) {
684 nMonth = m + 1;
685 i += 4;
686 j += nSkip;
687 bFind = TRUE;
688 break;
689 }
690 }
691
692 if (!bFind) {
693 nMonth = ParseStringInteger(value, j, nSkip, 4);
694 i += 4;
695 j += nSkip;
696 }
697 } break;
698 default:
699 i += 4;
700 j += 4;
701 break;
702 }
703 } else {
704 if (j >= value.GetLength() || format.GetAt(i) != value.GetAt(j)) {
705 bWrongFormat = TRUE;
706 bExit = TRUE;
707 }
708 i++;
709 j++;
710 }
711
712 if (oldj == j) {
713 bWrongFormat = TRUE;
714 bExit = TRUE;
715 }
716 }
717
718 break;
719 default:
720 if (value.GetLength() <= j) {
721 bExit = TRUE;
722 } else if (format.GetAt(i) != value.GetAt(j)) {
723 bWrongFormat = TRUE;
724 bExit = TRUE;
725 }
726
727 i++;
728 j++;
729 break;
730 }
731 }
732
733 if (bPm)
734 nHour += 12;
735
736 if (nYear >= 0 && nYear <= nYearSub)
737 nYear += 2000;
738
739 if (nMonth < 1 || nMonth > 12)
740 bWrongFormat = TRUE;
741
742 if (nDay < 1 || nDay > 31)
743 bWrongFormat = TRUE;
744
745 if (nHour < 0 || nHour > 24)
746 bWrongFormat = TRUE;
747
748 if (nMin < 0 || nMin > 60)
749 bWrongFormat = TRUE;
750
751 if (nSec < 0 || nSec > 60)
752 bWrongFormat = TRUE;
753
754 double dRet = 0;
755
756 if (bWrongFormat) {
757 dRet = ParseNormalDate(value, bWrongFormat);
758 } else {
759 dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
760 JS_MakeTime(nHour, nMin, nSec, 0));
761
762 if (JS_PortIsNan(dRet)) {
763 dRet = JS_DateParse(value.c_str());
764 }
765 }
766
767 if (JS_PortIsNan(dRet)) {
768 dRet = ParseNormalDate(value, bWrongFormat);
769 }
770
771 return dRet;
772 }
773
774 CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
775 const CFX_WideString& format) {
776 CFX_WideString sRet = L"", sPart = L"";
777
778 int nYear = JS_GetYearFromTime(dDate);
779 int nMonth = JS_GetMonthFromTime(dDate) + 1;
780 int nDay = JS_GetDayFromTime(dDate);
781 int nHour = JS_GetHourFromTime(dDate);
782 int nMin = JS_GetMinFromTime(dDate);
783 int nSec = JS_GetSecFromTime(dDate);
784
785 int i = 0;
786 while (i < format.GetLength()) {
787 FX_WCHAR c = format.GetAt(i);
788 int remaining = format.GetLength() - i - 1;
789 sPart = L"";
790 switch (c) {
791 case 'y':
792 case 'm':
793 case 'd':
794 case 'H':
795 case 'h':
796 case 'M':
797 case 's':
798 case 't':
799 if (remaining == 0 || format.GetAt(i + 1) != c) {
800 switch (c) {
801 case 'y':
802 sPart += c;
803 break;
804 case 'm':
805 sPart.Format(L"%d", nMonth);
806 break;
807 case 'd':
808 sPart.Format(L"%d", nDay);
809 break;
810 case 'H':
811 sPart.Format(L"%d", nHour);
812 break;
813 case 'h':
814 sPart.Format(L"%d", nHour > 12 ? nHour - 12 : nHour);
815 break;
816 case 'M':
817 sPart.Format(L"%d", nMin);
818 break;
819 case 's':
820 sPart.Format(L"%d", nSec);
821 break;
822 case 't':
823 sPart += nHour > 12 ? 'p' : 'a';
824 break;
825 }
826 i++;
827 } else if (remaining == 1 || format.GetAt(i + 2) != c) {
828 switch (c) {
829 case 'y':
830 sPart.Format(L"%02d", nYear - (nYear / 100) * 100);
831 break;
832 case 'm':
833 sPart.Format(L"%02d", nMonth);
834 break;
835 case 'd':
836 sPart.Format(L"%02d", nDay);
837 break;
838 case 'H':
839 sPart.Format(L"%02d", nHour);
840 break;
841 case 'h':
842 sPart.Format(L"%02d", nHour > 12 ? nHour - 12 : nHour);
843 break;
844 case 'M':
845 sPart.Format(L"%02d", nMin);
846 break;
847 case 's':
848 sPart.Format(L"%02d", nSec);
849 break;
850 case 't':
851 sPart = nHour > 12 ? L"pm" : L"am";
852 break;
853 }
854 i += 2;
855 } else if (remaining == 2 || format.GetAt(i + 3) != c) {
856 switch (c) {
857 case 'm':
858 i += 3;
859 if (nMonth > 0 && nMonth <= 12)
860 sPart += months[nMonth - 1];
861 break;
862 default:
863 i += 3;
864 sPart += c;
865 sPart += c;
866 sPart += c;
867 break;
868 }
869 } else if (remaining == 3 || format.GetAt(i + 4) != c) {
870 switch (c) {
871 case 'y':
872 sPart.Format(L"%04d", nYear);
873 i += 4;
874 break;
875 case 'm':
876 i += 4;
877 if (nMonth > 0 && nMonth <= 12)
878 sPart += fullmonths[nMonth - 1];
879 break;
880 default:
881 i += 4;
882 sPart += c;
883 sPart += c;
884 sPart += c;
885 sPart += c;
886 break;
887 }
888 } else {
889 i++;
890 sPart += c;
891 }
892 break;
893 default:
894 i++;
895 sPart += c;
896 break;
897 }
898
899 sRet += sPart;
900 }
901
902 return sRet;
903 }
904
905 /* -------------------------------------------------------------------------- */
906
907 // function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
908 // bCurrencyPrepend)
909 FX_BOOL CJS_PublicMethods::AFNumber_Format(IFXJS_Context* cc,
910 const CJS_Parameters& params,
911 CJS_Value& vRet,
912 CFX_WideString& sError) {
913 #if _FX_OS_ != _FX_ANDROID_
914 v8::Isolate* isolate = ::GetIsolate(cc);
915 CJS_Context* pContext = (CJS_Context*)cc;
916 ASSERT(pContext != NULL);
917 CJS_EventHandler* pEvent = pContext->GetEventHandler();
918 ASSERT(pEvent != NULL);
919
920 if (params.size() != 6) {
921 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
922 return FALSE;
923 }
924 if (!pEvent->m_pValue)
925 return FALSE;
926 CFX_WideString& Value = pEvent->Value();
927 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
928
929 if (strValue.IsEmpty())
930 return TRUE;
931
932 int iDec = params[0].ToInt();
933 int iSepStyle = params[1].ToInt();
934 int iNegStyle = params[2].ToInt();
935 // params[3] is iCurrStyle, it's not used.
936 std::wstring wstrCurrency(params[4].ToCFXWideString().c_str());
937 FX_BOOL bCurrencyPrepend = params[5].ToBool();
938
939 if (iDec < 0)
940 iDec = -iDec;
941
942 if (iSepStyle < 0 || iSepStyle > 3)
943 iSepStyle = 0;
944
945 if (iNegStyle < 0 || iNegStyle > 3)
946 iNegStyle = 0;
947
948 //////////////////////////////////////////////////////
949 // for processing decimal places
950 strValue.Replace(",", ".");
951 double dValue = atof(strValue);
952 if (iDec > 0)
953 dValue += DOUBLE_CORRECT;
954
955 int iDec2;
956 int iNegative = 0;
957
958 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
959 if (strValue.IsEmpty()) {
960 dValue = 0;
961 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
962 if (strValue.IsEmpty()) {
963 strValue = "0";
964 iDec2 = 1;
965 }
966 }
967
968 if (iDec2 < 0) {
969 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
970 strValue = "0" + strValue;
971 }
972 iDec2 = 0;
973 }
974 int iMax = strValue.GetLength();
975 if (iDec2 > iMax) {
976 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
977 strValue += "0";
978 }
979 iMax = iDec2 + 1;
980 }
981 ///////////////////////////////////////////////////////
982 // for processing seperator style
983 if (iDec2 < iMax) {
984 if (iSepStyle == 0 || iSepStyle == 1) {
985 strValue.Insert(iDec2, '.');
986 iMax++;
987 } else if (iSepStyle == 2 || iSepStyle == 3) {
988 strValue.Insert(iDec2, ',');
989 iMax++;
990 }
991
992 if (iDec2 == 0)
993 strValue.Insert(iDec2, '0');
994 }
995 if (iSepStyle == 0 || iSepStyle == 2) {
996 char cSeperator;
997 if (iSepStyle == 0)
998 cSeperator = ',';
999 else
1000 cSeperator = '.';
1001
1002 int iDecPositive;
1003 iDecPositive = iDec2;
1004
1005 for (iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
1006 strValue.Insert(iDecPositive, cSeperator);
1007 iMax++;
1008 }
1009 }
1010
1011 //////////////////////////////////////////////////////////////////////
1012 // for processing currency string
1013
1014 Value = CFX_WideString::FromLocal(strValue);
1015 std::wstring strValue2 = Value.c_str();
1016
1017 if (bCurrencyPrepend)
1018 strValue2 = wstrCurrency + strValue2;
1019 else
1020 strValue2 = strValue2 + wstrCurrency;
1021
1022 /////////////////////////////////////////////////////////////////////////
1023 // for processing negative style
1024 if (iNegative) {
1025 if (iNegStyle == 0) {
1026 strValue2.insert(0, L"-");
1027 }
1028 if (iNegStyle == 2 || iNegStyle == 3) {
1029 strValue2.insert(0, L"(");
1030 strValue2.insert(strValue2.length(), L")");
1031 }
1032 if (iNegStyle == 1 || iNegStyle == 3) {
1033 if (Field* fTarget = pEvent->Target_Field()) {
1034 CJS_Array arColor(isolate);
1035 CJS_Value vColElm(isolate);
1036 vColElm = L"RGB";
1037 arColor.SetElement(0, vColElm);
1038 vColElm = 1;
1039 arColor.SetElement(1, vColElm);
1040 vColElm = 0;
1041 arColor.SetElement(2, vColElm);
1042
1043 arColor.SetElement(3, vColElm);
1044
1045 CJS_PropValue vProp(isolate);
1046 vProp.StartGetting();
1047 vProp << arColor;
1048 vProp.StartSetting();
1049 fTarget->textColor(cc, vProp, sError); // red
1050 }
1051 }
1052 } else {
1053 if (iNegStyle == 1 || iNegStyle == 3) {
1054 if (Field* fTarget = pEvent->Target_Field()) {
1055 CJS_Array arColor(isolate);
1056 CJS_Value vColElm(isolate);
1057 vColElm = L"RGB";
1058 arColor.SetElement(0, vColElm);
1059 vColElm = 0;
1060 arColor.SetElement(1, vColElm);
1061 arColor.SetElement(2, vColElm);
1062 arColor.SetElement(3, vColElm);
1063
1064 CJS_PropValue vProp(isolate);
1065 vProp.StartGetting();
1066 fTarget->textColor(cc, vProp, sError);
1067
1068 CJS_Array aProp(isolate);
1069 vProp.ConvertToArray(aProp);
1070
1071 CPWL_Color crProp;
1072 CPWL_Color crColor;
1073 color::ConvertArrayToPWLColor(aProp, crProp);
1074 color::ConvertArrayToPWLColor(arColor, crColor);
1075
1076 if (crColor != crProp) {
1077 CJS_PropValue vProp2(isolate);
1078 vProp2.StartGetting();
1079 vProp2 << arColor;
1080 vProp2.StartSetting();
1081 fTarget->textColor(cc, vProp2, sError);
1082 }
1083 }
1084 }
1085 }
1086 Value = strValue2.c_str();
1087 #endif
1088 return TRUE;
1089 }
1090
1091 // function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
1092 // bCurrencyPrepend)
1093 FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(IFXJS_Context* cc,
1094 const CJS_Parameters& params,
1095 CJS_Value& vRet,
1096 CFX_WideString& sError) {
1097 CJS_Context* pContext = (CJS_Context*)cc;
1098 ASSERT(pContext != NULL);
1099 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1100 ASSERT(pEvent != NULL);
1101
1102 if (params.size() < 2)
1103 return FALSE;
1104 int iSepStyle = params[1].ToInt();
1105
1106 if (iSepStyle < 0 || iSepStyle > 3)
1107 iSepStyle = 0;
1108 if (!pEvent->m_pValue)
1109 return FALSE;
1110 CFX_WideString& val = pEvent->Value();
1111 CFX_WideString& w_strChange = pEvent->Change();
1112 CFX_WideString w_strValue = val;
1113
1114 if (pEvent->WillCommit()) {
1115 CFX_WideString wstrChange = w_strChange;
1116 CFX_WideString wstrValue = StrLTrim(w_strValue.c_str());
1117 if (wstrValue.IsEmpty())
1118 return TRUE;
1119
1120 CFX_WideString swTemp = wstrValue;
1121 swTemp.Replace(L",", L".");
1122 if (!IsNumber(swTemp.c_str())) {
1123 pEvent->Rc() = FALSE;
1124 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE);
1125 Alert(pContext, sError.c_str());
1126 return TRUE;
1127 }
1128 return TRUE; // it happens after the last keystroke and before validating,
1129 }
1130
1131 std::wstring w_strValue2 = w_strValue.c_str();
1132 std::wstring w_strChange2 = w_strChange.c_str();
1133 std::wstring w_strSelected;
1134 if (-1 != pEvent->SelStart())
1135 w_strSelected = w_strValue2.substr(pEvent->SelStart(),
1136 (pEvent->SelEnd() - pEvent->SelStart()));
1137 FX_BOOL bHasSign =
1138 (w_strValue2.find('-') != -1) && (w_strSelected.find('-') == -1);
1139 if (bHasSign) {
1140 // can't insert "change" in front to sign postion.
1141 if (pEvent->SelStart() == 0) {
1142 FX_BOOL& bRc = pEvent->Rc();
1143 bRc = FALSE;
1144 return TRUE;
1145 }
1146 }
1147
1148 char cSep = L'.';
1149
1150 switch (iSepStyle) {
1151 case 0:
1152 case 1:
1153 cSep = L'.';
1154 break;
1155 case 2:
1156 case 3:
1157 cSep = L',';
1158 break;
1159 }
1160
1161 FX_BOOL bHasSep = (w_strValue2.find(cSep) != -1);
1162 for (std::wstring::iterator it = w_strChange2.begin();
1163 it != w_strChange2.end(); it++) {
1164 if (*it == cSep) {
1165 if (bHasSep) {
1166 FX_BOOL& bRc = pEvent->Rc();
1167 bRc = FALSE;
1168 return TRUE;
1169 }
1170 bHasSep = TRUE;
1171 continue;
1172 }
1173 if (*it == L'-') {
1174 if (bHasSign) {
1175 FX_BOOL& bRc = pEvent->Rc();
1176 bRc = FALSE;
1177 return TRUE;
1178 }
1179 if (it != w_strChange2.begin()) // sign's position is not correct
1180 {
1181 FX_BOOL& bRc = pEvent->Rc();
1182 bRc = FALSE;
1183 return TRUE;
1184 }
1185 if (pEvent->SelStart() != 0) {
1186 FX_BOOL& bRc = pEvent->Rc();
1187 bRc = FALSE;
1188 return TRUE;
1189 }
1190 bHasSign = TRUE;
1191 continue;
1192 }
1193
1194 if (!IsDigit(*it)) {
1195 FX_BOOL& bRc = pEvent->Rc();
1196 bRc = FALSE;
1197 return TRUE;
1198 }
1199 }
1200
1201 std::wstring w_prefix = w_strValue2.substr(0, pEvent->SelStart());
1202 std::wstring w_postfix;
1203 if (pEvent->SelEnd() < (int)w_strValue2.length())
1204 w_postfix = w_strValue2.substr(pEvent->SelEnd());
1205 w_strValue2 = w_prefix + w_strChange2 + w_postfix;
1206 w_strValue = w_strValue2.c_str();
1207 val = w_strValue;
1208 return TRUE;
1209 }
1210
1211 // function AFPercent_Format(nDec, sepStyle)
1212 FX_BOOL CJS_PublicMethods::AFPercent_Format(IFXJS_Context* cc,
1213 const CJS_Parameters& params,
1214 CJS_Value& vRet,
1215 CFX_WideString& sError) {
1216 #if _FX_OS_ != _FX_ANDROID_
1217 CJS_Context* pContext = (CJS_Context*)cc;
1218 ASSERT(pContext != NULL);
1219 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1220 ASSERT(pEvent != NULL);
1221
1222 if (params.size() != 2) {
1223 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1224 return FALSE;
1225 }
1226 if (!pEvent->m_pValue)
1227 return FALSE;
1228
1229 CFX_WideString& Value = pEvent->Value();
1230 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1231 if (strValue.IsEmpty())
1232 return TRUE;
1233
1234 int iDec = params[0].ToInt();
1235 if (iDec < 0)
1236 iDec = -iDec;
1237
1238 int iSepStyle = params[1].ToInt();
1239 if (iSepStyle < 0 || iSepStyle > 3)
1240 iSepStyle = 0;
1241
1242 //////////////////////////////////////////////////////
1243 // for processing decimal places
1244 double dValue = atof(strValue);
1245 dValue *= 100;
1246 if (iDec > 0)
1247 dValue += DOUBLE_CORRECT; //УÕý
1248
1249 int iDec2;
1250 int iNegative = 0;
1251 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1252 if (strValue.IsEmpty()) {
1253 dValue = 0;
1254 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1255 }
1256
1257 if (iDec2 < 0) {
1258 for (int iNum = 0; iNum < abs(iDec2); iNum++) {
1259 strValue = "0" + strValue;
1260 }
1261 iDec2 = 0;
1262 }
1263 int iMax = strValue.GetLength();
1264 if (iDec2 > iMax) {
1265 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++) {
1266 strValue += "0";
1267 }
1268 iMax = iDec2 + 1;
1269 }
1270 ///////////////////////////////////////////////////////
1271 // for processing seperator style
1272 if (iDec2 < iMax) {
1273 if (iSepStyle == 0 || iSepStyle == 1) {
1274 strValue.Insert(iDec2, '.');
1275 iMax++;
1276 } else if (iSepStyle == 2 || iSepStyle == 3) {
1277 strValue.Insert(iDec2, ',');
1278 iMax++;
1279 }
1280
1281 if (iDec2 == 0)
1282 strValue.Insert(iDec2, '0');
1283 }
1284 if (iSepStyle == 0 || iSepStyle == 2) {
1285 char cSeperator;
1286 if (iSepStyle == 0)
1287 cSeperator = ',';
1288 else
1289 cSeperator = '.';
1290
1291 int iDecPositive;
1292 iDecPositive = iDec2;
1293
1294 for (iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
1295 strValue.Insert(iDecPositive, cSeperator);
1296 iMax++;
1297 }
1298 }
1299 ////////////////////////////////////////////////////////////////////
1300 // negative mark
1301 if (iNegative)
1302 strValue = "-" + strValue;
1303 strValue += "%";
1304 Value = CFX_WideString::FromLocal(strValue);
1305 #endif
1306 return TRUE;
1307 }
1308 // AFPercent_Keystroke(nDec, sepStyle)
1309 FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(IFXJS_Context* cc,
1310 const CJS_Parameters& params,
1311 CJS_Value& vRet,
1312 CFX_WideString& sError) {
1313 return AFNumber_Keystroke(cc, params, vRet, sError);
1314 }
1315
1316 // function AFDate_FormatEx(cFormat)
1317 FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IFXJS_Context* cc,
1318 const CJS_Parameters& params,
1319 CJS_Value& vRet,
1320 CFX_WideString& sError) {
1321 CJS_Context* pContext = (CJS_Context*)cc;
1322 ASSERT(pContext != NULL);
1323 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1324 ASSERT(pEvent != NULL);
1325
1326 if (params.size() != 1) {
1327 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1328 return FALSE;
1329 }
1330 if (!pEvent->m_pValue)
1331 return FALSE;
1332
1333 CFX_WideString& val = pEvent->Value();
1334 CFX_WideString strValue = val;
1335 if (strValue.IsEmpty())
1336 return TRUE;
1337
1338 CFX_WideString sFormat = params[0].ToCFXWideString();
1339 FX_BOOL bWrongFormat = FALSE;
1340 double dDate = 0.0f;
1341
1342 if (strValue.Find(L"GMT") != -1) {
1343 // for GMT format time
1344 // such as "Tue Aug 11 14:24:16 GMT+08002009"
1345 dDate = MakeInterDate(strValue);
1346 } else {
1347 dDate = MakeRegularDate(strValue, sFormat, bWrongFormat);
1348 }
1349
1350 if (JS_PortIsNan(dDate)) {
1351 CFX_WideString swMsg;
1352 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1353 sFormat.c_str());
1354 Alert(pContext, swMsg.c_str());
1355 return FALSE;
1356 }
1357
1358 val = MakeFormatDate(dDate, sFormat);
1359 return TRUE;
1360 }
1361
1362 double CJS_PublicMethods::MakeInterDate(CFX_WideString strValue) {
1363 int nHour;
1364 int nMin;
1365 int nSec;
1366 int nYear;
1367 int nMonth;
1368 int nDay;
1369
1370 CFX_WideStringArray wsArray;
1371 CFX_WideString sMonth = L"";
1372 CFX_WideString sTemp = L"";
1373 int nSize = strValue.GetLength();
1374
1375 for (int i = 0; i < nSize; i++) {
1376 FX_WCHAR c = strValue.GetAt(i);
1377 if (c == L' ' || c == L':') {
1378 wsArray.Add(sTemp);
1379 sTemp = L"";
1380 continue;
1381 }
1382
1383 sTemp += c;
1384 }
1385
1386 wsArray.Add(sTemp);
1387 if (wsArray.GetSize() != 8)
1388 return 0;
1389
1390 sTemp = wsArray[1];
1391 if (sTemp.Compare(L"Jan") == 0)
1392 nMonth = 1;
1393 if (sTemp.Compare(L"Feb") == 0)
1394 nMonth = 2;
1395 if (sTemp.Compare(L"Mar") == 0)
1396 nMonth = 3;
1397 if (sTemp.Compare(L"Apr") == 0)
1398 nMonth = 4;
1399 if (sTemp.Compare(L"May") == 0)
1400 nMonth = 5;
1401 if (sTemp.Compare(L"Jun") == 0)
1402 nMonth = 6;
1403 if (sTemp.Compare(L"Jul") == 0)
1404 nMonth = 7;
1405 if (sTemp.Compare(L"Aug") == 0)
1406 nMonth = 8;
1407 if (sTemp.Compare(L"Sep") == 0)
1408 nMonth = 9;
1409 if (sTemp.Compare(L"Oct") == 0)
1410 nMonth = 10;
1411 if (sTemp.Compare(L"Nov") == 0)
1412 nMonth = 11;
1413 if (sTemp.Compare(L"Dec") == 0)
1414 nMonth = 12;
1415
1416 nDay = (int)ParseStringToNumber(wsArray[2].c_str());
1417 nHour = (int)ParseStringToNumber(wsArray[3].c_str());
1418 nMin = (int)ParseStringToNumber(wsArray[4].c_str());
1419 nSec = (int)ParseStringToNumber(wsArray[5].c_str());
1420 nYear = (int)ParseStringToNumber(wsArray[7].c_str());
1421
1422 double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay),
1423 JS_MakeTime(nHour, nMin, nSec, 0));
1424
1425 if (JS_PortIsNan(dRet)) {
1426 dRet = JS_DateParse(strValue.c_str());
1427 }
1428
1429 return dRet;
1430 }
1431
1432 // AFDate_KeystrokeEx(cFormat)
1433 FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(IFXJS_Context* cc,
1434 const CJS_Parameters& params,
1435 CJS_Value& vRet,
1436 CFX_WideString& sError) {
1437 CJS_Context* pContext = (CJS_Context*)cc;
1438 ASSERT(pContext != NULL);
1439 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1440 ASSERT(pEvent != NULL);
1441
1442 if (params.size() != 1) {
1443 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
1444 return FALSE;
1445 }
1446
1447 if (pEvent->WillCommit()) {
1448 if (!pEvent->m_pValue)
1449 return FALSE;
1450 CFX_WideString strValue = pEvent->Value();
1451 if (strValue.IsEmpty())
1452 return TRUE;
1453
1454 CFX_WideString sFormat = params[0].ToCFXWideString();
1455 FX_BOOL bWrongFormat = FALSE;
1456 double dRet = MakeRegularDate(strValue, sFormat, bWrongFormat);
1457 if (bWrongFormat || JS_PortIsNan(dRet)) {
1458 CFX_WideString swMsg;
1459 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1460 sFormat.c_str());
1461 Alert(pContext, swMsg.c_str());
1462 pEvent->Rc() = FALSE;
1463 return TRUE;
1464 }
1465 }
1466 return TRUE;
1467 }
1468
1469 FX_BOOL CJS_PublicMethods::AFDate_Format(IFXJS_Context* cc,
1470 const CJS_Parameters& params,
1471 CJS_Value& vRet,
1472 CFX_WideString& sError) {
1473 v8::Isolate* isolate = ::GetIsolate(cc);
1474
1475 if (params.size() != 1) {
1476 CJS_Context* pContext = (CJS_Context*)cc;
1477 ASSERT(pContext != NULL);
1478
1479 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1480 return FALSE;
1481 }
1482
1483 int iIndex = params[0].ToInt();
1484 const FX_WCHAR* cFormats[] = {L"m/d",
1485 L"m/d/yy",
1486 L"mm/dd/yy",
1487 L"mm/yy",
1488 L"d-mmm",
1489 L"d-mmm-yy",
1490 L"dd-mmm-yy",
1491 L"yy-mm-dd",
1492 L"mmm-yy",
1493 L"mmmm-yy",
1494 L"mmm d, yyyy",
1495 L"mmmm d, yyyy",
1496 L"m/d/yy h:MM tt",
1497 L"m/d/yy HH:MM"};
1498
1499 ASSERT(iIndex < FX_ArraySize(cFormats));
1500
1501 if (iIndex < 0)
1502 iIndex = 0;
1503 if (iIndex >= FX_ArraySize(cFormats))
1504 iIndex = 0;
1505 CJS_Parameters newParams;
1506 CJS_Value val(isolate, cFormats[iIndex]);
1507 newParams.push_back(val);
1508 return AFDate_FormatEx(cc, newParams, vRet, sError);
1509 }
1510
1511 // AFDate_KeystrokeEx(cFormat)
1512 FX_BOOL CJS_PublicMethods::AFDate_Keystroke(IFXJS_Context* cc,
1513 const CJS_Parameters& params,
1514 CJS_Value& vRet,
1515 CFX_WideString& sError) {
1516 v8::Isolate* isolate = ::GetIsolate(cc);
1517
1518 if (params.size() != 1) {
1519 CJS_Context* pContext = (CJS_Context*)cc;
1520 ASSERT(pContext != NULL);
1521
1522 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1523 return FALSE;
1524 }
1525
1526 int iIndex = params[0].ToInt();
1527 const FX_WCHAR* cFormats[] = {L"m/d",
1528 L"m/d/yy",
1529 L"mm/dd/yy",
1530 L"mm/yy",
1531 L"d-mmm",
1532 L"d-mmm-yy",
1533 L"dd-mmm-yy",
1534 L"yy-mm-dd",
1535 L"mmm-yy",
1536 L"mmmm-yy",
1537 L"mmm d, yyyy",
1538 L"mmmm d, yyyy",
1539 L"m/d/yy h:MM tt",
1540 L"m/d/yy HH:MM"};
1541
1542 ASSERT(iIndex < FX_ArraySize(cFormats));
1543
1544 if (iIndex < 0)
1545 iIndex = 0;
1546 if (iIndex >= FX_ArraySize(cFormats))
1547 iIndex = 0;
1548 CJS_Parameters newParams;
1549 CJS_Value val(isolate, cFormats[iIndex]);
1550 newParams.push_back(val);
1551 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1552 }
1553
1554 // function AFTime_Format(ptf)
1555 FX_BOOL CJS_PublicMethods::AFTime_Format(IFXJS_Context* cc,
1556 const CJS_Parameters& params,
1557 CJS_Value& vRet,
1558 CFX_WideString& sError) {
1559 v8::Isolate* isolate = ::GetIsolate(cc);
1560
1561 if (params.size() != 1) {
1562 CJS_Context* pContext = (CJS_Context*)cc;
1563 ASSERT(pContext != NULL);
1564 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1565 return FALSE;
1566 }
1567
1568 int iIndex = params[0].ToInt();
1569 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1570 L"h:MM:ss tt"};
1571
1572 ASSERT(iIndex < FX_ArraySize(cFormats));
1573
1574 if (iIndex < 0)
1575 iIndex = 0;
1576 if (iIndex >= FX_ArraySize(cFormats))
1577 iIndex = 0;
1578 CJS_Parameters newParams;
1579 CJS_Value val(isolate, cFormats[iIndex]);
1580 newParams.push_back(val);
1581 return AFDate_FormatEx(cc, newParams, vRet, sError);
1582 }
1583
1584 FX_BOOL CJS_PublicMethods::AFTime_Keystroke(IFXJS_Context* cc,
1585 const CJS_Parameters& params,
1586 CJS_Value& vRet,
1587 CFX_WideString& sError) {
1588 v8::Isolate* isolate = ::GetIsolate(cc);
1589 if (params.size() != 1) {
1590 CJS_Context* pContext = (CJS_Context*)cc;
1591 ASSERT(pContext != NULL);
1592 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1593 return FALSE;
1594 }
1595
1596 int iIndex = params[0].ToInt();
1597 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
1598 L"h:MM:ss tt"};
1599
1600 ASSERT(iIndex < FX_ArraySize(cFormats));
1601
1602 if (iIndex < 0)
1603 iIndex = 0;
1604 if (iIndex >= FX_ArraySize(cFormats))
1605 iIndex = 0;
1606 CJS_Parameters newParams;
1607 CJS_Value val(isolate, cFormats[iIndex]);
1608 newParams.push_back(val);
1609 return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
1610 }
1611
1612 FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IFXJS_Context* cc,
1613 const CJS_Parameters& params,
1614 CJS_Value& vRet,
1615 CFX_WideString& sError) {
1616 return AFDate_FormatEx(cc, params, vRet, sError);
1617 }
1618
1619 FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(IFXJS_Context* cc,
1620 const CJS_Parameters& params,
1621 CJS_Value& vRet,
1622 CFX_WideString& sError) {
1623 return AFDate_KeystrokeEx(cc, params, vRet, sError);
1624 }
1625
1626 // function AFSpecial_Format(psf)
1627 FX_BOOL CJS_PublicMethods::AFSpecial_Format(IFXJS_Context* cc,
1628 const CJS_Parameters& params,
1629 CJS_Value& vRet,
1630 CFX_WideString& sError) {
1631 CJS_Context* pContext = (CJS_Context*)cc;
1632 ASSERT(pContext != NULL);
1633
1634 if (params.size() != 1) {
1635 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1636 return FALSE;
1637 }
1638
1639 std::string cFormat;
1640 int iIndex = params[0].ToInt();
1641
1642 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1643 ASSERT(pEvent != NULL);
1644
1645 if (!pEvent->m_pValue)
1646 return FALSE;
1647 CFX_WideString& Value = pEvent->Value();
1648 std::string strSrc = CFX_ByteString::FromUnicode(Value).c_str();
1649
1650 switch (iIndex) {
1651 case 0:
1652 cFormat = "99999";
1653 break;
1654 case 1:
1655 cFormat = "99999-9999";
1656 break;
1657 case 2: {
1658 std::string NumberStr;
1659 util::printx("9999999999", strSrc, NumberStr);
1660 if (NumberStr.length() >= 10)
1661 cFormat = "(999) 999-9999";
1662 else
1663 cFormat = "999-9999";
1664 break;
1665 }
1666 case 3:
1667 cFormat = "999-99-9999";
1668 break;
1669 }
1670
1671 std::string strDes;
1672 util::printx(cFormat, strSrc, strDes);
1673 Value = CFX_WideString::FromLocal(strDes.c_str());
1674 return TRUE;
1675 }
1676
1677 // function AFSpecial_KeystrokeEx(mask)
1678 FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(IFXJS_Context* cc,
1679 const CJS_Parameters& params,
1680 CJS_Value& vRet,
1681 CFX_WideString& sError) {
1682 CJS_Context* pContext = (CJS_Context*)cc;
1683 ASSERT(pContext != NULL);
1684 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1685
1686 ASSERT(pEvent != NULL);
1687
1688 if (params.size() < 1) {
1689 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1690 return FALSE;
1691 }
1692
1693 if (!pEvent->m_pValue)
1694 return FALSE;
1695 CFX_WideString& valEvent = pEvent->Value();
1696
1697 CFX_WideString wstrMask = params[0].ToCFXWideString();
1698 if (wstrMask.IsEmpty())
1699 return TRUE;
1700
1701 std::wstring wstrValue = valEvent.c_str();
1702
1703 if (pEvent->WillCommit()) {
1704 if (wstrValue.empty())
1705 return TRUE;
1706 int iIndexMask = 0;
1707 for (std::wstring::iterator it = wstrValue.begin(); it != wstrValue.end();
1708 it++) {
1709 wchar_t w_Value = *it;
1710 if (!maskSatisfied(w_Value, wstrMask[iIndexMask]))
1711 break;
1712 iIndexMask++;
1713 }
1714
1715 if (iIndexMask != wstrMask.GetLength() ||
1716 (iIndexMask != wstrValue.size() && wstrMask.GetLength() != 0)) {
1717 Alert(
1718 pContext,
1719 JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
1720 pEvent->Rc() = FALSE;
1721 }
1722 return TRUE;
1723 }
1724
1725 CFX_WideString& wideChange = pEvent->Change();
1726 std::wstring wChange = wideChange.c_str();
1727 if (wChange.empty())
1728 return TRUE;
1729
1730 int iIndexMask = pEvent->SelStart();
1731
1732 if (wstrValue.length() - (pEvent->SelEnd() - pEvent->SelStart()) +
1733 wChange.length() >
1734 (FX_DWORD)wstrMask.GetLength()) {
1735 Alert(pContext,
1736 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1737 pEvent->Rc() = FALSE;
1738 return TRUE;
1739 }
1740
1741 if (iIndexMask >= wstrMask.GetLength() && (!wChange.empty())) {
1742 Alert(pContext,
1743 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1744 pEvent->Rc() = FALSE;
1745 return TRUE;
1746 }
1747
1748 for (std::wstring::iterator it = wChange.begin(); it != wChange.end(); it++) {
1749 if (iIndexMask >= wstrMask.GetLength()) {
1750 Alert(pContext,
1751 JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
1752 pEvent->Rc() = FALSE;
1753 return TRUE;
1754 }
1755 wchar_t w_Mask = wstrMask[iIndexMask];
1756 if (!isReservedMaskChar(w_Mask)) {
1757 *it = w_Mask;
1758 }
1759 wchar_t w_Change = *it;
1760 if (!maskSatisfied(w_Change, w_Mask)) {
1761 pEvent->Rc() = FALSE;
1762 return TRUE;
1763 }
1764 iIndexMask++;
1765 }
1766
1767 wideChange = wChange.c_str();
1768 return TRUE;
1769 }
1770
1771 // function AFSpecial_Keystroke(psf)
1772 FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(IFXJS_Context* cc,
1773 const CJS_Parameters& params,
1774 CJS_Value& vRet,
1775 CFX_WideString& sError) {
1776 v8::Isolate* isolate = ::GetIsolate(cc);
1777
1778 CJS_Context* pContext = (CJS_Context*)cc;
1779 ASSERT(pContext != NULL);
1780 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1781 ASSERT(pEvent != NULL);
1782
1783 if (params.size() != 1) {
1784 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1785 return FALSE;
1786 }
1787
1788 std::string cFormat;
1789 int iIndex = params[0].ToInt();
1790
1791 if (!pEvent->m_pValue)
1792 return FALSE;
1793 // CJS_Value val = pEvent->Value();
1794 CFX_WideString& val = pEvent->Value();
1795 std::string strSrc = CFX_ByteString::FromUnicode(val).c_str();
1796 std::wstring wstrChange = pEvent->Change().c_str();
1797
1798 switch (iIndex) {
1799 case 0:
1800 cFormat = "99999";
1801 break;
1802 case 1:
1803 // cFormat = "99999-9999";
1804 cFormat = "999999999";
1805 break;
1806 case 2: {
1807 std::string NumberStr;
1808 util::printx("9999999999", strSrc, NumberStr);
1809 if (strSrc.length() + wstrChange.length() > 7)
1810 // cFormat = "(999) 999-9999";
1811 cFormat = "9999999999";
1812 else
1813 // cFormat = "999-9999";
1814 cFormat = "9999999";
1815 break;
1816 }
1817 case 3:
1818 // cFormat = "999-99-9999";
1819 cFormat = "999999999";
1820 break;
1821 }
1822
1823 CJS_Parameters params2;
1824 CJS_Value vMask(isolate, cFormat.c_str());
1825 params2.push_back(vMask);
1826
1827 return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
1828 }
1829
1830 FX_BOOL CJS_PublicMethods::AFMergeChange(IFXJS_Context* cc,
1831 const CJS_Parameters& params,
1832 CJS_Value& vRet,
1833 CFX_WideString& sError) {
1834 CJS_Context* pContext = (CJS_Context*)cc;
1835 ASSERT(pContext != NULL);
1836 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
1837 ASSERT(pEventHandler != NULL);
1838
1839 if (params.size() != 1) {
1840 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1841 return FALSE;
1842 }
1843
1844 CFX_WideString swValue;
1845 if (pEventHandler->m_pValue != NULL)
1846 swValue = pEventHandler->Value();
1847
1848 if (pEventHandler->WillCommit()) {
1849 vRet = swValue.c_str();
1850 return TRUE;
1851 }
1852
1853 CFX_WideString prefix, postfix;
1854
1855 if (pEventHandler->SelStart() >= 0)
1856 prefix = swValue.Mid(0, pEventHandler->SelStart());
1857 else
1858 prefix = L"";
1859
1860 if (pEventHandler->SelEnd() >= 0 &&
1861 pEventHandler->SelEnd() <= swValue.GetLength())
1862 postfix = swValue.Mid(pEventHandler->SelEnd(),
1863 swValue.GetLength() - pEventHandler->SelEnd());
1864 else
1865 postfix = L"";
1866
1867 vRet = (prefix + pEventHandler->Change() + postfix).c_str();
1868
1869 return TRUE;
1870 }
1871
1872 FX_BOOL CJS_PublicMethods::AFParseDateEx(IFXJS_Context* cc,
1873 const CJS_Parameters& params,
1874 CJS_Value& vRet,
1875 CFX_WideString& sError) {
1876 CJS_Context* pContext = (CJS_Context*)cc;
1877 ASSERT(pContext != NULL);
1878
1879 if (params.size() != 2) {
1880 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1881 return FALSE;
1882 }
1883
1884 CFX_WideString sValue = params[0].ToCFXWideString();
1885 CFX_WideString sFormat = params[1].ToCFXWideString();
1886
1887 FX_BOOL bWrongFormat = FALSE;
1888 double dDate = MakeRegularDate(sValue, sFormat, bWrongFormat);
1889
1890 if (JS_PortIsNan(dDate)) {
1891 CFX_WideString swMsg;
1892 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str(),
1893 sFormat.c_str());
1894 Alert((CJS_Context*)cc, swMsg.c_str());
1895 return FALSE;
1896 }
1897
1898 vRet = dDate;
1899 return TRUE;
1900 }
1901
1902 FX_BOOL CJS_PublicMethods::AFSimple(IFXJS_Context* cc,
1903 const CJS_Parameters& params,
1904 CJS_Value& vRet,
1905 CFX_WideString& sError) {
1906 if (params.size() != 3) {
1907 CJS_Context* pContext = (CJS_Context*)cc;
1908 ASSERT(pContext != NULL);
1909
1910 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1911 return FALSE;
1912 }
1913
1914 vRet = (double)AF_Simple(params[0].ToCFXWideString().c_str(),
1915 params[1].ToDouble(), params[2].ToDouble());
1916 return TRUE;
1917 }
1918
1919 FX_BOOL CJS_PublicMethods::AFMakeNumber(IFXJS_Context* cc,
1920 const CJS_Parameters& params,
1921 CJS_Value& vRet,
1922 CFX_WideString& sError) {
1923 if (params.size() != 1) {
1924 CJS_Context* pContext = (CJS_Context*)cc;
1925 ASSERT(pContext != NULL);
1926
1927 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1928 return FALSE;
1929 }
1930 vRet = ParseStringToNumber(params[0].ToCFXWideString().c_str());
1931 return TRUE;
1932 }
1933
1934 FX_BOOL CJS_PublicMethods::AFSimple_Calculate(IFXJS_Context* cc,
1935 const CJS_Parameters& params,
1936 CJS_Value& vRet,
1937 CFX_WideString& sError) {
1938 v8::Isolate* isolate = ::GetIsolate(cc);
1939
1940 CJS_Context* pContext = (CJS_Context*)cc;
1941 ASSERT(pContext != NULL);
1942
1943 if (params.size() != 2) {
1944 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1945 return FALSE;
1946 }
1947
1948 CJS_Value params1 = params[1];
1949
1950 if (!params1.IsArrayObject() && params1.GetType() != VT_string) {
1951 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1952 return FALSE;
1953 }
1954
1955 CPDFSDK_Document* pReaderDoc = pContext->GetReaderDocument();
1956 ASSERT(pReaderDoc != NULL);
1957
1958 CPDFSDK_InterForm* pReaderInterForm = pReaderDoc->GetInterForm();
1959 ASSERT(pReaderInterForm != NULL);
1960
1961 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
1962 ASSERT(pInterForm != NULL);
1963
1964 double dValue;
1965 CFX_WideString sFunction = params[0].ToCFXWideString();
1966 if (wcscmp(sFunction.c_str(), L"PRD") == 0)
1967 dValue = 1.0;
1968 else
1969 dValue = 0.0;
1970
1971 CJS_Array FieldNameArray = AF_MakeArrayFromList(isolate, params1);
1972
1973 int nFieldsCount = 0;
1974
1975 for (int i = 0, isz = FieldNameArray.GetLength(); i < isz; i++) {
1976 CJS_Value jsValue(isolate);
1977 FieldNameArray.GetElement(i, jsValue);
1978 CFX_WideString wsFieldName = jsValue.ToCFXWideString();
1979
1980 for (int j = 0, jsz = pInterForm->CountFields(wsFieldName); j < jsz; j++) {
1981 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName)) {
1982 double dTemp = 0.0;
1983
1984 switch (pFormField->GetFieldType()) {
1985 case FIELDTYPE_TEXTFIELD:
1986 case FIELDTYPE_COMBOBOX: {
1987 dTemp = ParseStringToNumber(pFormField->GetValue().c_str());
1988 break;
1989 }
1990 case FIELDTYPE_PUSHBUTTON: {
1991 dTemp = 0.0;
1992 break;
1993 }
1994 case FIELDTYPE_CHECKBOX:
1995 case FIELDTYPE_RADIOBUTTON: {
1996 dTemp = 0.0;
1997 for (int c = 0, csz = pFormField->CountControls(); c < csz; c++) {
1998 if (CPDF_FormControl* pFormCtrl = pFormField->GetControl(c)) {
1999 if (pFormCtrl->IsChecked()) {
2000 dTemp +=
2001 ParseStringToNumber(pFormCtrl->GetExportValue().c_str());
2002 break;
2003 } else
2004 continue;
2005 }
112 } 2006 }
113 }
114 else if (!IsDigit(c))
115 {
116 return FALSE;
117 }
118 p++;
119 }
120
121 return TRUE;
122 }
123
124 FX_BOOL CJS_PublicMethods::IsDigit(wchar_t ch)
125 {
126 return (ch >= L'0' && ch <= L'9');
127 }
128
129 FX_BOOL CJS_PublicMethods::IsDigit(char ch)
130 {
131 return (ch >= '0' && ch <= '9');
132 }
133
134 FX_BOOL CJS_PublicMethods::IsAlphabetic(wchar_t ch)
135 {
136 return ((ch >= L'a' && ch <= L'z') || (ch >= L'A' && ch <= L'Z'));
137 }
138
139 FX_BOOL CJS_PublicMethods::IsAlphaNumeric(wchar_t ch)
140 {
141 return (IsDigit(ch) || IsAlphabetic(ch));
142 }
143
144 FX_BOOL CJS_PublicMethods::maskSatisfied(wchar_t c_Change,wchar_t c_Mask)
145 {
146 switch (c_Mask)
147 {
148 case L'9':
149 return IsDigit(c_Change);
150 case L'A':
151 return IsAlphabetic(c_Change);
152 case L'O':
153 return IsAlphaNumeric(c_Change);
154 case L'X':
155 return TRUE;
156 default:
157 return (c_Change == c_Mask);
158 }
159 }
160
161 FX_BOOL CJS_PublicMethods::isReservedMaskChar(wchar_t ch)
162 {
163 return ch == L'9' || ch == L'A' || ch == L'O' || ch == L'X';
164 }
165
166 double CJS_PublicMethods::AF_Simple(const FX_WCHAR* sFuction, double dValue1, do uble dValue2)
167 {
168 if (FXSYS_wcsicmp(sFuction,L"AVG") == 0 || FXSYS_wcsicmp(sFuction,L"SUM") == 0)
169 {
170 return dValue1 + dValue2;
171 }
172 if (FXSYS_wcsicmp(sFuction, L"PRD") == 0)
173 {
174 return dValue1 * dValue2;
175 }
176 if (FXSYS_wcsicmp(sFuction,L"MIN") == 0)
177 {
178 return FX_MIN(dValue1, dValue2);
179 }
180 if (FXSYS_wcsicmp(sFuction,L"MAX") == 0)
181 {
182 return FX_MAX(dValue1, dValue2);
183 }
184 return dValue1;
185 }
186
187 CFX_WideString CJS_PublicMethods::StrLTrim(const FX_WCHAR* pStr)
188 {
189 while (*pStr && *pStr == L' ') pStr++;
190
191 return pStr;
192 }
193
194 CFX_WideString CJS_PublicMethods::StrRTrim(const FX_WCHAR* pStr)
195 {
196 const FX_WCHAR* p = pStr;
197 while (*p) p++;
198 while (p > pStr && *(p - 1) == L' ') p--;
199
200 return CFX_WideString(pStr, p - pStr);
201 }
202
203 CFX_WideString CJS_PublicMethods::StrTrim(const FX_WCHAR* pStr)
204 {
205 return StrRTrim(StrLTrim(pStr).c_str());
206 }
207
208 CFX_ByteString CJS_PublicMethods::StrLTrim(const FX_CHAR* pStr)
209 {
210 while (*pStr && *pStr == ' ') pStr++;
211
212 return pStr;
213 }
214
215 CFX_ByteString CJS_PublicMethods::StrRTrim(const FX_CHAR* pStr)
216 {
217 const FX_CHAR* p = pStr;
218 while (*p) p++;
219 while (p > pStr && *(p - 1) == L' ') p--;
220
221 return CFX_ByteString(pStr,p-pStr);
222 }
223
224 CFX_ByteString CJS_PublicMethods::StrTrim(const FX_CHAR* pStr)
225 {
226 return StrRTrim(StrLTrim(pStr));
227 }
228
229 double CJS_PublicMethods::ParseNumber(const FX_WCHAR* swSource, FX_BOOL& bAllDig its, FX_BOOL& bDot, FX_BOOL& bSign, FX_BOOL& bKXJS)
230 {
231 bDot = FALSE;
232 bSign = FALSE;
233 bKXJS = FALSE;
234
235 FX_BOOL bDigitExist = FALSE;
236
237 const FX_WCHAR* p = swSource;
238 wchar_t c;
239
240 const FX_WCHAR* pStart = NULL;
241 const FX_WCHAR* pEnd = NULL;
242
243 while ((c = *p))
244 {
245 if (!pStart && c != L' ')
246 {
247 pStart = p;
248 }
249
250 pEnd = p;
251 p++;
252 }
253
254 if (!pStart)
255 {
256 bAllDigits = FALSE;
257 return 0;
258 }
259
260 while (pEnd != pStart)
261 {
262 if (*pEnd == L' ')
263 pEnd --;
264 else
265 break; 2007 break;
266 } 2008 }
267 2009 case FIELDTYPE_LISTBOX: {
268 double dRet = 0; 2010 dTemp = 0.0;
269 p = pStart; 2011 if (pFormField->CountSelectedItems() > 1)
270 bAllDigits = TRUE; 2012 break;
271 CFX_WideString swDigits; 2013 else {
272 2014 dTemp = ParseStringToNumber(pFormField->GetValue().c_str());
273 while (p <= pEnd) 2015 break;
274 {
275 c = *p;
276
277 if (IsDigit(c))
278 {
279 swDigits += c;
280 bDigitExist = TRUE;
281 }
282 else
283 {
284 switch (c)
285 {
286 case L' ':
287 bAllDigits = FALSE;
288 break;
289 case L'.':
290 case L',':
291 if (!bDot)
292 {
293 if (bDigitExist)
294 {
295 swDigits += L'.';
296 }
297 else
298 {
299 swDigits += L'0';
300 swDigits += L'.';
301 bDigitExist = TRUE;
302 }
303
304 bDot = TRUE;
305 break;
306 }
307 case 'e':
308 case 'E':
309 if (!bKXJS)
310 {
311 p++;
312 c = *p;
313 if (c == '+' || c == '-')
314 {
315 bKXJS = TRUE;
316 swDigits += 'e';
317 swDigits += c;
318 }
319 break;
320 }
321 case L'-':
322 if (!bDigitExist && !bSign)
323 {
324 swDigits += c;
325 bSign = TRUE;
326 break;
327 }
328 default:
329 bAllDigits = FALSE;
330
331 if (p != pStart && !bDot && bDigitExist)
332 {
333 swDigits += L'.';
334 bDot = TRUE;
335 }
336 else
337 {
338 bDot = FALSE;
339 bDigitExist = FALSE;
340 swDigits = L"";
341 }
342 break;
343 } 2016 }
344 } 2017 }
345 2018 default:
346 p++;
347 }
348
349 if (swDigits.GetLength() > 0 && swDigits.GetLength() < 17)
350 {
351 CFX_ByteString sDigits = swDigits.UTF8Encode();
352
353 if (bKXJS)
354 {
355 dRet = atof(sDigits);
356 }
357 else
358 {
359 if (bDot)
360 {
361 char* pStopString;
362 dRet = ::strtod(sDigits, &pStopString);
363 }
364 else
365 {
366 dRet = atol(sDigits);
367 }
368 }
369
370 }
371
372 return dRet;
373 }
374
375 double CJS_PublicMethods::ParseStringToNumber(const FX_WCHAR* swSource)
376 {
377 FX_BOOL bAllDigits = FALSE;
378 FX_BOOL bDot = FALSE;
379 FX_BOOL bSign = FALSE;
380 FX_BOOL bKXJS = FALSE;
381
382 return ParseNumber(swSource, bAllDigits, bDot, bSign, bKXJS);
383 }
384
385 FX_BOOL CJS_PublicMethods::ConvertStringToNumber(const FX_WCHAR* swSource, doubl e & dRet, FX_BOOL & bDot)
386 {
387 FX_BOOL bAllDigits = FALSE;
388 FX_BOOL bSign = FALSE;
389 FX_BOOL bKXJS = FALSE;
390
391 dRet = ParseNumber(swSource, bAllDigits, bDot, bSign, bKXJS);
392
393 return bAllDigits;
394 }
395
396 CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(v8::Isolate* isolate, CJS_Valu e val)
397 {
398 CJS_Array StrArray(isolate);
399 if(val.IsArrayObject())
400 {
401 val.ConvertToArray(StrArray);
402 return StrArray;
403 }
404 CFX_WideString wsStr = val.ToCFXWideString();
405 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
406 const char * p = (const char *)t;
407
408
409 int ch = ',' ;
410 int nIndex = 0;
411
412 while (*p)
413 {
414 const char * pTemp = strchr(p, ch);
415 if (pTemp == NULL)
416 {
417 StrArray.SetElement(nIndex, CJS_Value(isolate, StrTrim(p).c_str()));
418 break; 2019 break;
419 } 2020 }
420 else 2021
421 { 2022 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 ||
422 char * pSub = new char[pTemp - p + 1]; 2023 wcscmp(sFunction.c_str(), L"MAX") == 0))
423 strncpy(pSub, p, pTemp - p); 2024 dValue = dTemp;
424 *(pSub + (pTemp - p)) = '\0'; 2025
425 2026 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
426 StrArray.SetElement(nIndex, CJS_Value(isolate, StrTrim(pSub).c_str() )); 2027
427 delete []pSub; 2028 nFieldsCount++;
428 2029 }
429 nIndex ++; 2030 }
430 p = ++pTemp; 2031 }
431 } 2032
432 2033 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
433 } 2034 dValue /= nFieldsCount;
434 return StrArray; 2035
435 } 2036 dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
436 2037 FXSYS_pow((double)10, (double)6);
437 int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& string,int nStar t,int& nSkip, int nMaxStep) 2038 CJS_Value jsValue(isolate, dValue);
438 { 2039 if ((CJS_EventHandler*)pContext->GetEventHandler()->m_pValue)
439 int nRet = 0; 2040 ((CJS_EventHandler*)pContext->GetEventHandler())->Value() =
440 nSkip = 0; 2041 jsValue.ToCFXWideString();
441 for (int i=nStart, sz=string.GetLength(); i < sz; i++) 2042
442 { 2043 return TRUE;
443 if (i-nStart > 10)
444 break;
445
446 FX_WCHAR c = string.GetAt(i);
447 if (IsDigit((wchar_t)c))
448 {
449 nRet = nRet * 10 + (c - '0');
450 nSkip = i - nStart + 1;
451 if (nSkip >= nMaxStep)
452 break;
453 }
454 else
455 break;
456 }
457
458 return nRet;
459 }
460
461 CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& string , int nStart, int& nSkip)
462 {
463 CFX_WideString swRet;
464 nSkip = 0;
465 for (int i=nStart, sz=string.GetLength(); i < sz; i++)
466 {
467 FX_WCHAR c = string.GetAt(i);
468 if ((c >= L'a' && c <= L'z') || (c >= L'A' && c <= L'Z'))
469 {
470 swRet += c;
471 nSkip = i - nStart + 1;
472 }
473 else
474 break;
475 }
476
477 return swRet;
478 }
479
480 double CJS_PublicMethods::ParseNormalDate(const CFX_WideString & value, FX_BOOL& bWrongFormat)
481 {
482 double dt = JS_GetDateTime();
483
484 int nYear = JS_GetYearFromTime(dt);
485 int nMonth = JS_GetMonthFromTime(dt) + 1;
486 int nDay = JS_GetDayFromTime(dt);
487 int nHour = JS_GetHourFromTime(dt);
488 int nMin = JS_GetMinFromTime(dt);
489 int nSec = JS_GetSecFromTime(dt);
490
491 int number[3];
492
493 int nSkip = 0;
494 int nLen = value.GetLength();
495 int nIndex = 0;
496 int i = 0;
497 while (i < nLen)
498 {
499 if (nIndex > 2) break;
500
501 FX_WCHAR c = value.GetAt(i);
502 if (IsDigit((wchar_t)c))
503 {
504 number[nIndex++] = ParseStringInteger(value, i, nSkip, 4);
505 i += nSkip;
506 }
507 else
508 {
509 i ++;
510 }
511 }
512
513 if (nIndex == 2)
514 {
515 // case2: month/day
516 // case3: day/month
517 if ((number[0] >= 1 && number[0] <= 12) && (number[1] >= 1 && number[1] <= 31))
518 {
519 nMonth = number[0];
520 nDay = number[1];
521 }
522 else if ((number[0] >= 1 && number[0] <= 31) && (number[1] >= 1 && numbe r[1] <= 12))
523 {
524 nDay = number[0];
525 nMonth = number[1];
526 }
527
528 bWrongFormat = FALSE;
529 }
530 else if (nIndex == 3)
531 {
532 // case1: year/month/day
533 // case2: month/day/year
534 // case3: day/month/year
535
536 if (number[0] > 12 && (number[1] >= 1 && number[1] <= 12) && (number[2] >= 1 && number[2] <= 31))
537 {
538 nYear = number[0];
539 nMonth = number[1];
540 nDay = number[2];
541 }
542 else if ((number[0] >= 1 && number[0] <= 12) && (number[1] >= 1 && numbe r[1] <= 31) && number[2] > 31)
543 {
544 nMonth = number[0];
545 nDay = number[1];
546 nYear = number[2];
547 }
548 else if ((number[0] >= 1 && number[0] <= 31) && (number[1] >= 1 && numbe r[1] <= 12) && number[2] > 31)
549 {
550 nDay = number[0];
551 nMonth = number[1];
552 nYear = number[2];
553 }
554
555 bWrongFormat = FALSE;
556 }
557 else
558 {
559 bWrongFormat = TRUE;
560 return dt;
561 }
562
563 CFX_WideString swTemp;
564 swTemp.Format(L"%d/%d/%d %d:%d:%d",nMonth,nDay,nYear,nHour,nMin,nSec);
565 return JS_DateParse(swTemp.c_str());
566 }
567
568 double CJS_PublicMethods::MakeRegularDate(const CFX_WideString & value, const CF X_WideString & format, FX_BOOL& bWrongFormat)
569 {
570 double dt = JS_GetDateTime();
571
572 if (format.IsEmpty() || value.IsEmpty())
573 return dt;
574
575 int nYear = JS_GetYearFromTime(dt);
576 int nMonth = JS_GetMonthFromTime(dt) + 1;
577 int nDay = JS_GetDayFromTime(dt);
578 int nHour = JS_GetHourFromTime(dt);
579 int nMin = JS_GetMinFromTime(dt);
580 int nSec = JS_GetSecFromTime(dt);
581
582 int nYearSub = 99; //nYear - 2000;
583
584 FX_BOOL bPm = FALSE;
585 FX_BOOL bExit = FALSE;
586 bWrongFormat = FALSE;
587
588 int i=0;
589 int j=0;
590
591 while (i < format.GetLength())
592 {
593 if (bExit) break;
594
595 FX_WCHAR c = format.GetAt(i);
596 switch (c)
597 {
598 case ':':
599 case '.':
600 case '-':
601 case '\\':
602 case '/':
603 i++;
604 j++;
605 break;
606
607 case 'y':
608 case 'm':
609 case 'd':
610 case 'H':
611 case 'h':
612 case 'M':
613 case 's':
614 case 't':
615 {
616 int oldj = j;
617 int nSkip = 0;
618 int remaining = format.GetLength() - i - 1;
619
620 if (remaining == 0 || format.GetAt(i+1) != c)
621 {
622 switch (c)
623 {
624 case 'y':
625 i++;
626 j++;
627 break;
628 case 'm':
629 nMonth = ParseStringInteger(value, j, nSkip, 2);
630 i++;
631 j += nSkip;
632 break;
633 case 'd':
634 nDay = ParseStringInteger(value, j, nSkip, 2);
635 i++;
636 j += nSkip;
637 break;
638 case 'H':
639 nHour = ParseStringInteger(value, j, nSkip, 2);
640 i++;
641 j += nSkip;
642 break;
643 case 'h':
644 nHour = ParseStringInteger(value, j, nSkip, 2);
645 i++;
646 j += nSkip;
647 break;
648 case 'M':
649 nMin = ParseStringInteger(value, j, nSkip, 2);
650 i++;
651 j += nSkip;
652 break;
653 case 's':
654 nSec = ParseStringInteger(value, j, nSkip, 2);
655 i++;
656 j += nSkip;
657 break;
658 case 't':
659 bPm = (j < value.GetLength() && value.GetAt(j) = = 'p');
660 i++;
661 j++;
662 break;
663 }
664 }
665 else if (remaining == 1 || format.GetAt(i+2) != c)
666 {
667 switch (c)
668 {
669 case 'y':
670 nYear = ParseStringInteger(value, j, nSkip, 4);
671 i += 2;
672 j += nSkip;
673 break;
674 case 'm':
675 nMonth = ParseStringInteger(value, j, nSkip, 2);
676 i += 2;
677 j += nSkip;
678 break;
679 case 'd':
680 nDay = ParseStringInteger(value, j, nSkip, 2);
681 i += 2;
682 j += nSkip;
683 break;
684 case 'H':
685 nHour = ParseStringInteger(value, j, nSkip, 2);
686 i += 2;
687 j += nSkip;
688 break;
689 case 'h':
690 nHour = ParseStringInteger(value, j, nSkip, 2);
691 i += 2;
692 j += nSkip;
693 break;
694 case 'M':
695 nMin = ParseStringInteger(value, j, nSkip, 2);
696 i += 2;
697 j += nSkip;
698 break;
699 case 's':
700 nSec = ParseStringInteger(value, j, nSkip, 2);
701 i += 2;
702 j += nSkip;
703 break;
704 case 't':
705 bPm = (j + 1 < value.GetLength() && value.GetAt( j) == 'p' && value.GetAt(j+1) == 'm');
706 i += 2;
707 j += 2;
708 break;
709 }
710 }
711 else if (remaining == 2 || format.GetAt(i+3) != c)
712 {
713 switch (c)
714 {
715 case 'm':
716 {
717 CFX_WideString sMonth = ParseStringString(va lue, j, nSkip);
718 FX_BOOL bFind = FALSE;
719 for (int m = 0; m < 12; m++)
720 {
721 if (sMonth.CompareNoCase(months[m]) == 0 )
722 {
723 nMonth = m + 1;
724 i+=3;
725 j+=nSkip;
726 bFind = TRUE;
727 break;
728 }
729 }
730
731 if (!bFind)
732 {
733 nMonth = ParseStringInteger(value, j, nS kip, 3);
734 i+=3;
735 j += nSkip;
736 }
737 }
738 break;
739 case 'y':
740 break;
741 default:
742 i+=3;
743 j+=3;
744 break;
745 }
746 }
747 else if (remaining == 3 || format.GetAt(i+4) != c)
748 {
749 switch (c)
750 {
751
752
753 case 'y':
754 nYear = ParseStringInteger(value, j, nSkip, 4);
755 j += nSkip;
756 i += 4;
757 break;
758 case 'm':
759 {
760 FX_BOOL bFind = FALSE;
761
762 CFX_WideString sMonth = ParseStringString(va lue, j, nSkip);
763 sMonth.MakeLower();
764
765 for (int m = 0; m < 12; m++)
766 {
767 CFX_WideString sFullMonths = fullmonths[ m];
768 sFullMonths.MakeLower();
769
770 if (sFullMonths.Find(sMonth.c_str(), 0) != -1)
771 {
772 nMonth = m + 1;
773 i += 4;
774 j += nSkip;
775 bFind = TRUE;
776 break;
777 }
778 }
779
780 if (!bFind)
781 {
782 nMonth = ParseStringInteger(value, j, nS kip, 4);
783 i+=4;
784 j += nSkip;
785 }
786 }
787 break;
788 default:
789 i += 4;
790 j += 4;
791 break;
792 }
793 }
794 else
795 {
796 if (j >= value.GetLength() || format.GetAt(i) != value.G etAt(j))
797 {
798 bWrongFormat = TRUE;
799 bExit = TRUE;
800 }
801 i++;
802 j++;
803 }
804
805 if (oldj == j)
806 {
807 bWrongFormat = TRUE;
808 bExit = TRUE;
809 }
810 }
811
812 break;
813 default:
814 if (value.GetLength() <= j)
815 {
816 bExit = TRUE;
817 }
818 else if (format.GetAt(i) != value.GetAt(j))
819 {
820 bWrongFormat = TRUE;
821 bExit = TRUE;
822 }
823
824 i++;
825 j++;
826 break;
827 }
828 }
829
830 if (bPm) nHour += 12;
831
832 if (nYear >= 0 && nYear <= nYearSub)
833 nYear += 2000;
834
835 if (nMonth < 1 || nMonth > 12)
836 bWrongFormat = TRUE;
837
838 if (nDay < 1 || nDay > 31)
839 bWrongFormat = TRUE;
840
841 if (nHour < 0 || nHour > 24)
842 bWrongFormat = TRUE;
843
844 if (nMin < 0 || nMin > 60)
845 bWrongFormat = TRUE;
846
847 if (nSec < 0 || nSec > 60)
848 bWrongFormat = TRUE;
849
850 double dRet = 0;
851
852 if (bWrongFormat)
853 {
854 dRet = ParseNormalDate(value, bWrongFormat);
855 }
856 else
857 {
858 dRet = JS_MakeDate(JS_MakeDay(nYear,nMonth - 1,nDay),JS_MakeTime(nHour, nMin, nSec, 0));
859
860 if (JS_PortIsNan(dRet))
861 {
862 dRet = JS_DateParse(value.c_str());
863 }
864 }
865
866 if (JS_PortIsNan(dRet))
867 {
868 dRet = ParseNormalDate(value, bWrongFormat);
869 }
870
871 return dRet;
872
873 }
874
875 CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate, const CFX_WideStr ing & format)
876 {
877 CFX_WideString sRet = L"",sPart = L"";
878
879 int nYear = JS_GetYearFromTime(dDate);
880 int nMonth = JS_GetMonthFromTime(dDate) + 1;
881 int nDay = JS_GetDayFromTime(dDate);
882 int nHour = JS_GetHourFromTime(dDate);
883 int nMin = JS_GetMinFromTime(dDate);
884 int nSec = JS_GetSecFromTime(dDate);
885
886 int i = 0;
887 while (i < format.GetLength())
888 {
889 FX_WCHAR c = format.GetAt(i);
890 int remaining = format.GetLength() - i - 1;
891 sPart = L"";
892 switch (c)
893 {
894 case 'y':
895 case 'm':
896 case 'd':
897 case 'H':
898 case 'h':
899 case 'M':
900 case 's':
901 case 't':
902 if (remaining == 0 || format.GetAt(i+1) != c)
903 {
904 switch (c)
905 {
906 case 'y':
907 sPart += c;
908 break;
909 case 'm':
910 sPart.Format(L"%d",nMonth);
911 break;
912 case 'd':
913 sPart.Format(L"%d",nDay);
914 break;
915 case 'H':
916 sPart.Format(L"%d",nHour);
917 break;
918 case 'h':
919 sPart.Format(L"%d",nHour>12?nHour - 12:nHour);
920 break;
921 case 'M':
922 sPart.Format(L"%d",nMin);
923 break;
924 case 's':
925 sPart.Format(L"%d",nSec);
926 break;
927 case 't':
928 sPart += nHour>12?'p':'a';
929 break;
930 }
931 i++;
932 }
933 else if (remaining == 1 || format.GetAt(i+2) != c)
934 {
935 switch (c)
936 {
937 case 'y':
938 sPart.Format(L"%02d",nYear - (nYear / 100) * 100);
939 break;
940 case 'm':
941 sPart.Format(L"%02d",nMonth);
942 break;
943 case 'd':
944 sPart.Format(L"%02d",nDay);
945 break;
946 case 'H':
947 sPart.Format(L"%02d",nHour);
948 break;
949 case 'h':
950 sPart.Format(L"%02d",nHour>12?nHour - 12:nHour);
951 break;
952 case 'M':
953 sPart.Format(L"%02d",nMin);
954 break;
955 case 's':
956 sPart.Format(L"%02d",nSec);
957 break;
958 case 't':
959 sPart = nHour>12? L"pm": L"am";
960 break;
961 }
962 i+=2;
963 }
964 else if (remaining == 2 || format.GetAt(i+3) != c)
965 {
966 switch (c)
967 {
968 case 'm':
969 i+=3;
970 if (nMonth > 0&&nMonth <= 12)
971 sPart += months[nMonth - 1];
972 break;
973 default:
974 i+=3;
975 sPart += c;
976 sPart += c;
977 sPart += c;
978 break;
979 }
980 }
981 else if (remaining == 3 || format.GetAt(i+4) != c)
982 {
983 switch (c)
984 {
985 case 'y':
986 sPart.Format(L"%04d",nYear);
987 i += 4;
988 break;
989 case 'm':
990 i+=4;
991 if (nMonth > 0&&nMonth <= 12)
992 sPart += fullmonths[nMonth - 1];
993 break;
994 default:
995 i += 4;
996 sPart += c;
997 sPart += c;
998 sPart += c;
999 sPart += c;
1000 break;
1001 }
1002 }
1003 else
1004 {
1005 i++;
1006 sPart += c;
1007 }
1008 break;
1009 default:
1010 i++;
1011 sPart += c;
1012 break;
1013 }
1014
1015 sRet += sPart;
1016 }
1017
1018 return sRet;
1019 }
1020
1021 /* -------------------------------------------------------------------------- */
1022
1023 //function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency, bCu rrencyPrepend)
1024 FX_BOOL CJS_PublicMethods::AFNumber_Format(IFXJS_Context* cc, const CJS_Paramete rs& params, CJS_Value& vRet, CFX_WideString& sError)
1025 {
1026 #if _FX_OS_ != _FX_ANDROID_
1027 v8::Isolate* isolate = ::GetIsolate(cc);
1028 CJS_Context* pContext = (CJS_Context *)cc;
1029 ASSERT(pContext != NULL);
1030 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1031 ASSERT(pEvent != NULL);
1032
1033 if (params.size() != 6)
1034 {
1035 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1036 return FALSE;
1037 }
1038 if(!pEvent->m_pValue)
1039 return FALSE;
1040 CFX_WideString& Value = pEvent->Value();
1041 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1042
1043 if (strValue.IsEmpty()) return TRUE;
1044
1045 int iDec = params[0].ToInt();
1046 int iSepStyle = params[1].ToInt();
1047 int iNegStyle = params[2].ToInt();
1048 // params[3] is iCurrStyle, it's not used.
1049 std::wstring wstrCurrency(params[4].ToCFXWideString().c_str());
1050 FX_BOOL bCurrencyPrepend = params[5].ToBool();
1051
1052 if (iDec < 0) iDec = -iDec;
1053
1054 if (iSepStyle < 0 || iSepStyle > 3)
1055 iSepStyle = 0;
1056
1057 if (iNegStyle < 0 || iNegStyle > 3)
1058 iNegStyle = 0;
1059
1060
1061 //////////////////////////////////////////////////////
1062 //for processing decimal places
1063 strValue.Replace(",", ".");
1064 double dValue = atof(strValue);
1065 if (iDec > 0)
1066 dValue += DOUBLE_CORRECT;
1067
1068 int iDec2;
1069 int iNegative = 0;
1070
1071 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1072 if (strValue.IsEmpty())
1073 {
1074 dValue = 0;
1075 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1076 if (strValue.IsEmpty())
1077 {
1078 strValue = "0";
1079 iDec2 = 1;
1080 }
1081
1082 }
1083
1084 if (iDec2 < 0)
1085 {
1086 for (int iNum = 0;iNum < abs(iDec2);iNum++)
1087 {
1088 strValue = "0" + strValue;
1089 }
1090 iDec2 = 0;
1091
1092 }
1093 int iMax = strValue.GetLength();
1094 if (iDec2 > iMax)
1095 {
1096 for (int iNum = 0;iNum <= iDec2 - iMax ;iNum++)
1097 {
1098 strValue += "0";
1099 }
1100 iMax = iDec2+1;
1101 }
1102 ///////////////////////////////////////////////////////
1103 //for processing seperator style
1104 if (iDec2 < iMax)
1105 {
1106 if (iSepStyle == 0 || iSepStyle == 1)
1107 {
1108 strValue.Insert(iDec2, '.');
1109 iMax++;
1110 }
1111 else if (iSepStyle == 2 || iSepStyle == 3)
1112 {
1113 strValue.Insert(iDec2, ',');
1114 iMax++;
1115 }
1116
1117 if (iDec2 == 0)
1118 strValue.Insert(iDec2, '0');
1119 }
1120 if (iSepStyle == 0 || iSepStyle == 2)
1121 {
1122 char cSeperator;
1123 if (iSepStyle == 0)
1124 cSeperator = ',';
1125 else
1126 cSeperator = '.';
1127
1128 int iDecPositive;
1129 iDecPositive = iDec2;
1130
1131 for (iDecPositive = iDec2 -3; iDecPositive > 0;iDecPositive -= 3)
1132 {
1133 strValue.Insert(iDecPositive, cSeperator);
1134 iMax++;
1135 }
1136 }
1137
1138 //////////////////////////////////////////////////////////////////////
1139 //for processing currency string
1140
1141 Value = CFX_WideString::FromLocal(strValue);
1142 std::wstring strValue2 = Value.c_str();
1143
1144 if (bCurrencyPrepend)
1145 strValue2 = wstrCurrency + strValue2;
1146 else
1147 strValue2 = strValue2 + wstrCurrency;
1148
1149
1150
1151 /////////////////////////////////////////////////////////////////////////
1152 //for processing negative style
1153 if (iNegative)
1154 {
1155 if (iNegStyle == 0)
1156 {
1157 strValue2.insert(0,L"-");
1158 }
1159 if (iNegStyle == 2 || iNegStyle == 3)
1160 {
1161 strValue2.insert(0,L"(");
1162 strValue2.insert(strValue2.length(),L")");
1163 }
1164 if (iNegStyle == 1 || iNegStyle == 3)
1165 {
1166 if (Field * fTarget = pEvent->Target_Field())
1167 {
1168 CJS_Array arColor(isolate);
1169 CJS_Value vColElm(isolate);
1170 vColElm = L"RGB";
1171 arColor.SetElement(0,vColElm);
1172 vColElm = 1;
1173 arColor.SetElement(1,vColElm);
1174 vColElm = 0;
1175 arColor.SetElement(2,vColElm);
1176
1177 arColor.SetElement(3,vColElm);
1178
1179 CJS_PropValue vProp(isolate);
1180 vProp.StartGetting();
1181 vProp<<arColor;
1182 vProp.StartSetting();
1183 fTarget->textColor(cc,vProp,sError);// red
1184 }
1185 }
1186 }
1187 else
1188 {
1189 if (iNegStyle == 1 || iNegStyle == 3)
1190 {
1191 if (Field *fTarget = pEvent->Target_Field())
1192 {
1193 CJS_Array arColor(isolate);
1194 CJS_Value vColElm(isolate);
1195 vColElm = L"RGB";
1196 arColor.SetElement(0,vColElm);
1197 vColElm = 0;
1198 arColor.SetElement(1,vColElm);
1199 arColor.SetElement(2,vColElm);
1200 arColor.SetElement(3,vColElm);
1201
1202 CJS_PropValue vProp(isolate);
1203 vProp.StartGetting();
1204 fTarget->textColor(cc,vProp,sError);
1205
1206 CJS_Array aProp(isolate);
1207 vProp.ConvertToArray(aProp);
1208
1209 CPWL_Color crProp;
1210 CPWL_Color crColor;
1211 color::ConvertArrayToPWLColor(aProp, crProp);
1212 color::ConvertArrayToPWLColor(arColor, crColor);
1213
1214 if (crColor != crProp)
1215 {
1216 CJS_PropValue vProp2(isolate);
1217 vProp2.StartGetting();
1218 vProp2<<arColor;
1219 vProp2.StartSetting();
1220 fTarget->textColor(cc,vProp2,sError);
1221 }
1222 }
1223 }
1224 }
1225 Value = strValue2.c_str();
1226 #endif
1227 return TRUE;
1228 }
1229
1230 //function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency, bCurrencyPrepend)
1231 FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(IFXJS_Context* cc, const CJS_Param eters& params, CJS_Value& vRet, CFX_WideString& sError)
1232 {
1233 CJS_Context* pContext = (CJS_Context *)cc;
1234 ASSERT(pContext != NULL);
1235 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1236 ASSERT(pEvent != NULL);
1237
1238 if(params.size() < 2)
1239 return FALSE;
1240 int iSepStyle = params[1].ToInt();
1241
1242 if (iSepStyle < 0 || iSepStyle > 3)
1243 iSepStyle = 0;
1244 if(!pEvent->m_pValue)
1245 return FALSE;
1246 CFX_WideString & val = pEvent->Value();
1247 CFX_WideString & w_strChange = pEvent->Change();
1248 CFX_WideString w_strValue = val;
1249
1250 if (pEvent->WillCommit())
1251 {
1252 CFX_WideString wstrChange = w_strChange;
1253 CFX_WideString wstrValue = StrLTrim(w_strValue.c_str());
1254 if (wstrValue.IsEmpty())
1255 return TRUE;
1256
1257 CFX_WideString swTemp = wstrValue;
1258 swTemp.Replace(L",", L".");
1259 if (!IsNumber(swTemp.c_str()))
1260 {
1261 pEvent->Rc() = FALSE;
1262 sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE );
1263 Alert(pContext, sError.c_str());
1264 return TRUE;
1265 }
1266 return TRUE; // it happens after the last keystroke and before validatin g,
1267 }
1268
1269 std::wstring w_strValue2 = w_strValue.c_str();
1270 std::wstring w_strChange2 = w_strChange.c_str();
1271 std::wstring w_strSelected;
1272 if(-1 != pEvent->SelStart())
1273 w_strSelected = w_strValue2.substr(pEvent->SelStart(),(pEvent->SelEnd() - pEvent->SelStart()));
1274 FX_BOOL bHasSign = (w_strValue2.find('-') != -1) && (w_strSelected.find('-') == -1);
1275 if (bHasSign)
1276 {
1277 //can't insert "change" in front to sign postion.
1278 if (pEvent->SelStart() == 0)
1279 {
1280 FX_BOOL &bRc = pEvent->Rc();
1281 bRc = FALSE;
1282 return TRUE;
1283 }
1284 }
1285
1286 char cSep = L'.';
1287
1288 switch (iSepStyle)
1289 {
1290 case 0:
1291 case 1:
1292 cSep = L'.';
1293 break;
1294 case 2:
1295 case 3:
1296 cSep = L',';
1297 break;
1298 }
1299
1300 FX_BOOL bHasSep = (w_strValue2.find(cSep) != -1);
1301 for (std::wstring::iterator it = w_strChange2.begin(); it != w_strChange2.en d(); it++)
1302 {
1303 if (*it == cSep)
1304 {
1305 if (bHasSep)
1306 {
1307 FX_BOOL &bRc = pEvent->Rc();
1308 bRc = FALSE;
1309 return TRUE;
1310 }
1311 bHasSep = TRUE;
1312 continue;
1313 }
1314 if (*it == L'-')
1315 {
1316 if (bHasSign)
1317 {
1318 FX_BOOL &bRc = pEvent->Rc();
1319 bRc = FALSE;
1320 return TRUE;
1321 }
1322 if (it != w_strChange2.begin()) //sign's position is not correct
1323 {
1324 FX_BOOL &bRc = pEvent->Rc();
1325 bRc = FALSE;
1326 return TRUE;
1327 }
1328 if (pEvent->SelStart() != 0)
1329 {
1330 FX_BOOL &bRc = pEvent->Rc();
1331 bRc = FALSE;
1332 return TRUE;
1333 }
1334 bHasSign = TRUE;
1335 continue;
1336 }
1337
1338 if (!IsDigit(*it))
1339 {
1340 FX_BOOL &bRc = pEvent->Rc();
1341 bRc = FALSE;
1342 return TRUE;
1343 }
1344 }
1345
1346
1347 std::wstring w_prefix = w_strValue2.substr(0,pEvent->SelStart());
1348 std::wstring w_postfix;
1349 if (pEvent->SelEnd()<(int)w_strValue2.length())
1350 w_postfix = w_strValue2.substr(pEvent->SelEnd());
1351 w_strValue2 = w_prefix + w_strChange2 + w_postfix;
1352 w_strValue = w_strValue2.c_str();
1353 val = w_strValue;
1354 return TRUE;
1355
1356 }
1357
1358 //function AFPercent_Format(nDec, sepStyle)
1359 FX_BOOL CJS_PublicMethods::AFPercent_Format(IFXJS_Context* cc, const CJS_Paramet ers& params, CJS_Value& vRet, CFX_WideString& sError)
1360 {
1361 #if _FX_OS_ != _FX_ANDROID_
1362 CJS_Context* pContext = (CJS_Context *)cc;
1363 ASSERT(pContext != NULL);
1364 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1365 ASSERT(pEvent != NULL);
1366
1367 if (params.size() != 2)
1368 {
1369 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1370 return FALSE;
1371 }
1372 if(!pEvent->m_pValue)
1373 return FALSE;
1374
1375 CFX_WideString& Value = pEvent->Value();
1376 CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
1377 if (strValue.IsEmpty())
1378 return TRUE;
1379
1380 int iDec = params[0].ToInt();
1381 if (iDec < 0)
1382 iDec = -iDec;
1383
1384 int iSepStyle = params[1].ToInt();
1385 if (iSepStyle < 0 || iSepStyle > 3)
1386 iSepStyle = 0;
1387
1388 //////////////////////////////////////////////////////
1389 //for processing decimal places
1390 double dValue = atof(strValue);
1391 dValue *= 100;
1392 if (iDec > 0)
1393 dValue += DOUBLE_CORRECT;//УÕý
1394
1395 int iDec2;
1396 int iNegative = 0;
1397 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1398 if (strValue.IsEmpty())
1399 {
1400 dValue = 0;
1401 strValue = fcvt(dValue, iDec, &iDec2, &iNegative);
1402 }
1403
1404 if (iDec2 < 0)
1405 {
1406 for (int iNum = 0; iNum < abs(iDec2); iNum++)
1407 {
1408 strValue = "0" + strValue;
1409 }
1410 iDec2 = 0;
1411
1412 }
1413 int iMax = strValue.GetLength();
1414 if (iDec2 > iMax)
1415 {
1416 for (int iNum = 0; iNum <= iDec2 - iMax; iNum++)
1417 {
1418 strValue += "0";
1419 }
1420 iMax = iDec2+1;
1421 }
1422 ///////////////////////////////////////////////////////
1423 //for processing seperator style
1424 if (iDec2 < iMax)
1425 {
1426 if (iSepStyle == 0 || iSepStyle == 1)
1427 {
1428 strValue.Insert(iDec2, '.');
1429 iMax++;
1430 }
1431 else if (iSepStyle == 2 || iSepStyle == 3)
1432 {
1433 strValue.Insert(iDec2, ',');
1434 iMax++;
1435 }
1436
1437 if (iDec2 == 0)
1438 strValue.Insert(iDec2, '0');
1439 }
1440 if (iSepStyle == 0 || iSepStyle == 2)
1441 {
1442 char cSeperator;
1443 if (iSepStyle == 0)
1444 cSeperator = ',';
1445 else
1446 cSeperator = '.';
1447
1448 int iDecPositive;
1449 iDecPositive = iDec2;
1450
1451 for (iDecPositive = iDec2 -3; iDecPositive > 0; iDecPositive -= 3)
1452 {
1453 strValue.Insert(iDecPositive,cSeperator);
1454 iMax++;
1455 }
1456 }
1457 ////////////////////////////////////////////////////////////////////
1458 //negative mark
1459 if (iNegative)
1460 strValue = "-" + strValue;
1461 strValue += "%";
1462 Value = CFX_WideString::FromLocal(strValue);
1463 #endif
1464 return TRUE;
1465 }
1466 //AFPercent_Keystroke(nDec, sepStyle)
1467 FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(IFXJS_Context* cc, const CJS_Para meters& params, CJS_Value& vRet, CFX_WideString& sError)
1468 {
1469 return AFNumber_Keystroke(cc,params,vRet,sError);
1470 }
1471
1472 //function AFDate_FormatEx(cFormat)
1473 FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IFXJS_Context* cc, const CJS_Paramete rs& params, CJS_Value& vRet, CFX_WideString& sError)
1474 {
1475 CJS_Context* pContext = (CJS_Context *)cc;
1476 ASSERT(pContext != NULL);
1477 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1478 ASSERT(pEvent != NULL);
1479
1480 if (params.size() != 1)
1481 {
1482 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1483 return FALSE;
1484 }
1485 if(!pEvent->m_pValue)
1486 return FALSE;
1487
1488 CFX_WideString& val = pEvent->Value();
1489 CFX_WideString strValue = val;
1490 if (strValue.IsEmpty())
1491 return TRUE;
1492
1493 CFX_WideString sFormat = params[0].ToCFXWideString();
1494 FX_BOOL bWrongFormat = FALSE;
1495 double dDate = 0.0f;
1496
1497 if(strValue.Find(L"GMT") != -1)
1498 {
1499 //for GMT format time
1500 //such as "Tue Aug 11 14:24:16 GMT+08002009"
1501 dDate = MakeInterDate(strValue);
1502 }
1503 else
1504 {
1505 dDate = MakeRegularDate(strValue,sFormat,bWrongFormat);
1506 }
1507
1508 if (JS_PortIsNan(dDate))
1509 {
1510 CFX_WideString swMsg;
1511 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str() , sFormat.c_str());
1512 Alert(pContext, swMsg.c_str());
1513 return FALSE;
1514 }
1515
1516 val = MakeFormatDate(dDate,sFormat);
1517 return TRUE;
1518 }
1519
1520 double CJS_PublicMethods::MakeInterDate(CFX_WideString strValue)
1521 {
1522 int nHour;
1523 int nMin;
1524 int nSec;
1525 int nYear;
1526 int nMonth;
1527 int nDay;
1528
1529 CFX_WideStringArray wsArray;
1530 CFX_WideString sMonth = L"";
1531 CFX_WideString sTemp = L"";
1532 int nSize = strValue.GetLength();
1533
1534 for(int i = 0; i < nSize; i++)
1535 {
1536 FX_WCHAR c = strValue.GetAt(i);
1537 if(c == L' ' || c == L':')
1538 {
1539 wsArray.Add(sTemp);
1540 sTemp = L"";
1541 continue;
1542 }
1543
1544 sTemp += c;
1545 }
1546
1547 wsArray.Add(sTemp);
1548 if(wsArray.GetSize() != 8)return 0;
1549
1550 sTemp = wsArray[1];
1551 if(sTemp.Compare(L"Jan") == 0) nMonth = 1;
1552 if(sTemp.Compare(L"Feb") == 0) nMonth = 2;
1553 if(sTemp.Compare(L"Mar") == 0) nMonth = 3;
1554 if(sTemp.Compare(L"Apr") == 0) nMonth = 4;
1555 if(sTemp.Compare(L"May") == 0) nMonth = 5;
1556 if(sTemp.Compare(L"Jun") == 0) nMonth = 6;
1557 if(sTemp.Compare(L"Jul") == 0) nMonth = 7;
1558 if(sTemp.Compare(L"Aug") == 0) nMonth = 8;
1559 if(sTemp.Compare(L"Sep") == 0) nMonth = 9;
1560 if(sTemp.Compare(L"Oct") == 0) nMonth = 10;
1561 if(sTemp.Compare(L"Nov") == 0) nMonth = 11;
1562 if(sTemp.Compare(L"Dec") == 0) nMonth = 12;
1563
1564 nDay = (int)ParseStringToNumber(wsArray[2].c_str());
1565 nHour = (int)ParseStringToNumber(wsArray[3].c_str());
1566 nMin = (int)ParseStringToNumber(wsArray[4].c_str());
1567 nSec = (int)ParseStringToNumber(wsArray[5].c_str());
1568 nYear = (int)ParseStringToNumber(wsArray[7].c_str());
1569
1570 double dRet = JS_MakeDate(JS_MakeDay(nYear,nMonth - 1,nDay),JS_MakeTime(nHou r, nMin, nSec, 0));
1571
1572 if (JS_PortIsNan(dRet))
1573 {
1574 dRet = JS_DateParse(strValue.c_str());
1575 }
1576
1577 return dRet;
1578 }
1579
1580 //AFDate_KeystrokeEx(cFormat)
1581 FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(IFXJS_Context* cc, const CJS_Param eters& params, CJS_Value& vRet, CFX_WideString& sError)
1582 {
1583 CJS_Context* pContext = (CJS_Context *)cc;
1584 ASSERT(pContext != NULL);
1585 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1586 ASSERT(pEvent != NULL);
1587
1588 if (params.size() != 1)
1589 {
1590 sError = L"AFDate_KeystrokeEx's parameters' size r not correct";
1591 return FALSE;
1592 }
1593
1594 if (pEvent->WillCommit())
1595 {
1596 if(!pEvent->m_pValue)
1597 return FALSE;
1598 CFX_WideString strValue = pEvent->Value();
1599 if (strValue.IsEmpty())
1600 return TRUE;
1601
1602 CFX_WideString sFormat = params[0].ToCFXWideString();
1603 FX_BOOL bWrongFormat = FALSE;
1604 double dRet = MakeRegularDate(strValue,sFormat,bWrongFormat);
1605 if (bWrongFormat || JS_PortIsNan(dRet))
1606 {
1607 CFX_WideString swMsg;
1608 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_s tr(), sFormat.c_str());
1609 Alert(pContext, swMsg.c_str());
1610 pEvent->Rc() = FALSE;
1611 return TRUE;
1612 }
1613 }
1614 return TRUE;
1615 }
1616
1617 FX_BOOL CJS_PublicMethods::AFDate_Format(IFXJS_Context* cc, const CJS_Parameters & params, CJS_Value& vRet, CFX_WideString& sError)
1618 {
1619 v8::Isolate* isolate = ::GetIsolate(cc);
1620
1621 if (params.size() != 1)
1622 {
1623 CJS_Context* pContext = (CJS_Context*)cc;
1624 ASSERT(pContext != NULL);
1625
1626 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1627 return FALSE;
1628 }
1629
1630 int iIndex = params[0].ToInt();
1631 const FX_WCHAR* cFormats[] = {L"m/d", L"m/d/yy", L"mm/dd/yy", L"mm/yy", L"d -mmm", L"d-mmm-yy", L"dd-mmm-yy",
1632 L"yy-mm-dd", L"mmm-yy", L"mmmm-yy", L"mmm d, yyyy", L"mmmm d, yyyy",
1633 L"m/d/yy h:MM tt", L"m/d/yy HH:MM" };
1634
1635 ASSERT(iIndex < FX_ArraySize(cFormats));
1636
1637 if (iIndex < 0)
1638 iIndex = 0;
1639 if (iIndex >= FX_ArraySize(cFormats))
1640 iIndex = 0;
1641 CJS_Parameters newParams;
1642 CJS_Value val(isolate,cFormats[iIndex]);
1643 newParams.push_back(val);
1644 return AFDate_FormatEx(cc,newParams,vRet,sError);
1645 }
1646
1647 //AFDate_KeystrokeEx(cFormat)
1648 FX_BOOL CJS_PublicMethods::AFDate_Keystroke(IFXJS_Context* cc, const CJS_Paramet ers& params, CJS_Value& vRet, CFX_WideString& sError)
1649 {
1650 v8::Isolate* isolate = ::GetIsolate(cc);
1651
1652 if (params.size() != 1)
1653 {
1654 CJS_Context* pContext = (CJS_Context*)cc;
1655 ASSERT(pContext != NULL);
1656
1657 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1658 return FALSE;
1659 }
1660
1661 int iIndex = params[0].ToInt();
1662 const FX_WCHAR* cFormats[] = {L"m/d", L"m/d/yy", L"mm/dd/yy", L"mm/yy", L"d -mmm", L"d-mmm-yy", L"dd-mmm-yy",
1663 L"yy-mm-dd", L"mmm-yy", L"mmmm-yy", L"mmm d, yyyy", L"mmmm d, yyyy",
1664 L"m/d/yy h:MM tt", L"m/d/yy HH:MM" };
1665
1666 ASSERT(iIndex<FX_ArraySize(cFormats));
1667
1668 if (iIndex < 0)
1669 iIndex = 0;
1670 if (iIndex >= FX_ArraySize(cFormats))
1671 iIndex = 0;
1672 CJS_Parameters newParams;
1673 CJS_Value val(isolate,cFormats[iIndex]);
1674 newParams.push_back(val);
1675 return AFDate_KeystrokeEx(cc,newParams,vRet,sError);
1676 }
1677
1678 //function AFTime_Format(ptf)
1679 FX_BOOL CJS_PublicMethods::AFTime_Format(IFXJS_Context* cc, const CJS_Parameters & params, CJS_Value& vRet, CFX_WideString& sError)
1680 {
1681 v8::Isolate* isolate = ::GetIsolate(cc);
1682
1683 if (params.size() != 1)
1684 {
1685 CJS_Context* pContext = (CJS_Context*)cc;
1686 ASSERT(pContext != NULL);
1687 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1688 return FALSE;
1689 }
1690
1691 int iIndex = params[0].ToInt();
1692 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss", L"h:MM:ss t t"};
1693
1694 ASSERT(iIndex<FX_ArraySize(cFormats));
1695
1696 if (iIndex < 0)
1697 iIndex = 0;
1698 if (iIndex >= FX_ArraySize(cFormats))
1699 iIndex = 0;
1700 CJS_Parameters newParams;
1701 CJS_Value val(isolate,cFormats[iIndex]);
1702 newParams.push_back(val);
1703 return AFDate_FormatEx(cc,newParams,vRet,sError);
1704 }
1705
1706 FX_BOOL CJS_PublicMethods::AFTime_Keystroke(IFXJS_Context* cc, const CJS_Paramet ers& params, CJS_Value& vRet, CFX_WideString& sError)
1707 {
1708 v8::Isolate* isolate = ::GetIsolate(cc);
1709 if (params.size() != 1)
1710 {
1711 CJS_Context* pContext = (CJS_Context*)cc;
1712 ASSERT(pContext != NULL);
1713 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1714 return FALSE;
1715 }
1716
1717 int iIndex = params[0].ToInt();
1718 const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss", L"h:MM:ss t t"};
1719
1720 ASSERT(iIndex<FX_ArraySize(cFormats));
1721
1722 if (iIndex < 0)
1723 iIndex = 0;
1724 if (iIndex >= FX_ArraySize(cFormats))
1725 iIndex = 0;
1726 CJS_Parameters newParams;
1727 CJS_Value val(isolate,cFormats[iIndex]);
1728 newParams.push_back(val);
1729 return AFDate_KeystrokeEx(cc,newParams,vRet,sError);
1730 }
1731
1732 FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IFXJS_Context* cc, const CJS_Paramete rs& params, CJS_Value& vRet, CFX_WideString& sError)
1733 {
1734 return AFDate_FormatEx(cc,params,vRet,sError);
1735 }
1736
1737 FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(IFXJS_Context* cc, const CJS_Param eters& params, CJS_Value& vRet, CFX_WideString& sError)
1738 {
1739 return AFDate_KeystrokeEx(cc,params,vRet,sError);
1740 }
1741
1742 //function AFSpecial_Format(psf)
1743 FX_BOOL CJS_PublicMethods::AFSpecial_Format(IFXJS_Context* cc, const CJS_Paramet ers& params, CJS_Value& vRet, CFX_WideString& sError)
1744 {
1745 CJS_Context* pContext = (CJS_Context *)cc;
1746 ASSERT(pContext != NULL);
1747
1748 if (params.size() != 1)
1749 {
1750 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1751 return FALSE;
1752 }
1753
1754 std::string cFormat;
1755 int iIndex = params[0].ToInt();
1756
1757 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1758 ASSERT(pEvent != NULL);
1759
1760 if(!pEvent->m_pValue)
1761 return FALSE;
1762 CFX_WideString& Value = pEvent->Value();
1763 std::string strSrc = CFX_ByteString::FromUnicode(Value).c_str();
1764
1765 switch (iIndex)
1766 {
1767 case 0:
1768 cFormat = "99999";
1769 break;
1770 case 1:
1771 cFormat = "99999-9999";
1772 break;
1773 case 2:
1774 {
1775 std::string NumberStr;
1776 util::printx("9999999999", strSrc,NumberStr);
1777 if (NumberStr.length() >= 10 )
1778 cFormat = "(999) 999-9999";
1779 else
1780 cFormat = "999-9999";
1781 break;
1782 }
1783 case 3:
1784 cFormat = "999-99-9999";
1785 break;
1786 }
1787
1788 std::string strDes;
1789 util::printx(cFormat,strSrc,strDes);
1790 Value = CFX_WideString::FromLocal(strDes.c_str());
1791 return TRUE;
1792 }
1793
1794
1795 //function AFSpecial_KeystrokeEx(mask)
1796 FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(IFXJS_Context* cc, const CJS_Pa rameters& params, CJS_Value& vRet, CFX_WideString& sError)
1797 {
1798 CJS_Context* pContext = (CJS_Context *)cc;
1799 ASSERT(pContext != NULL);
1800 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1801
1802 ASSERT(pEvent != NULL);
1803
1804 if (params.size() < 1)
1805 {
1806 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1807 return FALSE;
1808 }
1809
1810 if(!pEvent->m_pValue)
1811 return FALSE;
1812 CFX_WideString& valEvent = pEvent->Value();
1813
1814 CFX_WideString wstrMask = params[0].ToCFXWideString();
1815 if (wstrMask.IsEmpty())
1816 return TRUE;
1817
1818 std::wstring wstrValue = valEvent.c_str();
1819
1820 if (pEvent->WillCommit())
1821 {
1822 if (wstrValue.empty())
1823 return TRUE;
1824 int iIndexMask = 0;
1825 for (std::wstring::iterator it = wstrValue.begin(); it != wstrValue.end( ); it++)
1826 {
1827 wchar_t w_Value = *it;
1828 if (!maskSatisfied(w_Value,wstrMask[iIndexMask]))
1829 break;
1830 iIndexMask++;
1831 }
1832
1833 if (iIndexMask != wstrMask.GetLength() || (iIndexMask != wstrValue.size( ) && wstrMask.GetLength() != 0))
1834 {
1835 Alert(pContext, JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KE YSTROKE).c_str());
1836 pEvent->Rc() = FALSE;
1837 }
1838 return TRUE;
1839 }
1840
1841 CFX_WideString &wideChange = pEvent->Change();
1842 std::wstring wChange = wideChange.c_str();
1843 if (wChange.empty())
1844 return TRUE;
1845
1846 int iIndexMask = pEvent->SelStart();
1847
1848 if (wstrValue.length() - (pEvent->SelEnd()-pEvent->SelStart()) + wChange.len gth() > (FX_DWORD)wstrMask.GetLength())
1849 {
1850 Alert(pContext, JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG). c_str());
1851 pEvent->Rc() = FALSE;
1852 return TRUE;
1853 }
1854
1855 if (iIndexMask >= wstrMask.GetLength() && (!wChange.empty()))
1856 {
1857 Alert(pContext, JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG). c_str());
1858 pEvent->Rc() = FALSE;
1859 return TRUE;
1860 }
1861
1862 for (std::wstring::iterator it = wChange.begin(); it != wChange.end(); it++)
1863 {
1864 if (iIndexMask >= wstrMask.GetLength())
1865 {
1866 Alert(pContext, JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLO NG).c_str());
1867 pEvent->Rc() = FALSE;
1868 return TRUE;
1869 }
1870 wchar_t w_Mask = wstrMask[iIndexMask];
1871 if (!isReservedMaskChar(w_Mask))
1872 {
1873 *it = w_Mask;
1874 }
1875 wchar_t w_Change = *it;
1876 if (!maskSatisfied(w_Change,w_Mask))
1877 {
1878 pEvent->Rc() = FALSE;
1879 return TRUE;
1880 }
1881 iIndexMask++;
1882 }
1883
1884 wideChange = wChange.c_str();
1885 return TRUE;
1886 }
1887
1888
1889 //function AFSpecial_Keystroke(psf)
1890 FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(IFXJS_Context* cc, const CJS_Para meters& params, CJS_Value& vRet, CFX_WideString& sError)
1891 {
1892 v8::Isolate* isolate = ::GetIsolate(cc);
1893
1894 CJS_Context* pContext = (CJS_Context *)cc;
1895 ASSERT(pContext != NULL);
1896 CJS_EventHandler* pEvent = pContext->GetEventHandler();
1897 ASSERT(pEvent != NULL);
1898
1899 if (params.size() != 1)
1900 {
1901 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1902 return FALSE;
1903 }
1904
1905 std::string cFormat;
1906 int iIndex = params[0].ToInt();
1907
1908 if(!pEvent->m_pValue)
1909 return FALSE;
1910 //CJS_Value val = pEvent->Value();
1911 CFX_WideString& val = pEvent->Value();
1912 std::string strSrc = CFX_ByteString::FromUnicode(val).c_str();
1913 std::wstring wstrChange = pEvent->Change().c_str();
1914
1915 switch (iIndex)
1916 {
1917 case 0:
1918 cFormat = "99999";
1919 break;
1920 case 1:
1921 //cFormat = "99999-9999";
1922 cFormat = "999999999";
1923 break;
1924 case 2:
1925 {
1926 std::string NumberStr;
1927 util::printx("9999999999", strSrc,NumberStr);
1928 if (strSrc.length() + wstrChange.length() > 7 )
1929 //cFormat = "(999) 999-9999";
1930 cFormat = "9999999999";
1931 else
1932 //cFormat = "999-9999";
1933 cFormat = "9999999";
1934 break;
1935 }
1936 case 3:
1937 //cFormat = "999-99-9999";
1938 cFormat = "999999999";
1939 break;
1940 }
1941
1942 CJS_Parameters params2;
1943 CJS_Value vMask(isolate, cFormat.c_str());
1944 params2.push_back(vMask);
1945
1946 return AFSpecial_KeystrokeEx(cc,params2,vRet,sError);
1947 }
1948
1949 FX_BOOL CJS_PublicMethods::AFMergeChange(IFXJS_Context* cc, const CJS_Parameters & params, CJS_Value& vRet, CFX_WideString& sError)
1950 {
1951 CJS_Context* pContext = (CJS_Context *)cc;
1952 ASSERT(pContext != NULL);
1953 CJS_EventHandler* pEventHandler = pContext->GetEventHandler();
1954 ASSERT(pEventHandler != NULL);
1955
1956 if (params.size() != 1)
1957 {
1958 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1959 return FALSE;
1960 }
1961
1962 CFX_WideString swValue;
1963 if (pEventHandler->m_pValue != NULL)
1964 swValue = pEventHandler->Value();
1965
1966 if (pEventHandler->WillCommit())
1967 {
1968 vRet = swValue.c_str();
1969 return TRUE;
1970 }
1971
1972 CFX_WideString prefix,postfix;
1973
1974 if (pEventHandler->SelStart() >= 0)
1975 prefix = swValue.Mid(0,pEventHandler->SelStart());
1976 else
1977 prefix = L"";
1978
1979
1980 if (pEventHandler->SelEnd() >= 0 && pEventHandler->SelEnd() <= swValue.GetLe ngth())
1981 postfix = swValue.Mid(pEventHandler->SelEnd(), swValue.GetLength() - pEv entHandler->SelEnd());
1982 else postfix = L"";
1983
1984 vRet = (prefix + pEventHandler->Change() + postfix).c_str();
1985
1986 return TRUE;
1987 }
1988
1989 FX_BOOL CJS_PublicMethods::AFParseDateEx(IFXJS_Context* cc, const CJS_Parameters & params, CJS_Value& vRet, CFX_WideString& sError)
1990 {
1991 CJS_Context* pContext = (CJS_Context *)cc;
1992 ASSERT(pContext != NULL);
1993
1994 if (params.size() != 2)
1995 {
1996 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
1997 return FALSE;
1998 }
1999
2000 CFX_WideString sValue = params[0].ToCFXWideString();
2001 CFX_WideString sFormat = params[1].ToCFXWideString();
2002
2003 FX_BOOL bWrongFormat = FALSE;
2004 double dDate = MakeRegularDate(sValue,sFormat,bWrongFormat);
2005
2006 if (JS_PortIsNan(dDate))
2007 {
2008 CFX_WideString swMsg;
2009 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSPARSEDATE).c_str() , sFormat.c_str());
2010 Alert((CJS_Context *)cc, swMsg.c_str());
2011 return FALSE;
2012 }
2013
2014 vRet = dDate;
2015 return TRUE;
2016 }
2017
2018 FX_BOOL CJS_PublicMethods::AFSimple(IFXJS_Context* cc, const CJS_Parameters& par ams, CJS_Value& vRet, CFX_WideString& sError)
2019 {
2020 if (params.size() != 3)
2021 {
2022 CJS_Context* pContext = (CJS_Context *)cc;
2023 ASSERT(pContext != NULL);
2024
2025 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
2026 return FALSE;
2027 }
2028
2029 vRet = (double)AF_Simple(params[0].ToCFXWideString().c_str(), params[1].ToDo uble(), params[2].ToDouble());
2030 return TRUE;
2031 }
2032
2033 FX_BOOL CJS_PublicMethods::AFMakeNumber(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError)
2034 {
2035 if (params.size() != 1)
2036 {
2037 CJS_Context* pContext = (CJS_Context *)cc;
2038 ASSERT(pContext != NULL);
2039
2040 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
2041 return FALSE;
2042 }
2043 vRet = ParseStringToNumber(params[0].ToCFXWideString().c_str());
2044 return TRUE;
2045 }
2046
2047 FX_BOOL CJS_PublicMethods::AFSimple_Calculate(IFXJS_Context* cc, const CJS_Param eters& params, CJS_Value& vRet, CFX_WideString& sError)
2048 {
2049 v8::Isolate* isolate = ::GetIsolate(cc);
2050
2051 CJS_Context* pContext = (CJS_Context *)cc;
2052 ASSERT(pContext != NULL);
2053
2054 if (params.size() != 2)
2055 {
2056 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
2057 return FALSE;
2058 }
2059
2060 CJS_Value params1 = params[1];
2061
2062 if (!params1.IsArrayObject() && params1.GetType() != VT_string)
2063 {
2064 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
2065 return FALSE;
2066 }
2067
2068 CPDFSDK_Document* pReaderDoc = pContext->GetReaderDocument();
2069 ASSERT(pReaderDoc != NULL);
2070
2071 CPDFSDK_InterForm* pReaderInterForm = pReaderDoc->GetInterForm();
2072 ASSERT(pReaderInterForm != NULL);
2073
2074 CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
2075 ASSERT(pInterForm != NULL);
2076
2077 double dValue;
2078 CFX_WideString sFunction = params[0].ToCFXWideString();
2079 if (wcscmp(sFunction.c_str(), L"PRD") == 0)
2080 dValue = 1.0;
2081 else
2082 dValue = 0.0;
2083
2084 CJS_Array FieldNameArray = AF_MakeArrayFromList(isolate,params1);
2085
2086 int nFieldsCount = 0;
2087
2088 for (int i=0,isz=FieldNameArray.GetLength(); i<isz; i++)
2089 {
2090 CJS_Value jsValue(isolate);
2091 FieldNameArray.GetElement(i,jsValue);
2092 CFX_WideString wsFieldName = jsValue.ToCFXWideString();
2093
2094 for (int j=0,jsz=pInterForm->CountFields(wsFieldName); j<jsz; j++)
2095 {
2096 if (CPDF_FormField* pFormField = pInterForm->GetField(j, wsFieldName ))
2097 {
2098 double dTemp = 0.0;
2099
2100 switch (pFormField->GetFieldType())
2101 {
2102 case FIELDTYPE_TEXTFIELD:
2103 case FIELDTYPE_COMBOBOX:
2104 {
2105 dTemp = ParseStringToNumber(pFormField->GetValue().c_str ());
2106 break;
2107 }
2108 case FIELDTYPE_PUSHBUTTON:
2109 {
2110 dTemp = 0.0;
2111 break;
2112 }
2113 case FIELDTYPE_CHECKBOX:
2114 case FIELDTYPE_RADIOBUTTON:
2115 {
2116 dTemp = 0.0;
2117 for (int c=0,csz=pFormField->CountControls(); c<csz; c++ )
2118 {
2119 if (CPDF_FormControl* pFormCtrl = pFormField->GetCon trol(c))
2120 {
2121 if (pFormCtrl->IsChecked())
2122 {
2123 dTemp += ParseStringToNumber(pFormCtrl->GetE xportValue().c_str());
2124 break;
2125 }
2126 else
2127 continue;
2128 }
2129 }
2130 break;
2131 }
2132 case FIELDTYPE_LISTBOX:
2133 {
2134 dTemp = 0.0;
2135 if (pFormField->CountSelectedItems() > 1)
2136 break;
2137 else
2138 {
2139 dTemp = ParseStringToNumber(pFormField->GetValue().c _str());
2140 break;
2141 }
2142 }
2143 default:
2144 break;
2145 }
2146
2147 if (i == 0 && j == 0 && (wcscmp(sFunction.c_str(), L"MIN") == 0 || wcscmp(sFunction.c_str(), L"MAX") == 0))
2148 dValue = dTemp;
2149
2150 dValue = AF_Simple(sFunction.c_str(), dValue, dTemp);
2151
2152 nFieldsCount++;
2153 }
2154 }
2155 }
2156
2157 if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
2158 dValue /= nFieldsCount;
2159
2160 dValue = (double)floor(dValue * FXSYS_pow((double)10,(double)6) + 0.49) / FX SYS_pow((double)10,(double)6);
2161 CJS_Value jsValue(isolate,dValue);
2162 if((CJS_EventHandler*)pContext->GetEventHandler()->m_pValue)
2163 ((CJS_EventHandler*)pContext->GetEventHandler())->Value() = jsValue.ToCF XWideString();
2164
2165 return TRUE;
2166 } 2044 }
2167 2045
2168 /* This function validates the current event to ensure that its value is 2046 /* This function validates the current event to ensure that its value is
2169 ** within the specified range. */ 2047 ** within the specified range. */
2170 2048
2171 FX_BOOL CJS_PublicMethods::AFRange_Validate(IFXJS_Context* cc, const CJS_Paramet ers& params, CJS_Value& vRet, CFX_WideString& sError) 2049 FX_BOOL CJS_PublicMethods::AFRange_Validate(IFXJS_Context* cc,
2172 { 2050 const CJS_Parameters& params,
2173 CJS_Context* pContext = (CJS_Context *)cc; 2051 CJS_Value& vRet,
2174 ASSERT(pContext != NULL); 2052 CFX_WideString& sError) {
2175 CJS_EventHandler* pEvent = pContext->GetEventHandler(); 2053 CJS_Context* pContext = (CJS_Context*)cc;
2176 ASSERT(pEvent != NULL); 2054 ASSERT(pContext != NULL);
2177 2055 CJS_EventHandler* pEvent = pContext->GetEventHandler();
2178 if (params.size() != 4) 2056 ASSERT(pEvent != NULL);
2179 { 2057
2180 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); 2058 if (params.size() != 4) {
2181 return FALSE; 2059 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
2182 } 2060 return FALSE;
2183 2061 }
2184 if(!pEvent->m_pValue) 2062
2185 return FALSE; 2063 if (!pEvent->m_pValue)
2186 if (pEvent->Value().IsEmpty() ) 2064 return FALSE;
2187 return TRUE; 2065 if (pEvent->Value().IsEmpty())
2188 double dEentValue = atof(CFX_ByteString::FromUnicode(pEvent->Value()));
2189 FX_BOOL bGreaterThan = params[0].ToBool();
2190 double dGreaterThan = params[1].ToDouble();
2191 FX_BOOL bLessThan = params[2].ToBool();
2192 double dLessThan = params[3].ToDouble();
2193 CFX_WideString swMsg;
2194
2195 if (bGreaterThan && bLessThan)
2196 {
2197 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
2198 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE1).c_str( ),
2199 params[1].ToCFXWideString().c_str(),
2200 params[3].ToCFXWideString().c_str());
2201 }
2202 else if (bGreaterThan)
2203 {
2204 if (dEentValue < dGreaterThan)
2205 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str( ),
2206 params[1].ToCFXWideString().c_str());
2207 }
2208 else if (bLessThan)
2209 {
2210 if (dEentValue > dLessThan)
2211 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str( ),
2212 params[3].ToCFXWideString().c_str());
2213 }
2214
2215 if (!swMsg.IsEmpty())
2216 {
2217 Alert(pContext, swMsg.c_str());
2218 pEvent->Rc() = FALSE;
2219 }
2220 return TRUE; 2066 return TRUE;
2221 } 2067 double dEentValue = atof(CFX_ByteString::FromUnicode(pEvent->Value()));
2222 2068 FX_BOOL bGreaterThan = params[0].ToBool();
2223 FX_BOOL CJS_PublicMethods::AFExtractNums(IFXJS_Context* cc, const CJS_Parameters & params, CJS_Value& vRet, CFX_WideString& sError) 2069 double dGreaterThan = params[1].ToDouble();
2224 { 2070 FX_BOOL bLessThan = params[2].ToBool();
2225 v8::Isolate* isolate = ::GetIsolate(cc); 2071 double dLessThan = params[3].ToDouble();
2226 CJS_Context* pContext = (CJS_Context*)cc; 2072 CFX_WideString swMsg;
2227 ASSERT(pContext != NULL); 2073
2228 2074 if (bGreaterThan && bLessThan) {
2229 if (params.size() != 1) 2075 if (dEentValue < dGreaterThan || dEentValue > dLessThan)
2230 { 2076 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE1).c_str(),
2231 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); 2077 params[1].ToCFXWideString().c_str(),
2232 return FALSE; 2078 params[3].ToCFXWideString().c_str());
2233 } 2079 } else if (bGreaterThan) {
2234 2080 if (dEentValue < dGreaterThan)
2235 CJS_Array nums(isolate); 2081 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE2).c_str(),
2236 2082 params[1].ToCFXWideString().c_str());
2237 CFX_WideString str = params[0].ToCFXWideString(); 2083 } else if (bLessThan) {
2238 CFX_WideString sPart; 2084 if (dEentValue > dLessThan)
2239 2085 swMsg.Format(JSGetStringFromID(pContext, IDS_STRING_JSRANGE3).c_str(),
2240 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',') 2086 params[3].ToCFXWideString().c_str());
2241 str = L"0" + str; 2087 }
2242 2088
2243 int nIndex = 0; 2089 if (!swMsg.IsEmpty()) {
2244 for (int i=0, sz=str.GetLength(); i<sz; i++) 2090 Alert(pContext, swMsg.c_str());
2245 { 2091 pEvent->Rc() = FALSE;
2246 FX_WCHAR wc = str.GetAt(i); 2092 }
2247 if (IsDigit((wchar_t)wc)) 2093 return TRUE;
2248 { 2094 }
2249 sPart += wc; 2095
2250 } 2096 FX_BOOL CJS_PublicMethods::AFExtractNums(IFXJS_Context* cc,
2251 else 2097 const CJS_Parameters& params,
2252 { 2098 CJS_Value& vRet,
2253 if (sPart.GetLength() > 0) 2099 CFX_WideString& sError) {
2254 { 2100 v8::Isolate* isolate = ::GetIsolate(cc);
2255 nums.SetElement(nIndex,CJS_Value(isolate,sPart.c_str())); 2101 CJS_Context* pContext = (CJS_Context*)cc;
2256 sPart = L""; 2102 ASSERT(pContext != NULL);
2257 nIndex ++; 2103
2258 } 2104 if (params.size() != 1) {
2259 } 2105 sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
2260 } 2106 return FALSE;
2261 2107 }
2262 if (sPart.GetLength() > 0) 2108
2263 { 2109 CJS_Array nums(isolate);
2264 nums.SetElement(nIndex,CJS_Value(isolate,sPart.c_str())); 2110
2265 } 2111 CFX_WideString str = params[0].ToCFXWideString();
2266 2112 CFX_WideString sPart;
2267 if (nums.GetLength() > 0) 2113
2268 vRet = nums; 2114 if (str.GetAt(0) == L'.' || str.GetAt(0) == L',')
2269 else 2115 str = L"0" + str;
2270 vRet.SetNull(); 2116
2271 2117 int nIndex = 0;
2272 return TRUE; 2118 for (int i = 0, sz = str.GetLength(); i < sz; i++) {
2273 } 2119 FX_WCHAR wc = str.GetAt(i);
2120 if (IsDigit((wchar_t)wc)) {
2121 sPart += wc;
2122 } else {
2123 if (sPart.GetLength() > 0) {
2124 nums.SetElement(nIndex, CJS_Value(isolate, sPart.c_str()));
2125 sPart = L"";
2126 nIndex++;
2127 }
2128 }
2129 }
2130
2131 if (sPart.GetLength() > 0) {
2132 nums.SetElement(nIndex, CJS_Value(isolate, sPart.c_str()));
2133 }
2134
2135 if (nums.GetLength() > 0)
2136 vRet = nums;
2137 else
2138 vRet.SetNull();
2139
2140 return TRUE;
2141 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698