2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIo_RegistryImpl.cpp
19 * @brief This is the implementation file for _RegistryImpl class.
24 #include <unique_ptr.h>
29 #include <FBaseInteger.h>
30 #include <FBaseDouble.h>
31 #include <FBaseFloat.h>
32 #include <FBaseUuId.h>
33 #include <FBaseByteBuffer.h>
34 #include <FBaseColHashMap.h>
35 #include <FBaseColIEnumerator.h>
36 #include <FBaseUtilStringTokenizer.h>
37 #include <FBaseResult.h>
38 #include <FBaseSysLog.h>
40 #include <FIoRegistry.h>
42 #include <FBase_StringConverter.h>
43 #include <FIo_FileImpl.h>
44 #include <FIo_RegistryImpl.h>
45 #include <FIo_NormalRegistry.h>
46 #include <FIo_SecureRegistry.h>
47 #include <FIo_SecureIoUtil.h>
50 using namespace Tizen::Base;
51 using namespace Tizen::Base::Utility;
52 using namespace Tizen::Base::Collection;
54 namespace Tizen { namespace Io
57 static const int _MAX_REG_OPENMODE_LENGTH = 2;
59 _RegistryImpl::_RegistryImpl(void)
60 : __pRegistryCore(null)
65 _RegistryImpl::~_RegistryImpl(void)
67 delete __pRegistryCore;
68 if (__loc != (locale_t)0)
75 _RegistryImpl::ConvertRegistryOpenMode(long legacyMode, char* pOpenMode)
79 case REG_OPEN_READ_ONLY:
80 strcpy(pOpenMode, "r\0");
82 case REG_OPEN_READ_WRITE:
83 strcpy(pOpenMode, "r+\0");
85 case (REG_OPEN_READ_WRITE | REG_OPEN_CREATE):
86 strcpy(pOpenMode, "a+\0");
89 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode is invalid.");
95 // This method will be removed in a future release.
97 _RegistryImpl::Construct(const String& regPath, long legacyMode, const ByteBuffer* pSecretKey)
99 SysAssertf(__pRegistryCore == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
101 unique_ptr<char[]> pOpenMode(new char[_MAX_REG_OPENMODE_LENGTH + 1]);
102 SysTryReturnResult(NID_IO, pOpenMode != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
104 bool isValid = _RegistryImpl::ConvertRegistryOpenMode(legacyMode, pOpenMode.get());
105 SysTryReturnResult(NID_IO, isValid == true, E_INVALID_ARG, "The specified openMode is invalid.");
107 return Construct(regPath, pOpenMode.get(), pSecretKey);
111 _RegistryImpl::Construct(const String& regPath, const char* pOpenMode, const ByteBuffer* pSecretKey)
113 SysAssertf(__pRegistryCore == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
115 result r = E_SUCCESS;
118 unique_ptr<_NormalRegistry> pNormalRegistry(new (std::nothrow) _NormalRegistry());
119 SysTryReturnResult(NID_IO, pNormalRegistry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
121 r = pNormalRegistry->Construct(regPath, pOpenMode);
124 SysLogException(NID_IO, r, "[%s] Propagated.", GetErrorMessage(r));
127 __pRegistryCore = pNormalRegistry.release();
131 unique_ptr<_SecureRegistry> pSecureRegistry(new (std::nothrow) _SecureRegistry());
132 SysTryReturnResult(NID_IO, pSecureRegistry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
134 r = pSecureRegistry->Construct(regPath, pOpenMode, pSecretKey);
137 SysLogException(NID_IO, r, "[%s] Propagated.", GetErrorMessage(r));
140 __pRegistryCore = pSecureRegistry.release();
143 __loc = newlocale(LC_ALL_MASK, "C", (locale_t)0);
144 if (__loc == (locale_t)0)
146 SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to create locale object. errno: %d (%s)",
147 errno, strerror(errno));
154 _RegistryImpl::Remove(const String& regPath)
156 return _FileImpl::Remove(regPath);
160 _RegistryImpl::AddSection(const String& sectionName)
162 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
163 return __pRegistryCore->AddSection(sectionName);
167 _RegistryImpl::RemoveSection(const String& sectionName)
169 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
170 return __pRegistryCore->RemoveSection(sectionName);
174 _RegistryImpl::GetSectionListN(IList** pRetList)
176 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
177 return __pRegistryCore->GetSectionListN(pRetList);
182 _RegistryImpl::GetEntryList(const String& sectionName, Collection::HashMap& retMap)
184 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
185 return __pRegistryCore->GetEntryList(sectionName, retMap);
190 _RegistryImpl::GetEntryListN(const String& sectionName, HashMap** pRetList)
192 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
193 return __pRegistryCore->GetEntryListN(sectionName, pRetList);
197 _RegistryImpl::GetAllEntriesN(const String& sectionName) const
199 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
200 return __pRegistryCore->GetAllEntriesN(sectionName);
204 _RegistryImpl::GetAllEntryNamesN(const String& sectionName) const
206 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
207 return __pRegistryCore->GetAllEntryNamesN(sectionName);
211 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, float& entryValue)
215 result r = GetValue(sectionName, entryName, value);
218 r = Float::Parse(value, entryValue);
221 r = E_PARSING_FAILED;
224 uselocale(LC_GLOBAL_LOCALE);
229 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, double& entryValue)
233 result r = GetValue(sectionName, entryName, value);
236 r = Double::Parse(value, entryValue);
239 r = E_PARSING_FAILED;
242 uselocale(LC_GLOBAL_LOCALE);
247 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, String& entryValue)
249 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
250 return __pRegistryCore->GetValue(sectionName, entryName, entryValue);
254 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, float entryValue)
257 result r = AddValue(sectionName, entryName, Float::ToString(entryValue));
258 uselocale(LC_GLOBAL_LOCALE);
263 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, double entryValue)
266 result r = AddValue(sectionName, entryName, Double::ToString(entryValue));
267 uselocale(LC_GLOBAL_LOCALE);
272 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, const String& entryValue)
274 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
275 return __pRegistryCore->AddValue(sectionName, entryName, entryValue);
279 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, float entryValue)
282 result r = SetValue(sectionName, entryName, Float::ToString(entryValue));
283 uselocale(LC_GLOBAL_LOCALE);
288 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, double entryValue)
291 result r = SetValue(sectionName, entryName, Double::ToString(entryValue));
292 uselocale(LC_GLOBAL_LOCALE);
297 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, const String& entryValue)
299 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
300 return __pRegistryCore->SetValue(sectionName, entryName, entryValue);
304 _RegistryImpl::RemoveValue(const String& sectionName, const String& entryName)
306 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
307 return __pRegistryCore->RemoveValue(sectionName, entryName);
311 _RegistryImpl::Flush(void)
313 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
314 return __pRegistryCore->Flush();
318 _RegistryImpl::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer* pKey)
320 return _SecureRegistry::ConvertToSecureRegistry(plainRegPath, secureRegPath, pKey);
324 _RegistryImpl::GetSectionIndex(const String& sectionName)
326 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
327 return __pRegistryCore->GetSectionIndex(sectionName);
331 _RegistryImpl::Removekey(const String& sectionName, const String& keyName)
333 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
334 __pRegistryCore->Removekey(sectionName, keyName);
338 _RegistryImpl::GetAllSectionCount(void)
340 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
341 return __pRegistryCore->GetAllSectionCount();
346 _RegistryImpl::GetAllEntryCount(int sectionIndex)
348 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
349 return __pRegistryCore->GetAllEntryCount(sectionIndex);
353 _RegistryImpl::RemoveEntry(int sectionIndex, int entryIndex)
355 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
356 __pRegistryCore->RemoveEntry(sectionIndex, entryIndex);
360 _RegistryImpl::ModifyEntryValue(int sectionIndex, int entryIndex, _RegValueType type, const void* pValue, int size)
362 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
363 __pRegistryCore->ModifyEntryValue(sectionIndex, entryIndex, type, pValue, size);
367 _RegistryImpl::GetEntryIndex(int sectionIndex, const String& entryName)
369 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
370 return __pRegistryCore->GetEntryIndex(sectionIndex, entryName);
374 _RegistryImpl::GetEntryName(int sectionIndex, int entryIndex)
376 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
377 return __pRegistryCore->GetEntryName(sectionIndex, entryIndex);
381 _RegistryImpl::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType type, void* pValue, int* pSize)
383 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
384 if (type == REG_VALUE_TYPE_REAL)
387 __pRegistryCore->GetEntryValue(sectionIndex, entryIndex, type, pValue, pSize);
388 uselocale(LC_GLOBAL_LOCALE);
392 __pRegistryCore->GetEntryValue(sectionIndex, entryIndex, type, pValue, pSize);
397 _RegistryImpl::GetSectionName(int sectionIndex)
399 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
400 return __pRegistryCore->GetSectionName(sectionIndex);
404 _RegistryImpl::GetSectionByIndex(int sectionIndex)
406 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
407 return __pRegistryCore->GetSectionByIndex(sectionIndex);
411 _RegistryImpl::GetInstance(Registry& registry)
413 return registry.__pRegistryImpl;
417 _RegistryImpl::GetInstance(const Registry& registry)
419 return registry.__pRegistryImpl;
423 _RegistryImpl::LockN(FileLockType lockType)
425 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
426 return __pRegistryCore->LockN(lockType);
430 _RegistryImpl::TryToLockN(FileLockType lockType)
432 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
433 return __pRegistryCore->TryToLockN(lockType);