Apply unique_ptr on Text
[platform/framework/native/appfw.git] / src / text / FTextEncoding.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                FTextEncoding.cpp
19  * @brief               This is the implementation file for Encoding class.
20  */
21
22 #include <FTextAsciiEncoding.h>
23 #include <FTextUtf8Encoding.h>
24 #include <FTextLatin1Encoding.h>
25 #include <FTextGsmEncoding.h>
26 #include <FTextUcs2Encoding.h>
27 #include <FBaseSysLog.h>
28 #include "FText_EncodingImpl.h"
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32
33 namespace Tizen { namespace Text
34 {
35
36 Encoding::Encoding(void)
37         : _pEncodingImpl(null)
38 {
39
40 }
41
42 Encoding::~Encoding(void)
43 {
44
45 }
46
47 result
48 Encoding::GetByteCount(const String& str, int& byteCount) const
49 {
50         SysAssertf(_pEncodingImpl != null, "Not yet constructed! Construct() should be called before use.");
51         return _pEncodingImpl->GetByteCount(str, byteCount);
52 }
53
54
55 result
56 Encoding::GetByteCount(const WcharBuffer& chars, int& byteCount) const
57 {
58         SysAssertf(_pEncodingImpl != null, "Not yet constructed! Construct() should be called before use.");
59         return _pEncodingImpl->GetByteCount(chars, byteCount);
60 }
61
62
63 result
64 Encoding::GetByteCount(const WcharBuffer& chars, int charIndex, int charCount, int& byteCount) const
65 {
66         SysAssertf(_pEncodingImpl != null, "Not yet constructed! Construct() should be called before use.");
67         return _pEncodingImpl->GetByteCount(chars, charIndex, charCount, byteCount);
68 }
69
70 result
71 Encoding::GetCharCount(const ByteBuffer& bytes, int& charCount) const
72 {
73         SysAssertf(_pEncodingImpl != null, "Not yet constructed! Construct() should be called before use.");
74         return _pEncodingImpl->GetCharCount(bytes, charCount);
75 }
76
77
78 result
79 Encoding::GetCharCount(const ByteBuffer& bytes, int byteIndex, int byteCount, int& charCount) const
80 {
81         SysAssertf(_pEncodingImpl != null, "Not yet constructed! Construct() should be called before use.");
82         return _pEncodingImpl->GetCharCount(bytes, byteIndex, byteCount, charCount);
83 }
84
85 Utf8Encoding&
86 Encoding::GetUtf8Encoding(void)
87 {
88         static Utf8Encoding utf8;
89         return utf8;
90 }
91
92 Ucs2Encoding&
93 Encoding::GetUcs2Encoding(void)
94 {
95         static Ucs2Encoding ucs2;
96         return ucs2;
97 }
98
99 GsmEncoding&
100 Encoding::GetGsmEncoding(void)
101 {
102         static GsmEncoding gsm;
103         return gsm;
104 }
105
106 Latin1Encoding&
107 Encoding::GetLatin1Encoding(void)
108 {
109         static Latin1Encoding latin1;
110         return latin1;
111 }
112
113 AsciiEncoding&
114 Encoding::GetAsciiEncoding(void)
115 {
116         static AsciiEncoding ascii;
117         return ascii;
118 }
119
120 Encoding*
121 Encoding::GetEncodingN(const Tizen::Base::String& encodingType)
122 {
123         ClearLastResult();
124         result r = E_SUCCESS;
125
126         std::unique_ptr< _EncodingImpl > pEncodingImpl(new (std::nothrow) _EncodingImpl());
127         SysTryReturn(NID_TEXT, pEncodingImpl != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
128
129         r = pEncodingImpl->Construct(encodingType);
130         SysTryReturn(NID_TEXT, !IsFailed(r), null, r , "[%s] Encoder construct failed.", GetErrorMessage(r));
131         return pEncodingImpl.release();
132 }
133
134 String
135 Encoding::GetEncodingType(const Encoding& encoding)
136 {
137         return encoding.GetEncodingType();
138 }
139
140 IList*
141 Encoding::GetAvailableEncodingsN(void)
142 {
143         return _EncodingImpl::GetAvailableEncodingsImplN();
144 }
145
146 ByteBuffer*
147 Encoding::ConvertN(const Encoding& src, const Encoding& dst, const ByteBuffer& srcBytes)
148 {
149         return _EncodingImpl::ConvertImplN(src, dst, srcBytes);
150 }
151
152 ByteBuffer*
153 Encoding::ConvertN(const Encoding& src, const Encoding& dst, const ByteBuffer& srcBytes, int index, int count)
154 {
155         return _EncodingImpl::ConvertImplN(src, dst, srcBytes, index, count);
156 }
157
158 } } // Tizen::Text