sync with tizen_2.0
[platform/framework/native/appfw.git] / inc / FBaseUtilStringTokenizer.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FBaseUtilStringTokenizer.h
20  * @brief               This is the header file for the %StringTokenizer class.
21  *
22  * @see                 String()
23  *
24  * This header file contains the declarations of the %StringTokenizer class.
25  */
26
27 #ifndef _FBASE_UTIL_STRING_TOKENIZER_H_
28 #define _FBASE_UTIL_STRING_TOKENIZER_H_
29
30 #include <FBaseObject.h>
31 #include <FBaseString.h>
32
33
34 namespace Tizen { namespace Base { namespace Utility
35 {
36 /**
37  * @class       StringTokenizer
38  * @brief       This class is used to break a String into tokens.
39  *
40  * @since 2.0
41  *
42  * The %StringTokenizer class is used to break a String into tokens. It also provides ways to count the number of tokens.
43  *
44  * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/string_tokenizer.htm">String Tokenizer</a>.
45  *
46  * The following example demonstrates how to use the %StringTokenizer class.
47  *
48  * @code
49  *      #include <FBase.h>
50  *
51  *      using namespace Tizen::Base;
52  *      using namespace Tizen::Base::Utility;
53  *
54  *      void
55  *      MyClass::StringTokenizerSample(void)
56  *      {
57  *              String str(L"Tizen-StringTokenizer/Sample code");
58  *              String delim(L" /-");
59  *
60  *              // Creates a StringTokenizer instance
61  *              StringTokenizer strTok(str, delim);
62  *
63  *              int count = strTok.GetTokenCount();             // count == 4
64  *
65  *              String token;
66  *              while (strTok.HasMoreTokens())
67  *              {
68  *                      strTok.GetNextToken(token);                     // Tizen, StringTokenizer, Sample, code
69  *              }
70  *      }
71  * @endcode
72  *
73  *
74  */
75
76 class _OSP_EXPORT_ StringTokenizer
77         : public Object
78 {
79 public:
80         /**
81          * Initializes an instance of %StringTokenizer with the specified parameters.
82          *
83          * @since 2.0
84          *
85          * @param[in]   value           An instance of String to parse
86          * @param[in]   delimiters      The delimiters
87          * @param[in]   isToken         A flag indicating whether to return the delimiters as tokens @n
88          *                                                      Set to @c true to treat the delimiters as tokens, @n
89          *                                                      else @c false to skip the delimiters.
90          *
91          */
92         StringTokenizer(const String& value, const String& delimiters = L" \t\n\r\f", bool isToken = false);
93
94         /**
95          * This destructor overrides Tizen::Base::Object::~Object().
96          *
97          * @since 2.0
98          *
99          * @remarks             The internally allocated memory block is freed when the instance is destroyed.
100          */
101         virtual ~StringTokenizer(void);
102
103         /**
104          * Gets the token count indicating the number of times the GetNextToken() method can be called before it returns an error code.
105          *
106          * @since 2.0
107          *
108          * @return              An integer value indicating the number of times the GetNextToken() method can be called
109          */
110         int GetTokenCount(void);
111
112         /**
113          * Checks whether there are any more tokens in the string after the current position.
114          *
115          * @since 2.0
116          *
117          * @return              @c true if there is at least one more token in the string after the current position, @n
118          *                              else @c false
119          */
120         bool HasMoreTokens(void);
121
122         /**
123          * Gets the next token from the string tokenizer.
124          *
125          * @since 2.0
126          *
127          * @return              An error code
128          * @param[out]  token   The next token
129          * @exception   E_SUCCESS                               The method is successful.
130          * @exception   E_OUT_OF_RANGE                  The string tokenizer has no more tokens.
131          */
132         result GetNextToken(String& token);
133
134         /**
135          * Sets the new delimiters.
136          *
137          * @since 2.0
138          *
139          * @return              An error code
140          * @param[in]   delimiters      The new delimiters
141          * @exception   E_SUCCESS                               The method is successful.
142          * @exception   E_INVALID_ARG                   The specified input parameter is invalid.
143          *
144          * The following example demonstrates how to use the %SetDelimiters() method.
145          *
146          * @code
147          *
148          *      String str(L"Tizen-Framework-Utility::StringTokenizer/class");
149          *      String delim(L":-");
150          *
151          *      String token;
152          *
153          *      StringTokenizer strTok(str, delim);
154          *
155          *      for (int i = 0; i < 3; i++)
156          *      {
157          *              strTok.GetNextToken(token);                     // Tizen, Framework, Utility
158          *      }
159          *
160          *      String newDelim(L"/");
161          *
162          *      strTok.SetDelimiters(newDelim);
163          *      strTok.GetNextToken(token);                             // token == L"::StringTokenizer"
164          *
165          * @endcode
166          */
167         result SetDelimiters(const String& delimiters);
168
169 private:
170         //
171         // Sets the maximum characters of delimiters.
172         //
173         // @return      An @c wchar_t value indicating the maximum characters
174         //
175         wchar_t SetMaxDelimChar(void);
176
177         //
178         // Skips the delimiters starting from the specified position.
179         //
180         // @return      If the delimiter is not regarded as a token, the method returns the index of the first non-delimiter character. @n
181         //                      If the delimiter is regarded as a token, the method returns the starting position.
182         // @param[in]   position        The position from where the delimiters need to skip
183         //
184         int SkipDelimiters(int position);
185
186         //
187         // Scans the tokens starting from the specified position.
188         //
189         // @return              The index of the next delimiter after the specified position
190         // @param[in] position  The position to start scan
191         //
192         int ScanToken(int position);
193
194
195 private:
196         String __string;
197         String __delimiters;
198         int __curPosition;
199         int __newPosition;
200         int __maxStrLen;
201         wchar_t __maxDelimChar;
202         bool __isToken;
203         bool __isDelimChanged;
204
205         friend class _StringTokenizerImpl;
206         class _StringTokenizerImpl* __pStringTokenizerImpl;
207
208 }; // StringTokenizer
209
210 }}} // Tizen::Base::Utility
211
212 #endif //_FBASE_UTIL_STRING_TOKENIZER_H_