2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FBaseUtilScanner.h
20 * @brief This is the header file for the %Scanner class.
22 * This header file contains declarations and definitions of the %Scanner class.
26 #ifndef _FBASE_UTIL_SCANNER_H_
27 #define _FBASE_UTIL_SCANNER_H_
29 #include <FBaseString.h>
30 #include <FBaseUtilRegularExpression.h>
32 namespace Tizen { namespace Base { namespace Utility
36 * @brief This class provides to parse primitive types and strings, and supports to use regular expressions and various encoding schemes.
39 * The %Scanner class breaks the input, which can be either a %String instance or strings from a file, into tokens using a delimiter.
40 * The delimiter is set to whitespace as a default value. The resulting tokens are converted into values of different types according
41 * to the methods in %Scanner.
43 * The following example demonstrates how to use the %Scanner class.
46 * String str = L"1 12 34.5 58 false";
50 * scan.Construct(str);
52 * ret = scan.IsNextTokenConvertibleToSignedChar(); //ret true
54 * scan.GetNextSignedChar(val); //val 1
56 * ret = scan.IsNextTokenConvertibleToShort(10); //ret true
58 * scan.GetNextShort(shortVal); //shortVal 12
60 * ret = scan.IsNextTokenConvertibleToInt(10); //ret false
61 * ret = scan.IsNextTokenConvertibleToFloat(); //ret true
63 * scan.GetNextFloat(floatVal); //floatVal 34.5
65 * ret = scan.IsNextTokenConvertibleToInt(16); //ret true
67 * scan.GetNextInt(intVal, 16); //intVal 88
69 * ret = scan.IsNextTokenConvertibleToBool(); //ret true
70 * scan.GetNextBool(ret); //ret false
74 class _OSP_EXPORT_ Scanner
75 : public Tizen::Base::Object
80 * The object is not fully constructed after this constructor is called. @n
81 * For full construction, the Construct() method must be called right after calling this constructor.
88 * This destructor overrides Tizen::Base::Object::~Object().
92 virtual ~Scanner(void);
95 * Initializes the current instance of %Scanner with the specified instance of String.
98 * @return An error code
99 * @param[in] inputStr The string to scan
100 * @exception E_SUCCESS The method is successful.
101 * @exception E_INVALID_ARG The length of the specified @c string parameter is @c 0.
103 result Construct(const String& inputStr);
106 * Initializes the current instance of %Scanner with a file that includes the strings that are being scanned.
109 * @return An error code
110 * @param[in] inputFilePath The file to read to construct input data
111 * @param[in] encodingScheme The encoding type of the file
112 * @exception E_SUCCESS The method is successful.
113 * @exception E_FILE_NOT_FOUND The input file does not exist.
114 * @exception E_IO The file operation is invalid.
115 * @exception E_INVALID_ARG The specified encoding scheme does not exist.
116 * @remarks The supported encoding schemes are ASCII, GSM, UCS-2, UCS-2LE, UCS-2BE, UCS-4, UCS-4LE, UCS-4BE, UTF-8, UTF-16, UTF-16LE, UTF-16BE, UTF-32, UTF-32LE, UTF-32BE, ISO-8859-1~16 (except ISO-8859-12), Windows-874, Windows-1250 ~ Windows-1258, KSC5601, BIG5, GB2312, Shift_JIS, and ISO-2022-jp.
118 result Construct(const String& inputFilePath, const String& encodingScheme);
121 * Gets the substring of the input data matching to the pattern constructed from the specified string. @n
122 * Delimiter will be ignored and the returned string does not include any line terminator.
123 * If such a pattern in the input data is not found upto the next line terminator, return an empty string through the out parameter, matchedStr.
126 * @return An error code
127 * @param[in] patternStr The string to construct the pattern
128 * @param[out] matchedStr The matched string or empty string
129 * @exception E_SUCCESS The method is successful.
130 * @exception E_INVALID_ARG The length of the specified @c string parameter is @c 0.
131 * @exception E_DATA_NOT_FOUND No substring is found in the input data of current instance.
133 result FindInLine(const String& patternStr, String& matchedStr);
136 * Gets the substring matching the pattern from the input data. @n
137 * Delimiter will be ignored and the returned string does not include any line terminator.
138 * If such a pattern in the input data is not found upto the next line terminator, return an empty string through the out parameter, matchedStr.
141 * @return An error code
142 * @param[in] pattern The pattern to compile for finding substring from input data
143 * @param[out] matchedStr The matched string or empty string
144 * @exception E_SUCCESS The method is successful.
145 * @exception E_DATA_NOT_FOUND No substring is found in the input data of current instance.
148 result FindInLine(const RegularExpression& pattern, String& matchedStr);
151 * Gets the next token as @c signed @c char. @n Next token is converted to @c signed @c char using default radix.
152 * The @c signed @c char can hold value from -128 to 127.
155 * @return An error code
156 * @param[out] nextSignedChar The next token as a @c signed @c char
157 * @exception E_SUCCESS The method is successful.
158 * @exception E_NUM_FORMAT The next token cannot be translated into a valid signed @c char value.
159 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
161 result GetNextSignedChar(signed char& nextSignedChar) const;
164 * Gets the next token as @c signed @c char. @n Next token is converted to @c signed @c char using specified @c radix.
165 * The @c signed @c char can hold value from -128 to 127.
168 * @return An error code
169 * @param[out] nextSignedChar The next token as a @c signed @c char
170 * @param[in] radix The radix to use for conversion
171 * @exception E_SUCCESS The method is successful.
172 * @exception E_NUM_FORMAT The next token cannot be translated into a valid @c signed @c char value.
173 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
176 result GetNextSignedChar(signed char& nextSignedChar, int radix) const;
179 * Gets the next token as @c int. @n Next token is converted to signed integer using default radix.
180 * The signed integer can hold value from -2^31 to 2^31-1.
183 * @return An error code
184 * @param[out] nextInt The next token as a signed integer
185 * @exception E_SUCCESS The method is successful.
186 * @exception E_NUM_FORMAT The next token cannot be translated into a valid integer value.
187 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
189 result GetNextInt(int& nextInt) const;
192 * Gets the next token as @c int. @n Next token is converted to signed integer using specified @c radix.
193 * The signed integer can hold value from -2^31 to 2^31-1.
196 * @return An error code
197 * @param[out] nextInt The next token as a signed integer
198 * @param[in] radix The radix to use for conversion
199 * @exception E_SUCCESS The method is successful.
200 * @exception E_NUM_FORMAT The next token cannot be translated into a valid integer value.
201 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
203 result GetNextInt(int& nextInt, int radix) const;
206 * Gets the next token as @c short. @n Next token is converted to signed @c short using default radix.
207 * The signed @c short can hold value from -2^15 to 2^15-1.
210 * @return An error code
211 * @param[out] nextShort The next token as a signed @c short
212 * @exception E_SUCCESS The method is successful.
213 * @exception E_NUM_FORMAT The next token cannot be translated into a valid @c short value.
214 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
216 result GetNextShort(short& nextShort) const;
219 * Gets the next token as @c short. @n Next token is converted to signed @c short using specified @c radix.
220 * The signed @c short can hold value from -2^15 to 2^15-1.
223 * @return An error code
224 * @param[out] nextShort The next token as a signed @c short
225 * @param[in] radix The radix to use for conversion
226 * @exception E_SUCCESS The method is successful.
227 * @exception E_NUM_FORMAT The next token cannot be translated into a valid @c short value.
228 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
230 result GetNextShort(short& nextShort, int radix) const;
233 * Gets the next token as @c long @c long. @n Next token is converted to signed @c long @c long using default radix.
234 * The signed @c long @c long can hold value from -2^63 to 2^63-1.
237 * @return An error code
238 * @param[out] nextLongLong The next token as a signed @c long @c long
239 * @exception E_SUCCESS The method is successful.
240 * @exception E_NUM_FORMAT The next token cannot be translated into a valid @c long @c long value.
241 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
243 result GetNextLongLong(long long& nextLongLong) const;
246 * Gets the next token as @c long @c long. @n Next token is converted to signed @c long @c long using specified @c radix.
247 * The signed @c long @c long can hold value from -2^63 to 2^63-1.
250 * @return An error code
251 * @param[out] nextLongLong The next token as a signed @c long @c long
252 * @param[in] radix The radix to use for conversion
253 * @exception E_SUCCESS The method is successful.
254 * @exception E_NUM_FORMAT The next token cannot be translated into a valid @c long @c long value.
255 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
257 result GetNextLongLong(long long& nextLongLong, int radix) const;
260 * Gets the next token as @c float. @n Next token is converted to @c float.
261 * The signed @c float can hold a single-precision 32-bit floating number.
264 * @return An error code
265 * @param[out] nextFloat The next token as a @c float
266 * @exception E_SUCCESS The method is successful.
267 * @exception E_NUM_FORMAT The next token cannot be translated into a valid @c float value.
268 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
270 result GetNextFloat(float& nextFloat) const;
273 * Gets the next token as @c double. @n Next token is converted to @c double.
274 * The signed @c double can hold a double-precision 64-bit floating number.
277 * @return An error code
278 * @param[out] nextDouble The next token as a @c double
279 * @exception E_SUCCESS The method is successful.
280 * @exception E_NUM_FORMAT The next token cannot be translated into a valid @c double value.
281 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
283 result GetNextDouble(double& nextDouble) const;
286 * Gets the next token as @c bool. @n Next token is converted to boolean.
287 * Nothing can be converted, except true/TRUE or false/FALSE.
290 * @return An error code
291 * @param[out] nextBool The next token as @c bool
292 * @exception E_SUCCESS The method is successful.
293 * @exception E_NUM_FORMAT The next token cannot be translated into a valid @c bool value.
294 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
296 result GetNextBool(bool& nextBool) const;
299 * Gets the input string up to next line delimiter and advances the %Scanner to the beginning of the next line. @n
300 * The returned string does not include any line terminator.
303 * @return An error code
304 * @param[out] nextLine The next line as an instance of String
305 * @exception E_SUCCESS The method is successful.
306 * @exception E_DATA_NOT_ENOUGH There are no remaining lines for the current instance of %Scanner.
308 * The following example demonstrates how to use the %GetNextLine() method.
312 * String str = "1 hundred bricks\n2 bricks\n3 hundred bricks\n4 bricks";
317 * scan.Construct(str);
319 * if (scan.HasNextLine())
321 * scan.GetNextLine(out); //returns 1 hundred bricks
330 result GetNextLine(String& nextLine) const;
333 * Gets the next token as String.
336 * @return An error code
337 * @param[out] nextTok The next token as an instance of String
338 * @exception E_SUCCESS The method is successful.
339 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
341 * The following example demonstrates how to use the %GetNextToken() method.
344 * String str = "1 hundred bricks 2 bricks 3 hundred bricks 4 bricks";
349 * scan.Construct(str);
350 * String delimiter(L"\\s*bricks\\s*");
351 * scan.SetDelimiter(delimiter);
353 * if (scan.HasNext())
355 * scan.GetNextToken(out); //returns 1 hundred
364 result GetNextToken(String& nextTok) const;
367 * Gets the next token as String if it matches to the pattern constructed from the specified string.
370 * @return An error code
371 * @param[in] pattern The string to construct the pattern
372 * @param[out] nextTok The next token as an instance of String, @n
373 * else an empty string if not matched
374 * @exception E_SUCCESS The method is successful.
375 * @exception E_DATA_NOT_FOUND The next token does not match to the pattern.
376 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
378 result GetNextToken(const String& pattern, String& nextTok) const;
381 * Gets the next token as String if it matches to the pattern.
384 * @return An error code
385 * @param[in] pattern The pattern to find.
386 * @param[out] nextTok The next token as an instance of String, @n
387 * else an empty string if not matched
388 * @exception E_SUCCESS The method is successful.
389 * @exception E_DATA_NOT_FOUND The next token does not match to the pattern.
390 * @exception E_DATA_NOT_ENOUGH There are no remaining tokens for the current instance of %Scanner.
392 result GetNextToken(const RegularExpression& pattern, String& nextTok) const;
395 * Gets the radix of this %Scanner.
398 * @return The current radix value
400 int GetRadix(void) const;
403 * Gets the delimiter of this %Scanner.
406 * @return The current delimiter string
408 String GetDelimiter(void) const;
411 * Checks whether the current instance of %Scanner has another token.
414 * @return @c true if the current instance has another token, @n
417 bool HasNextToken(void) const;
420 * Checks whether the next token matches to the pattern constructed from the specified string.
423 * @return @c true if the next token matches to the pattern constructed from the specified string, @n
425 * @param[in] pattern The string to construct the pattern
427 bool HasNextToken(const String& pattern) const;
430 * Checks whether the next token matches to the pattern.
433 * @return @c true if the next token matches to the pattern, @n
435 * @param[in] pattern The pattern to find
437 bool HasNextToken(const RegularExpression& pattern) const;
440 * Checks whether the next token can be translated into a valid @c signed @c char value in the default radix.
441 * The @c signed @c char can hold value from -128 to 127.
444 * @return @c true if the next token can be translated into a valid @c signed @c char value in the current radix, @n
447 bool IsNextTokenConvertibleToSignedChar(void) const;
450 * Checks whether the next token can be translated into a valid @c signed @c char value in the specified @c radix.
451 * The @c signed @c char can hold value from -128 to 127.
454 * @return @c true if the next token can be translated into a valid @c signed @c char value in the current radix, @n
456 * @param[in] radix The radix to use to translate the token as a valid @c signed @c char value
458 bool IsNextTokenConvertibleToSignedChar(int radix) const;
461 * Checks whether the next token can be translated into a valid @c int value in the default radix. @n
462 * The signed integer can hold value from -2^31 to 2^31-1.
465 * @return @c true if the next token can be translated into a valid @c int value in the current radix, @n
468 bool IsNextTokenConvertibleToInt(void) const;
471 * Checks whether the next token can be translated into a valid @c int value in the specified @c radix. @n
472 * The signed integer can hold value from -2^31 to 2^31-1.
475 * @return @c true if the next token can be translated into a valid @c int value in the current radix, @n
477 * @param[in] radix The radix to use to translate the token as a valid @c int value
479 bool IsNextTokenConvertibleToInt(int radix) const;
482 * Checks whether the next token can be translated into a valid @c short value in the default radix. @n
483 * The signed @c short can hold value from -2^15 to 2^15-1.
486 * @return @c true if the next token can be translated into a valid @c short value in the current radix, @n
489 bool IsNextTokenConvertibleToShort(void) const;
492 * Checks whether the next token can be translated into a valid @c short value in the specified @c radix. @n
493 * The signed @c short can hold value from -2^15 to 2^15-1.
496 * @return @c true if the next token can be translated into a valid @c short value in the current radix, @n
498 * @param[in] radix The radix to use to translate the token as a valid @c short value
500 bool IsNextTokenConvertibleToShort(int radix) const;
503 * Checks whether the next token can be translated into a valid @c long @c long value. @n
504 * The signed @c long @c long can hold value from -2^63 to 2^63-1.
507 * @return @c true if the next token can be translated into a valid @c long @c long value in the current radix, @n
510 bool IsNextTokenConvertibleToLongLong(void) const;
513 * Checks whether the next token can be translated into a valid @c float value. @n
514 * The signed @c float can hold a single-precision 32-bit floating number.
517 * @return @c true if the next token can be translated into a valid @c float value in the current radix, @n
520 bool IsNextTokenConvertibleToFloat(void) const;
523 * Checks whether the next token can be translated into a valid @c double value. @n
524 * The signed @c double can hold a double-precision 64-bit floating number.
527 * @return @c true if the next token can be translated into a valid @c double value in the current radix, @n
530 bool IsNextTokenConvertibleToDouble(void) const;
533 * Checks whether the next token can be translated into a valid @c bool value. @n
534 * Nothing can be converted except true/TRUE or false/FALSE.
537 * @return @c true if the next token can be translated into a valid @c bool value, @n
540 bool IsNextTokenConvertibleToBool(void) const;
543 * Checks whether the input data of the current instance of %Scanner has another line.
546 * @return @c true if the input data of the current instance of %Scanner has another line, @n
549 bool HasNextLine(void) const;
552 * Sets the radix of the current instance of %Scanner to the specified @c radix.
555 * @param[in] radix The radix to use for conversion
558 void SetRadix(int radix);
561 * Sets the delimiter of the current instance of %Scanner to the pattern constructed from the specified delimiter.
564 * @param[in] patternStr The delimiter to construct the pattern
566 void SetDelimiter(const String& patternStr);
569 * Sets the delimiter of the current instance of %Scanner to the specified @c pattern.
572 * @param[in] pattern The pattern to use as a delimiter
574 void SetDelimiter(const RegularExpression& pattern);
577 * Skips the pattern constructed from the specified string.
580 * @param[in] patternStr The string to construct the pattern
581 * @remarks If no match to the specified pattern is found at the current position, nothing is skipped.
583 * The following example demonstrates how to use the %Skip() method.
587 * const String str("28 1 Ball 2 Ball 3.4 Ball 5 Ball red Ball blue Ball 8 89 67 21474836899.11 NaN 3.4e+5 +23 89 01010 456 123.456");
588 * const String skipStr("28 1 ");
593 * scan.Construct(str);
595 * //Skips "28 1" from input string
596 * scan.Skip(skipStr);
598 * if (scan1.HasNextToken())
600 * scan.GetNextToken(out); //returns Ball
609 void Skip(const String& patternStr);
612 * Skips the specified @c pattern.
615 * @param[in] pattern The pattern to use to skip
616 * @remarks If no match to the specified pattern is found at the current position, nothing is skipped.
618 void Skip(const RegularExpression& pattern);
621 friend class _ScannerImpl;
622 class _ScannerImpl* __pScannerImpl;
625 }}} // Tizen::Base::Utility
627 #endif // _FBASE_UTIL_SCANNER_H_