Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / base / FBaseUuId.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                FBaseUuId.cpp
19  * @brief               This is the implementation file for UuId class.
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <wchar.h>
25 #include <uuid/uuid.h>
26 #include <FBaseUuId.h>
27 #include <FBaseResult.h>
28 #include <FBaseByteBuffer.h>
29 #include <FBaseSysLog.h>
30 #include "FBase_StringConverter.h"
31 #include <unique_ptr.h>
32
33
34 namespace Tizen { namespace Base
35 {
36
37 const static UUID __INVALID_UUID = {0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
38
39 UuId::UuId(void)
40         : __pUuIdImpl(null)
41 {
42         uuid_clear(this->uuid);
43 }
44
45 UuId::UuId(const UuId& uuid)
46         : __pUuIdImpl(null)
47 {
48         uuid_copy(this->uuid, uuid.uuid);
49 }
50
51 UuId::UuId(const UUID& uuid)
52         : __pUuIdImpl(null)
53 {
54         memcpy(this->uuid, &uuid, sizeof(this->uuid));
55
56         ConvertToUuId(this->uuid);
57 }
58
59 UuId::UuId(const byte uuid[16])
60         : __pUuIdImpl(null)
61 {
62         uuid_copy(this->uuid, uuid);
63 }
64
65 UuId::~UuId(void)
66 {
67 }
68
69 bool
70 operator ==(const UUID& uuid1, const UuId& uuid2)
71 {
72         byte uuid[16] = {0,};
73         memcpy(uuid, &uuid1, sizeof(uuid));
74         UuId::ConvertToUuId(uuid);
75
76         int ret = uuid_compare(uuid, uuid2.uuid);
77         if (ret == 0)
78         {
79                 return true;
80         }
81         else
82         {
83                 return false;
84         }
85 }
86
87 bool
88 UuId::operator ==(const UuId& uuid) const
89 {
90         int ret = uuid_compare(this->uuid, uuid.uuid);
91         if (ret == 0)
92         {
93                 return true;
94         }
95
96         return false;
97 }
98
99 bool
100 operator !=(const UUID& uuid1, const UuId& uuid2)
101 {
102         if (uuid1 == uuid2)
103         {
104                 return false;
105         }
106         else
107         {
108                 return true;
109         }
110 }
111
112 bool
113 UuId::operator !=(const UuId& uuid) const
114 {
115         if (*this == uuid)
116         {
117                 return false;
118         }
119         else
120         {
121                 return true;
122         }
123 }
124
125 UuId&
126 UuId::operator =(const UuId& uuid)
127 {
128         if (&uuid != this)
129         {
130                 uuid_copy(this->uuid, uuid.uuid);
131         }
132         return (*this);
133 }
134
135 bool
136 UuId::Equals(const Object& obj) const
137 {
138         const UuId* pOther = dynamic_cast<const UuId*>(&obj);
139
140         if (pOther == null)
141         {
142                 return false;
143         }
144
145         if (*this == *pOther)
146         {
147                 return true;
148         }
149         else
150         {
151                 return false;
152         }
153 }
154
155 int
156 UuId::GetHashCode(void) const
157 {
158         String str = ToString();
159
160         return str.GetHashCode();
161 }
162
163 String
164 UuId::ToString(void) const
165 {
166         /*
167         char tmpuuid[sizeof(UUID)];
168         unsigned int x = 0;
169         unsigned short s1 = 0;
170         unsigned short s2 = 0;
171         unsigned char c1[2];
172         unsigned char c2[6];
173         int pos = 0;
174
175         char pChar[1 + 2 * (sizeof(unsigned long) + sizeof(unsigned short) * 2 + sizeof(unsigned char) * 8) + 4];
176
177         memcpy(tmpuuid, &(__uuid), sizeof(UUID));
178
179         memcpy(&x, tmpuuid, sizeof(unsigned long));
180         pos += sizeof(unsigned long);
181
182         memcpy(&s1, tmpuuid + pos, sizeof(unsigned short));
183         pos += sizeof(unsigned short);
184
185         memcpy(&s2, tmpuuid + pos, sizeof(unsigned short));
186         pos += sizeof(unsigned short);
187
188         memcpy(&c1, tmpuuid + pos, sizeof(unsigned char) * 2);
189         pos += sizeof(unsigned char) * 2;
190
191         memcpy(&c2, tmpuuid + pos, sizeof(unsigned char) * 6);
192
193         sprintf(pChar, "%.8x-%.4x-%.4x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x",
194                         x, s1, s2, c1[0], c1[1], c2[0], c2[1], c2[2], c2[3], c2[4], c2[5]);
195
196         return String(pChar);
197         */
198
199         char uuid_str[36] = {0,};
200         
201         uuid_unparse(this->uuid, uuid_str);
202
203         return String(uuid_str);
204 }
205
206 UUID
207 UuId::ToUUID(void) const
208 {
209         UUID uuid;
210         byte uuidValue[16] = {0,};
211         
212         uuid_copy(uuidValue, this->uuid);
213         ConvertToUuId(uuidValue);
214
215         memcpy(&uuid, uuidValue, sizeof(UUID));
216
217         return uuid;
218 }
219
220 result
221 UuId::Parse(const String& str, UuId& uuid)
222 {
223         /*
224         char c[32] = {0};
225         int length = str.GetLength();
226
227         SysTryReturn(NID_BASE, length == 36, E_INVALID_ARG, E_INVALID_ARG, ("[E_INVALID_ARG] The length of str(%d) MUST be 36."), length);
228
229         SysTryReturn(NID_BASE, !(str[8] != L'-' || str[13] != L'-' || str[18] != L'-' || str[23] != L'-')
230                           , E_INVALID_ARG, E_INVALID_ARG, ("[E_INVALID_ARG] The str(%ls) is not valid UuId type"), str.GetPointer());
231
232         // str[i] must be one of '0~9' or 'A~F' or 'a~f'
233         for (int i = 0; i < length; i++)
234         {
235                 if (str[i] == L'-')
236                 {
237                         if (i == 8 || i == 13 || i == 18 || i == 23)
238                         {
239                                 continue;
240                         }
241                         else
242                         {
243                                 SysLogException(NID_BASE, E_INVALID_ARG, "[E_INVALID_ARG] The str(%ls) is not valid UuId type", str.GetPointer());
244                                 return E_INVALID_ARG;
245                         }
246                 }
247
248                 SysTryReturn(NID_BASE, (str[i] >= '0' && str[i] <= '9') || (str[i] >= 'A' && str[i] <= 'F') ||
249                                    (str[i] >= 'a' && str[i] <= 'f'), E_INVALID_ARG, E_INVALID_ARG, ("[E_INVALID_ARG] The str(%ls) MUST consist of only alphanumeric characters and '-'."), str.GetPointer());
250         }
251
252         char mbstring[length + 1];
253         wchar_t wchar_tstring[length + 1];
254
255         wcsncpy(wchar_tstring, str.GetPointer(), length);
256         wchar_tstring[length] = 0;
257
258         length = wcstombs(mbstring, wchar_tstring, length);
259         SysTryReturn(NID_BASE, length == 36, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The str(%ls) is not valid UuId type", str.GetPointer());
260
261         sscanf(mbstring,
262                    "%c%c%c%c%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c%c%c%c%c%c%c%c%c",
263                    &c[0], &c[1], &c[2], &c[3], &c[4], &c[5], &c[6], &c[7],
264                    &c[8], &c[9], &c[10], &c[11],
265                    &c[12], &c[13], &c[14], &c[15],
266                    &c[16], &c[17], &c[18], &c[19],
267                    &c[20], &c[21], &c[22], &c[23], &c[24], &c[25], &c[26], &c[27], &c[28], &c[29], &c[30], &c[31]);
268
269         for (int i = 0; i < 32; ++i)
270         {
271                 if (c[i] >= 'a')
272                 {
273                         c[i] = (char) (c[i] - 'a' + (char) 10);
274                 }
275                 else if (c[i] >= 'A')
276                 {
277                         c[i] = (char) (c[i] - 'A' + (char) 10);
278                 }
279                 else
280                 {
281                         c[i] -= '0';
282                 }
283         }
284
285         uuid.__uuid.x = c[0] * 0x10000000
286                                         + c[1] * 0x1000000
287                                         + c[2] * 0x100000
288                                         + c[3] * 0x10000
289                                         + c[4] * 0x1000
290                                         + c[5] * 0x100
291                                         + c[6] * 0x10
292                                         + c[7];
293         uuid.__uuid.s1 = c[8] * 0x1000
294                                          + c[9] * 0x100
295                                          + c[10] * 0x10
296                                          + c[11];
297         uuid.__uuid.s2 = c[12] * 0x1000
298                                          + c[13] * 0x100
299                                          + c[14] * 0x10
300                                          + c[15];
301
302         uuid.__uuid.c[0] = c[16] * 0x10 + c[17];
303         uuid.__uuid.c[1] = c[18] * 0x10 + c[19];
304         uuid.__uuid.c[2] = c[20] * 0x10 + c[21];
305         uuid.__uuid.c[3] = c[22] * 0x10 + c[23];
306         uuid.__uuid.c[4] = c[24] * 0x10 + c[25];
307         uuid.__uuid.c[5] = c[26] * 0x10 + c[27];
308         uuid.__uuid.c[6] = c[28] * 0x10 + c[29];
309         uuid.__uuid.c[7] = c[30] * 0x10 + c[31];
310
311         return E_SUCCESS;
312         */
313
314         int length = str.GetLength();
315
316         SysTryReturnResult(NID_BASE, length == 36, E_INVALID_ARG, "The length of str(%d) MUST be 36.", length);
317
318         SysTryReturnResult(NID_BASE, !(str[8] != L'-' || str[13] != L'-' || str[18] != L'-' || str[23] != L'-')
319                           ,E_INVALID_ARG, "The str(%ls) is not valid UuId type.", str.GetPointer());
320
321         uuid_t uuidValue;
322         std::unique_ptr<char []> pStr(_StringConverter::CopyToCharArrayN(str));
323         SysTryReturnResult(NID_BASE, pStr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
324         int ret = uuid_parse(pStr.get(), uuidValue);
325
326         SysTryReturnResult(NID_BASE, ret == 0,  E_INVALID_ARG, "The str(%ls) MUST consist of only alphanumeric characters and '-'.", str.GetPointer());
327
328         uuid_copy(uuid.uuid, uuidValue);
329
330         return E_SUCCESS;
331 }
332
333 UuId*
334 UuId::GenerateN(void)
335 {
336         uuid_t out;
337         UuId* pUuid = new (std::nothrow) UuId();
338         if (pUuid == null)
339         {
340                 SysLogException(NID_BASE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
341                 return null;
342         }
343
344         uuid_generate(out);
345
346         uuid_copy(pUuid->uuid, out);
347
348         return pUuid;
349 }
350
351 UuId
352 UuId::GetInvalidUuId(void)
353 {
354         UuId invalidUuId(__INVALID_UUID);
355         return invalidUuId;
356 }
357
358 void
359 UuId::ConvertToUuId(byte uuid[16])
360 {
361         byte value = 0x00;
362
363         for (int i=0; i<2; i++)
364         {
365                 value = uuid[i];
366                 uuid[i] = uuid[3-i];
367                 uuid[3-i] = value;
368         }
369
370         value = uuid[4];
371         uuid[4] = uuid[5];
372         uuid[5] = value;
373
374         value = uuid[6];
375         uuid[6] = uuid[7];
376         uuid[7] = value;
377 }
378
379 }} //Tizen::Base