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_RegistryCore.cpp
19 * @brief This is the implementation file for _RegistryCore class.
24 #include <unique_ptr.h>
28 #include <FBaseInteger.h>
29 #include <FBaseDouble.h>
30 #include <FBaseFloat.h>
31 #include <FBaseUuId.h>
32 #include <FBaseByteBuffer.h>
33 #include <FBaseColIEnumerator.h>
34 #include <FBaseUtilStringTokenizer.h>
35 #include <FBaseResult.h>
36 #include <FBaseSysLog.h>
37 #include <FBaseColAllElementsDeleter.h>
39 #include <FIoRegistry.h>
41 #include <FBase_StringConverter.h>
42 #include <FBase_LocalizedNumParser.h>
43 #include <FBase_NativeError.h>
44 #include <FApp_AppInfo.h>
45 #include "FIo_FileImpl.h"
46 #include "FIo_NormalRegistry.h"
47 #include "FIo_SecureRegistry.h"
48 #include "FIo_RegistryCore.h"
49 #include "FIo_SecureIoUtil.h"
52 using namespace Tizen::Base;
53 using namespace Tizen::Base::Utility;
54 using namespace Tizen::Base::Collection;
56 namespace Tizen { namespace Io
59 static const size_t _MAX_REG_OPENMODE_LENGTH = 2;
61 #define _REGISTRY_SECTION_MARKER_CHAR L'#'
62 #define _REGISTRY_SECTION_VALUE_ASSIGNMENT_MARKER_CHAR L'='
64 class _RegistrySection
68 ~_RegistrySection(void);
71 LinkedList __entryList;
72 friend class _RegistryCore;
75 _RegistrySection::~_RegistrySection(void)
77 __entryList.RemoveAll(true);
88 _RegistryCore::_RegistryCore(void)
101 _RegistryCore::~_RegistryCore(void)
107 _RegistryCore::VerifyRegistryOpenMode(const char* pOpenMode)
109 if (pOpenMode == null)
111 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode is null.");
115 if (strlen(pOpenMode) > _MAX_REG_OPENMODE_LENGTH)
117 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
121 switch (pOpenMode[0])
135 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
139 switch (pOpenMode[1])
144 if (pOpenMode[2] == '\0')
152 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
156 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
164 _RegistryCore::Parse(void)
166 result r = E_SUCCESS;
168 bool sectionFound = false;
171 unique_ptr<char[]> pEntryName(null);
172 unique_ptr<char[]> pEntryValue(null);
173 int firstTokenPos = 0;
176 if (_pBuffer == null)
178 SysLog(NID_IO, "The buffer is empty.");
182 String inputStr = String((char*)_pBuffer);
183 StringTokenizer lineTok(inputStr, L"\n");
185 r = lineTok.GetNextToken(line);
187 while (r == E_SUCCESS)
190 if (line.GetLength() == 0)
192 r = lineTok.GetNextToken(line);
196 // find registry section marker
197 line.GetCharAt(0, ch);
198 if (ch == _REGISTRY_SECTION_MARKER_CHAR)
202 // skip '#' and "\n' at the end
203 r = line.SubString(1, line.GetLength() - 1, sectionName);
204 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
206 // if End of the document follows section name without newline character.
207 sectionName.GetCharAt(sectionName.GetLength() - 1, ch);
210 sectionName.SetLength(sectionName.GetLength() - 1); //Remove '\n'
213 // for compatibility check if line contains '\r' at the end
214 sectionName.GetCharAt(sectionName.GetLength() - 1, ch);
217 sectionName.SetLength(sectionName.GetLength() - 1); //Remove '\r'
220 //TODO: check if remaining data in sectionName is valid or not
221 //after removing '#', '\n', and '\r', sectionName should contain at least 1 valid character
223 sectionName.GetLength() > 0, r = E_PARSING_FAILED, E_PARSING_FAILED,
224 "[E_PARSING_FAILED] Section length cannot be <2.");
226 unique_ptr<char[]> pSectionName(_StringConverter::CopyToCharArrayN(sectionName));
227 SysTryCatch(NID_IO, pSectionName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
228 "[E_OUT_OF_MEMORY] The memory is insufficient.");
230 if (!_RegistryCore::CheckSectionName(pSectionName.get()))
232 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
233 "[E_PARSING_FAILED] Section name could not be parsed.");
236 r = _RegistryCore::AddSection(_sectionList, sectionName);
237 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
239 else // not a section.. but may belongs to a section (entry)
241 // ignores blank lines
242 line.GetCharAt(0, ch);
243 if ((ch == L'\n') || (ch == L'\r'))
245 r = lineTok.GetNextToken(line);
250 // Need to check for tabs, spaces in the beginning of each line
252 // if no section found till now.. ignore
253 if (sectionFound == false)
255 r = lineTok.GetNextToken(line);
259 // if End of the document follows entry value without newline character.
260 line.GetCharAt(line.GetLength() - 1, ch);
263 line.SetLength(line.GetLength() - 1); //Remove '\n'
266 line.GetCharAt(line.GetLength() - 1, ch);
269 line.SetLength(line.GetLength() - 1); //Remove '\r'
272 String entryName("");
275 // we will have sectionitem=value (or) sectionitem= (or) sectionitem
276 // as our string. Break this into entry and value
277 StringTokenizer strTok(line, L"=");
279 // extract entry name
280 if (strTok.HasMoreTokens())
282 line.IndexOf(L'=', 0, firstTokenPos); // position of first occurance of '=' in a line
283 if (firstTokenPos == 0)
285 // entryName should not be empty. i.e., "=value" or just '=' is invalid case
286 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
287 "[E_PARSING_FAILED] Entry name cannot be empty.");
291 strTok.GetNextToken(entryName);
294 else // missing '=' in entry line or entry line does not seem to contain "xxx=xxx" format
296 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED, "[E_PARSING_FAILED] invalid entry Line found.");
299 // check if entry name contains invalid chars
300 pEntryName.reset(_StringConverter::CopyToCharArrayN(entryName));
301 SysTryCatch(NID_IO, pEntryName, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
302 "[E_OUT_OF_MEMORY] The memory is insufficient.");
304 if (!_RegistryCore::CheckEntryName(pEntryName.get()))
306 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
307 "[E_PARSING_FAILED] Entry name could not be parsed.");
310 line.SubString(firstTokenPos + 1, entryVal); // extract entry value
312 // check if entry value contains invalid chars
313 pEntryValue.reset(_StringConverter::CopyToCharArrayN(entryVal));
314 SysTryCatch(NID_IO, pEntryValue, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
315 "[E_OUT_OF_MEMORY] The memory is insufficient.");
317 length = strlen(pEntryValue.get());
320 if (!_RegistryCore::CheckEntryValue(pEntryValue.get()))
322 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
323 "[E_PARSING_FAILED] Entry value could not be parsed.");
327 // add the entry and value
328 r = _RegistryCore::AddValue(_sectionList, sectionName, entryName, entryVal);
329 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
331 r = lineTok.GetNextToken(line);
334 if (r == E_OUT_OF_RANGE)
342 if (r == E_PARSING_FAILED && Tizen::App::_AppInfo::IsOspCompat() == false)
344 r = E_INVALID_FORMAT;
347 SysLog(NID_IO, "[%s] exception occurred.", GetErrorMessage(r));
352 _RegistryCore::Load(const String& regPath, const char* pOpenMode)
354 result r = E_SUCCESS;
355 delete[] _pBuffer; //clear existing buffer
359 // open registry file
360 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl());
361 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
363 r = pFileImpl->Construct(regPath, pOpenMode, null);
364 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Constructing the file has failed.", GetErrorMessage(r));
366 // reset the section list to be sure that its empty
367 _sectionList.RemoveAll(true);
369 if (_truncate == false)
371 // load the registry file
372 r = pFileImpl->ReadN((char**)&_pBuffer, _length);
379 __pFileImpl = pFileImpl.release();
385 _RegistryCore::AddSection(const String& sectionName)
387 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
389 result r = E_SUCCESS;
390 bool sectionFound = false;
392 // Get section information
393 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
396 return GetLastResult();
399 while (pEnum->MoveNext() == E_SUCCESS)
401 _RegistrySection* pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
402 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
403 GetErrorMessage(GetLastResult()));
405 if (pRegSection->__sectionName == sectionName)
412 if (sectionFound == true)
414 return E_SECTION_ALREADY_EXIST;
417 // create a key with section name
418 unique_ptr<_RegistrySection> pRegSection(new (std::nothrow) _RegistrySection());
419 SysTryReturnResult(NID_IO, pRegSection != null, E_OUT_OF_MEMORY,
420 "The memory is insufficient.");
422 pRegSection->__sectionName = sectionName;
424 // add new section and section-entry map
425 r = _sectionList.Add(*(pRegSection.release()));
426 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
433 _RegistryCore::RemoveSection(const String& sectionName)
435 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
437 result r = E_SUCCESS;
438 _RegistrySection* pRegSection = null;
439 bool sectionFound = false;
441 if (_sectionList.GetCount() == 0)
443 return E_SECTION_NOT_FOUND;
446 // Get section information
447 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
450 return GetLastResult();
453 while (pEnum->MoveNext() == E_SUCCESS)
455 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
456 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagating.",
457 GetErrorMessage(GetLastResult()));
459 if (pRegSection->__sectionName == sectionName)
466 if (sectionFound == false)
468 return E_SECTION_NOT_FOUND;
471 r = _sectionList.Remove(*pRegSection, true);
472 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
479 _RegistryCore::GetSectionListN(IList** pRetList)
481 result r = E_SUCCESS;
482 unique_ptr<IEnumerator> pEnum(null);
483 _RegistrySection* pSection = null;
485 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
486 SysTryReturnResult(NID_IO, pRetList != null, E_INVALID_ARG, "pList cannot be null.");
488 // construct an array list to be returned
489 unique_ptr<ArrayList, AllElementsDeleter> pSectionList(new (std::nothrow) ArrayList());
490 SysTryReturnResult(NID_IO, pSectionList != null, E_OUT_OF_MEMORY,
491 "The memory is insufficient.");
493 r = pSectionList->Construct();
494 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
496 // if no sectons, return empty list
497 if (_sectionList.GetCount() == 0)
499 *pRetList = pSectionList.release();
503 // copy from class section list to the new list and return
504 pEnum.reset(_sectionList.GetEnumeratorN());
505 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.", GetErrorMessage(GetLastResult()));
507 while (pEnum->MoveNext() == E_SUCCESS)
509 pSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
510 SysTryReturn(NID_IO, pSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
511 GetErrorMessage(GetLastResult()));
513 unique_ptr<String> pSectionName(new (std::nothrow) String(pSection->__sectionName));
514 SysTryReturnResult(NID_IO, pSectionName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
516 r = pSectionList->Add(*(pSectionName.release()));
517 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
520 *pRetList = pSectionList.release();
526 _RegistryCore::GetEntryList(const String& sectionName, Collection::HashMap& retMap)
528 result r = E_SUCCESS;
529 unique_ptr<IEnumerator> pEnum(null);
530 unique_ptr<String> pKeyEntryStr(null);
531 unique_ptr<String> pKeyValStr(null);
532 bool sectionFound = false;
533 _RegistrySection* pRegSection = null;
534 _RegistryEntry* pRegEntry = null;
536 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
538 if (_sectionList.GetCount() == 0)
540 return E_SECTION_NOT_FOUND;
543 // Get section information
544 pEnum.reset(_sectionList.GetEnumeratorN());
545 SysTryReturnResult(NID_IO, pEnum != null, E_SYSTEM, "Section list is empty.");
547 while (pEnum->MoveNext() == E_SUCCESS)
549 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
550 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
551 GetErrorMessage(GetLastResult()));
553 if (pRegSection->__sectionName == sectionName)
560 if (sectionFound == false)
562 return E_SECTION_NOT_FOUND;
565 // copy from item list to the new list and return
566 pEnum.reset((pRegSection->__entryList).GetEnumeratorN());
567 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
568 GetErrorMessage(GetLastResult()));
570 while (pEnum->MoveNext() == E_SUCCESS)
572 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
573 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
574 GetErrorMessage(GetLastResult()));
576 pKeyEntryStr.reset(new (std::nothrow) String((pRegEntry->entryName)));
577 SysTryCatch(NID_IO, pKeyEntryStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
578 "[E_OUT_OF_MEMORY] The memory is insufficient.");
579 pKeyValStr.reset(new (std::nothrow) String((pRegEntry->entryValue)));
580 SysTryCatch(NID_IO, pKeyValStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
581 "[E_OUT_OF_MEMORY] The memory is insufficient.");
583 r = retMap.Add(*(pKeyEntryStr.release()), *(pKeyValStr.release()));
584 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
590 retMap.RemoveAll(true);
596 _RegistryCore::GetEntryListN(const String& sectionName, HashMap** pRetList)
598 result r = E_SUCCESS;
600 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
601 SysTryReturnResult(NID_IO, pRetList != null, E_INVALID_ARG, "pRetList cannot be null.");
603 // construct an array list to be returned
604 unique_ptr<HashMap, AllElementsDeleter> pEntryList(new (std::nothrow) HashMap());
605 SysTryReturnResult(NID_IO, pEntryList != null, E_OUT_OF_MEMORY,
606 "The memory is insufficient.");
607 r = pEntryList->Construct();
608 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
609 r = GetEntryList(sectionName, *(pEntryList.get()));
610 *pRetList = pEntryList.release();
616 _RegistryCore::GetAllEntriesN(const String& sectionName)
618 result r = E_SUCCESS;
619 HashMap* pMap = null;
621 r = GetEntryListN(sectionName, &pMap);
622 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
624 SetLastResult(E_SUCCESS);
633 _RegistryCore::GetAllEntryNamesN(const String& sectionName)
635 result r = E_SUCCESS;
637 unique_ptr<ArrayList, AllElementsDeleter> pEntryList(new (std::nothrow) ArrayList());
638 SysTryReturn(NID_IO, pEntryList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
640 r = pEntryList->Construct();
641 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
643 unique_ptr<IMap, AllElementsDeleter> pMap(this->GetAllEntriesN(sectionName));
644 SysTryReturn(NID_IO, pMap != null, null, GetLastResult(), "[%s] Getting all entries was failed.",
645 GetErrorMessage(GetLastResult()));
647 unique_ptr<IMapEnumerator> pMapEnum(pMap->GetMapEnumeratorN());
648 SysTryReturn(NID_IO, pMapEnum, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
649 while (pMapEnum->MoveNext() == E_SUCCESS)
651 String* pKey = dynamic_cast< String* >(pMapEnum->GetKey());
652 SysTryReturn(NID_IO, pKey != null, null, E_IO, "[E_OUT_OF_MEMORY] The system error occurred.");
654 String* pEntryName = new (std::nothrow) String(*pKey);
655 SysTryReturn(NID_IO, pEntryName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
657 r = pEntryList->Add(*pEntryName);
658 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
661 SetLastResult(E_SUCCESS);
662 return pEntryList.release();
666 _RegistryCore::GetValue(const String& sectionName, const String& entryName, String& valStr)
668 bool sectionFound = false;
669 bool entryFound = false;
670 _RegistrySection* pRegSection = null;
671 _RegistryEntry* pRegEntry = null;
673 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
675 if (_sectionList.GetCount() == 0)
677 return E_SECTION_NOT_FOUND;
680 // Get section information
681 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
684 return GetLastResult();
687 while (pEnum->MoveNext() == E_SUCCESS)
689 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
690 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
691 GetErrorMessage(GetLastResult()));
693 if (pRegSection->__sectionName == sectionName)
700 if (sectionFound == false)
702 return E_SECTION_NOT_FOUND;
705 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
708 return GetLastResult();
711 while (pEnum->MoveNext() == E_SUCCESS)
713 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
714 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
715 GetErrorMessage(GetLastResult()));
717 if (pRegEntry->entryName == entryName)
719 valStr = pRegEntry->entryValue;
725 SysTryLog(NID_IO, entryFound, "Entry not found");
727 return((entryFound == true) ? E_SUCCESS : E_KEY_NOT_FOUND);
731 _RegistryCore::AddValue(const String& sectionName, const String& entryName, const String& valStr)
733 result r = E_SUCCESS;
734 bool sectionFound = false;
735 bool entryFound = false;
736 _RegistrySection* pRegSection = null;
739 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
741 // Get section information
742 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
745 return GetLastResult();
748 while (pEnum->MoveNext() == E_SUCCESS)
750 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
751 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
752 GetErrorMessage(GetLastResult()));
754 if (pRegSection->__sectionName == sectionName)
761 if (sectionFound == false)
763 return E_SECTION_NOT_FOUND;
766 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
769 return GetLastResult();
772 while (pEnum->MoveNext() == E_SUCCESS)
774 _RegistryEntry* pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
775 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
776 GetErrorMessage(GetLastResult()));
778 if (pRegEntry->entryName == entryName)
785 if (entryFound == true)
787 return E_KEY_ALREADY_EXIST;
790 unique_ptr<_RegistryEntry> pRegEntry(new (std::nothrow) _RegistryEntry());
791 SysTryReturnResult(NID_IO, pRegEntry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
793 unique_ptr<char[]> pEntryName(_StringConverter::CopyToCharArrayN(entryName));
794 SysTryReturnResult(NID_IO, pEntryName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
796 length = strlen(pEntryName.get());
797 if (!_RegistryCore::CheckEntryName(pEntryName.get()))
799 SysTryReturnResult(NID_IO, false, E_PARSING_FAILED, "Entry name could not be parsed.");
802 unique_ptr<char[]> pEntryValue(_StringConverter::CopyToCharArrayN(valStr));
803 SysTryReturnResult(NID_IO, pEntryValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
805 length = strlen(pEntryValue.get());
806 if (length > 0 && !_RegistryCore::CheckEntryValue(pEntryValue.get()))
808 SysTryReturnResult(NID_IO, false, E_PARSING_FAILED, "Entry value could not be parsed.");
811 pRegEntry->entryName = entryName;
812 pRegEntry->entryValue = valStr;
814 r = pRegSection->__entryList.Add(*(pRegEntry.release()));
815 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
822 _RegistryCore::SetValue(const String& sectionName, const String& entryName, const String& valStr)
824 result r = E_SUCCESS;
825 bool sectionFound = false;
826 bool entryFound = false;
827 _RegistryEntry* pRegEntry = null;
828 _RegistrySection* pRegSection = null;
832 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
834 if (_sectionList.GetCount() == 0)
836 return E_SECTION_NOT_FOUND;
839 // Get section information
840 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
843 return GetLastResult();
846 while (pEnum->MoveNext() == E_SUCCESS)
848 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
849 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
850 GetErrorMessage(GetLastResult()));
852 if (pRegSection->__sectionName == sectionName)
859 if (sectionFound == false)
861 return E_SECTION_NOT_FOUND;
864 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
867 return GetLastResult();
870 while (pEnum->MoveNext() == E_SUCCESS)
872 pRegEntry = dynamic_cast< _RegistryEntry* >(pEnum->GetCurrent());
873 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
874 GetErrorMessage(GetLastResult()));
876 if (pRegEntry->entryName == entryName)
884 if (entryFound == false)
886 return E_KEY_NOT_FOUND;
889 if (valStr.GetLength() > 0)
891 unique_ptr<char[]> pEntryValue(_StringConverter::CopyToCharArrayN(valStr));
892 SysTryReturnResult(NID_IO, pEntryValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
894 length = strlen(pEntryValue.get());
895 if (!_RegistryCore::CheckEntryValue(pEntryValue.get()))
897 return E_PARSING_FAILED;
901 pRegEntry->entryValue = valStr;
902 r = pRegSection->__entryList.SetAt(*pRegEntry, entryIndex);
903 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
910 _RegistryCore::RemoveValue(const String& sectionName, const String& entryName)
912 result r = E_SUCCESS;
913 bool sectionFound = false;
914 bool entryFound = false;
915 _RegistryEntry* pRegEntry = null;
916 _RegistrySection* pRegSection = null;
918 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
920 if (_sectionList.GetCount() <= 0)
922 return E_SECTION_NOT_FOUND;
925 // Get section information
926 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
929 return E_SECTION_NOT_FOUND;
932 while (pEnum->MoveNext() == E_SUCCESS)
934 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
935 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
936 GetErrorMessage(GetLastResult()));
938 if (pRegSection->__sectionName == sectionName)
945 if (sectionFound == false)
947 return E_SECTION_NOT_FOUND;
950 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
953 return E_SECTION_NOT_FOUND;
956 while (pEnum->MoveNext() == E_SUCCESS)
958 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
959 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
960 GetErrorMessage(GetLastResult()));
962 if (pRegEntry->entryName == entryName)
969 if (entryFound == false)
971 return E_KEY_NOT_FOUND;
974 r = pRegSection->__entryList.Remove(*pRegEntry, true);
975 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
982 _RegistryCore::Flush(void)
988 _RegistryCore::Write(void)
990 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
992 if (_pBuffer == null)
994 SysLog(NID_IO, "The buffer is empty.");
998 result r = E_SUCCESS;
1000 String openMode(L"w+");
1001 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
1002 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1004 //TODO: Secure mode is to be handled
1005 r = pFileImpl->Construct(_regPath, "w+", null);
1006 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1008 r = pFileImpl->Write(_pBuffer, _length);
1009 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1011 r = pFileImpl->Flush();
1012 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1018 _RegistryCore::PrepareToWrite(void)
1020 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1022 result r = E_SUCCESS;
1023 _RegistryEntry* pRegEntry = null;
1024 _RegistrySection* pRegSection = null;
1027 delete[] _pBuffer; //clear existing buffer
1031 if (_write == false)
1033 SysLog(NID_IO, "[E_ILLEGAL_ACCESS] Registry cannot be flushed as it was opened in read only mode.");
1034 return E_ILLEGAL_ACCESS;
1037 // if no sections, do nothing
1038 if (_sectionList.GetCount() == 0)
1040 unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(_regPath));
1041 int res = truncate(pFilePath.get(), 0);
1042 SysTryReturnResult(NID_IO, res == 0, __ConvertNativeErrorToResult(errno),
1043 "Failed to truncate. errno: %d (%s)", errno, strerror(errno));
1047 // write the registry map to a file
1048 // Get section information
1049 unique_ptr<IEnumerator> pSectionEnum(_sectionList.GetEnumeratorN());
1050 SysTryReturn(NID_IO, pSectionEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.",
1051 GetErrorMessage(GetLastResult()));
1053 while (pSectionEnum->MoveNext() == E_SUCCESS)
1056 pRegSection = dynamic_cast <_RegistrySection*>(pSectionEnum->GetCurrent());
1057 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1058 GetErrorMessage(GetLastResult()));
1060 // write section name to file
1061 r = inputBuffer.Append(String(L"#" + pRegSection->__sectionName + String(L'\n')));
1062 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1064 // read the entries for this section
1065 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1066 SysTryReturn(NID_IO, pSectionEnum != null, GetLastResult(), GetLastResult(), "[%s] Entry list is empty.",
1067 GetErrorMessage(GetLastResult()));
1069 while (pEntryEnum->MoveNext() == E_SUCCESS)
1071 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1072 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1073 GetErrorMessage(GetLastResult()));
1075 // write entry name to file
1076 r = inputBuffer.Append(pRegEntry->entryName + String(L"=" + pRegEntry->entryValue + String(L'\n')));
1077 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1082 _pBuffer = (byte*)_StringConverter::CopyToCharArrayN(inputBuffer);
1083 SysTryReturn(NID_IO, _pBuffer != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1084 GetErrorMessage(GetLastResult()));
1086 _length = strlen((char*)_pBuffer);
1092 _RegistryCore::GetSectionIndex(const String& sectionName)
1094 bool sectionFound = false;
1095 _RegistrySection* pRegSection = null;
1098 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1099 SysTryReturn(NID_IO, sectionName.GetLength() > 0, -1, E_INVALID_ARG,
1100 "[E_INVALID_ARG] sectionName.GetLength() <= 0");
1102 SysTryReturn(NID_IO, _sectionList.GetCount() > 0, -1, E_SECTION_NOT_FOUND, "[E_SECTION_NOT_FOUND] Section not found.");
1104 // Get section information
1105 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
1106 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.",
1107 GetErrorMessage(GetLastResult()));
1109 while (pEnum->MoveNext() == E_SUCCESS)
1111 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1112 SysTryReturn(NID_IO, pRegSection != null, -1, GetLastResult(), "[%s] Propagated.",
1113 GetErrorMessage(GetLastResult()));
1115 if (pRegSection->__sectionName == sectionName)
1117 sectionFound = true;
1123 if (sectionFound == false)
1125 return E_SECTION_NOT_FOUND;
1128 SetLastResult(E_SUCCESS);
1133 _RegistryCore::Removekey(const String& sectionName, const String& keyName)
1135 result r = E_SUCCESS;
1136 int sectionIndex = -1;
1137 int entryIndex = -1;
1139 sectionIndex = GetSectionIndex(sectionName);
1140 r = GetLastResult();
1141 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1143 entryIndex = GetEntryIndex(sectionIndex, keyName);
1144 r = GetLastResult();
1145 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1147 RemoveEntry(sectionIndex, entryIndex);
1148 r = GetLastResult();
1149 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1151 SetLastResult(E_SUCCESS);
1156 _RegistryCore::GetAllSectionCount(void)
1158 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1160 SetLastResult(E_SUCCESS);
1161 return _sectionList.GetCount();
1165 _RegistryCore::GetAllEntryCount(int sectionIndex)
1167 _RegistrySection* pRegSection = null;
1170 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1171 SysTryReturn(NID_IO, sectionIndex >= 0, 0, E_INVALID_ARG, "[E_INVALID_ARG] sectionIndex < 0.");
1172 listSize = _sectionList.GetCount();
1173 SysTryReturn(NID_IO, listSize > 0, 0, E_SECTION_NOT_FOUND, "[E_INVALID_ARG] section listSize <= 0.");
1174 SysTryReturn(NID_IO, sectionIndex >= listSize, 0, E_SECTION_NOT_FOUND,
1175 "[E_INVALID_ARG] sectionIndex > listSize.");
1177 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1178 SysTryReturn(NID_IO, pRegSection != null, 0, E_SECTION_NOT_FOUND,
1179 "[E_SECTION_NOT_FOUND] section was not found.");
1181 SetLastResult(E_SUCCESS);
1182 return pRegSection->__entryList.GetCount();
1186 _RegistryCore::RemoveEntry(int sectionIndex, int entryIndex)
1188 result r = E_SUCCESS;
1190 int entryListSize = 0;
1191 _RegistrySection* pRegSection = null;
1193 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1194 SysTryReturnVoidResult(NID_IO, sectionIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG] sectionIndex < 0.");
1195 SysTryReturnVoidResult(NID_IO, entryIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG] entryIndex < 0.");
1197 listSize = _sectionList.GetCount();
1198 SysTryReturnVoidResult(NID_IO, listSize > 0, E_SECTION_NOT_FOUND,
1199 "[E_SECTION_NOT_FOUND] section listSize <= 0.");
1200 SysTryReturnVoidResult(NID_IO, sectionIndex < listSize, E_SECTION_NOT_FOUND,
1201 "[E_SECTION_NOT_FOUND] sectionIndex >= listSize.");
1203 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1204 SysTryReturnVoidResult(NID_IO, pRegSection != null, E_SECTION_NOT_FOUND,
1205 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1207 r = GetLastResult();
1208 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r,"[%s] Propagated.", GetErrorMessage(r));
1210 entryListSize = pRegSection->__entryList.GetCount();
1211 SysTryReturnVoidResult(NID_IO, (entryListSize > 0 || entryIndex < entryListSize), E_KEY_NOT_FOUND,
1212 "[E_KEY_NOT_FOUND] entryListSize is 0 or entryIndex >= entryListSize.");
1214 r = pRegSection->__entryList.RemoveAt(entryIndex, true);
1215 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1217 SetLastResult(E_SUCCESS);
1221 _RegistryCore::ModifyEntryValue(int sectionIndex, int entryIndex, _RegValueType type, const void* pValue, int size)
1223 SetLastResult(E_UNSUPPORTED_OPERATION);
1227 _RegistryCore::GetEntryIndex(int sectionIndex, const String& entryName)
1229 _RegistrySection* pRegSection = null;
1230 _RegistryEntry* pRegEntry = null;
1232 bool entryFound = false;
1233 int entryIndex = -1;
1235 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1236 SysTryReturn(NID_IO, sectionIndex >= 0, -1, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1237 SysTryReturn(NID_IO, entryName.GetLength() > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG]entryName length < 0.");
1239 listSize = _sectionList.GetCount();
1240 SysTryReturn(NID_IO, listSize > 0, -1, E_SECTION_NOT_FOUND,
1241 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1242 SysTryReturn(NID_IO, sectionIndex < listSize, -1, E_SECTION_NOT_FOUND,
1243 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1245 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1246 SysTryReturn(NID_IO, pRegSection != null, -1, E_SECTION_NOT_FOUND,
1247 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1249 // read the entries for this section
1250 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1251 SysTryReturn(NID_IO, pEntryEnum != null, -1, GetLastResult(), "[%s] Entry list is empty.",
1252 GetErrorMessage(GetLastResult()));
1255 while (pEntryEnum->MoveNext() == E_SUCCESS)
1257 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1258 SysTryReturn(NID_IO, pRegEntry != null, -1, GetLastResult(), "[%s] Propagated.",
1259 GetErrorMessage(GetLastResult()));
1261 if (entryName == pRegEntry->entryName)
1269 SysTryReturn(NID_IO, entryFound != false, -1, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] entry was not found.");
1271 SetLastResult(E_SUCCESS);
1276 _RegistryCore::GetEntryName(int sectionIndex, int entryIndex)
1278 _RegistrySection* pRegSection = null;
1279 _RegistryEntry* pRegEntry = null;
1284 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1285 SysTryReturn(NID_IO, sectionIndex >= 0, empty, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1286 SysTryReturn(NID_IO, entryIndex >= 0, empty, E_INVALID_ARG, "[E_INVALID_ARG]entryIndex < 0.");
1288 listSize = _sectionList.GetCount();
1289 SysTryReturn(NID_IO, listSize > 0, empty, E_SECTION_NOT_FOUND,
1290 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1291 SysTryReturn(NID_IO, sectionIndex < listSize, empty, E_SECTION_NOT_FOUND,
1292 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1294 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1295 SysTryReturn(NID_IO, pRegSection != null, empty, E_SECTION_NOT_FOUND,
1296 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1298 // read the entries for this section
1299 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1300 SysTryReturn(NID_IO, pEntryEnum != null, empty, GetLastResult(), "[%s] Entry list is empty.",
1301 GetErrorMessage(GetLastResult()));
1303 tmpIndex = entryIndex;
1304 while (pEntryEnum->MoveNext() == E_SUCCESS)
1308 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1309 SysTryReturn(NID_IO, pRegEntry != null, empty, GetLastResult(), "[%s] Propagated.",
1310 GetErrorMessage(GetLastResult()));
1317 SysTryReturn(NID_IO, pRegEntry != null, empty, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] entry was not found.");
1319 SetLastResult(E_SUCCESS);
1320 return pRegEntry->entryName;
1324 _RegistryCore::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType type, void* pValue, int* pSize)
1326 _RegistrySection* pRegSection = null;
1327 _RegistryEntry* pRegEntry = null;
1328 result r = E_SUCCESS;
1330 int tmpEntryIndex = -1;
1331 int entryListSize = 0;
1332 String strValueEncoded;
1334 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1335 SysTryReturnVoidResult(NID_IO, sectionIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1336 SysTryReturnVoidResult(NID_IO, type >= 0 && type <= REG_VALUE_TYPE_MAX, E_INVALID_ARG,
1337 "[E_INVALID_ARG] invalid registry type.");
1338 SysTryReturnVoidResult(NID_IO, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG]pValue is null.");
1339 SysTryReturnVoidResult(NID_IO, pSize != null, E_INVALID_ARG, "[E_INVALID_ARG]pSize is null.");
1340 SysTryReturnVoidResult(NID_IO, *pSize > 0, E_INVALID_ARG, "[E_INVALID_ARG] *pSize < 0.");
1342 listSize = _sectionList.GetCount();
1343 SysTryReturnVoidResult(NID_IO, listSize > 0, E_SECTION_NOT_FOUND,
1344 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1345 SysTryReturnVoidResult(NID_IO, sectionIndex < listSize, E_SECTION_NOT_FOUND,
1346 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1348 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1349 SysTryReturnVoidResult(NID_IO, pRegSection != null, E_SECTION_NOT_FOUND,
1350 "[E_SECTION_NOT_FOUND] section at index (%d) is not available.", sectionIndex);
1352 entryListSize = pRegSection->__entryList.GetCount();
1353 SysTryReturnVoidResult(NID_IO, entryListSize > 0, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry list is empty.");
1354 SysTryReturnVoidResult(NID_IO, entryIndex < entryListSize, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry not found.");
1356 // read the entries for this section
1357 unique_ptr< IEnumerator > pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1358 SysTryReturnVoidResult(NID_IO, pEntryEnum != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND]Entry list is empty.");
1360 tmpEntryIndex = entryIndex;
1361 while (pEntryEnum->MoveNext() == E_SUCCESS)
1363 if (entryIndex == 0)
1365 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1366 SysTryReturnVoidResult(NID_IO, pRegEntry != null, GetLastResult(), "[%s] Propagated.",
1367 GetErrorMessage(GetLastResult()));
1375 SysTryReturnVoidResult(NID_IO, pRegEntry != null, E_KEY_NOT_FOUND,
1376 "[E_KEY_NOT_FOUND] Entry was not found.");
1378 strValueEncoded = pRegEntry->entryValue;
1380 DecodeData(strValueEncoded, type, pValue, pSize);
1381 r = GetLastResult();
1382 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1384 SetLastResult(E_SUCCESS);
1389 _RegistryCore::GetSectionName(int sectionIndex)
1391 _RegistrySection* pRegSection = null;
1392 result r = E_SUCCESS;
1394 String sectionName("");
1396 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1397 SysTryReturn(NID_IO, sectionIndex >= 0, sectionName, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1399 listSize = _sectionList.GetCount();
1400 SysTryReturn(NID_IO, listSize > 0, sectionName, E_SECTION_NOT_FOUND,
1401 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1402 SysTryReturn(NID_IO, sectionIndex < listSize, sectionName, E_SECTION_NOT_FOUND,
1403 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1405 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1406 SysTryReturn(NID_IO, pRegSection != null, sectionName, E_SECTION_NOT_FOUND,
1407 "[E_SECTION_NOT_FOUND] sectionIndex is not available.");
1409 r = GetLastResult();
1410 SysTryReturn(NID_IO, !IsFailed(r), sectionName, r, "[%s] Propagated.", GetErrorMessage(r));
1412 SetLastResult(E_SUCCESS);
1413 return pRegSection->__sectionName;
1417 _RegistryCore::GetSectionByIndex(int sectionIndex)
1419 _RegistrySection* pRegSection = null;
1420 int index = sectionIndex;
1422 // Get section information
1423 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
1424 SysTryReturn(NID_IO, pEnum != null, null, GetLastResult(),
1425 "[%s] section list is empty.", GetErrorMessage(GetLastResult()));
1427 while (pEnum->MoveNext() == E_SUCCESS)
1431 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
1432 SysTryReturn(NID_IO, pRegSection != null, null, GetLastResult(), "[%s] Propagated.",
1433 GetErrorMessage(GetLastResult()));
1440 SysTryReturn(NID_IO, pRegSection != null, null, E_SECTION_NOT_FOUND,
1441 "[E_SECTION_NOT_FOUND] Section was not found.");
1443 SetLastResult(E_SUCCESS);
1449 _RegistryCore::DecodeData(const String& strValueEncoded, _RegValueType type, void* pValue, int* pSize)
1451 SysAssert(strValueEncoded.GetLength() > 0);
1454 SysAssert(pSize && *pSize > 0);
1456 SysTryReturnVoidResult(NID_IO, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument.");
1457 SysTryReturnVoidResult(NID_IO, pSize != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument.");
1461 case REG_VALUE_TYPE_INT: // (-2147483647 - 1) ~ 2147483647
1464 result r = E_SUCCESS;
1466 if (*pSize != sizeof(int))
1468 SetLastResult(E_INVALID_ARG);
1472 r = Integer::Parse(strValueEncoded, retIntVal);
1473 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1475 *(int*) pValue = retIntVal;
1479 case REG_VALUE_TYPE_REAL: // Holds signed IEEE 64-bit (8-byte) double-precision floating-point numbers
1480 // ranging in value from -1.79769313486231570E+308 through -4.94065645841246544E-324
1481 // for negative values and from 4.94065645841246544E-324 through 1.79769313486231570E+308
1482 // for positive values.
1484 double retDoubleVal = 0;
1486 if (*pSize != sizeof(double))
1488 SetLastResult(E_INVALID_ARG);
1492 retDoubleVal = _LocalizedNumParser::ToDouble(strValueEncoded, "C");
1493 result r = GetLastResult();
1494 SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1496 *(double*) pValue = retDoubleVal;
1501 case REG_VALUE_TYPE_STRING:
1503 // if(*pSize != sizeof(String))
1505 // SetLastResult(E_INVALID_ARG);
1508 *(String*) pValue = strValueEncoded;
1512 case REG_VALUE_TYPE_BINARY:
1514 SetLastResult(E_PARSING_FAILED);
1518 case REG_VALUE_TYPE_UUID:
1521 result r = E_SUCCESS;
1523 r = UuId::Parse(strValueEncoded, retUuIdVal);
1524 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1526 *(UuId*) pValue = retUuIdVal;
1530 case REG_VALUE_TYPE_UUIDSET:
1532 SetLastResult(E_PARSING_FAILED);
1537 SysAssert(0); // should not happen!
1541 SetLastResult(E_SUCCESS);
1546 _RegistryCore::AddSection(LinkedList& sectionList, const String& sectionName)
1548 result r = E_SUCCESS;
1549 bool sectionFound = false;
1551 // Get section information
1552 unique_ptr<IEnumerator> pEnum(sectionList.GetEnumeratorN());
1555 return GetLastResult();
1558 while (pEnum->MoveNext() == E_SUCCESS)
1560 _RegistrySection* pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1561 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1562 GetErrorMessage(GetLastResult()));
1564 if (pRegSection->__sectionName == sectionName)
1566 sectionFound = true;
1571 if (sectionFound == true)
1573 return E_SECTION_ALREADY_EXIST;
1576 // create a key with section name
1577 unique_ptr<_RegistrySection> pRegSection(new (std::nothrow) _RegistrySection());
1579 SysTryReturnResult(NID_IO, pRegSection != null, E_OUT_OF_MEMORY,
1580 "The memory is insufficient.");
1582 pRegSection->__sectionName = sectionName;
1584 // add new section and section-entry map
1585 r = sectionList.Add(*(pRegSection.release()));
1586 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1592 _RegistryCore::AddValue(LinkedList& sectionList, const String& sectionName, const String& entryName, const String& valStr)
1594 result r = E_SUCCESS;
1595 bool sectionFound = false;
1596 bool entryFound = false;
1597 _RegistrySection* pRegSection = null;
1599 // Get section information
1600 unique_ptr<IEnumerator> pEnum(sectionList.GetEnumeratorN());
1603 return GetLastResult();
1606 while (pEnum->MoveNext() == E_SUCCESS)
1608 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1609 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1610 GetErrorMessage(GetLastResult()));
1612 if (pRegSection->__sectionName == sectionName)
1614 sectionFound = true;
1619 if (sectionFound == false)
1621 return E_SECTION_NOT_FOUND;
1624 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
1627 return GetLastResult();
1630 while (pEnum->MoveNext() == E_SUCCESS)
1632 _RegistryEntry* pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
1633 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1634 GetErrorMessage(GetLastResult()));
1636 if (pRegEntry->entryName == entryName)
1643 if (entryFound == true)
1645 return E_KEY_ALREADY_EXIST;
1648 unique_ptr<_RegistryEntry> pRegEntry(new (std::nothrow) _RegistryEntry());
1649 SysTryReturnResult(NID_IO, pRegEntry != null, E_OUT_OF_MEMORY,
1650 "The memory is insufficient.");
1652 pRegEntry->entryName = entryName;
1653 pRegEntry->entryValue = valStr;
1655 r = pRegSection->__entryList.Add(*(pRegEntry.release()));
1656 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1662 _RegistryCore::CheckSectionName(char* pSectionName)
1669 if (pSectionName == null)
1674 pFrom = pSectionName;
1675 length = strlen(pSectionName);
1676 pTo = pSectionName + (length - 1);
1678 if (!pFrom || !pTo || pFrom > pTo)
1683 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1685 if (*pTemp == '\0' || *pTemp == '\n' || *pTemp == '#' || *pTemp == '=')
1695 _RegistryCore::CheckEntryName(char* pEntryName)
1702 if (pEntryName == null)
1708 length = strlen(pEntryName);
1709 pTo = pEntryName + (length - 1);
1711 if (!pFrom || !pTo || pFrom > pTo)
1716 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1718 if (*pTemp == '\0' || *pTemp == '\n' || *pTemp == '#' || *pTemp == '=')
1728 _RegistryCore::CheckEntryValue(char* pEntryVal)
1735 if (pEntryVal == null)
1741 length = strlen(pEntryVal);
1742 pTo = pEntryVal + (length - 1);
1744 if (!pFrom || !pTo || pFrom > pTo)
1749 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1751 if (*pTemp == '\0' || *pTemp == '\n')
1761 _RegistryCore::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer* pKey)
1763 result r = E_SUCCESS;
1765 if (File::IsFileExist(secureRegPath))
1767 r = GetLastResult();
1770 r = E_FILE_ALREADY_EXIST;
1771 SysLog(NID_IO, "[E_FILE_ALREADY_EXIST] The secure registry already exists.");
1775 SysLog(NID_IO, "[%s]Propagated.", GetErrorMessage(r));
1780 unique_ptr<_NormalRegistry> pNormalReg(new (std::nothrow) _NormalRegistry());
1781 SysTryReturnResult(NID_IO, pNormalReg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1783 r = pNormalReg->Load(plainRegPath, "r");
1784 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1786 unique_ptr<_SecureRegistry> pSecureReg(new (std::nothrow) _SecureRegistry());
1787 SysTryReturnResult(NID_IO, pSecureReg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1789 r = pSecureReg->Construct(secureRegPath, "w+", pKey);
1790 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1792 if (pNormalReg->_pBuffer != null && pNormalReg->_length > 0)
1794 pSecureReg->_pBuffer = pNormalReg->_pBuffer;
1795 pSecureReg->_length = pNormalReg->_length;
1797 pNormalReg->_pBuffer = null;
1798 pNormalReg->_length = 0;
1800 r = pSecureReg->Parse();
1801 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1804 r = pSecureReg->Flush();
1805 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1811 _RegistryCore::LockN(FileLockType lockType)
1813 SysAssertf(_constructed == true && __pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
1814 return __pFileImpl->LockN(lockType);
1818 _RegistryCore::TryToLockN(FileLockType lockType)
1820 SysAssertf(_constructed == true && __pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
1821 return __pFileImpl->TryToLockN(lockType);