a586c7cff1174f1bcec3985aa138be23ac55d38d
[platform/framework/native/appfw.git] / src / base / FBase_StringConverter.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                 FBase_StringConverter.cpp
19 * @brief                This is the implementation for _StringConverter class.
20 */
21
22 #include <stdlib.h>
23 #include <new>
24 #include <FBaseResult.h>
25 #include "FBase_StringConverter.h"
26 #include <FBaseSysLog.h>
27
28
29
30 namespace Tizen { namespace Base
31 {
32
33 char*
34 _StringConverter::CopyToCharArrayN(const String& str)
35 {
36         return CopyToCharArrayN(str.GetPointer());
37 }
38
39 char*
40 _StringConverter::CopyToCharArrayN(const wchar_t* pValue)
41 {
42         char* pRet = null;
43
44         int len = wcstombs(0, pValue, 0);
45         SysTryReturn(NID_BASE, len != -1, null, E_INVALID_ARG, "[%s] Invalid argument is used. Invalid string.",
46                 GetErrorMessage(E_INVALID_ARG));
47
48         pRet = new (std::nothrow) char[len + 1];
49         SysTryReturn(NID_BASE, pRet != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
50                 GetErrorMessage(E_OUT_OF_MEMORY));
51
52         len = wcstombs(pRet, pValue, len);
53         pRet[len] = 0;
54
55         return pRet;
56 }
57
58 }} //Tizen::Base