Merge "Fix a timezone defect." into tizen_2.2
[platform/framework/native/appfw.git] / src / text / FText_EncoderImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file                FText_EncoderImpl.cpp
19  * @brief               This is the implementation file for _EncoderImpl class.
20  */
21
22 #include "FText_EncodingCore.h"
23 #include <FBaseSysLog.h>
24 #include "FText_EncoderImpl.h"
25
26 using namespace Tizen::Base;
27 using namespace Tizen::Base::Collection;
28 using namespace Tizen::Base::Utility;
29
30 namespace Tizen { namespace Text
31 {
32
33 _EncoderImpl::_EncoderImpl(void)
34         : __pEncoder(null)
35         , __encodingType("")
36 {
37 }
38
39 _EncoderImpl::~_EncoderImpl(void)
40 {
41
42 }
43
44 _EncoderImpl*
45 _EncoderImpl::GetEncoderN(const String& encodingType)
46 {
47         SysTryReturn(NID_TEXT, (encodingType == L"UTF-8"), null, E_INVALID_ARG,
48                         "[%s] Invalid argument is used.encdoingType(%ls) is not UTF-8.Failed to open encoder.", GetErrorMessage(E_INVALID_ARG), encodingType.GetPointer());
49
50         std::unique_ptr<_EncoderImpl> pEncoderImpl( new (std::nothrow) _EncoderImpl);
51         SysTryReturn(NID_TEXT, pEncoderImpl, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
52
53         pEncoderImpl->__pEncoder.reset(_EncodingCore::GetEncodingCoreN(encodingType, L"WCHAR_T"));
54
55         result r = GetLastResult();
56         SysTryReturn(NID_TEXT, (pEncoderImpl->__pEncoder != null), null, r,
57                            "[%s] Failed to open encoder.", GetErrorMessage(r));
58
59         pEncoderImpl->__encodingType = encodingType;
60         pEncoderImpl->_pEncoderImpl = pEncoderImpl.get();
61
62         SetLastResult(E_SUCCESS);
63         return pEncoderImpl.release();
64 }
65
66 result
67 _EncoderImpl::Encode(const wchar_t* pSrc, int srcLength, ByteBuffer*& pByteBuffer, int index, bool flush) const
68 {
69         SysAssertf(__pEncoder != null, "Not yet constructed! Construct() should be called before use.");
70
71         SysTryReturnResult(NID_TEXT, (pSrc != null), E_INVALID_ARG, "[%s] Invalid argument is used. pSrc can not be null.", GetErrorMessage(E_INVALID_ARG));
72         SysTryReturnResult(NID_TEXT, (srcLength >= 0), E_INVALID_ARG,
73                                 "[%s] Invalid argument is used. srcLength(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), srcLength);
74         SysTryReturnResult(NID_TEXT, (index >= 0), E_INVALID_ARG,
75                                 "[%s] Invalid argument is used. index(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), index);
76
77         result r = E_SUCCESS;
78         int currPosition = 0;
79         int retLength = 0;
80         bool isMemAllocReq = false;
81
82         std::unique_ptr<byte[]> pDst(__pEncoder->EncodeExN(pSrc, srcLength, retLength, flush));
83         r = GetLastResult();
84         SysTryReturnResult(NID_TEXT, (pDst != null), r, "[%s] Decoding failed", GetErrorMessage(r));
85
86         ByteBuffer* pOutBuffer = pByteBuffer;
87         if (retLength > 0)
88         {
89                 if (pOutBuffer == null)
90                 {
91                         isMemAllocReq = true;
92                         pOutBuffer = new (std::nothrow) ByteBuffer;
93                         SysTryCatch(NID_TEXT, (pOutBuffer != null), ,
94                                            E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Byte Buffer allocation failed");
95
96                         r = pOutBuffer->Construct(retLength);
97                         SysTryCatch(NID_TEXT, !IsFailed(r), , r, "[%s] Unable to Construct Byte buffer", GetErrorMessage(r));
98
99                         pByteBuffer = pOutBuffer;
100                 }
101                 else
102                 {
103                         currPosition = pOutBuffer->GetPosition();
104
105                         r = pOutBuffer->SetPosition(index);
106                         SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to set Byte buffer position", GetErrorMessage(r));
107
108                 }
109
110                 r = pOutBuffer->SetArray(pDst.get(), 0, retLength);
111                 SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to fill Byte buffer", GetErrorMessage(r));
112
113                 r = pOutBuffer->SetPosition(currPosition);
114                 SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to set Byte buffer position", GetErrorMessage(r));
115
116                 return r;
117         }
118
119 CATCH:
120         if (isMemAllocReq)
121         {
122                 delete pByteBuffer;
123                 pByteBuffer = null;
124         }
125         return r;
126 }
127
128 result
129 _EncoderImpl::GetByteCount(const WcharBuffer& chars, int charIndex, int charCount, int& byteCount, bool flush) const
130 {
131         SysAssertf(__pEncoder != null, "Not yet constructed! Construct() should be called before use.");
132
133         byteCount = 0;
134         int inBufSize = chars.GetLimit();
135         result r = CheckBufferInput(inBufSize, charIndex, charCount);
136         SysTryReturn(NID_TEXT, (!IsFailed(r)), r, r, "[%s] Input validation failed", GetErrorMessage(r));
137
138         return __pEncoder->GetByteCount((chars.GetPointer() + charIndex), charCount, byteCount);
139 }
140
141 ByteBuffer*
142 _EncoderImpl::GetBytesN(const WcharBuffer& chars, bool flush) const
143 {
144         SysAssertf(__pEncoder != null, "Not yet constructed! Construct() should be called before use.");
145
146         ByteBuffer* pBuffer = null;
147         int srcLength = 0;
148         result r = GetBufferSize(chars, srcLength);
149         SysTryReturn(NID_TEXT, (!IsFailed(r)), null, r, "[%s] Input validation failed", GetErrorMessage(r));
150
151         r = Encode(chars.GetPointer(), srcLength, pBuffer, 0, flush);
152
153         SetLastResult(r);
154         return pBuffer;
155 }
156
157 ByteBuffer*
158 _EncoderImpl::GetBytesN(const WcharBuffer& chars, int charIndex, int charCount, bool flush) const
159 {
160         SysAssertf(__pEncoder != null, "Not yet constructed! Construct() should be called before use.");
161
162         ByteBuffer* pBuffer = null;
163         int srcLength = chars.GetLimit();
164         result r = CheckBufferInput(srcLength, charIndex, charCount);
165         SysTryReturn(NID_TEXT, (!IsFailed(r)), null, r, "[%s] Input validation failed", GetErrorMessage(r));
166
167         r = Encode(chars.GetPointer() + charIndex, charCount, pBuffer, 0, flush);
168
169         SetLastResult(r);
170         return pBuffer;
171 }
172
173 result
174 _EncoderImpl::GetBufferSize(const WcharBuffer& chars, int& charBufSize) const
175 {
176         // It is assumed that user has set limit properly
177         charBufSize = chars.GetLimit();
178
179         // Adjusting byteBufSize if bytes has null char at the end
180         wchar_t lastChar = -1;
181         result r = chars.Get(charBufSize - 1, lastChar);
182         SysTryReturnResult(NID_TEXT, (!IsFailed(r)), E_INVALID_ARG, "[%s] Last byte check failed", GetErrorMessage(r));
183
184         if ((lastChar == '\0')and(charBufSize > 1))
185         {
186                 charBufSize -= 1;
187         }
188         SysTryReturnResult(NID_TEXT, (charBufSize > 0), E_INVALID_ARG,
189                         "[%s] Invalid argument is used. charBufSize [%d] is invalid.", GetErrorMessage(E_INVALID_ARG), charBufSize);
190
191         return E_SUCCESS;
192 }
193
194 result
195 _EncoderImpl::CheckBufferInput(int inBufSize, int inIndex, int inCount) const
196 {
197         SysTryReturnResult(NID_TEXT, (inIndex >= 0), E_INVALID_ARG,
198                                 "[%s] Invalid argument is used. inIndex(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), inIndex);
199         SysTryReturnResult(NID_TEXT, (inCount >= 0), E_INVALID_ARG,
200                                 "[%s] Invalid argument is used. inCount(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), inCount);
201         SysTryReturnResult(NID_TEXT, (inBufSize > 0), E_INVALID_ARG,
202                                 "[%s] Invalid argument is used. inBufSize(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), inBufSize);
203
204         SysTryReturnResult(NID_TEXT, (inBufSize > inIndex), E_OUT_OF_RANGE,
205                                 "[%s] inIndex(%d) is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE), inIndex);
206         SysTryReturnResult(NID_TEXT, (inBufSize >= inCount), E_OUT_OF_RANGE,
207                                 "[%s] inCount(%d) is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE), inCount);
208         SysTryReturnResult(NID_TEXT, (inBufSize >= (inIndex + inCount)), E_UNDERFLOW,
209                         "[%s] sum of the length of the inIndex(%d) and inCount(%d) is greater than inBufSize(%d).",
210                         GetErrorMessage(E_UNDERFLOW), inIndex, inCount, inBufSize);
211
212         return E_SUCCESS;
213 }
214
215 } } // Tizen::Text