Merge "Fix DataSet for exception case" into tizen_2.1
[platform/framework/native/appfw.git] / src / text / FText_EncodingCore.cpp
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                FText_EncodingCore.cpp
20  * @brief               This is the implementation file for _EncodingCore class.
21  */
22
23 // Includes
24 #include <unique_ptr.h>
25 #include "FText_EncodingCore.h"
26 #include <FBaseSysLog.h>
27 #include <FBaseColLinkedList.h>
28 #include "FText_IcuEncodingCore.h"
29 #include "FText_GsmEncodingCore.h"
30 #include "FText_Ucs2EncodingCore.h"
31 #include "FText_Iso885916EncodingCore.h"
32
33 using namespace std;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36
37 namespace Tizen { namespace Text
38 {
39
40 _EncodingCore::_EncodingCore(void)
41         : _encodingTo()
42         , _encodingFrom()
43 {
44 }
45
46 _EncodingCore::~_EncodingCore(void)
47 {
48 }
49
50
51 _EncodingCore*
52 _EncodingCore::GetEncodingCoreN(const Tizen::Base::String& encodingFrom, const Tizen::Base::String& encodingTo)
53 {
54         _EncodingCore* pEncoderDecoder = null;
55
56         pEncoderDecoder = _Ucs2EncodingCore::GetEncodingCoreImplN(encodingFrom, encodingTo);
57         if (pEncoderDecoder)
58         {
59                 return pEncoderDecoder;
60         }
61
62         pEncoderDecoder = _IcuEncodingCore::GetEncodingCoreImplN(encodingFrom, encodingTo);
63         if (pEncoderDecoder)
64         {
65                 return pEncoderDecoder;
66         }
67
68         pEncoderDecoder = _GsmEncodingCore::GetEncodingCoreImplN(encodingFrom, encodingTo);
69         if (pEncoderDecoder)
70         {
71                 return pEncoderDecoder;
72         }
73
74         pEncoderDecoder = _Iso885916EncodingCore::GetEncodingCoreImplN(encodingFrom, encodingTo);
75         if (pEncoderDecoder)
76         {
77                 return pEncoderDecoder;
78         }
79
80         return null;
81 }
82
83 IList*
84 _EncodingCore::GetAvailableEncodingsN(void)
85 {
86         ClearLastResult();
87         result r = E_SUCCESS;
88
89         unique_ptr<LinkedList, AllElementsDeleter> pList(new (std::nothrow) LinkedList());
90         SysTryReturn(NID_TEXT, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
91
92         _IcuEncodingCore icuConverter;
93         _GsmEncodingCore gsmConverter;
94         _Ucs2EncodingCore ucs2Converter;
95         _Iso885916EncodingCore iso885916Converter;
96
97         r = icuConverter.GetAvailableEncodingsN(pList.get());
98         SysTryReturn(NID_TEXT, !IsFailed(r), null, r, "[%s] Available encoding list is empty. List allocation failed.", GetErrorMessage(r));
99
100         r = gsmConverter.GetAvailableEncodingsN(pList.get());
101         SysTryReturn(NID_TEXT, !IsFailed(r), null, r, "[%s] Available encoding list is empty. List allocation failed.", GetErrorMessage(r));
102
103         r = ucs2Converter.GetAvailableEncodingsN(pList.get());
104         SysTryReturn(NID_TEXT, !IsFailed(r), null, r, "[%s] Available encoding list is empty. List allocation failed.", GetErrorMessage(r));
105
106         r = iso885916Converter.GetAvailableEncodingsN(pList.get());
107         SysTryReturn(NID_TEXT, !IsFailed(r), null, r, "[%s] Available encoding list is empty. List allocation failed.", GetErrorMessage(r));
108
109         return pList.release();
110 }
111
112 byte*
113 _EncodingCore::ConvertN(_EncodingCore* pDstConverter, const byte* pSrc, int srcLength, int& retLength)
114 {
115         result r = E_SUCCESS;
116
117         SysTryReturn(NID_TEXT, pDstConverter != null, null, E_INVALID_ARG,
118                 "[%s] Invalid argument is used. Unable to get encoder decoder object. pDstConverter can not be null", GetErrorMessage(E_INVALID_ARG));
119
120         unique_ptr<wchar_t[]> pWchar(DecodeN(pSrc, srcLength, retLength));
121         r = GetLastResult();
122         SysTryReturn(NID_TEXT, (pWchar != null), null, r, "[%s] Decoding failed", GetErrorMessage(r));
123
124         unique_ptr<byte[]> pByte(pDstConverter->EncodeN(pWchar.get(), retLength, retLength));
125         r = GetLastResult();
126         SysTryReturn(NID_TEXT, (pByte != null), null, r, "[%s] Decoding failed", GetErrorMessage(r));
127
128         SetLastResult(E_SUCCESS);
129         return pByte.release();
130 }
131
132 byte*
133 _EncodingCore::EncodeExN(const wchar_t* pSrc, int srcLength, int& retLength, bool flush)
134 {
135         SetLastResult(E_UNSUPPORTED_OPERATION);
136         return null;
137 }
138
139 wchar_t*
140 _EncodingCore::DecodeExN(const byte* pSrc, int srcLength, int& retLength, bool flush)
141 {
142         SetLastResult(E_UNSUPPORTED_OPERATION);
143         return null;
144 }
145
146 } } // Tizen::Text