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 FText_EncodingImpl.h
19 * @brief This is the header file for the _EncodingImpl class.
22 #ifndef _FTEXT_INTERNAL_ENCODING_IMPL_H_
23 #define _FTEXT_INTERNAL_ENCODING_IMPL_H_
26 #include <unique_ptr.h>
27 #include <FTextEncoding.h>
31 namespace Tizen { namespace Text
42 * This is the constructor for this class.
48 * This is the destructor for this class.
50 virtual ~_EncodingImpl(void);
54 * Constructs the instance of this class.
56 result Construct(const Tizen::Base::String& encoding);
62 * Calculates the number of bytes required to encode the specified string.
65 * @return An error code
66 * @param[in] str The string to encode
67 * @param[out] byteCount The number of bytes required to encode @c str
69 * @exception E_SUCCESS The method was successful.
70 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
71 * The specified @c str is an empty string.
72 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds @n
73 * specified by the character encoding scheme.
75 * @remarks This method can be used to determine the exact number of bytes
76 * that are produced if the specified string is encoded.
78 * @see GetMaxByteCount()
80 virtual result GetByteCount(const Tizen::Base::String& str, int& byteCount) const;
83 * Calculates the number of bytes required to encode the specified characters.
86 * @return An error code
87 * @param[in] chars The buffer containing the character array to encode
88 * @param[out] byteCount The number of bytes required to encode the specified range of characters
89 * @exception E_SUCCESS The method was successful.
90 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
91 * The specified @c chars is empty.
92 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
93 * @remarks This method can be used to determine the exact number of bytes
94 * that are produced if the specified array of characters is encoded.
96 * @see GetMaxByteCount(), GetByteCount()
98 virtual result GetByteCount(const Tizen::Base::WcharBuffer& chars, int& byteCount) const;
101 * Calculates the number of bytes required to encode a range of characters in the specified WcharBuffer.
104 * @return An error code
105 * @param[in] chars The buffer containing the character array to encode
106 * @param[in] charIndex The starting index of the WcharBuffer to encode
107 * @param[in] charCount The number of characters to encode
108 * @param[out] byteCount The number of bytes required to encode the specified range of characters
109 * @exception E_SUCCESS The method was successful.
110 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
111 * The specified @c chars is empty.
112 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
113 * The length of the specified @c charIndex or @c charCount is greater than the length of the specified @c chars.
114 * @exception E_UNDERFLOW This operation has caused an underflow. @n
115 * The sum of the length of the specified @c charIndex and @c charCount is greater than the length of the specified @c chars.
116 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
117 * @remarks This method can be used to determine the exact number of bytes
118 * that are produced if the specified array of characters is encoded.
120 * @see GetMaxByteCount(), GetByteCount()
122 virtual result GetByteCount(const Tizen::Base::WcharBuffer& chars, int charIndex, int charCount, int& byteCount) const;
125 * Encodes the specified WcharBuffer into a ByteBuffer.
128 * @return A new ByteBuffer where the resulting encoded string is stored @n
129 * @c null, if an exception occurs @n
130 * The buffer's limit is the position of the last encoded byte plus one in the buffer, and the position @c 0
131 * @param[in] chars The WcharBuffer to encode
132 * @exception E_SUCCESS The method was successful.
133 * @exception E_OUT_OF_MEMORY Insufficient memory.
134 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
135 * The specified @c chars is empty.
136 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
137 * @remarks The specific error code can be accessed using the GetLastResult() method.
139 * @see GetCharsN(), GetBytesN()
141 virtual Tizen::Base::ByteBuffer* GetBytesN(const Tizen::Base::WcharBuffer& chars) const;
144 * Encodes the specified string into a ByteBuffer.
147 * @return A new ByteBuffer where the resulting encoded string is stored @n
148 * @c null, if an exception occurs @n
149 * The buffer's limit is the position of the last encoded byte plus one in the buffer, and the position @c 0
150 * @param[in] str The string to encode
151 * @exception E_SUCCESS The method was successful.
152 * @exception E_OUT_OF_MEMORY Insufficient memory.
153 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
154 * The specified @c str is an empty string.
155 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
156 * @remarks The specific error code can be accessed using the GetLastResult() method.
160 virtual Tizen::Base::ByteBuffer* GetBytesN(const Tizen::Base::String& str) const;
163 * Encodes a range of characters from the specified WcharBuffer into a ByteBuffer.
164 * The ByteBuffer's position and limit are not changed.
167 * @return An error code
168 * @param[in] chars The buffer containing the character array to encode
169 * @param[in] charIndex The starting index of the WcharBuffer to encode
170 * @param[in] charCount The number of characters to encode
171 * @param[out] bytes The ByteBuffer where the resulting encoded string is stored
172 * @param[in] byteIndex The starting index of the resulting encoding in the ByteBuffer
173 * @exception E_SUCCESS The method was successful.
174 * @exception E_OUT_OF_MEMORY Insufficient memory.
175 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
176 * The specified @c chars or @c bytes is empty.
177 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
178 * The length of the specified @c charIndex or @c charCount is greater than the length of the specified @c chars.
179 * @exception E_UNDERFLOW This operation has caused an underflow. @n
180 * The sum of the length of the specified @c charIndex and @c charCount is greater than the length of the specified @c chars.
181 * @exception E_OVERFLOW This operation has caused an overflow. @n
182 * The specified @c bytes do not contain sufficient space to store the encoded characters.
183 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
184 * @remarks This method encodes a range of characters in a WcharBuffer into a range of bytes in a ByteBuffer.
188 virtual result GetBytes(const Tizen::Base::WcharBuffer& chars, int charIndex, int charCount, Tizen::Base::ByteBuffer& bytes, int byteIndex) const;
191 * Encodes the specified range of a string into the specified range of a ByteBuffer.
192 * The ByteBuffer's position and limit are not changed.
195 * @return An error code
196 * @param[in] str The string to encode
197 * @param[in] charIndex The starting index of the WcharBuffer to encode
198 * @param[in] charCount The number of characters to encode
199 * @param[out] bytes The ByteBuffer where the resulting encoded string is stored
200 * @param[in] byteIndex The starting index of the resulting encoding in the ByteBuffer
201 * @exception E_SUCCESS The method was successful.
202 * @exception E_OUT_OF_MEMORY Insufficient memory.
203 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
204 * The specified @c str or @c bytes is empty.
205 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
206 * The length of the specified @c charIndex or @c charCount is greater than the length of the specified @c str.
207 * @exception E_UNDERFLOW This operation has caused an underflow. @n
208 * The sum of the length of the specified @c charIndex and @c charCount is greater than the length of the specified @c str.
209 * @exception E_OVERFLOW This operation has caused an overflow. @n
210 * The specified @c bytes does not contain sufficient space to store the encoded characters.
211 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
215 virtual result GetBytes(const Tizen::Base::String& str, int charIndex, int charCount, Tizen::Base::ByteBuffer& bytes, int byteIndex) const;
218 * Calculates the number of characters produced by decoding a ByteBuffer.
221 * @return An error code
222 * @param[in] bytes The ByteBuffer to decode
223 * @param[out] charCount The number of characters produced by decoding a range of bytes in the specified ByteBuffer
224 * @exception E_SUCCESS The method was successful.
225 * @exception E_OUT_OF_MEMORY Insufficient memory.
226 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
227 * The specified @c bytes is empty.
228 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
229 * @remarks This method can be used to determine the exact number of characters
230 * that are produced if the specified range of bytes is encoded.
232 * @see GetMaxCharCount(), GetCharCount()
234 virtual result GetCharCount(const Tizen::Base::ByteBuffer& bytes, int& charCount) const;
237 * Calculates the number of characters produced by decoding a specified range of elements in a ByteBuffer.
240 * @return An error code
241 * @param[in] bytes The ByteBuffer to decode
242 * @param[in] byteIndex The starting index where decoding begins
243 * @param[in] byteCount The number of bytes to decode
244 * @param[out] charCount The number of characters produced by decoding a range of bytes in the specified ByteBuffer
245 * @exception E_SUCCESS The method was successful.
246 * @exception E_OUT_OF_MEMORY Insufficient memory.
247 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
248 * The specified @c bytes is empty.
249 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
250 * The length of the specified @c byteIndex or @c byteCount is greater than the length of the specified @c bytes.
251 * @exception E_UNDERFLOW This operation has caused an underflow. @n
252 * The sum of the length of the specified @c byteIndex and @c byteCount is greater than the length of the specified @c bytes.
253 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
254 * @remarks This method can be used to determine the exact number of characters
255 * that are produced if the specified range of bytes is encoded.
257 * @see GetMaxCharCount(), GetCharCount()
259 virtual result GetCharCount(const Tizen::Base::ByteBuffer& bytes, int byteIndex, int byteCount, int& charCount) const;
262 * Decodes a ByteBuffer into a WcharBuffer.
265 * @return A new WcharBuffer where the resulting decoded data is stored @n
266 * @c null, if an exception occurs @n
267 * The buffer's limit is the position of the last decoded byte plus one in the buffer, and the position is @c 0
268 * @param[in] bytes The buffer containing ByteBuffer to decode
269 * @exception E_SUCCESS The method was successful.
270 * @exception E_OUT_OF_MEMORY Insufficient memory.
271 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
272 * The specified @c bytes is empty.
273 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
274 * @remarks The specific error code can be accessed using the GetLastResult() method.
276 * @see GetBytesN(), GetCharsN()
278 virtual Tizen::Base::WcharBuffer* GetCharsN(const Tizen::Base::ByteBuffer& bytes) const;
281 * Decodes a range of bytes in ByteBuffer into a range of characters in WcharBuffer.
282 * The WcharBuffer's position and limit are not changed.
285 * @return An error code
286 * @param[in] bytes The buffer containing the ByteBuffer to decode
287 * @param[in] byteIndex The starting index where decoding begins
288 * @param[in] byteCount The number of bytes to decode
289 * @param[out] chars The buffer containing WcharBuffer where the resulting decoded data is stored
290 * @param[in] charIndex The starting index of the resulting decoding in the WcharBuffer
291 * @exception E_SUCCESS The method was successful.
292 * @exception E_OUT_OF_MEMORY Insufficient memory.
293 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
294 * The specified @c bytes is empty.
295 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
296 * The length of the specified @c byteIndex or @c byteCount is greater than the length of the specified @c bytes.
297 * @exception E_UNDERFLOW This operation has caused an underflow. @n
298 * The sum of the length of the specified @c byteIndex and @c byteCount is greater than the length of the specified @c bytes.
299 * @exception E_OVERFLOW This operation has caused an overflow. @n
300 * The specified @c chars does not contain sufficient space to store the decoded bytes.
301 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
303 * @see GetBytes(), GetChars()
305 virtual result GetChars(const Tizen::Base::ByteBuffer& bytes, int byteIndex, int byteCount, Tizen::Base::WcharBuffer& chars, int charIndex) const;
308 * Gets a string containing the decoded representation of a range of bytes in the specified ByteBuffer.
311 * @return An error code
312 * @param[in] bytes The ByteBuffer to decode
313 * @param[out] str A String containing the decoded representation of a range of bytes in the specified ByteBuffer
314 * @exception E_SUCCESS The method was successful.
315 * @exception E_OUT_OF_MEMORY Insufficient memory.
316 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
317 * The specified @c bytes is empty.
318 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
320 * @see GetBytesN(), GetString()
322 virtual result GetString(const Tizen::Base::ByteBuffer& bytes, Tizen::Base::String& str) const;
325 * Gets a string containing the decoded representation of a range of bytes in the specified ByteBuffer.
328 * @return An error code
329 * @param[in] bytes The buffer containing the ByteBuffer to decode
330 * @param[in] index The starting index where decoding begins
331 * @param[in] count The number of bytes to decode
332 * @param[out] str A String containing the decoded representation of a range of bytes in the specified ByteBuffer
333 * @exception E_SUCCESS The method was successful.
334 * @exception E_OUT_OF_MEMORY Insufficient memory.
335 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
336 * The specified @c bytes is empty.
337 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
338 * The sum of the length of the specified @c index and @c count is greater than the length of the specified @c bytes.
339 * @exception E_UNDERFLOW This operation has caused an underflow. @n
340 * The sum of the length of the specified @c index and @c count is greater than the length of the specified @c bytes.
341 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
343 * @see GetBytes(), GetString()
345 virtual result GetString(const Tizen::Base::ByteBuffer& bytes, int index, int count, Tizen::Base::String& str) const;
348 * Gets the maximum number of bytes required for encoding the specified number of characters.
351 * @return The maximum number of bytes required for encoding a given number of characters
352 * @param[in] charCount The number of characters to encode
353 * @remarks This method can be used to determine an appropriate buffer size for the byte arrays passed to the GetBytes() method for encoding.
355 * @see GetByteCount(), GetBytes()
357 virtual int GetMaxByteCount(int charCount) const;
360 * Gets the maximum number of characters produced by decoding the specified number of bytes.
363 * @return The maximum number of characters produced by decoding the specified number of bytes
364 * @param[in] byteCount The number of bytes to encode
365 * @remarks This method can be used to determine an appropriate buffer size for character arrays passed to
366 * GetChars() or a decoder for encoding.
368 * @see GetCharCount(), GetChars()
370 virtual int GetMaxCharCount(int byteCount) const;
373 * Gets the encoder for the current encoding.
376 * @return A pointer to Encoder for the current encoding
377 * @remarks Contrary to GetBytes(), an Encoder can convert partial sequences of characters into
378 * partial sequences of bytes by maintaining an appropriate state between the conversions. @n
379 * Currently, only Utf8Encoding supports this method. Other classes return @c null.
383 virtual Encoder* GetEncoderN(void) const;
386 * Gets the decoder for the current encoding.
389 * @return A decoder for the current encoding
390 * @remarks Contrary to the GetChars() methods, a Decoder can convert partial sequences of bytes
391 * into partial sequences of characters by maintaining an appropriate state between the conversions. @n
392 * Currently, only Utf8Encoding supports this method. Other classes return @c null.
396 virtual Decoder* GetDecoderN(void) const;
399 * Gets the encoding type of the current instance.
402 * @return An encoding type
404 virtual Tizen::Base::String GetEncodingType(void) const;
407 * Gets a list of all the available encodings.
410 * @return A list of String instances (ASCII, UTF-8, ISO-8859-1, ISO-8859-2, Windows-1254, ...) @n
411 * @c null, if the method fails
412 * @exception E_SUCCESS The method was successful.
413 * @exception E_SYSTEM A system error occurred.
414 * @remarks The specific error code can be accessed using the GetLastResult() method.
416 static Tizen::Base::Collection::IList* GetAvailableEncodingsImplN(void);
419 * Converts a range of bytes in a ByteBuffer from one encoding to another.
422 * @return A new buffer for storing result of the conversion @n
423 * @c null, if an exception occurs @n
424 * The buffer's limit is the position of the last converted byte plus one, and the position @c 0
425 * @param[in] src The source of the encoding
426 * @param[in] dst The destination of the encoding
427 * @param[in] srcBytes The ByteBuffer where the resulting encoded string is stored
428 * @param[in] index The starting index of the resulting encoding in the ByteBuffer
429 * @param[in] count The number of bytes to convert
430 * @exception E_SUCCESS The method was successful.
431 * @exception E_OUT_OF_MEMORY Insufficient memory.
432 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
433 * The specified @c srcBytes is empty.
434 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
435 * The @c index or @c count is greater than the length of @c srcBytes.
436 * @exception E_UNDERFLOW This operation has caused an underflow. @n
437 * The sum of the length of the specified @c index and @c count is greater than the length of the specified @c srcBytes.
438 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
439 * @remarks The specific error code can be accessed using the GetLastResult() method.
441 * @see GetBytes(), GetChars()
443 static Tizen::Base::ByteBuffer* ConvertImplN(const Encoding& src, const Encoding& dst,
444 const Tizen::Base::ByteBuffer& srcBytes, int index, int count);
447 * Converts a ByteBuffer from one encoding to another.
450 * @return A new buffer for storing the result of the conversion @n
451 * @c null, if an exception occurs @n
452 * The buffer's limit is the position of the last converted byte plus one, and the position @c 0
453 * @param[in] src The source of the encoding
454 * @param[in] dst The destination of the encoding
455 * @param[in] srcBytes The ByteBuffer where the resulting encoded string is stored
456 * @exception E_SUCCESS The method was successful.
457 * @exception E_OUT_OF_MEMORY Insufficient memory.
458 * @exception E_INVALID_ARG A specified input parameter is invalid. @n
459 * The specified @c srcBytes is empty.
460 * @exception E_INVALID_ENCODING_RANGE The specified string contains code points that are outside the bounds specified by the character encoding scheme.
461 * @remarks The specific error code can be accessed using the GetLastResult() method.
463 * @see GetBytes(), GetChars()
465 static Tizen::Base::ByteBuffer* ConvertImplN(const Encoding& src, const Encoding& dst,
466 const Tizen::Base::ByteBuffer& srcBytes);
470 result Encode(const wchar_t* pSrc, int srcLength, Tizen::Base::ByteBuffer*& pByteBuffer, int index) const;
471 result Decode(const byte* pSrc, int srcLength, Tizen::Base::WcharBuffer*& pWcharBuffer, int index) const;
472 result Decode(const byte* pSrc, int srcLength, Tizen::Base::String& outStr, int index) const;
474 result GetBufferSize(const Tizen::Base::WcharBuffer& chars, int& charBufSize) const;
475 result GetBufferSize(const Tizen::Base::ByteBuffer& bytes, int& byteBufSize) const;
476 result CheckBufferInput(int inBufSize, int inIndex, int inCount) const;
477 result CheckBufferInput(int inBufSize, int inIndex, int inCount, int outBufSize, int outIndex) const;
479 bool IsEncodingSupported(const Tizen::Base::String& encodingStr) const;
481 _EncodingImpl(const _EncodingImpl& encodingImpl);
482 _EncodingImpl& operator =(const _EncodingImpl& encodingImpl);
485 Tizen::Base::String __encodingType;
486 std::unique_ptr<_EncodingCore> __pEncodingCore;
488 friend class Encoding;
494 #endif //_FTEXT_INTERNAL_ENCODING_IMPL_H_