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.
23 #include <unique_ptr.h>
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>
36 #include <FIoRegistry.h>
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>
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Utility;
49 using namespace Tizen::Base::Collection;
51 namespace Tizen { namespace Io
54 static const int _MAX_REG_OPENMODE_LENGTH = 2;
56 _RegistryImpl::_RegistryImpl(void)
57 : __pRegistryCore(null)
61 _RegistryImpl::~_RegistryImpl(void)
63 delete __pRegistryCore;
67 _RegistryImpl::ConvertRegistryOpenMode(long legacyMode, char* pOpenMode)
71 case REG_OPEN_READ_ONLY:
72 strcpy(pOpenMode, "r\0");
74 case REG_OPEN_READ_WRITE:
75 strcpy(pOpenMode, "r+\0");
77 case (REG_OPEN_READ_WRITE | REG_OPEN_CREATE):
78 strcpy(pOpenMode, "a+\0");
81 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode is invalid.");
87 // This method will be removed in a future release.
89 _RegistryImpl::Construct(const String& regPath, long legacyMode, const ByteBuffer* pSecretKey)
91 SysAssertf(__pRegistryCore == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
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.");
96 bool isValid = _RegistryImpl::ConvertRegistryOpenMode(legacyMode, pOpenMode.get());
97 SysTryReturnResult(NID_IO, isValid == true, E_INVALID_ARG, "The specified openMode is invalid.");
99 return Construct(regPath, pOpenMode.get(), pSecretKey);
103 _RegistryImpl::Construct(const String& regPath, const char* pOpenMode, const ByteBuffer* pSecretKey)
105 SysAssertf(__pRegistryCore == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
107 result r = E_SUCCESS;
110 unique_ptr<_NormalRegistry> pNormalRegistry(new (std::nothrow) _NormalRegistry());
111 SysTryReturnResult(NID_IO, pNormalRegistry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
113 r = pNormalRegistry->Construct(regPath, pOpenMode);
116 SysLogException(NID_IO, r, "[%s] Propagated.", GetErrorMessage(r));
119 __pRegistryCore = pNormalRegistry.release();
123 unique_ptr<_SecureRegistry> pSecureRegistry(new (std::nothrow) _SecureRegistry());
124 SysTryReturnResult(NID_IO, pSecureRegistry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
126 r = pSecureRegistry->Construct(regPath, pOpenMode, pSecretKey);
129 SysLogException(NID_IO, r, "[%s] Propagated.", GetErrorMessage(r));
132 __pRegistryCore = pSecureRegistry.release();
139 _RegistryImpl::Remove(const String& regPath)
141 return _FileImpl::Remove(regPath);
145 _RegistryImpl::AddSection(const String& sectionName)
147 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
148 return __pRegistryCore->AddSection(sectionName);
152 _RegistryImpl::RemoveSection(const String& sectionName)
154 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
155 return __pRegistryCore->RemoveSection(sectionName);
159 _RegistryImpl::GetSectionListN(IList** pRetList)
161 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
162 return __pRegistryCore->GetSectionListN(pRetList);
167 _RegistryImpl::GetEntryList(const String& sectionName, Collection::HashMap& retMap)
169 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
170 return __pRegistryCore->GetEntryList(sectionName, retMap);
175 _RegistryImpl::GetEntryListN(const String& sectionName, HashMap** pRetList)
177 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
178 return __pRegistryCore->GetEntryListN(sectionName, pRetList);
182 _RegistryImpl::GetAllEntriesN(const String& sectionName) const
184 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
185 return __pRegistryCore->GetAllEntriesN(sectionName);
189 _RegistryImpl::GetAllEntryNamesN(const String& sectionName) const
191 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
192 return __pRegistryCore->GetAllEntryNamesN(sectionName);
196 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, float& entryValue)
199 result r = __pRegistryCore->GetValue(sectionName, entryName, value);
200 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
202 float fValue = _LocalizedNumParser::ToFloat(value, "C");
204 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_PARSING_FAILED, "Failed to parse the data.");
212 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, double& entryValue)
215 result r = __pRegistryCore->GetValue(sectionName, entryName, value);
216 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Propagated.", GetErrorMessage(r));
218 double dValue = _LocalizedNumParser::ToDouble(value, "C");
220 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_PARSING_FAILED, "Failed to parse the data.");
228 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, String& entryValue)
230 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
231 return __pRegistryCore->GetValue(sectionName, entryName, entryValue);
235 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, float entryValue)
237 return AddValue(sectionName, entryName, _LocalizedNumParser::ToString(entryValue, "C"));
241 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, double entryValue)
243 return AddValue(sectionName, entryName, _LocalizedNumParser::ToString(entryValue, "C"));
247 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, const String& entryValue)
249 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
250 return __pRegistryCore->AddValue(sectionName, entryName, entryValue);
254 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, float entryValue)
256 return SetValue(sectionName, entryName, _LocalizedNumParser::ToString(entryValue, "C"));
260 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, double entryValue)
262 return SetValue(sectionName, entryName, _LocalizedNumParser::ToString(entryValue, "C"));
266 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, const String& entryValue)
268 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
269 return __pRegistryCore->SetValue(sectionName, entryName, entryValue);
273 _RegistryImpl::RemoveValue(const String& sectionName, const String& entryName)
275 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
276 return __pRegistryCore->RemoveValue(sectionName, entryName);
280 _RegistryImpl::Flush(void)
282 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
283 return __pRegistryCore->Flush();
287 _RegistryImpl::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer* pKey)
289 return _SecureRegistry::ConvertToSecureRegistry(plainRegPath, secureRegPath, pKey);
293 _RegistryImpl::GetSectionIndex(const String& sectionName)
295 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
296 return __pRegistryCore->GetSectionIndex(sectionName);
300 _RegistryImpl::Removekey(const String& sectionName, const String& keyName)
302 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
303 __pRegistryCore->Removekey(sectionName, keyName);
307 _RegistryImpl::GetAllSectionCount(void)
309 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
310 return __pRegistryCore->GetAllSectionCount();
315 _RegistryImpl::GetAllEntryCount(int sectionIndex)
317 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
318 return __pRegistryCore->GetAllEntryCount(sectionIndex);
322 _RegistryImpl::RemoveEntry(int sectionIndex, int entryIndex)
324 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
325 __pRegistryCore->RemoveEntry(sectionIndex, entryIndex);
329 _RegistryImpl::ModifyEntryValue(int sectionIndex, int entryIndex, _RegValueType type, const void* pValue, int size)
331 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
332 __pRegistryCore->ModifyEntryValue(sectionIndex, entryIndex, type, pValue, size);
336 _RegistryImpl::GetEntryIndex(int sectionIndex, const String& entryName)
338 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
339 return __pRegistryCore->GetEntryIndex(sectionIndex, entryName);
343 _RegistryImpl::GetEntryName(int sectionIndex, int entryIndex)
345 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
346 return __pRegistryCore->GetEntryName(sectionIndex, entryIndex);
350 _RegistryImpl::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType type, void* pValue, int* pSize)
352 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
354 __pRegistryCore->GetEntryValue(sectionIndex, entryIndex, type, pValue, pSize);
358 _RegistryImpl::GetSectionName(int sectionIndex)
360 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
361 return __pRegistryCore->GetSectionName(sectionIndex);
365 _RegistryImpl::GetSectionByIndex(int sectionIndex)
367 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
368 return __pRegistryCore->GetSectionByIndex(sectionIndex);
372 _RegistryImpl::GetInstance(Registry& registry)
374 return registry.__pRegistryImpl;
378 _RegistryImpl::GetInstance(const Registry& registry)
380 return registry.__pRegistryImpl;
384 _RegistryImpl::LockN(FileLockType lockType)
386 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
387 return __pRegistryCore->LockN(lockType);
391 _RegistryImpl::TryToLockN(FileLockType lockType)
393 SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
394 return __pRegistryCore->TryToLockN(lockType);