Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_wearable / core / src / string.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file        string.cpp
18  * @author      Piotr Marcinkiewicz (p.marcinkiew@samsung.com)
19  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20  * @version     1.0
21  */
22 #include <stddef.h>
23 #include <dpl/string.h>
24 #include <dpl/char_traits.h>
25 #include <dpl/errno_string.h>
26 #include <dpl/exception.h>
27 #include <dpl/scoped_array.h>
28 #include <dpl/log/log.h>
29 #include <string>
30 #include <vector>
31 #include <algorithm>
32 #include <cstring>
33 #include <errno.h>
34 #include <iconv.h>
35 #include <unicode/ustring.h>
36
37 // TODO: Completely move to ICU
38 namespace DPL {
39 namespace //anonymous
40 {
41 class ASCIIValidator
42 {
43     const std::string& m_TestedString;
44
45   public:
46     ASCIIValidator(const std::string& aTestedString);
47
48     void operator()(char aCharacter) const;
49 };
50
51 ASCIIValidator::ASCIIValidator(const std::string& aTestedString) :
52     m_TestedString(aTestedString)
53 {}
54
55 void ASCIIValidator::operator()(char aCharacter) const
56 {
57     // Check for ASCII data range
58     if (aCharacter <= 0) {
59         ThrowMsg(
60             StringException::InvalidASCIICharacter,
61             "invalid character code " << static_cast<int>(aCharacter)
62                                       << " from string [" <<
63             m_TestedString
64                                       << "] passed as ASCII");
65     }
66 }
67
68 const iconv_t gc_IconvOperError = reinterpret_cast<iconv_t>(-1);
69 const size_t gc_IconvConvertError = static_cast<size_t>(-1);
70 } // namespace anonymous
71
72 String FromUTF8String(const std::string& aIn)
73 {
74     if (aIn.empty()) {
75         return String();
76     }
77
78     size_t inbytes = aIn.size();
79
80     // Default iconv UTF-32 module adds BOM (4 bytes) in from of string
81     // The worst case is when 8bit UTF-8 char converts to 32bit UTF-32
82     // newsize = oldsize * 4 + end + bom
83     // newsize - bytes for UTF-32 string
84     // oldsize - letters in UTF-8 string
85     // end - end character for UTF-32 (\0)
86     // bom - Unicode header in front of string (0xfeff)
87     size_t outbytes = sizeof(wchar_t) * (inbytes + 2);
88     std::vector<wchar_t> output(inbytes + 2, 0);
89
90     size_t outbytesleft = outbytes;
91     char* inbuf = const_cast<char*>(aIn.c_str());
92
93     // vector is used to provide buffer for iconv which expects char* buffer
94     // but during conversion from UTF32 uses internaly wchar_t
95     char* outbuf = reinterpret_cast<char*>(&output[0]);
96
97     iconv_t iconvHandle = iconv_open("UTF-32", "UTF-8");
98
99     if (gc_IconvOperError == iconvHandle) {
100         int error = errno;
101
102         ThrowMsg(StringException::IconvInitErrorUTF8ToUTF32,
103                  "iconv_open failed for " << "UTF-32 <- UTF-8" <<
104                  "error: " << GetErrnoString(error));
105     }
106
107     size_t iconvRet = iconv(iconvHandle,
108                             &inbuf,
109                             &inbytes,
110                             &outbuf,
111                             &outbytesleft);
112
113     iconv_close(iconvHandle);
114
115     if (gc_IconvConvertError == iconvRet) {
116         ThrowMsg(StringException::IconvConvertErrorUTF8ToUTF32,
117                  "iconv failed for " << "UTF-32 <- UTF-8" << "error: "
118                                      << GetErrnoString());
119     }
120
121     // Ignore BOM in front of UTF-32
122     return &output[1];
123 }
124
125 std::string ToUTF8String(const DPL::String& aIn)
126 {
127     if (aIn.empty()) {
128         return std::string();
129     }
130
131     size_t inbytes = aIn.size() * sizeof(wchar_t);
132     size_t outbytes = inbytes + sizeof(char);
133
134     // wstring returns wchar_t but iconv expects char*
135     // iconv internally is processing input as wchar_t
136     char* inbuf = reinterpret_cast<char*>(const_cast<wchar_t*>(aIn.c_str()));
137     std::vector<char> output(inbytes, 0);
138     char* outbuf = &output[0];
139
140     size_t outbytesleft = outbytes;
141
142     iconv_t iconvHandle = iconv_open("UTF-8", "UTF-32");
143
144     if (gc_IconvOperError == iconvHandle) {
145         ThrowMsg(StringException::IconvInitErrorUTF32ToUTF8,
146                  "iconv_open failed for " << "UTF-8 <- UTF-32"
147                                           << "error: " << GetErrnoString());
148     }
149
150     size_t iconvRet = iconv(iconvHandle,
151                             &inbuf,
152                             &inbytes,
153                             &outbuf,
154                             &outbytesleft);
155
156     iconv_close(iconvHandle);
157
158     if (gc_IconvConvertError == iconvRet) {
159         ThrowMsg(StringException::IconvConvertErrorUTF32ToUTF8,
160                  "iconv failed for " << "UTF-8 <- UTF-32"
161                                      << "error: " << GetErrnoString());
162     }
163
164     return &output[0];
165 }
166
167 String FromASCIIString(const std::string& aString)
168 {
169     String output;
170
171     std::for_each(aString.begin(), aString.end(), ASCIIValidator(aString));
172     std::copy(aString.begin(), aString.end(), std::back_inserter<String>(output));
173
174     return output;
175 }
176
177 String FromUTF32String(const std::wstring& aString)
178 {
179     return String(&aString[0]);
180 }
181
182 static UChar *ConvertToICU(const String &inputString)
183 {
184     ScopedArray<UChar> outputString;
185     int32_t size = 0;
186     int32_t convertedSize = 0;
187     UErrorCode error = U_ZERO_ERROR;
188
189     // Calculate size of output string
190     ::u_strFromWCS(NULL,
191                    0,
192                    &size,
193                    inputString.c_str(),
194                    -1,
195                    &error);
196
197     if (error == U_ZERO_ERROR ||
198         error == U_BUFFER_OVERFLOW_ERROR)
199     {
200         // What buffer size is ok ?
201         LogPedantic("ICU: Output buffer size: " << size);
202     } else {
203         ThrowMsg(StringException::ICUInvalidCharacterFound,
204                  "ICU: Failed to retrieve output string size. Error: "
205                  << error);
206     }
207
208     // Allocate proper buffer
209     outputString.Reset(new UChar[size + 1]);
210     ::memset(outputString.Get(), 0, sizeof(UChar) * (size + 1));
211
212     error = U_ZERO_ERROR;
213
214     // Do conversion
215     ::u_strFromWCS(outputString.Get(),
216                    size + 1,
217                    &convertedSize,
218                    inputString.c_str(),
219                    -1,
220                    &error);
221
222     if (!U_SUCCESS(error)) {
223         ThrowMsg(StringException::ICUInvalidCharacterFound,
224                  "ICU: Failed to convert string. Error: " << error);
225     }
226
227     // Done
228     return outputString.Release();
229 }
230
231 int StringCompare(const String &left,
232                   const String &right,
233                   bool caseInsensitive)
234 {
235     // Convert input strings
236     ScopedArray<UChar> leftICU(ConvertToICU(left));
237     ScopedArray<UChar> rightICU(ConvertToICU(right));
238
239     if (caseInsensitive) {
240         return static_cast<int>(u_strcasecmp(leftICU.Get(), rightICU.Get(), 0));
241     } else {
242         return static_cast<int>(u_strcmp(leftICU.Get(), rightICU.Get()));
243     }
244 }
245 } //namespace DPL
246
247 std::ostream& operator<<(std::ostream& aStream, const DPL::String& aString)
248 {
249     return aStream << DPL::ToUTF8String(aString);
250 }