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.
18 * @file FTextUtf8Encoder.h
19 * @brief This is the header file for the %Utf8Encoder class.
21 * This header file contains the declarations of the %Utf8Encoder class.
24 #ifndef _FTEXT_UTF8_ENCODER_H_
25 #define _FTEXT_UTF8_ENCODER_H_
27 #include <FTextEncoder.h>
28 #include <FBaseByteBuffer.h>
31 namespace Tizen { namespace Text
36 * @brief This class is an implementation of the UTF-8 encoder.
40 * @final This class is not intended for extension.
42 * The %Utf8Encoder class converts the blocks of characters into encoded blocks of bytes.
43 * Universal Transformation Format-8 (UTF-8) is a translated language that is used on the internet. It converts the
44 * unicode into 8-bit bytes. @n UTF-8 encoding supports all unicode character values and surrogates.
45 * Note that %Utf8Encoder is not used in reality, because keeping the internal state of encoding from unicode
46 * to UTF-8 can be avoided if the translation unit is even.
47 * This means that the real implementation of %Utf8Encoder is the same as that of Utf8Encoding in this state.
49 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/text/converting_text_data_separate_blocks.htm">Converting Text Data in Separate Blocks</a>.
51 * The following example demonstrates how to use the %Utf8Encoder class.
57 * using namespace Tizen::Base;
58 * using namespace Tizen::Text;
61 * MyClass::Utf8EncoderSample(void)
65 * // Fills a WcharBuffer with the unicode strings to encode it into UTF8 format bytes.
66 * WcharBuffer* pMB = PrepareUtf8EncoderSample();
68 * int charBufSize = pMB->GetCapacity();
71 * utf8En.GetByteCount(*pMB, 0, charBufSize, byteCount);
74 * enBytes.Construct(byteCount);
77 * int charCount = 100;
79 * while (charIndex < charBufSize)
81 * ByteBuffer* pBB = null;
83 * if (charBufSize - charIndex < charCount)
85 * charCount = charBufSize - charIndex;
88 * // Converts Unicode to Utf8 using Utf8Encoder.
89 * pBB = utf8En.GetBytesN(*pMB, charIndex, charCount); // flush is false
91 * enBytes.CopyFrom(*pBB);
92 * charIndex += charCount;
97 * enBytes.SetByte('\0');
106 class _OSP_EXPORT_ Utf8Encoder
111 * This is the default constructor for this class.
119 * This is the destructor for this class. @n
120 * This destructor overrides Tizen::Text::Encoder::~Encoder().
124 virtual ~Utf8Encoder(void);
127 * Gets the total number of bytes that are required to encode a range of characters in the specified Tizen::Base::WcharBuffer instance.
131 * @return An error code
132 * @param[in] chars An instance of Tizen::Base::WcharBuffer to encode
133 * @param[in] charIndex The index from where encoding begins in the Tizen::Base::WcharBuffer instance
134 * @param[in] charCount The total number of characters to encode
135 * @param[in] flush Set to @c true to allow this instance to flush its state at the end of the conversion, @n
137 * @param[out] byteCount The total number of bytes required to encode the specified range of characters
138 * @exception E_SUCCESS The method is successful.
139 * @exception E_INVALID_ARG A specified input parameter is invalid, or
140 * the specified @c chars is empty.
141 * @exception E_OUT_OF_RANGE The value of an argument is outside the valid range defined by the method, or
142 * the length of the specified @c charIndex or @c charCount is greater than the length of the specified @c chars.
143 * @exception E_UNDERFLOW This operation has caused the memory to underflow, or
144 * the sum of the length of the specified @c charIndex and @c charCount is greater than the length of the specified @c chars.
145 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
146 * @see Utf8Decoder::GetCharCount()
148 virtual result GetByteCount(const Tizen::Base::WcharBuffer& chars,
149 int charIndex, int charCount, int& byteCount, bool flush = false) const;
152 * Encodes an instance of Tizen::Base::WcharBuffer into an instance of Tizen::Base::ByteBuffer.
156 * @return A pointer to the Tizen::Base::ByteBuffer instance where the resultant encoded string is stored, @n
157 * else @c null if an exception occurs @n
158 * The buffer limit is the position of the last encoded byte plus one and the starting position is zero.
159 * @param[in] chars An instance of Tizen::Base::WcharBuffer to encode
160 * @param[in] flush Set to @c true to allow this instance to flush its state at the end of the conversion, @n
162 * @exception E_SUCCESS The method is successful.
163 * @exception E_OUT_OF_MEMORY The memory is insufficient.
164 * @exception E_INVALID_ARG A specified input parameter is invalid, or
165 * the specified @c chars is empty.
166 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
167 * @remarks The specific error code can be accessed using the GetLastResult() method.
168 * @see Utf8Decoder::GetCharsN()
170 virtual Tizen::Base::ByteBuffer* GetBytesN(const Tizen::Base::WcharBuffer& chars, bool flush = false) const;
173 * Encodes an instance of Tizen::Base::WcharBuffer into an instance of Tizen::Base::ByteBuffer as per the specified range.
177 * @return A pointer to the Tizen::Base::ByteBuffer instance where the resultant encoded string is stored, @n
178 * else @c null if an exception occurs @n
179 * The buffer limit is the position of the last encoded byte and the starting position is zero.
180 * @param[in] chars An instance of Tizen::Base::WcharBuffer to encode
181 * @param[in] charIndex The index from where encoding begins in the Tizen::Base::WcharBuffer instance
182 * @param[in] charCount The total number of characters to encode
183 * @param[in] flush Set to @c true to allow this instance to flush its state at the end of the conversion, @n
185 * @exception E_SUCCESS The method is successful.
186 * @exception E_OUT_OF_MEMORY The memory is insufficient.
187 * @exception E_INVALID_ARG A specified input parameter is invalid, or
188 * the specified @c chars is empty.
189 * @exception E_OUT_OF_RANGE The value of an argument is outside the valid range defined by the method, or
190 * the length of the specified @c charIndex or @c charCount is greater than the length of the specified @c chars.
191 * @exception E_UNDERFLOW This operation has caused the memory to underflow, or
192 * the sum of the length of the specified @c charIndex and @c charCount is greater than the length of the specified @c chars.
193 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds of the character encoding scheme.
194 * @remarks The specific error code can be accessed using the GetLastResult() method.
195 * @remarks The pointer to the Tizen::Base::ByteBuffer instance is not terminated by a @c null character.
196 * @see Utf8Decoder::GetCharsN()
198 virtual Tizen::Base::ByteBuffer* GetBytesN(const Tizen::Base::WcharBuffer& chars, int charIndex, int charCount,
199 bool flush = false) const;
203 * The implementation of this copy constructor is intentionally blank and declared as private to
204 * prohibit copying of objects.
206 Utf8Encoder(const Utf8Encoder& utf8Encoder);
209 * The implementation of this copy assignment operator is intentionally blank and declared as private
210 * to prohibit copying of objects.
212 Utf8Encoder& operator =(const Utf8Encoder& utf8Encoder);
214 friend class _Utf8EncoderImpl;
215 class _Utf8EncoderImpl* __pUtf8EncoderImpl;
219 #endif //_FTEXT_UTF8_ENCODER_H_