Making changes in number classes to maintain compatibility with 2.1 applications
[platform/framework/native/appfw.git] / src / base / FBase_NumberUtil.cpp
1 //
2 // Copyright (c) 2013 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_NumberUtil.cpp
19  * @brief               This is the implementation file for _NumberUtil class.
20  */
21 #include <wchar.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <FBaseInteger8.h>
25 #include <FBaseResult.h>
26 #include <FBaseCharacter.h>
27 #include <FBaseSysLog.h>
28 #include "FBase_NumberUtil.h"
29 #include "FApp_AppInfo.h"
30
31 namespace Tizen { namespace Base
32 {
33
34 _NumberUtil::~_NumberUtil(void)
35 {
36 }
37
38 result
39 _NumberUtil::FindRadix(String& inputStr, int& radix)
40 {
41         SysTryReturnResult(NID_BASE, inputStr.GetLength() >= 1, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
42         int startIndex = 0;
43         int minLength = 2;
44         wchar_t* pEnd = null;
45         if (inputStr[0] == L'-' || inputStr[0] == L'+')
46         {
47                 startIndex = 1;
48                 minLength = 3;
49         }
50
51         // Find radix
52         if (inputStr[startIndex] == L'#')
53         {
54                 radix = Character::RADIX_HEXADECIMAL;
55
56                 // Remove '#'
57                 inputStr.Remove(startIndex, 1);
58         }
59         else if (inputStr[startIndex] == L'0' && (inputStr.GetLength() >= minLength))
60         {
61                 if (inputStr[startIndex + 1] == L'x' || inputStr[startIndex + 1] == L'X')
62                 {
63                         radix = Character::RADIX_HEXADECIMAL;
64                 }
65                 else
66                 {
67                         radix = Character::RADIX_OCTAL;
68                 }
69         }
70         else
71         {
72                 radix = Character::RADIX_DECIMAL;
73         }
74         return E_SUCCESS;
75 }
76
77 result
78 _NumberUtil::Decode(const String& inputStr, long& value)
79 {
80         int radix = 0;
81         String str(inputStr);
82         result res = _NumberUtil::FindRadix(str,radix);
83         SysTryReturnResult(NID_BASE, res == E_SUCCESS, res, "Propagating.");
84         res = Parse(str, radix, value);
85         SysTryReturnResult(NID_BASE, res == E_SUCCESS, res, "_NumberUtil decode failed");
86         return E_SUCCESS;
87 }
88
89 result
90 _NumberUtil::Decode(const String& inputStr, long long& value)
91 {
92         int radix = 0;
93         String str(inputStr);
94         result res = _NumberUtil::FindRadix(str,radix);
95         SysTryReturnResult(NID_BASE, res == E_SUCCESS, res, "Propagating.");
96         res = Parse(str, radix, value);
97         SysTryReturnResult(NID_BASE, res == E_SUCCESS, res, "_NumberUtil decode failed");
98         return E_SUCCESS;
99 }
100
101 result
102 _NumberUtil::Parse(const String& inputStr, int radix, long& value)
103 {
104         SysTryReturnResult(NID_BASE, ((radix >= Character::RADIX_BINARY) && (radix <= 36)), E_OUT_OF_RANGE,
105                 "The radix MUST be between 2 and 36.");
106
107         int len = inputStr.GetLength();
108         SysTryReturnResult(NID_BASE, len > 0, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
109
110         errno = 0;
111         wchar_t* pEnd = null;
112         value = wcstol(inputStr.GetPointer(), &pEnd, radix);
113         int sysErrno = errno;
114
115         SysTryReturnResult(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, "_NumberUtil parse failed. Scan stopped at (%ls).", pEnd);
116         SysTryReturnResult(NID_BASE, sysErrno !=  ERANGE, E_OUT_OF_RANGE, "Parsed value cannot fit into a long.");
117         return E_SUCCESS;
118 }
119
120 result
121 _NumberUtil::Parse(const String& inputStr, int radix, long long& value)
122 {
123         SysTryReturnResult(NID_BASE, ((radix >= Character::RADIX_BINARY) && (radix <= 36)), E_OUT_OF_RANGE,
124                 "The radix MUST be between 2 and 36.");
125
126         int len = inputStr.GetLength();
127         SysTryReturnResult(NID_BASE, len > 0, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
128
129         errno = 0;
130         wchar_t* pEnd = null;
131         value = wcstoll(inputStr.GetPointer(), &pEnd, radix);
132         int sysErrno = errno;
133
134         SysTryReturnResult(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, "_NumberUtil parse failed. Scan stopped at (%ls).", pEnd);
135         SysTryReturnResult(NID_BASE, sysErrno !=  ERANGE, E_OUT_OF_RANGE, "Parsed value cannot fit into a long long.");
136         return E_SUCCESS;
137 }
138 }} // Tizen::Base