Change to use _LocalizedNumParser in IO
[platform/framework/native/appfw.git] / src / io / FIo_RegistryImpl.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        FIo_RegistryImpl.cpp
19  * @brief       This is the implementation file for _RegistryImpl class.
20  */
21
22 #include <unistd.h>
23 #include <unique_ptr.h>
24
25 #include <FBaseInteger.h>
26 #include <FBaseDouble.h>
27 #include <FBaseFloat.h>
28 #include <FBaseUuId.h>
29 #include <FBaseByteBuffer.h>
30 #include <FBaseColHashMap.h>
31 #include <FBaseColIEnumerator.h>
32 #include <FBaseUtilStringTokenizer.h>
33 #include <FBaseResult.h>
34 #include <FBaseSysLog.h>
35 #include <FIoFile.h>
36 #include <FIoRegistry.h>
37
38 #include <FBase_StringConverter.h>
39 #include <FBase_LocalizedNumParser.h>
40 #include <FIo_FileImpl.h>
41 #include <FIo_RegistryImpl.h>
42 #include <FIo_NormalRegistry.h>
43 #include <FIo_SecureRegistry.h>
44 #include <FIo_SecureIoUtil.h>
45
46 using namespace std;
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Utility;
49 using namespace Tizen::Base::Collection;
50
51 namespace Tizen { namespace Io
52 {
53
54 static const int _MAX_REG_OPENMODE_LENGTH = 2;
55
56 _RegistryImpl::_RegistryImpl(void)
57         : __pRegistryCore(null)
58 {
59 }
60
61 _RegistryImpl::~_RegistryImpl(void)
62 {
63         delete __pRegistryCore;
64 }
65
66 bool
67 _RegistryImpl::ConvertRegistryOpenMode(long legacyMode, char* pOpenMode)
68 {
69         switch (legacyMode)
70         {
71         case REG_OPEN_READ_ONLY:
72                 strcpy(pOpenMode, "r\0");
73                 break;
74         case REG_OPEN_READ_WRITE:
75                 strcpy(pOpenMode, "r+\0");
76                 break; 
77         case (REG_OPEN_READ_WRITE | REG_OPEN_CREATE):
78                 strcpy(pOpenMode, "a+\0");
79                 break;
80         default:
81                 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode is invalid.");
82                 return false;
83         }
84         return true;
85 }
86
87 // This method will be removed in a future release.
88 result
89 _RegistryImpl::Construct(const String& regPath, long legacyMode, const ByteBuffer* pSecretKey)
90 {
91         SysAssertf(__pRegistryCore == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
92
93         unique_ptr<char[]> pOpenMode(new char[_MAX_REG_OPENMODE_LENGTH + 1]);
94         SysTryReturnResult(NID_IO, pOpenMode != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
95
96         bool isValid = _RegistryImpl::ConvertRegistryOpenMode(legacyMode, pOpenMode.get());
97         SysTryReturnResult(NID_IO, isValid == true, E_INVALID_ARG, "The specified openMode is invalid.");
98
99         return Construct(regPath, pOpenMode.get(), pSecretKey);
100 }
101
102 result
103 _RegistryImpl::Construct(const String& regPath, const char* pOpenMode, const ByteBuffer* pSecretKey)
104 {
105         SysAssertf(__pRegistryCore == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
106
107         result r = E_SUCCESS;
108         if (!pSecretKey)
109         {
110                 unique_ptr<_NormalRegistry> pNormalRegistry(new (std::nothrow) _NormalRegistry());
111                 SysTryReturnResult(NID_IO, pNormalRegistry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
112
113                 r = pNormalRegistry->Construct(regPath, pOpenMode);
114                 if (IsFailed(r))
115                 {
116                         SysLogException(NID_IO, r, "[%s] Propagated.", GetErrorMessage(r));
117                         return r;
118                 }
119                 __pRegistryCore = pNormalRegistry.release();
120         }
121         else
122         {
123                 unique_ptr<_SecureRegistry> pSecureRegistry(new (std::nothrow) _SecureRegistry());
124                 SysTryReturnResult(NID_IO, pSecureRegistry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
125
126                 r = pSecureRegistry->Construct(regPath, pOpenMode, pSecretKey);
127                 if (IsFailed(r))
128                 {
129                         SysLogException(NID_IO, r, "[%s] Propagated.", GetErrorMessage(r));
130                         return r;
131                 }
132                 __pRegistryCore = pSecureRegistry.release();
133         }
134
135         return r;
136 }
137
138 result
139 _RegistryImpl::Remove(const String& regPath)
140 {
141         return _FileImpl::Remove(regPath);
142 }
143
144 result
145 _RegistryImpl::AddSection(const String& sectionName)
146 {
147         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
148         return __pRegistryCore->AddSection(sectionName);
149 }
150
151 result
152 _RegistryImpl::RemoveSection(const String& sectionName)
153 {
154         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
155         return __pRegistryCore->RemoveSection(sectionName);
156 }
157
158 result
159 _RegistryImpl::GetSectionListN(IList** pRetList)
160 {
161         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
162         return __pRegistryCore->GetSectionListN(pRetList);
163 }
164
165
166 result
167 _RegistryImpl::GetEntryList(const String& sectionName, Collection::HashMap& retMap)
168 {
169         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
170         return __pRegistryCore->GetEntryList(sectionName, retMap);
171 }
172
173
174 result
175 _RegistryImpl::GetEntryListN(const String& sectionName, HashMap** pRetList)
176 {
177         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
178         return __pRegistryCore->GetEntryListN(sectionName, pRetList);
179 }
180
181 IMap*
182 _RegistryImpl::GetAllEntriesN(const String& sectionName) const
183 {
184         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
185         return __pRegistryCore->GetAllEntriesN(sectionName);
186 }
187
188 IList*
189 _RegistryImpl::GetAllEntryNamesN(const String& sectionName) const
190 {
191         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
192         return __pRegistryCore->GetAllEntryNamesN(sectionName);
193 }
194
195 result
196 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, float& entryValue)
197 {
198         String value;
199         result r = __pRegistryCore->GetValue(sectionName, entryName, value);
200         SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
201
202         float fValue = _LocalizedNumParser::ToFloat(value, "C");
203         r = GetLastResult();
204         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_PARSING_FAILED, "Failed to parse the data.");
205
206         entryValue = fValue;
207
208         return E_SUCCESS;
209 }
210
211 result
212 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, double& entryValue)
213 {
214         String value;
215         result r = __pRegistryCore->GetValue(sectionName, entryName, value);
216         SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
217
218         double dValue = _LocalizedNumParser::ToDouble(value, "C");
219         r = GetLastResult();
220         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_PARSING_FAILED, "Failed to parse the data.");
221
222         entryValue = dValue;
223
224         return E_SUCCESS;
225 }
226
227 result
228 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, String& entryValue)
229 {
230         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
231         return __pRegistryCore->GetValue(sectionName, entryName, entryValue);
232 }
233
234 result
235 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, float entryValue)
236 {
237         return AddValue(sectionName, entryName, _LocalizedNumParser::ToString(entryValue, "C"));
238 }
239
240 result
241 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, double entryValue)
242 {
243         return AddValue(sectionName, entryName, _LocalizedNumParser::ToString(entryValue, "C"));
244 }
245
246 result
247 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, const String& entryValue)
248 {
249         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
250         return __pRegistryCore->AddValue(sectionName, entryName, entryValue);
251 }
252
253 result
254 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, float entryValue)
255 {
256         return SetValue(sectionName, entryName, _LocalizedNumParser::ToString(entryValue, "C"));
257 }
258
259 result
260 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, double entryValue)
261 {
262         return SetValue(sectionName, entryName, _LocalizedNumParser::ToString(entryValue, "C"));
263 }
264
265 result
266 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, const String& entryValue)
267 {
268         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
269         return __pRegistryCore->SetValue(sectionName, entryName, entryValue);
270 }
271
272 result
273 _RegistryImpl::RemoveValue(const String& sectionName, const String& entryName)
274 {
275         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
276         return __pRegistryCore->RemoveValue(sectionName, entryName);
277 }
278
279 result
280 _RegistryImpl::Flush(void)
281 {
282         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
283         return __pRegistryCore->Flush();
284 }
285
286 result
287 _RegistryImpl::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer* pKey)
288 {
289         return _SecureRegistry::ConvertToSecureRegistry(plainRegPath, secureRegPath, pKey);
290 }
291
292 int
293 _RegistryImpl::GetSectionIndex(const String& sectionName)
294 {
295         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
296         return __pRegistryCore->GetSectionIndex(sectionName);
297 }
298
299 void
300 _RegistryImpl::Removekey(const String& sectionName, const String& keyName)
301 {
302         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
303         __pRegistryCore->Removekey(sectionName, keyName);
304 }
305
306 int
307 _RegistryImpl::GetAllSectionCount(void)
308 {
309         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
310         return __pRegistryCore->GetAllSectionCount();
311 }
312
313
314 int
315 _RegistryImpl::GetAllEntryCount(int sectionIndex)
316 {
317         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
318         return __pRegistryCore->GetAllEntryCount(sectionIndex);
319 }
320
321 void
322 _RegistryImpl::RemoveEntry(int sectionIndex, int entryIndex)
323 {
324         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
325         __pRegistryCore->RemoveEntry(sectionIndex, entryIndex);
326 }
327
328 void
329 _RegistryImpl::ModifyEntryValue(int sectionIndex, int entryIndex, _RegValueType type, const void* pValue, int size)
330 {
331         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
332         __pRegistryCore->ModifyEntryValue(sectionIndex, entryIndex, type, pValue, size);
333 }
334
335 int
336 _RegistryImpl::GetEntryIndex(int sectionIndex, const String& entryName)
337 {
338         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
339         return __pRegistryCore->GetEntryIndex(sectionIndex, entryName);
340 }
341
342 String
343 _RegistryImpl::GetEntryName(int sectionIndex, int entryIndex)
344 {
345         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
346         return __pRegistryCore->GetEntryName(sectionIndex, entryIndex);
347 }
348
349 void
350 _RegistryImpl::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType type, void* pValue, int* pSize)
351 {
352         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
353
354         __pRegistryCore->GetEntryValue(sectionIndex, entryIndex, type, pValue, pSize);
355 }
356
357 String
358 _RegistryImpl::GetSectionName(int sectionIndex)
359 {
360         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
361         return __pRegistryCore->GetSectionName(sectionIndex);
362 }
363
364 void*
365 _RegistryImpl::GetSectionByIndex(int sectionIndex)
366 {
367         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
368         return __pRegistryCore->GetSectionByIndex(sectionIndex);
369 }
370
371 _RegistryImpl*
372 _RegistryImpl::GetInstance(Registry& registry)
373 {
374         return registry.__pRegistryImpl;
375 }
376
377 const _RegistryImpl*
378 _RegistryImpl::GetInstance(const Registry& registry)
379 {
380         return registry.__pRegistryImpl;
381 }
382
383 FileLock*
384 _RegistryImpl::LockN(FileLockType lockType)
385 {
386         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
387         return __pRegistryCore->LockN(lockType);
388 }
389
390 FileLock*
391 _RegistryImpl::TryToLockN(FileLockType lockType)
392 {
393         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
394         return __pRegistryCore->TryToLockN(lockType);
395 }
396
397 }} // Tizen::Io
398