N_SE-36002 : fix submode for implicit resolution
[platform/framework/native/appfw.git] / src / base / utility / FBaseUtilScanner.cpp
1 #include <sys/stat.h>
2 #include <stdio.h>
3 #include <errno.h>
4
5 #include <FBaseCharacter.h>
6 #include <FBaseResult.h>
7 #include <FBaseUtilScanner.h>
8 #include "FBaseUtilRegularExpression.h"
9 #include <FBaseColArrayList.h>
10 #include <FBaseSysLog.h>
11 #include "FBaseUtil_ScannerImpl.h"
12
13
14 using namespace Tizen::Base::Collection;
15 using namespace Tizen::Base::Utility;
16 using namespace Tizen::Base;
17
18 namespace Tizen { namespace Base { namespace Utility
19 {
20 Scanner::Scanner(void)
21         :__pScannerImpl(null)
22 {
23
24 }
25
26 Scanner::~Scanner()
27 {
28         delete __pScannerImpl;
29         __pScannerImpl = null;
30 }
31
32 result Scanner::Construct(const String& inputStr)
33 {
34         result r = E_SUCCESS;
35         SysAssertf(__pScannerImpl == null, "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class.");
36         SysTryReturnResult(NID_BASE_UTIL, !inputStr.IsEmpty(), E_INVALID_ARG, "[E_INVALID_ARG] The length of the input data is zero.");
37         __pScannerImpl = new (std::nothrow) _ScannerImpl();
38         SysTryReturnResult(NID_BASE_UTIL, __pScannerImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed");
39
40         r = __pScannerImpl->Construct(inputStr);
41         return r;
42 }
43
44 result Scanner::Construct(const String& inputFilePath, const String& encodingScheme)
45 {
46         result r = E_SUCCESS;
47         SysAssertf(__pScannerImpl == null, "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class.");
48         SysTryReturn(NID_BASE_UTIL, !inputFilePath.IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The length of the file name is zero.");
49         SysTryReturn(NID_BASE_UTIL, !encodingScheme.IsEmpty(), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The length of the encoding scheme is zero.");
50
51         __pScannerImpl = new (std::nothrow) _ScannerImpl();
52         SysTryReturnResult(NID_BASE_UTIL, __pScannerImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed");
53
54         r = __pScannerImpl->Construct(inputFilePath, encodingScheme);
55         return r;
56 }
57
58 bool Scanner::HasNextToken(void) const
59 {
60         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
61         return __pScannerImpl->HasNextToken();
62 }
63
64 bool Scanner::HasNextToken(const RegularExpression& pattern) const
65 {
66         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
67         return __pScannerImpl->HasNextToken(pattern);
68 }
69
70 bool Scanner::HasNextToken(const String& pattern) const
71 {
72         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
73         return __pScannerImpl->HasNextToken(pattern);
74 }
75
76
77 result Scanner::GetNextToken(String& nextTok) const
78 {
79         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
80         return __pScannerImpl->GetNextToken(nextTok);
81 }
82
83 result Scanner::GetNextToken(const RegularExpression& pattern, String& nextTok) const
84 {
85         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
86         return __pScannerImpl->GetNextToken(pattern, nextTok);
87 }
88
89 result Scanner::GetNextToken(const String& pattern, String& nextTok) const
90 {
91         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
92         return __pScannerImpl->GetNextToken(pattern, nextTok);
93 }
94
95 result Scanner::GetNextSignedChar(signed char& nextSignedChar) const
96 {
97         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
98         return  __pScannerImpl->GetNextSignedChar(nextSignedChar );
99 }
100
101 result Scanner::GetNextSignedChar(signed char& nextSignedChar, int radix) const
102 {
103         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
104         SysTryReturnResult(NID_BASE_UTIL, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) || (radix == Character::RADIX_DECIMAL) ||
105                                 (radix == Character::RADIX_HEXADECIMAL)),E_NUM_FORMAT,
106                                 "[E_NUM_FORMAT] The radix(%d) MUST be one of 2, 8, 10 and 16.", radix);
107
108         return  __pScannerImpl->GetNextSignedChar(nextSignedChar, radix);
109 }
110
111 bool Scanner::IsNextTokenConvertibleToSignedChar(void) const
112 {
113         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
114
115         return __pScannerImpl->IsNextTokenConvertibleToSignedChar();
116 }
117
118 bool Scanner::IsNextTokenConvertibleToSignedChar(int radix) const
119 {
120         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
121         SysTryReturnResult(NID_BASE_UTIL, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) || (radix == Character::RADIX_DECIMAL) ||
122                                 (radix == Character::RADIX_HEXADECIMAL)),E_NUM_FORMAT,
123                                 "[E_NUM_FORMAT] The radix(%d) MUST be one of 2, 8, 10 and 16.", radix);
124
125         return __pScannerImpl->IsNextTokenConvertibleToSignedChar(radix);
126 }
127
128 result Scanner::GetNextInt(int& nextInt) const
129 {
130         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
131         return __pScannerImpl->GetNextInt(nextInt);
132 }
133
134 result Scanner::GetNextInt(int& nextInt, int radix) const
135 {
136         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
137         SysTryReturnResult(NID_BASE_UTIL, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) || (radix == Character::RADIX_DECIMAL) ||
138                                 (radix == Character::RADIX_HEXADECIMAL)),E_NUM_FORMAT,
139                                 "[E_NUM_FORMAT] The radix(%d) MUST be one of 2, 8, 10 and 16.", radix);
140
141         return __pScannerImpl->GetNextInt(nextInt, radix);
142 }
143
144 bool Scanner::IsNextTokenConvertibleToInt(void) const
145 {
146         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
147         return __pScannerImpl->IsNextTokenConvertibleToInt();
148 }
149
150 bool Scanner::IsNextTokenConvertibleToInt(int radix) const
151 {
152         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
153         SysTryReturnResult(NID_BASE_UTIL, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) || (radix == Character::RADIX_DECIMAL) ||
154                                 (radix == Character::RADIX_HEXADECIMAL)),E_NUM_FORMAT,
155                                 "[E_NUM_FORMAT] The radix(%d) MUST be one of 2, 8, 10 and 16.", radix);
156
157         return __pScannerImpl->IsNextTokenConvertibleToInt(radix);
158 }
159
160 result Scanner::GetNextShort(short& nextShort) const
161 {
162         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
163         return __pScannerImpl->GetNextShort(nextShort);
164 }
165
166 result Scanner::GetNextShort(short& nextShort, int radix) const
167 {
168         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
169         SysTryReturnResult(NID_BASE_UTIL, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) || (radix == Character::RADIX_DECIMAL) ||
170                                 (radix == Character::RADIX_HEXADECIMAL)),E_NUM_FORMAT,
171                                 "[E_NUM_FORMAT] The radix(%d) MUST be one of 2, 8, 10 and 16.", radix);
172
173         return __pScannerImpl->GetNextShort(nextShort, radix);
174 }
175
176 bool Scanner::IsNextTokenConvertibleToShort(void) const
177 {
178         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
179         return __pScannerImpl->IsNextTokenConvertibleToShort();
180 }
181
182 bool Scanner::IsNextTokenConvertibleToShort(int radix) const
183 {
184         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
185         SysTryReturnResult(NID_BASE_UTIL, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) || (radix == Character::RADIX_DECIMAL) ||
186                                 (radix == Character::RADIX_HEXADECIMAL)),E_NUM_FORMAT,
187                                 "[E_NUM_FORMAT] The radix(%d) MUST be one of 2, 8, 10 and 16.", radix);
188
189
190         return __pScannerImpl->IsNextTokenConvertibleToShort(radix);
191 }
192
193 result Scanner::GetNextLongLong(long long& nextLongLong) const
194 {
195         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
196         return __pScannerImpl->GetNextLongLong(nextLongLong);
197 }
198
199 result Scanner::GetNextLongLong(long long& nextLongLong, int radix) const
200 {
201 //This always returns nextLongLong in radix 10. This function will be implemented correctly once LongLong::Parse() is implemnted for different radix
202         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
203         SysTryReturnResult(NID_BASE_UTIL, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) || (radix == Character::RADIX_DECIMAL) ||
204                                 (radix == Character::RADIX_HEXADECIMAL)),E_NUM_FORMAT,
205                                 "[E_NUM_FORMAT] The radix(%d) MUST be one of 2, 8, 10 and 16.", radix);
206         return __pScannerImpl->GetNextLongLong(nextLongLong);
207 }
208
209 bool Scanner::IsNextTokenConvertibleToLongLong(void) const
210 {
211         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
212         return __pScannerImpl->IsNextTokenConvertibleToLongLong();
213 }
214
215 result Scanner::GetNextFloat(float& nextFloat) const
216 {
217         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
218         return __pScannerImpl->GetNextFloat(nextFloat);
219 }
220
221
222 bool Scanner::IsNextTokenConvertibleToFloat(void) const
223 {
224         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
225         return __pScannerImpl->IsNextTokenConvertibleToFloat();
226 }
227
228 result Scanner::GetNextDouble(double& nextDouble) const
229 {
230         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
231         return __pScannerImpl->GetNextDouble(nextDouble);
232 }
233
234 bool Scanner::IsNextTokenConvertibleToDouble(void) const
235 {
236         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
237         return __pScannerImpl->IsNextTokenConvertibleToDouble();
238 }
239
240 result Scanner::GetNextBool(bool& nextBool) const
241 {
242         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
243         return __pScannerImpl->GetNextBool(nextBool);
244 }
245
246 bool Scanner::IsNextTokenConvertibleToBool(void) const
247 {
248         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
249         return __pScannerImpl->IsNextTokenConvertibleToBool();
250 }
251
252 bool Scanner::HasNextLine(void) const
253 {
254         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
255         return __pScannerImpl->HasNextLine();
256 }
257
258
259 result Scanner::GetNextLine(String& nextLine) const
260 {
261         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
262         return __pScannerImpl->GetNextLine(nextLine);
263
264 }
265
266 result Scanner::FindInLine(const String& patternStr, String& matchedStr)
267 {
268         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
269         SysTryReturnResult(NID_BASE_UTIL, !patternStr.IsEmpty(), E_INVALID_ARG, "[E_INVALID_ARG] The length of the input data is zero.");
270         return __pScannerImpl->FindInLine(patternStr, matchedStr);
271 }
272
273 result Scanner::FindInLine(const RegularExpression& pattern, String& matchedStr)
274 {
275         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
276         String str(pattern.GetPattern());
277         SysTryReturnResult(NID_BASE_UTIL, !str.IsEmpty(), E_INVALID_ARG, "[E_INVALID_ARG] The length of the input data is zero.");
278         return __pScannerImpl->FindInLine(pattern, matchedStr);
279 }
280
281 void Scanner::Skip(const String& patternStr)
282 {
283         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
284         SysTryReturn(NID_BASE_UTIL, !patternStr.IsEmpty(), , E_INVALID_ARG, "[%s] The length of the input data is zero.", GetErrorMessage(E_INVALID_ARG));
285         __pScannerImpl->Skip(patternStr);
286
287 }
288
289 void Scanner::Skip(const RegularExpression& pattern)
290 {
291         String str(pattern.GetPattern());
292         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
293         SysTryReturn(NID_BASE_UTIL, !str.IsEmpty(), , E_INVALID_ARG, "[%s] The length of the input data is zero.", GetErrorMessage(E_INVALID_ARG));
294         __pScannerImpl->Skip(pattern);
295
296 }
297
298 int Scanner::GetRadix(void) const
299 {
300         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
301         return __pScannerImpl->GetRadix();
302 }
303
304 String Scanner::GetDelimiter(void) const
305 {
306         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
307         return __pScannerImpl->GetDelimiter();
308 }
309
310 void Scanner::SetDelimiter(const String& patternStr)
311 {
312         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
313         __pScannerImpl->SetDelimiter(patternStr);
314 }
315
316 void Scanner::SetDelimiter(const RegularExpression& pattern)
317 {
318         SysAssertf(__pScannerImpl != null, "Not yet constructed! Construct() should be called before use.");
319         String delimPat(pattern.GetPattern());
320         __pScannerImpl->SetDelimiter(delimPat);
321 }
322
323 void Scanner::SetRadix(int radix)
324 {
325         if(__pScannerImpl)
326         {
327                 if(((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
328                         (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)))
329                 {
330                         __pScannerImpl->SetRadix(radix);
331                 }
332         }
333 }
334 }}}   // Osp::Base::Utility