2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FIo_RegistryImpl.cpp
20 * @brief This is the implementation file for _RegistryImpl class.
25 #include <unique_ptr.h>
30 #include <FBaseInteger.h>
31 #include <FBaseDouble.h>
32 #include <FBaseFloat.h>
33 #include <FBaseUuId.h>
34 #include <FBaseByteBuffer.h>
35 #include <FBaseColHashMap.h>
36 #include <FBaseColIEnumerator.h>
37 #include <FBaseUtilStringTokenizer.h>
38 #include <FBaseResult.h>
39 #include <FBaseSysLog.h>
41 #include <FIoRegistry.h>
43 #include <FBase_StringConverter.h>
44 #include <FIo_FileImpl.h>
45 #include <FIo_RegistryImpl.h>
46 #include <FIo_NormalRegistry.h>
47 #include <FIo_SecureRegistry.h>
48 #include <FIo_SecureIoUtil.h>
51 using namespace Tizen::Base;
52 using namespace Tizen::Base::Utility;
53 using namespace Tizen::Base::Collection;
55 namespace Tizen { namespace Io
58 static const int _MAX_REG_OPENMODE_LENGTH = 2;
60 _RegistryImpl::_RegistryImpl(void)
61 : __pRegistryCore(null)
66 _RegistryImpl::~_RegistryImpl(void)
68 delete __pRegistryCore;
69 if (__loc != (locale_t)0)
76 _RegistryImpl::ConvertRegistryOpenMode(long legacyMode, char* pOpenMode)
80 case REG_OPEN_READ_ONLY:
81 strcpy(pOpenMode, "r\0");
83 case REG_OPEN_READ_WRITE:
84 strcpy(pOpenMode, "r+\0");
86 case (REG_OPEN_READ_WRITE | REG_OPEN_CREATE):
87 strcpy(pOpenMode, "a+\0");
90 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode is invalid.");
96 // This method will be removed in a future release.
98 _RegistryImpl::Construct(const String& regPath, long legacyMode, const ByteBuffer* pSecretKey)
100 SysAssertf(__pRegistryCore == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
102 unique_ptr<char[]> pOpenMode(new char[_MAX_REG_OPENMODE_LENGTH + 1]);
103 SysTryReturnResult(NID_IO, pOpenMode != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
105 bool isValid = _RegistryImpl::ConvertRegistryOpenMode(legacyMode, pOpenMode.get());
106 SysTryReturnResult(NID_IO, isValid == true, E_INVALID_ARG, "The specified openMode is invalid.");
108 return Construct(regPath, pOpenMode.get(), pSecretKey);
112 _RegistryImpl::Construct(const String& regPath, const char* pOpenMode, const ByteBuffer* pSecretKey)
114 SysAssertf(__pRegistryCore == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
116 result r = E_SUCCESS;
119 unique_ptr<_NormalRegistry> pNormalRegistry(new (std::nothrow) _NormalRegistry());
120 SysTryReturnResult(NID_IO, pNormalRegistry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
122 r = pNormalRegistry->Construct(regPath, pOpenMode);
125 SysLogException(NID_IO, r, "[%s] Propagated.", GetErrorMessage(r));
128 __pRegistryCore = pNormalRegistry.release();
132 unique_ptr<_SecureRegistry> pSecureRegistry(new (std::nothrow) _SecureRegistry());
133 SysTryReturnResult(NID_IO, pSecureRegistry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
135 r = pSecureRegistry->Construct(regPath, pOpenMode, pSecretKey);
138 SysLogException(NID_IO, r, "[%s] Propagated.", GetErrorMessage(r));
141 __pRegistryCore = pSecureRegistry.release();
144 __loc = newlocale(LC_ALL_MASK, "C", (locale_t)0);
145 if (__loc == (locale_t)0)
147 SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to create locale object. errno: %d (%s)",
148 errno, strerror(errno));
155 _RegistryImpl::Remove(const String& regPath)
157 return _FileImpl::Remove(regPath);
161 _RegistryImpl::AddSection(const String& sectionName)
163 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
164 return __pRegistryCore->AddSection(sectionName);
168 _RegistryImpl::RemoveSection(const String& sectionName)
170 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
171 return __pRegistryCore->RemoveSection(sectionName);
175 _RegistryImpl::GetSectionListN(IList** pRetList)
177 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
178 return __pRegistryCore->GetSectionListN(pRetList);
183 _RegistryImpl::GetEntryList(const String& sectionName, Collection::HashMap& retMap)
185 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
186 return __pRegistryCore->GetEntryList(sectionName, retMap);
191 _RegistryImpl::GetEntryListN(const String& sectionName, HashMap** pRetList)
193 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
194 return __pRegistryCore->GetEntryListN(sectionName, pRetList);
198 _RegistryImpl::GetAllEntriesN(const String& sectionName) const
200 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
201 return __pRegistryCore->GetAllEntriesN(sectionName);
205 _RegistryImpl::GetAllEntryNamesN(const String& sectionName) const
207 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
208 return __pRegistryCore->GetAllEntryNamesN(sectionName);
212 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, float& entryValue)
216 result r = GetValue(sectionName, entryName, value);
219 r = Float::Parse(value, entryValue);
222 r = E_PARSING_FAILED;
225 uselocale(LC_GLOBAL_LOCALE);
230 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, double& entryValue)
234 result r = GetValue(sectionName, entryName, value);
237 r = Double::Parse(value, entryValue);
240 r = E_PARSING_FAILED;
243 uselocale(LC_GLOBAL_LOCALE);
248 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, String& entryValue)
250 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
251 return __pRegistryCore->GetValue(sectionName, entryName, entryValue);
255 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, float entryValue)
258 result r = AddValue(sectionName, entryName, Float::ToString(entryValue));
259 uselocale(LC_GLOBAL_LOCALE);
264 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, double entryValue)
267 result r = AddValue(sectionName, entryName, Double::ToString(entryValue));
268 uselocale(LC_GLOBAL_LOCALE);
273 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, const String& entryValue)
275 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
276 return __pRegistryCore->AddValue(sectionName, entryName, entryValue);
280 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, float entryValue)
283 result r = SetValue(sectionName, entryName, Float::ToString(entryValue));
284 uselocale(LC_GLOBAL_LOCALE);
289 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, double entryValue)
292 result r = SetValue(sectionName, entryName, Double::ToString(entryValue));
293 uselocale(LC_GLOBAL_LOCALE);
298 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, const String& entryValue)
300 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
301 return __pRegistryCore->SetValue(sectionName, entryName, entryValue);
305 _RegistryImpl::RemoveValue(const String& sectionName, const String& entryName)
307 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
308 return __pRegistryCore->RemoveValue(sectionName, entryName);
312 _RegistryImpl::Flush(void)
314 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
315 return __pRegistryCore->Flush();
319 _RegistryImpl::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer* pKey)
321 return _SecureRegistry::ConvertToSecureRegistry(plainRegPath, secureRegPath, pKey);
325 _RegistryImpl::GetSectionIndex(const String& sectionName)
327 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
328 return __pRegistryCore->GetSectionIndex(sectionName);
332 _RegistryImpl::Removekey(const String& sectionName, const String& keyName)
334 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
335 __pRegistryCore->Removekey(sectionName, keyName);
339 _RegistryImpl::GetAllSectionCount(void)
341 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
342 return __pRegistryCore->GetAllSectionCount();
347 _RegistryImpl::GetAllEntryCount(int sectionIndex)
349 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
350 return __pRegistryCore->GetAllEntryCount(sectionIndex);
354 _RegistryImpl::RemoveEntry(int sectionIndex, int entryIndex)
356 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
357 __pRegistryCore->RemoveEntry(sectionIndex, entryIndex);
361 _RegistryImpl::ModifyEntryValue(int sectionIndex, int entryIndex, _RegValueType type, const void* pValue, int size)
363 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
364 __pRegistryCore->ModifyEntryValue(sectionIndex, entryIndex, type, pValue, size);
368 _RegistryImpl::GetEntryIndex(int sectionIndex, const String& entryName)
370 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
371 return __pRegistryCore->GetEntryIndex(sectionIndex, entryName);
375 _RegistryImpl::GetEntryName(int sectionIndex, int entryIndex)
377 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
378 return __pRegistryCore->GetEntryName(sectionIndex, entryIndex);
382 _RegistryImpl::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType type, void* pValue, int* pSize)
384 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
385 if (type == REG_VALUE_TYPE_REAL)
388 __pRegistryCore->GetEntryValue(sectionIndex, entryIndex, type, pValue, pSize);
389 uselocale(LC_GLOBAL_LOCALE);
393 __pRegistryCore->GetEntryValue(sectionIndex, entryIndex, type, pValue, pSize);
398 _RegistryImpl::GetSectionName(int sectionIndex)
400 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
401 return __pRegistryCore->GetSectionName(sectionIndex);
405 _RegistryImpl::GetSectionByIndex(int sectionIndex)
407 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
408 return __pRegistryCore->GetSectionByIndex(sectionIndex);
412 _RegistryImpl::GetInstance(Registry& registry)
414 return registry.__pRegistryImpl;
418 _RegistryImpl::GetInstance(const Registry& registry)
420 return registry.__pRegistryImpl;
424 _RegistryImpl::LockN(FileLockType lockType)
426 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
427 return __pRegistryCore->LockN(lockType);
431 _RegistryImpl::TryToLockN(FileLockType lockType)
433 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
434 return __pRegistryCore->TryToLockN(lockType);