Merge "GetPackageAppInfoListN() is added with package, app filter." into tizen_2.1
[platform/framework/native/appfw.git] / src / base / FBaseInteger.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                FBaseInteger.cpp
20  * @brief               This is the implementation file for Integer class.
21  * @see                 Number
22  */
23
24 #include <wchar.h>
25 #include <limits.h>
26 #include <errno.h>
27 #include <FBaseInteger.h>
28 #include <FBaseResult.h>
29 #include <FBaseCharacter.h>
30 #include <FBaseSysLog.h>
31
32 namespace Tizen { namespace Base
33 {
34
35 Integer::Integer(int value)
36         : value(value)
37         , __pIntegerImpl(null)
38 {
39 }
40
41 Integer::Integer(const Integer& value)
42         : value(value.value)
43         , __pIntegerImpl(null)
44 {
45 }
46
47 Integer::~Integer(void)
48 {
49
50 }
51
52 Integer&
53 Integer::operator =(const Integer& rhs)
54 {
55         if (&rhs != this)
56         {
57                 value = rhs.value;
58         }
59         return *this;
60 }
61
62 int
63 Integer::Compare(int i1, int i2)
64 {
65         return(i1 < i2 ? -1 : (i1 == i2 ? 0 : 1));
66 }
67
68 int
69 Integer::CompareTo(const Integer& value) const
70 {
71         return(Integer::Compare(this->value, value.value));
72 }
73
74 bool
75 Integer::Equals(const Object& obj) const
76 {
77         const Integer* pOther = dynamic_cast <const Integer*>(&obj);
78         if (pOther == null)
79         {
80                 return false;
81         }
82
83         return value == (*pOther).value;
84 }
85
86 result
87 Integer::Decode(const String& s, int& ret)
88 {
89         SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
90                 "[%s] The length of s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
91
92         int radix = 0;
93         wchar_t* pEnd = null;
94         String str(s);
95
96         // Find radix
97         if (s[0] == L'#')
98         {
99                 radix = Character::RADIX_HEXADECIMAL;
100
101                 // Remove '#'
102                 str.Remove(0, 1);
103         }
104         else if (s[0] == L'0' && (s.GetLength() >= 2))
105         {
106                 if (s[1] == L'x' || s[1] == L'X')
107                 {
108                         radix = Character::RADIX_HEXADECIMAL;
109                 }
110                 else
111                 {
112                         radix = Character::RADIX_OCTAL;
113                 }
114         }
115         else
116         {
117                 radix = Character::RADIX_DECIMAL;
118         }
119
120         errno = 0;
121         ret = wcstol(str.GetPointer(), &pEnd, radix);
122         SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
123                 "[%s] Integer decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
124         SysTryReturn(NID_BASE, !((ret == LONG_MAX || ret == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
125                 "[%s] Decoded value cannot fit into an Integer.", GetErrorMessage(E_NUM_FORMAT));
126
127         return E_SUCCESS;
128 }
129
130 int
131 Integer::GetHashCode(void) const
132 {
133         return value;
134 }
135
136 int
137 Integer::GetHashCode(int val)
138 {
139         return val;
140 }
141
142 result
143 Integer::Parse(const String& s, int& ret)
144 {
145         int len = s.GetLength();
146         SysTryReturn(NID_BASE, (len > 0 && len < 12), E_NUM_FORMAT, E_NUM_FORMAT,
147                 "[%s] The length of s(%ls) MUST be greater than 0 and less than 12.",
148                 GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
149
150         return Parse(s, Character::RADIX_DECIMAL, ret);
151 }
152
153 result
154 Integer::Parse(const String& s, int radix, int& ret)
155 {
156         SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
157                 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
158                 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
159
160         int len = s.GetLength();
161         SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
162                 GetErrorMessage(E_NUM_FORMAT));
163
164         errno = 0;
165         wchar_t* pEnd = null;
166         int tmpRet = wcstol(s.GetPointer(), &pEnd, radix);
167         SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
168                 "[%s] Integer parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
169         SysTryReturn(NID_BASE, !((tmpRet == LONG_MAX || tmpRet == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
170                 "[%s] Parsed value cannot fit into an Integer.", GetErrorMessage(E_NUM_FORMAT));
171
172         ret = tmpRet;
173         return E_SUCCESS;
174 }
175
176 char
177 Integer::ToChar(void) const
178 {
179         return static_cast<char> (value);
180 }
181
182 short
183 Integer::ToShort(void) const
184 {
185         return static_cast<short> (value);
186 }
187
188 int
189 Integer::ToInt(void) const
190 {
191         return value;
192 }
193
194 long
195 Integer::ToLong(void) const
196 {
197         return static_cast<long> (value);
198 }
199
200 long long
201 Integer::ToLongLong(void) const
202 {
203         return static_cast<long long> (value);
204 }
205
206 float
207 Integer::ToFloat(void) const
208 {
209         return static_cast<float> (value);
210 }
211
212 double
213 Integer::ToDouble(void) const
214 {
215         return static_cast<double> (value);
216 }
217
218 String
219 Integer::ToString(void) const
220 {
221         return(Integer::ToString(value));
222 }
223
224 String
225 Integer::ToString(int value)
226 {
227         const static unsigned int INTEGER_LENGTH_MAX = 11;
228
229         wchar_t sValue[INTEGER_LENGTH_MAX + 1] = {0, };
230         swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
231
232         return String(sValue);
233 }
234
235 }} //Tizen::Base