Merge "Update deprecated libprivilege-control API functions." into tizen
[platform/framework/native/appfw.git] / src / base / utility / FBaseUtil_ScannerImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @File        :       FBaseUtil_ScannerImpl.cpp
19  * @brief       :       Implementation for _ScannerImpl Class
20  */
21
22 #include <sys/stat.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <wchar.h>
26
27 #include <FBaseCharacter.h>
28 #include <FBaseInt8.h>
29 #include <FBaseInteger.h>
30 #include <FBaseShort.h>
31 #include <FBaseLong.h>
32 #include <FBaseLongLong.h>
33 #include <FBaseFloat.h>
34 #include <FBaseDouble.h>
35 #include <FBaseResult.h>
36 #include "FBaseUtil_ScannerImpl.h"
37 #include "FBaseUtilRegularExpression.h"
38 #include <FBaseColArrayList.h>
39 #include <FBaseSysLog.h>
40 #include "FBaseUtil_RegularExpressionImpl.h"
41 #include "FBase_StringConverter.h"
42 #include "FBaseUtil_IcuConverter.h"
43 #include "FBase_NativeError.h"
44
45 #define ANY_PATTERN L"\\s*[^\\s]*"
46 #define DEFAULT_DELIMITER_PATTERN L"\\s+"
47 #define COMPLETE_LINE_PATTERN L".*(\r\n|[\u000A\n\r\u2028\u2029\u0085])|.+$"
48 #define DEFAULT_LINE_SEPARATOR_PATTERN L"[\u000A\n\r\u2028\u2029\u0085]*"
49
50 #define DEFAULT_GROUP_SEPARATOR L","
51 #define DEFAULT_DECIMAL_SEPARATOR L"."
52 #define DEFAULT_NEGATIVE_PREFIX L"-"
53
54 using namespace Tizen::Base::Collection;
55 using namespace Tizen::Base::Utility;
56 using namespace Tizen::Base;
57
58 namespace Tizen { namespace Base { namespace Utility
59 {
60 _ScannerImpl::_ScannerImpl(void)
61         : __radix(10)
62         , __delimiter(DEFAULT_DELIMITER_PATTERN)
63         , __pParseStr(null)
64         , __isAllocatedOnHeap(false)
65         , __position(0)
66 {
67 }
68
69 _ScannerImpl::~_ScannerImpl()
70 {
71         if (__isAllocatedOnHeap)
72         {
73                 delete[] __pParseStr;
74                 __pParseStr = null;
75         }
76 }
77
78 result
79 _ScannerImpl::Construct(const String& str)
80 {
81         result r = E_SUCCESS;
82         __pParseStr = str.GetPointer();
83         return r;
84 }
85
86 result
87 _ScannerImpl::Construct(const String& filePath, const String& encodingScheme)
88 {
89         result r = E_SUCCESS;
90         FILE* pFile = null;
91         char* pFilePath = null;
92         bool res = false;
93         _ICUConverter converter;
94         struct stat st;
95         off_t fileSize = 0;
96         char* buffer = null;
97         int retVal = 0;
98         long int readCnt = 0;
99
100         pFilePath = _StringConverter::CopyToCharArrayN(filePath);
101         SysTryReturnResult(NID_BASE_UTIL, pFilePath != null, GetLastResult(), "File path length < 0 or E_OUT_OF_MEMORY");
102
103         res = converter.OpenConverter(encodingScheme);
104         r = GetLastResult();
105         SysTryCatch(NID_BASE_UTIL, res == true, , r, "[%s] Encoding scheme not supported", GetErrorMessage(r));
106
107         pFile = fopen(pFilePath, "r");
108         SysTryCatch(NID_BASE_UTIL, pFile != null, r = E_FILE_NOT_FOUND, E_FILE_NOT_FOUND,
109                 "[%s] Failed to open the file.", GetErrorMessage(__ConvertNativeErrorToResult(errno)));
110
111         retVal = stat(pFilePath, &st);
112         SysTryCatch(NID_BASE_UTIL, retVal == 0, r = E_IO, E_IO,
113                 "[%s] Failed to get information about the file.", GetErrorMessage(__ConvertNativeErrorToResult(errno)));
114
115         fileSize = st.st_size + 1;      // +1 for null-terminated string
116
117         buffer = static_cast< char* >(calloc(fileSize, sizeof(char)));
118         SysTryCatch(NID_BASE_UTIL, buffer != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
119         readCnt = fread(buffer, 1, fileSize - 1, pFile);
120
121         if (readCnt < fileSize)
122         {
123                 int ret = ferror(pFile);
124                 SysTryCatch(NID_BASE_UTIL, ret == 0, r = E_IO, E_IO, "[%s] Failed to perform read operation.", GetErrorMessage(E_IO));
125         }
126
127         __pParseStr = converter.ConvertToUcharN(buffer, fileSize);
128         SysTryCatch(NID_BASE_UTIL, __pParseStr != null, r = E_IO, E_IO, "[%s] charcater conversion failed.", GetErrorMessage(E_IO));
129         __isAllocatedOnHeap = true;
130
131 CATCH:
132         delete[] pFilePath;
133         pFilePath = null;
134
135         free(buffer);
136         buffer = null;
137
138         if (pFile != null)
139         {
140                 fclose(pFile);
141                 pFile = null;
142         }
143
144         return r;
145 }
146
147 bool
148 _ScannerImpl::HasNextToken(void)
149 {
150         String token;
151         int length = 0;
152
153         result r = GetNextTokenWithoutPattern(token, length);
154         SysTryLogReturn(NID_BASE_UTIL, r == E_SUCCESS, false, "Can not get the next token");
155
156         return true;
157 }
158
159 bool
160 _ScannerImpl::HasNextToken(const RegularExpression& pattern)
161 {
162         String patStr(pattern.GetPattern());
163         return HasNextToken(patStr);
164 }
165
166 bool
167 _ScannerImpl::HasNextToken(const String& pattern)
168 {
169         RegularExpression regex;
170         ArrayList matchedStrList(SingleObjectDeleter);
171         int length = 0;
172         String out;
173
174         result r = GetNextTokenWithoutPattern(out, length);
175         SysTryLogReturn(NID_BASE_UTIL, r != E_DATA_NOT_ENOUGH, false, "Input has no remaining tokens");
176
177         SysTryLogReturn(NID_BASE_UTIL, r == E_SUCCESS, false, "The next token does not match to the pattern");
178
179         r = regex.Construct(pattern, REGEX_UNICODE);
180         SysTryLogReturn(NID_BASE_UTIL, r == E_SUCCESS, false, "Regular expression construction failed");
181
182         r = matchedStrList.Construct();
183         SysTryLogReturn(NID_BASE_UTIL, r == E_SUCCESS, false, "Arraylist construction failed");
184
185         bool res = regex.Match(out, true, &matchedStrList);
186         SysTryLogReturn(NID_BASE_UTIL, res, res, "Match Failed");
187
188         return true;
189 }
190
191
192 result
193 _ScannerImpl::GetNextToken(String& nextStr)
194 {
195         int length = 0;
196
197         result r = GetNextTokenWithoutPattern(nextStr, length);
198         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_DATA_NOT_ENOUGH, "Can not get the next token");
199
200         __position = __position + length;
201
202         return r;
203 }
204
205 result
206 _ScannerImpl::GetNextToken(const RegularExpression& pattern, String& nextStr)
207 {
208         String patStr(pattern.GetPattern());
209         return GetNextToken(patStr, nextStr);
210 }
211
212 result
213 _ScannerImpl::GetNextToken(const String& pattern, String& nextStr)
214 {
215         RegularExpression regex;
216         ArrayList matchedStrList(SingleObjectDeleter);
217         int length = 0;
218         String out;
219
220         result r = GetNextTokenWithoutPattern(out, length);
221         SysTryReturnResult(NID_BASE_UTIL, r != E_DATA_NOT_ENOUGH, E_DATA_NOT_ENOUGH, "Input has no remaining tokens");
222
223         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_DATA_NOT_FOUND, "The next token does not match to the pattern");
224
225         r = regex.Construct(pattern, REGEX_UNICODE);
226         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_DATA_NOT_FOUND, "Regular expression construction failed");
227
228         r = matchedStrList.Construct();
229         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_DATA_NOT_FOUND, "Arraylist construction failed");
230
231         bool res = regex.Match(out, true, &matchedStrList);
232         SysTryReturnResult(NID_BASE_UTIL, res, E_DATA_NOT_FOUND, "Match Failed");
233
234         nextStr = out;
235         __position = __position + length;
236         return E_SUCCESS;
237 }
238
239 result
240 _ScannerImpl::GetNextSignedChar(signed char& nextSignedChar)
241 {
242         return GetNextSignedChar(nextSignedChar, __radix);
243 }
244
245 result
246 _ScannerImpl::GetNextSignedChar(signed char& nextSignedChar, int radix)
247 {
248         String out;
249         int length = 0;
250         char ch = 0x00;
251
252         result r = GetNextTokenWithoutPattern(out, length);
253         SysTryReturnResult(NID_BASE_UTIL, r != E_DATA_NOT_ENOUGH, E_DATA_NOT_ENOUGH, "Input has no remaining tokens");
254         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to signed char");
255
256         r = Int8::Parse(out, radix, ch);
257         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to signed char");
258
259         nextSignedChar = static_cast< signed char >(ch);
260         __position = __position + length;
261
262         return r;
263 }
264
265 bool
266 _ScannerImpl::IsNextTokenConvertibleToSignedChar(void)
267 {
268         return IsNextTokenConvertibleToSignedChar(__radix);
269 }
270
271 bool
272 _ScannerImpl::IsNextTokenConvertibleToSignedChar(int radix)
273 {
274         String out;
275         int length = 0;
276         char ch = 0;
277
278         result r = GetNextTokenWithoutPattern(out, length);
279         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[%s] Can not get the next token as signed char", GetErrorMessage(E_NUM_FORMAT));
280
281         r = Int8::Parse(out, radix, ch);
282         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[%s] Can not parse the token to signed char", GetErrorMessage(E_NUM_FORMAT));
283
284         return true;
285 }
286
287 result
288 _ScannerImpl::GetNextInt(int& nextInt)
289 {
290         return GetNextInt(nextInt, __radix);
291 }
292
293 result
294 _ScannerImpl::GetNextInt(int& nextInt, int radix)
295 {
296         String out;
297         int length = 0;
298
299         result r = GetNextTokenWithoutPattern(out, length);
300         SysTryReturnResult(NID_BASE_UTIL, r != E_DATA_NOT_ENOUGH, E_DATA_NOT_ENOUGH, "Input has no remaining tokens");
301         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to integer");
302
303         out.Replace(DEFAULT_GROUP_SEPARATOR, L"");
304         r = Integer::Parse(out, radix, nextInt);
305         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to integer");
306
307         __position = __position + length;
308
309         return r;
310 }
311
312 bool
313 _ScannerImpl::IsNextTokenConvertibleToInt(void)
314 {
315         return IsNextTokenConvertibleToInt(__radix);
316 }
317
318 bool
319 _ScannerImpl::IsNextTokenConvertibleToInt(int radix)
320 {
321         String out;
322         int val = 0;
323         int length = 0;
324
325         result r = GetNextTokenWithoutPattern(out, length);
326         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[%s] Can not get the next token as integer", GetErrorMessage(E_NUM_FORMAT));
327
328         out.Replace(DEFAULT_GROUP_SEPARATOR, L"");
329         r = Integer::Parse(out, radix, val);
330         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[%s] Can not get next Integer", GetErrorMessage(E_NUM_FORMAT));
331
332         return true;
333 }
334
335 result
336 _ScannerImpl::GetNextShort(short& nextShort)
337 {
338         return GetNextShort(nextShort, __radix);
339 }
340
341 result
342 _ScannerImpl::GetNextShort(short& nextShort, int radix)
343 {
344         String out;
345         int length = 0;
346
347         result r = GetNextTokenWithoutPattern(out, length);
348         SysTryReturnResult(NID_BASE_UTIL, r != E_DATA_NOT_ENOUGH, E_DATA_NOT_ENOUGH, "Input has no remaining tokens");
349         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to short");
350
351         out.Replace(DEFAULT_GROUP_SEPARATOR, L"");
352         r = Short::Parse(out, radix, nextShort);
353         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to short");
354
355         __position = __position + length;
356
357         return r;
358 }
359
360 bool
361 _ScannerImpl::IsNextTokenConvertibleToShort(void)
362 {
363         return IsNextTokenConvertibleToShort(__radix);
364 }
365
366 bool
367 _ScannerImpl::IsNextTokenConvertibleToShort(int radix)
368 {
369         String out;
370         short val = 0;
371         int length = 0;
372
373         result r = GetNextTokenWithoutPattern(out, length);
374         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[%s] Can not get next token as short", GetErrorMessage(E_NUM_FORMAT));
375
376         out.Replace(DEFAULT_GROUP_SEPARATOR, L"");
377         r = Short::Parse(out, radix, val);
378         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[%s] Can not get next short", GetErrorMessage(E_NUM_FORMAT));
379
380         return true;
381 }
382
383 result
384 _ScannerImpl::GetNextLongLong(long long& nextLongLong)
385 {
386         return GetNextLongLong(nextLongLong, __radix);
387 }
388
389 result
390 _ScannerImpl::GetNextLongLong(long long& nextLongLong, int radix)
391 {
392         String out;
393         int length = 0;
394
395         result r = GetNextTokenWithoutPattern(out, length);
396         SysTryReturnResult(NID_BASE_UTIL, r != E_DATA_NOT_ENOUGH, E_DATA_NOT_ENOUGH, "Input has no remaining tokens");
397         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to long long");
398
399         out.Replace(DEFAULT_GROUP_SEPARATOR, L"");
400         r = LongLong::Parse(out, radix, nextLongLong);
401         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to long long");
402
403         __position = __position + length;
404
405         return r;
406 }
407
408 bool
409 _ScannerImpl::IsNextTokenConvertibleToLongLong(void)
410 {
411         String out;
412         long long val = 0;
413         int length = 0;
414
415         result r = GetNextTokenWithoutPattern(out, length);
416         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[E_NUM_FORMAT] Can not get next token as long long");
417
418         r = LongLong::Parse(out, val);
419         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[E_NUM_FORMAT] Can not get next long long");
420
421         return true;
422 }
423
424 result
425 _ScannerImpl::GetNextFloat(float& nextFloat)
426 {
427         String out;
428         int length = 0;
429
430         result r = GetNextTokenWithoutPattern(out, length);
431         SysTryReturnResult(NID_BASE_UTIL, r != E_DATA_NOT_ENOUGH, E_DATA_NOT_ENOUGH, "Input has no remaining tokens");
432         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to float");
433
434         out.Replace(DEFAULT_GROUP_SEPARATOR, L"");
435         r = Float::Parse(out, nextFloat);
436         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to float");
437
438         __position = __position + length;
439
440         return r;
441 }
442
443
444 bool
445 _ScannerImpl::IsNextTokenConvertibleToFloat(void)
446 {
447         String out;
448         RegularExpression re;
449         float val = 0.0f;
450         int length = 0;
451
452         result r = GetNextTokenWithoutPattern(out, length);
453         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[%s] Can not get next token as float", GetErrorMessage(E_NUM_FORMAT));
454
455         out.Replace(DEFAULT_GROUP_SEPARATOR, L"");
456         r = Float::Parse(out, val);
457         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[%s] Can not get next float", GetErrorMessage(E_NUM_FORMAT));
458
459         return true;
460 }
461
462 result
463 _ScannerImpl::GetNextDouble(double& nextDouble)
464 {
465         String out;
466         int length = 0;
467
468         result r = GetNextTokenWithoutPattern(out, length);
469         SysTryReturnResult(NID_BASE_UTIL, r != E_DATA_NOT_ENOUGH, E_DATA_NOT_ENOUGH, "Input has no remaining tokens");
470         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to double");
471
472         out.Replace(DEFAULT_GROUP_SEPARATOR, L"");
473         r = Double::Parse(out, nextDouble);
474         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to double");
475
476         __position = __position + length;
477
478         return r;
479 }
480
481 bool
482 _ScannerImpl::IsNextTokenConvertibleToDouble(void)
483 {
484         String out;
485         double val = 0.0f;
486         int length = 0;
487
488         result r = GetNextTokenWithoutPattern(out, length);
489         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[%s] Can not get next token as double", GetErrorMessage(E_NUM_FORMAT));
490
491         out.Replace(DEFAULT_GROUP_SEPARATOR, L"");
492         r = Double::Parse(out, val);
493         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[%s] Can not get next token", GetErrorMessage(E_NUM_FORMAT));
494
495         return true;
496 }
497
498 result
499 _ScannerImpl::GetNextBool(bool& nextBool)
500 {
501         String out;
502         int length = 0;
503
504         result r = GetNextTokenWithoutPattern(out, length);
505         SysTryReturnResult(NID_BASE_UTIL, r != E_DATA_NOT_ENOUGH, E_DATA_NOT_ENOUGH, "Input has no remaining tokens");
506         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not parse the token to bool");
507
508         String temp;
509         r = out.ToUpper(temp);
510         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_NUM_FORMAT, "Can not get next bool");
511
512         if (!temp.CompareTo(L"TRUE"))
513         {
514                 nextBool = true;
515                 __position = __position + length;
516         }
517         else if (!temp.CompareTo(L"FALSE"))
518         {
519                 nextBool = false;
520                 __position = __position + length;
521         }
522         else
523         {
524                 r = E_NUM_FORMAT;
525         }
526
527         return r;
528 }
529
530 bool
531 _ScannerImpl::IsNextTokenConvertibleToBool(void)
532 {
533         String out;
534         int length = 0;
535
536         result r = GetNextTokenWithoutPattern(out, length);
537         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_NUM_FORMAT, "[%s] Can not get next token as bool", GetErrorMessage(E_NUM_FORMAT));
538
539         String temp;
540         out.ToUpper(temp);
541         return ((!temp.CompareTo(L"TRUE")) || (!temp.CompareTo(L"FALSE")));
542 }
543
544 bool
545 _ScannerImpl::HasNextLine(void)
546 {
547         String pattern(DEFAULT_LINE_SEPARATOR_PATTERN);
548         RegularExpression regex;
549         ArrayList matchedStrList(SingleObjectDeleter);
550
551         result r = regex.Construct(pattern, REGEX_UNICODE);
552         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_DATA_NOT_ENOUGH, "[%s] Regular expression construction failed", GetErrorMessage(E_DATA_NOT_ENOUGH));
553
554         r = matchedStrList.Construct();
555         SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, E_DATA_NOT_ENOUGH, "[%s] Arraylist construction failed", GetErrorMessage(E_DATA_NOT_ENOUGH));
556
557         bool res = regex.Match(String(__pParseStr + __position), false, &matchedStrList);
558         SysTryReturn(NID_BASE_UTIL, res == true, false, E_DATA_NOT_ENOUGH, "[%s] Match Failed", GetErrorMessage(E_DATA_NOT_ENOUGH));
559
560         return res;
561 }
562
563
564 result
565 _ScannerImpl::GetNextLine(String& nextLine)
566 {
567         String pattern(COMPLETE_LINE_PATTERN);
568         RegularExpression regex;
569         ArrayList matchedStrList(SingleObjectDeleter);
570
571         result r = regex.Construct(pattern, REGEX_UNICODE);
572         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_DATA_NOT_ENOUGH, "Regular expression construction failed");
573
574         r = matchedStrList.Construct();
575         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_DATA_NOT_ENOUGH, "Arraylist construction failed");
576
577         bool res = regex.Match(String(__pParseStr + __position), false, &matchedStrList);
578         SysTryReturnResult(NID_BASE_UTIL, res == true, E_DATA_NOT_ENOUGH, "Match Failed");
579
580         int matches = matchedStrList.GetCount();
581         SysTryReturnResult(NID_BASE_UTIL, matches > 0, E_DATA_NOT_FOUND, "Match Failed");
582
583         String temp = *(String*) matchedStrList.GetAt(0);
584         __position = __position + temp.GetLength();
585
586         RegularExpression re;
587         r = re.Construct(DEFAULT_LINE_SEPARATOR_PATTERN, REGEX_UNICODE);
588         re.Replace(temp, L"", true);
589         nextLine = temp;
590
591         return E_SUCCESS;
592 }
593
594 result
595 _ScannerImpl::FindInLine(const String& str, String& MatchedStr)
596 {
597         _RegularExpressionImpl regex;
598         ArrayList matchedStrList(SingleObjectDeleter);
599
600         result r = regex.Construct(str, REGEX_UNICODE);
601         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_DATA_NOT_FOUND, "Regular expression construction failed");
602
603         r = matchedStrList.Construct();
604         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_DATA_NOT_FOUND, "Arraylist construction failed");
605
606         bool res = regex.Match(String(__pParseStr + __position), false, &matchedStrList);
607         SysTryReturnResult(NID_BASE_UTIL, res == true, E_DATA_NOT_FOUND, "Match Failed");
608
609         int matches = matchedStrList.GetCount();
610         SysTryReturnResult(NID_BASE_UTIL, matches > 0, E_DATA_NOT_FOUND, "Match Failed");
611
612         MatchedStr = *(String*) matchedStrList.GetAt(0);
613         int matchEnd = regex.GetLastMatchEnd();
614         __position = __position + matchEnd;
615
616         return E_SUCCESS;
617 }
618
619 result
620 _ScannerImpl::FindInLine(const RegularExpression& pattern, String& MatchedStr)
621 {
622         String patStr(pattern.GetPattern());
623         return FindInLine(patStr, MatchedStr);
624 }
625
626 void
627 _ScannerImpl::Skip(const String& str)
628 {
629         RegularExpression regex;
630         ArrayList matchedStrList(SingleObjectDeleter);
631         String inputStr(__pParseStr + __position);
632
633         result r = regex.Construct(str, REGEX_UNICODE);
634         SysTryReturnVoidResult(NID_BASE_UTIL, r == E_SUCCESS, r, "Regular expression construction failed");
635
636         r = matchedStrList.Construct();
637         SysTryReturnVoidResult(NID_BASE_UTIL, r == E_SUCCESS, r, "ArrayList construction failed");
638
639         bool res = regex.Match(inputStr, false, &matchedStrList);
640         SysTryReturnVoidResult(NID_BASE_UTIL, res == true, E_SYSTEM, "Regular expression match failed");
641
642         int matches = matchedStrList.GetCount();
643         SysTryReturnVoidResult(NID_BASE_UTIL, matches > 0, E_SYSTEM, "Regular expression construction failed");
644
645         String skipToken = *(dynamic_cast< String* >(matchedStrList.GetAt(0)));
646         int length = skipToken.GetLength();
647         const wchar_t* wstr1 = skipToken.GetPointer();
648         const wchar_t* wstr2 = __pParseStr + __position;
649         bool isSame = true;
650
651         while (*wstr1 != 0x0000)
652         {
653                 if (*wstr1++ != *wstr2++)
654                 {
655                         isSame = false;
656                         break;
657                 }
658         }
659         if (isSame)
660         {
661                 __position = __position + length;
662         }
663 }
664
665 void
666 _ScannerImpl::Skip(const RegularExpression& pattern)
667 {
668         String patStr(pattern.GetPattern());
669         return Skip(patStr);
670 }
671
672 int
673 _ScannerImpl::GetRadix(void)
674 {
675         return __radix;
676 }
677
678 String
679 _ScannerImpl::GetDelimiter(void)
680 {
681         return __delimiter;
682 }
683
684 void
685 _ScannerImpl::SetDelimiter(const String& delimiter)
686 {
687         __delimiter = delimiter;
688 }
689
690 void
691 _ScannerImpl::SetDelimiter(const RegularExpression& pattern)
692 {
693         __delimiter = pattern.GetPattern();
694 }
695
696 void
697 _ScannerImpl::SetRadix(int radix)
698 {
699         if (((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
700                  (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)))
701         {
702                 __radix = radix;
703         }
704 }
705
706
707 result
708 _ScannerImpl::GetNextTokenWithoutPattern(String& ret, int& length)
709 {
710         SysTryReturnResult(NID_BASE_UTIL, ((__pParseStr + __position) != null) && ((*(__pParseStr + __position) != 0x0000)),
711                 E_DATA_NOT_ENOUGH, "Input has no remaining tokens.");
712
713         RegularExpression regDelim;
714         result r = regDelim.Construct(__delimiter, REGEX_UNICODE);
715         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_DATA_NOT_FOUND, "Regular expression construction failed");
716
717         ArrayList matchedStrList(SingleObjectDeleter);
718         r = matchedStrList.Construct();
719         SysTryReturnResult(NID_BASE_UTIL, r == E_SUCCESS, E_DATA_NOT_FOUND, "Arraylist construction failed");
720
721         String inputStr(__pParseStr + __position);
722         bool res = regDelim.Match(inputStr, false, &matchedStrList);
723
724         int matches = matchedStrList.GetCount();
725
726         if (matches > 0)
727         {
728                 String delimiter1 = *(dynamic_cast< String* >(matchedStrList.GetAt(0)));
729
730                 if (!delimiter1.IsEmpty())      // 1st delimiter is not empty
731                 {
732                         int delimLength1 = delimiter1.GetLength();
733                         int delim1Index = 0;
734                         inputStr.IndexOf(delimiter1, 0, delim1Index);
735
736                         if (delim1Index > 0) // Delimiter1 appears after the token i.e followed by token
737                         {
738                                 inputStr.SubString(0, delim1Index, ret);        // Extract the string in between first and second delimiter
739                                 length += delim1Index;
740                                 return E_SUCCESS;
741                         }
742                         // When the delimiter exists in first position, find the 2nd delimiter
743                         length += delimLength1;
744                         String tmpStr(__pParseStr + __position + length);
745                         SysTryReturnResult(NID_BASE_UTIL, tmpStr != null, E_DATA_NOT_FOUND, "There are no more tokens in input string");
746
747                         matchedStrList.RemoveAll();
748                         res = regDelim.Match(tmpStr, false, &matchedStrList);
749                         matches = matchedStrList.GetCount();
750                         if (matches > 0)
751                         {
752                                 String delimiter2 = *(dynamic_cast< String* >(matchedStrList.GetAt(0)));
753                                 // If the 2nd delimiter is empty, the token will be one character followed by 1st delimiter.
754                                 if (delimiter2.IsEmpty())
755                                 {
756                                         ret.Clear();
757                                         ret.Append(*(__pParseStr + __position + length));
758                                         length += 1;
759                                 }
760                                 else
761                                 {
762                                         int index2 = 0;
763                                         tmpStr.IndexOf(delimiter2, 0, index2);
764                                         tmpStr.SubString(0, index2, ret);       // Extract the string in between first and second delimiter
765                                         length += index2;
766                                 }
767                         }
768                         else // There are no second delimiters
769                         {
770                                 ret.Clear();
771                                 ret.Append(__pParseStr + __position + length);
772                                 length += ret.GetLength();
773                                 SysTryReturnResult(NID_BASE_UTIL, length > 0, E_DATA_NOT_FOUND, "There are no more tokens in input string");
774                         }
775
776                 }
777                 else    // 1st delimiter is empty
778                 {
779                         // Find 2nd delimiter from "__pParseStr + __position + length + 1"
780                         String tmpStr(__pParseStr + __position + length + 1);
781                         if (tmpStr == null)     // When InputStr is last token, extract the token.
782                         {
783                                 ret.Clear();
784                                 ret.Append(__pParseStr + __position + length);
785                                 length = ret.GetLength();
786                         }
787                         else
788                         {
789                                 matchedStrList.RemoveAll();
790                                 res = regDelim.Match(tmpStr, false, &matchedStrList);
791                                 matches = matchedStrList.GetCount();
792                                 SysTryReturnResult(NID_BASE_UTIL, matches > 0, E_DATA_NOT_FOUND, "There are no more tokens in input string");
793
794                                 String delimiter2 = *(dynamic_cast< String* >(matchedStrList.GetAt(0)));
795                                 if (delimiter2.IsEmpty())       // If the 2nd delimiter is also empty, the token will be one character followed by 1st delimiter.
796                                 {
797                                         ret.Clear();
798                                         ret.Append(*(__pParseStr + __position));
799                                         length = 1;
800                                 }
801                                 else
802                                 {
803                                         int index2 = 0;
804                                         inputStr.IndexOf(delimiter2, 0, index2);
805                                         inputStr.SubString(0, index2, ret);
806                                         length = index2;
807                                 }
808                         }
809                 }
810         }
811         else    //There are no delimiters
812         {
813                 ret.Clear();
814                 ret.Append(__pParseStr + __position);
815                 length = ret.GetLength();
816                 SysTryReturnResult(NID_BASE_UTIL, length > 0, E_DATA_NOT_FOUND, "There are no more tokens in input string");
817         }
818
819         return r;
820 }
821 }}}   // Tizen::Base::Utility