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_RegistryCore.cpp
20 * @brief This is the implementation file for _RegistryCore class.
25 #include <unique_ptr.h>
27 #include <FBaseInteger.h>
28 #include <FBaseDouble.h>
29 #include <FBaseFloat.h>
30 #include <FBaseUuId.h>
31 #include <FBaseByteBuffer.h>
32 #include <FBaseColIEnumerator.h>
33 #include <FBaseUtilStringTokenizer.h>
34 #include <FBaseResult.h>
35 #include <FBaseSysLog.h>
36 #include <FBaseColAllElementsDeleter.h>
38 #include <FIoRegistry.h>
40 #include <FBase_StringConverter.h>
41 #include <FApp_AppInfo.h>
42 #include "FIo_FileImpl.h"
43 #include "FIo_NormalRegistry.h"
44 #include "FIo_SecureRegistry.h"
45 #include "FIo_RegistryCore.h"
46 #include "FIo_SecureIoUtil.h"
49 using namespace Tizen::Base;
50 using namespace Tizen::Base::Utility;
51 using namespace Tizen::Base::Collection;
53 namespace Tizen { namespace Io
56 static const size_t _MAX_REG_OPENMODE_LENGTH = 2;
58 #define _REGISTRY_SECTION_MARKER_CHAR L'#'
59 #define _REGISTRY_SECTION_VALUE_ASSIGNMENT_MARKER_CHAR L'='
61 class _RegistrySection
65 ~_RegistrySection(void);
68 LinkedList __entryList;
69 friend class _RegistryCore;
72 _RegistrySection::~_RegistrySection(void)
74 __entryList.RemoveAll(true);
85 _RegistryCore::_RegistryCore(void)
96 _RegistryCore::~_RegistryCore(void)
101 _RegistryCore::VerifyRegistryOpenMode(const char* pOpenMode)
103 if (pOpenMode == null)
105 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode is null.");
109 if (strlen(pOpenMode) > _MAX_REG_OPENMODE_LENGTH)
111 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
115 switch (pOpenMode[0])
129 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
133 switch (pOpenMode[1])
138 if (pOpenMode[2] == '\0')
146 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
150 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
158 _RegistryCore::Parse(void)
160 result r = E_SUCCESS;
162 bool sectionFound = false;
165 unique_ptr<char[]> pEntryName(null);
166 unique_ptr<char[]> pEntryValue(null);
167 int firstTokenPos = 0;
170 if (_pBuffer == null)
172 SysLog(NID_IO, "The buffer is empty.");
176 String inputStr = String((char*)_pBuffer);
177 StringTokenizer lineTok(inputStr, L"\n");
179 r = lineTok.GetNextToken(line);
181 while (r == E_SUCCESS)
184 if (line.GetLength() == 0)
186 r = lineTok.GetNextToken(line);
190 // find registry section marker
191 line.GetCharAt(0, ch);
192 if (ch == _REGISTRY_SECTION_MARKER_CHAR)
196 // skip '#' and "\n' at the end
197 r = line.SubString(1, line.GetLength() - 1, sectionName);
198 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
200 // if End of the document follows section name without newline character.
201 sectionName.GetCharAt(sectionName.GetLength() - 1, ch);
204 sectionName.SetLength(sectionName.GetLength() - 1); //Remove '\n'
207 // for compatibility check if line contains '\r' at the end
208 sectionName.GetCharAt(sectionName.GetLength() - 1, ch);
211 sectionName.SetLength(sectionName.GetLength() - 1); //Remove '\r'
214 //TODO: check if remaining data in sectionName is valid or not
215 //after removing '#', '\n', and '\r', sectionName should contain at least 1 valid character
217 sectionName.GetLength() > 0, r = E_PARSING_FAILED, E_PARSING_FAILED,
218 "[E_PARSING_FAILED] Section length cannot be <2.");
220 unique_ptr<char[]> pSectionName(_StringConverter::CopyToCharArrayN(sectionName));
221 SysTryCatch(NID_IO, pSectionName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
222 "[E_OUT_OF_MEMORY] The memory is insufficient.");
224 if (!_RegistryCore::CheckSectionName(pSectionName.get()))
226 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
227 "[E_PARSING_FAILED] Section name could not be parsed.");
230 r = _RegistryCore::AddSection(_sectionList, sectionName);
231 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
233 else // not a section.. but may belongs to a section (entry)
235 // ignores blank lines
236 line.GetCharAt(0, ch);
237 if ((ch == L'\n') || (ch == L'\r'))
239 r = lineTok.GetNextToken(line);
244 // Need to check for tabs, spaces in the beginning of each line
246 // if no section found till now.. ignore
247 if (sectionFound == false)
249 r = lineTok.GetNextToken(line);
253 // if End of the document follows entry value without newline character.
254 line.GetCharAt(line.GetLength() - 1, ch);
257 line.SetLength(line.GetLength() - 1); //Remove '\n'
260 line.GetCharAt(line.GetLength() - 1, ch);
263 line.SetLength(line.GetLength() - 1); //Remove '\r'
266 String entryName("");
269 // we will have sectionitem=value (or) sectionitem= (or) sectionitem
270 // as our string. Break this into entry and value
271 StringTokenizer strTok(line, L"=");
273 // extract entry name
274 if (strTok.HasMoreTokens())
276 line.IndexOf(L'=', 0, firstTokenPos); // position of first occurance of '=' in a line
277 if (firstTokenPos == 0)
279 // entryName should not be empty. i.e., "=value" or just '=' is invalid case
280 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
281 "[E_PARSING_FAILED] Entry name cannot be empty.");
285 strTok.GetNextToken(entryName);
288 else // missing '=' in entry line or entry line does not seem to contain "xxx=xxx" format
290 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED, "[E_PARSING_FAILED] invalid entry Line found.");
293 // check if entry name contains invalid chars
294 pEntryName.reset(_StringConverter::CopyToCharArrayN(entryName));
295 SysTryCatch(NID_IO, pEntryName, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
296 "[E_OUT_OF_MEMORY] The memory is insufficient.");
298 if (!_RegistryCore::CheckEntryName(pEntryName.get()))
300 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
301 "[E_PARSING_FAILED] Entry name could not be parsed.");
304 line.SubString(firstTokenPos + 1, entryVal); // extract entry value
306 // check if entry value contains invalid chars
307 pEntryValue.reset(_StringConverter::CopyToCharArrayN(entryVal));
308 SysTryCatch(NID_IO, pEntryValue, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
309 "[E_OUT_OF_MEMORY] The memory is insufficient.");
311 length = strlen(pEntryValue.get());
314 if (!_RegistryCore::CheckEntryValue(pEntryValue.get()))
316 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
317 "[E_PARSING_FAILED] Entry value could not be parsed.");
321 // add the entry and value
322 r = _RegistryCore::AddValue(_sectionList, sectionName, entryName, entryVal);
323 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
325 r = lineTok.GetNextToken(line);
328 if (r == E_OUT_OF_RANGE)
336 if (r == E_PARSING_FAILED && Tizen::App::_AppInfo::IsOspCompat() == false)
338 r = E_INVALID_FORMAT;
341 SysLog(NID_IO, "[%s] exception occurred.", GetErrorMessage(r));
346 _RegistryCore::Load(const String& regPath, const char* pOpenMode)
348 result r = E_SUCCESS;
349 delete[] _pBuffer; //clear existing buffer
353 // open registry file
354 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl());
355 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
357 r = pFileImpl->Construct(regPath, pOpenMode, null);
358 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Constructing the file has failed.", GetErrorMessage(r));
360 // reset the section list to be sure that its empty
361 _sectionList.RemoveAll(true);
363 if (_truncate == false)
365 // load the registry file
366 r = pFileImpl->ReadN((char**)&_pBuffer, _length);
377 _RegistryCore::AddSection(const String& sectionName)
379 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
381 result r = E_SUCCESS;
382 bool sectionFound = false;
384 // Get section information
385 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
388 return GetLastResult();
391 while (pEnum->MoveNext() == E_SUCCESS)
393 _RegistrySection* pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
394 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
395 GetErrorMessage(GetLastResult()));
397 if (pRegSection->__sectionName == sectionName)
404 if (sectionFound == true)
406 return E_SECTION_ALREADY_EXIST;
409 // create a key with section name
410 unique_ptr<_RegistrySection> pRegSection(new (std::nothrow) _RegistrySection());
411 SysTryReturnResult(NID_IO, pRegSection != null, E_OUT_OF_MEMORY,
412 "The memory is insufficient.");
414 pRegSection->__sectionName = sectionName;
416 // add new section and section-entry map
417 r = _sectionList.Add(*(pRegSection.release()));
418 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
424 _RegistryCore::RemoveSection(const String& sectionName)
426 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
428 result r = E_SUCCESS;
429 _RegistrySection* pRegSection = null;
430 bool sectionFound = false;
432 if (_sectionList.GetCount() == 0)
434 return E_SECTION_NOT_FOUND;
437 // Get section information
438 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
441 return GetLastResult();
444 while (pEnum->MoveNext() == E_SUCCESS)
446 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
447 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagating.",
448 GetErrorMessage(GetLastResult()));
450 if (pRegSection->__sectionName == sectionName)
457 if (sectionFound == false)
459 return E_SECTION_NOT_FOUND;
462 r = _sectionList.Remove(*pRegSection, true);
463 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
469 _RegistryCore::GetSectionListN(IList** pRetList)
471 result r = E_SUCCESS;
472 unique_ptr<IEnumerator> pEnum(null);
473 _RegistrySection* pSection = null;
475 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
476 SysTryReturnResult(NID_IO, pRetList != null, E_INVALID_ARG, "pList cannot be null.");
478 // construct an array list to be returned
479 unique_ptr<ArrayList, AllElementsDeleter> pSectionList(new (std::nothrow) ArrayList());
480 SysTryReturnResult(NID_IO, pSectionList != null, E_OUT_OF_MEMORY,
481 "The memory is insufficient.");
483 r = pSectionList->Construct();
484 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
486 // if no sectons, return empty list
487 if (_sectionList.GetCount() == 0)
489 *pRetList = pSectionList.release();
493 // copy from class section list to the new list and return
494 pEnum.reset(_sectionList.GetEnumeratorN());
495 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.", GetErrorMessage(GetLastResult()));
497 while (pEnum->MoveNext() == E_SUCCESS)
499 pSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
500 SysTryReturn(NID_IO, pSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
501 GetErrorMessage(GetLastResult()));
503 unique_ptr<String> pSectionName(new (std::nothrow) String(pSection->__sectionName));
504 SysTryReturnResult(NID_IO, pSectionName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
506 r = pSectionList->Add(*(pSectionName.release()));
507 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
510 *pRetList = pSectionList.release();
516 _RegistryCore::GetEntryList(const String& sectionName, Collection::HashMap& retMap)
518 result r = E_SUCCESS;
519 unique_ptr<IEnumerator> pEnum(null);
520 unique_ptr<String> pKeyEntryStr(null);
521 unique_ptr<String> pKeyValStr(null);
522 bool sectionFound = false;
523 _RegistrySection* pRegSection = null;
524 _RegistryEntry* pRegEntry = null;
526 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
528 if (_sectionList.GetCount() == 0)
530 return E_SECTION_NOT_FOUND;
533 // Get section information
534 pEnum.reset(_sectionList.GetEnumeratorN());
535 SysTryReturnResult(NID_IO, pEnum != null, E_SYSTEM, "Section list is empty.");
537 while (pEnum->MoveNext() == E_SUCCESS)
539 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
540 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
541 GetErrorMessage(GetLastResult()));
543 if (pRegSection->__sectionName == sectionName)
550 if (sectionFound == false)
552 return E_SECTION_NOT_FOUND;
555 // copy from item list to the new list and return
556 pEnum.reset((pRegSection->__entryList).GetEnumeratorN());
557 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
558 GetErrorMessage(GetLastResult()));
560 while (pEnum->MoveNext() == E_SUCCESS)
562 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
563 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
564 GetErrorMessage(GetLastResult()));
566 pKeyEntryStr.reset(new (std::nothrow) String((pRegEntry->entryName)));
567 SysTryCatch(NID_IO, pKeyEntryStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
568 "[E_OUT_OF_MEMORY] The memory is insufficient.");
569 pKeyValStr.reset(new (std::nothrow) String((pRegEntry->entryValue)));
570 SysTryCatch(NID_IO, pKeyValStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
571 "[E_OUT_OF_MEMORY] The memory is insufficient.");
573 r = retMap.Add(*(pKeyEntryStr.release()), *(pKeyValStr.release()));
574 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
580 retMap.RemoveAll(true);
586 _RegistryCore::GetEntryListN(const String& sectionName, HashMap** pRetList)
588 result r = E_SUCCESS;
590 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
591 SysTryReturnResult(NID_IO, pRetList != null, E_INVALID_ARG, "pRetList cannot be null.");
593 // construct an array list to be returned
594 unique_ptr<HashMap, AllElementsDeleter> pEntryList(new (std::nothrow) HashMap());
595 SysTryReturnResult(NID_IO, pEntryList != null, E_OUT_OF_MEMORY,
596 "The memory is insufficient.");
597 r = pEntryList->Construct();
598 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
599 r = GetEntryList(sectionName, *(pEntryList.get()));
600 *pRetList = pEntryList.release();
606 _RegistryCore::GetAllEntriesN(const String& sectionName)
608 result r = E_SUCCESS;
609 HashMap* pMap = null;
611 r = GetEntryListN(sectionName, &pMap);
612 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
614 SetLastResult(E_SUCCESS);
623 _RegistryCore::GetAllEntryNamesN(const String& sectionName)
625 result r = E_SUCCESS;
627 unique_ptr<ArrayList, AllElementsDeleter> pEntryList(new (std::nothrow) ArrayList());
628 SysTryReturn(NID_IO, pEntryList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
630 r = pEntryList->Construct();
631 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
633 unique_ptr<IMap, AllElementsDeleter> pMap(this->GetAllEntriesN(sectionName));
634 SysTryReturn(NID_IO, pMap != null, null, GetLastResult(), "[%s] Getting all entries was failed.",
635 GetErrorMessage(GetLastResult()));
637 unique_ptr<IMapEnumerator> pMapEnum(pMap->GetMapEnumeratorN());
638 SysTryReturn(NID_IO, pMapEnum, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
639 while (pMapEnum->MoveNext() == E_SUCCESS)
641 String* pKey = dynamic_cast< String* >(pMapEnum->GetKey());
642 SysTryReturn(NID_IO, pKey != null, null, E_IO, "[E_OUT_OF_MEMORY] The system error occurred.");
644 String* pEntryName = new (std::nothrow) String(*pKey);
645 SysTryReturn(NID_IO, pEntryName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
647 r = pEntryList->Add(*pEntryName);
648 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
651 SetLastResult(E_SUCCESS);
652 return pEntryList.release();
656 _RegistryCore::GetValue(const String& sectionName, const String& entryName, String& valStr)
658 bool sectionFound = false;
659 bool entryFound = false;
660 _RegistrySection* pRegSection = null;
661 _RegistryEntry* pRegEntry = null;
663 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
665 if (_sectionList.GetCount() == 0)
667 return E_SECTION_NOT_FOUND;
670 // Get section information
671 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
674 return GetLastResult();
677 while (pEnum->MoveNext() == E_SUCCESS)
679 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
680 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
681 GetErrorMessage(GetLastResult()));
683 if (pRegSection->__sectionName == sectionName)
690 if (sectionFound == false)
692 return E_SECTION_NOT_FOUND;
695 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
698 return GetLastResult();
701 while (pEnum->MoveNext() == E_SUCCESS)
703 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
704 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
705 GetErrorMessage(GetLastResult()));
707 if (pRegEntry->entryName == entryName)
709 valStr = pRegEntry->entryValue;
715 SysTryLog(NID_IO, entryFound, "Entry not found");
717 return((entryFound == true) ? E_SUCCESS : E_KEY_NOT_FOUND);
721 _RegistryCore::AddValue(const String& sectionName, const String& entryName, const String& valStr)
723 result r = E_SUCCESS;
724 bool sectionFound = false;
725 bool entryFound = false;
726 _RegistrySection* pRegSection = null;
729 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
731 // Get section information
732 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
735 return GetLastResult();
738 while (pEnum->MoveNext() == E_SUCCESS)
740 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
741 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
742 GetErrorMessage(GetLastResult()));
744 if (pRegSection->__sectionName == sectionName)
751 if (sectionFound == false)
753 return E_SECTION_NOT_FOUND;
756 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
759 return GetLastResult();
762 while (pEnum->MoveNext() == E_SUCCESS)
764 _RegistryEntry* pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
765 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
766 GetErrorMessage(GetLastResult()));
768 if (pRegEntry->entryName == entryName)
775 if (entryFound == true)
777 return E_KEY_ALREADY_EXIST;
780 unique_ptr<_RegistryEntry> pRegEntry(new (std::nothrow) _RegistryEntry());
781 SysTryReturnResult(NID_IO, pRegEntry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
783 unique_ptr<char[]> pEntryName(_StringConverter::CopyToCharArrayN(entryName));
784 SysTryReturnResult(NID_IO, pEntryName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
786 length = strlen(pEntryName.get());
787 if (!_RegistryCore::CheckEntryName(pEntryName.get()))
789 SysTryReturnResult(NID_IO, false, E_PARSING_FAILED, "Entry name could not be parsed.");
792 unique_ptr<char[]> pEntryValue(_StringConverter::CopyToCharArrayN(valStr));
793 SysTryReturnResult(NID_IO, pEntryValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
795 length = strlen(pEntryValue.get());
796 if (length > 0 && !_RegistryCore::CheckEntryValue(pEntryValue.get()))
798 SysTryReturnResult(NID_IO, false, E_PARSING_FAILED, "Entry value could not be parsed.");
801 pRegEntry->entryName = entryName;
802 pRegEntry->entryValue = valStr;
804 r = pRegSection->__entryList.Add(*(pRegEntry.release()));
805 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
811 _RegistryCore::SetValue(const String& sectionName, const String& entryName, const String& valStr)
813 result r = E_SUCCESS;
814 bool sectionFound = false;
815 bool entryFound = false;
816 _RegistryEntry* pRegEntry = null;
817 _RegistrySection* pRegSection = null;
821 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
823 if (_sectionList.GetCount() == 0)
825 return E_SECTION_NOT_FOUND;
828 // Get section information
829 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
832 return GetLastResult();
835 while (pEnum->MoveNext() == E_SUCCESS)
837 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
838 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
839 GetErrorMessage(GetLastResult()));
841 if (pRegSection->__sectionName == sectionName)
848 if (sectionFound == false)
850 return E_SECTION_NOT_FOUND;
853 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
856 return GetLastResult();
859 while (pEnum->MoveNext() == E_SUCCESS)
861 pRegEntry = dynamic_cast< _RegistryEntry* >(pEnum->GetCurrent());
862 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
863 GetErrorMessage(GetLastResult()));
865 if (pRegEntry->entryName == entryName)
873 if (entryFound == false)
875 return E_KEY_NOT_FOUND;
878 if (valStr.GetLength() > 0)
880 unique_ptr<char[]> pEntryValue(_StringConverter::CopyToCharArrayN(valStr));
881 SysTryReturnResult(NID_IO, pEntryValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
883 length = strlen(pEntryValue.get());
884 if (!_RegistryCore::CheckEntryValue(pEntryValue.get()))
886 return E_PARSING_FAILED;
890 pRegEntry->entryValue = valStr;
891 r = pRegSection->__entryList.SetAt(*pRegEntry, entryIndex);
892 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
898 _RegistryCore::RemoveValue(const String& sectionName, const String& entryName)
900 result r = E_SUCCESS;
901 bool sectionFound = false;
902 bool entryFound = false;
903 _RegistryEntry* pRegEntry = null;
904 _RegistrySection* pRegSection = null;
906 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
908 if (_sectionList.GetCount() <= 0)
910 return E_SECTION_NOT_FOUND;
913 // Get section information
914 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
917 return E_SECTION_NOT_FOUND;
920 while (pEnum->MoveNext() == E_SUCCESS)
922 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
923 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
924 GetErrorMessage(GetLastResult()));
926 if (pRegSection->__sectionName == sectionName)
933 if (sectionFound == false)
935 return E_SECTION_NOT_FOUND;
938 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
941 return E_SECTION_NOT_FOUND;
944 while (pEnum->MoveNext() == E_SUCCESS)
946 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
947 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
948 GetErrorMessage(GetLastResult()));
950 if (pRegEntry->entryName == entryName)
957 if (entryFound == false)
959 return E_KEY_NOT_FOUND;
962 r = pRegSection->__entryList.Remove(*pRegEntry, true);
963 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
969 _RegistryCore::Flush(void)
975 _RegistryCore::Write(void)
977 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
979 if (_pBuffer == null)
981 SysLog(NID_IO, "The buffer is empty.");
985 result r = E_SUCCESS;
987 String openMode(L"w+");
988 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
989 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
991 //TODO: Secure mode is to be handled
992 r = pFileImpl->Construct(_regPath, "w+", null);
993 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
995 r = pFileImpl->Write(_pBuffer, _length);
996 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
998 r = pFileImpl->Flush();
999 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1005 _RegistryCore::PrepareToWrite(void)
1007 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1009 result r = E_SUCCESS;
1010 _RegistryEntry* pRegEntry = null;
1011 _RegistrySection* pRegSection = null;
1014 delete[] _pBuffer; //clear existing buffer
1018 if (_write == false)
1020 SysLog(NID_IO, "[E_ILLEGAL_ACCESS] Registry cannot be flushed as it was opened in read only mode.");
1021 return E_ILLEGAL_ACCESS;
1024 // if no sections, do nothing
1025 if (_sectionList.GetCount() == 0)
1027 unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(_regPath));
1028 truncate(pFilePath.get(), 0);
1033 // write the registry map to a file
1034 // Get section information
1035 unique_ptr<IEnumerator> pSectionEnum(_sectionList.GetEnumeratorN());
1036 SysTryReturn(NID_IO, pSectionEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.",
1037 GetErrorMessage(GetLastResult()));
1039 while (pSectionEnum->MoveNext() == E_SUCCESS)
1042 pRegSection = dynamic_cast <_RegistrySection*>(pSectionEnum->GetCurrent());
1043 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1044 GetErrorMessage(GetLastResult()));
1046 // write section name to file
1047 r = inputBuffer.Append(String(L"#" + pRegSection->__sectionName + String(L'\n')));
1048 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1050 // read the entries for this section
1051 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1052 SysTryReturn(NID_IO, pSectionEnum != null, GetLastResult(), GetLastResult(), "[%s] Entry list is empty.",
1053 GetErrorMessage(GetLastResult()));
1055 while (pEntryEnum->MoveNext() == E_SUCCESS)
1057 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1058 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1059 GetErrorMessage(GetLastResult()));
1061 // write entry name to file
1062 r = inputBuffer.Append(pRegEntry->entryName + String(L"=" + pRegEntry->entryValue + String(L'\n')));
1063 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1068 _pBuffer = (byte*)_StringConverter::CopyToCharArrayN(inputBuffer);
1069 SysTryReturn(NID_IO, _pBuffer != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1070 GetErrorMessage(GetLastResult()));
1072 _length = strlen((char*)_pBuffer);
1078 _RegistryCore::GetSectionIndex(const String& sectionName)
1080 bool sectionFound = false;
1081 _RegistrySection* pRegSection = null;
1084 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1085 SysTryReturn(NID_IO, sectionName.GetLength() > 0, -1, E_INVALID_ARG,
1086 "[E_INVALID_ARG] sectionName.GetLength() <= 0");
1088 SysTryReturn(NID_IO, _sectionList.GetCount() > 0, -1, E_SECTION_NOT_FOUND, "[E_SECTION_NOT_FOUND] Section not found.");
1090 // Get section information
1091 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
1092 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.",
1093 GetErrorMessage(GetLastResult()));
1095 while (pEnum->MoveNext() == E_SUCCESS)
1097 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1098 SysTryReturn(NID_IO, pRegSection != null, -1, GetLastResult(), "[%s] Propagated.",
1099 GetErrorMessage(GetLastResult()));
1101 if (pRegSection->__sectionName == sectionName)
1103 sectionFound = true;
1109 if (sectionFound == false)
1111 return E_SECTION_NOT_FOUND;
1114 SetLastResult(E_SUCCESS);
1119 _RegistryCore::Removekey(const String& sectionName, const String& keyName)
1121 result r = E_SUCCESS;
1122 int sectionIndex = -1;
1123 int entryIndex = -1;
1125 sectionIndex = GetSectionIndex(sectionName);
1126 r = GetLastResult();
1127 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1129 entryIndex = GetEntryIndex(sectionIndex, keyName);
1130 r = GetLastResult();
1131 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1133 RemoveEntry(sectionIndex, entryIndex);
1134 r = GetLastResult();
1135 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1137 SetLastResult(E_SUCCESS);
1142 _RegistryCore::GetAllSectionCount(void)
1144 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1146 SetLastResult(E_SUCCESS);
1147 return _sectionList.GetCount();
1151 _RegistryCore::GetAllEntryCount(int sectionIndex)
1153 _RegistrySection* pRegSection = null;
1156 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1157 SysTryReturn(NID_IO, sectionIndex >= 0, 0, E_INVALID_ARG, "[E_INVALID_ARG] sectionIndex < 0.");
1158 listSize = _sectionList.GetCount();
1159 SysTryReturn(NID_IO, listSize > 0, 0, E_SECTION_NOT_FOUND, "[E_INVALID_ARG] section listSize <= 0.");
1160 SysTryReturn(NID_IO, sectionIndex >= listSize, 0, E_SECTION_NOT_FOUND,
1161 "[E_INVALID_ARG] sectionIndex > listSize.");
1163 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1164 SysTryReturn(NID_IO, pRegSection != null, 0, E_SECTION_NOT_FOUND,
1165 "[E_SECTION_NOT_FOUND] section was not found.");
1167 SetLastResult(E_SUCCESS);
1168 return pRegSection->__entryList.GetCount();
1172 _RegistryCore::RemoveEntry(int sectionIndex, int entryIndex)
1174 result r = E_SUCCESS;
1176 int entryListSize = 0;
1177 _RegistrySection* pRegSection = null;
1179 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1180 SysTryReturnVoidResult(NID_IO, sectionIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG] sectionIndex < 0.");
1181 SysTryReturnVoidResult(NID_IO, entryIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG] entryIndex < 0.");
1183 listSize = _sectionList.GetCount();
1184 SysTryReturnVoidResult(NID_IO, listSize > 0, E_SECTION_NOT_FOUND,
1185 "[E_SECTION_NOT_FOUND] section listSize <= 0.");
1186 SysTryReturnVoidResult(NID_IO, sectionIndex < listSize, E_SECTION_NOT_FOUND,
1187 "[E_SECTION_NOT_FOUND] sectionIndex >= listSize.");
1189 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1190 SysTryReturnVoidResult(NID_IO, pRegSection != null, E_SECTION_NOT_FOUND,
1191 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1193 r = GetLastResult();
1194 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r,"[%s] Propagated.", GetErrorMessage(r));
1196 entryListSize = pRegSection->__entryList.GetCount();
1197 SysTryReturnVoidResult(NID_IO, (entryListSize > 0 || entryIndex < entryListSize), E_KEY_NOT_FOUND,
1198 "[E_KEY_NOT_FOUND] entryListSize is 0 or entryIndex >= entryListSize.");
1200 r = pRegSection->__entryList.RemoveAt(entryIndex, true);
1201 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1203 SetLastResult(E_SUCCESS);
1207 _RegistryCore::ModifyEntryValue(int sectionIndex, int entryIndex, _RegValueType type, const void* pValue, int size)
1209 SetLastResult(E_UNSUPPORTED_OPERATION);
1213 _RegistryCore::GetEntryIndex(int sectionIndex, const String& entryName)
1215 _RegistrySection* pRegSection = null;
1216 _RegistryEntry* pRegEntry = null;
1218 bool entryFound = false;
1219 int entryIndex = -1;
1221 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1222 SysTryReturn(NID_IO, sectionIndex >= 0, -1, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1223 SysTryReturn(NID_IO, entryName.GetLength() > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG]entryName length < 0.");
1225 listSize = _sectionList.GetCount();
1226 SysTryReturn(NID_IO, listSize > 0, -1, E_SECTION_NOT_FOUND,
1227 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1228 SysTryReturn(NID_IO, sectionIndex < listSize, -1, E_SECTION_NOT_FOUND,
1229 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1231 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1232 SysTryReturn(NID_IO, pRegSection != null, -1, E_SECTION_NOT_FOUND,
1233 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1235 // read the entries for this section
1236 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1237 SysTryReturn(NID_IO, pEntryEnum != null, -1, GetLastResult(), "[%s] Entry list is empty.",
1238 GetErrorMessage(GetLastResult()));
1241 while (pEntryEnum->MoveNext() == E_SUCCESS)
1243 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1244 SysTryReturn(NID_IO, pRegEntry != null, -1, GetLastResult(), "[%s] Propagated.",
1245 GetErrorMessage(GetLastResult()));
1247 if (entryName == pRegEntry->entryName)
1255 SysTryReturn(NID_IO, entryFound != false, -1, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] entry was not found.");
1257 SetLastResult(E_SUCCESS);
1262 _RegistryCore::GetEntryName(int sectionIndex, int entryIndex)
1264 _RegistrySection* pRegSection = null;
1265 _RegistryEntry* pRegEntry = null;
1270 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1271 SysTryReturn(NID_IO, sectionIndex >= 0, empty, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1272 SysTryReturn(NID_IO, entryIndex >= 0, empty, E_INVALID_ARG, "[E_INVALID_ARG]entryIndex < 0.");
1274 listSize = _sectionList.GetCount();
1275 SysTryReturn(NID_IO, listSize > 0, empty, E_SECTION_NOT_FOUND,
1276 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1277 SysTryReturn(NID_IO, sectionIndex < listSize, empty, E_SECTION_NOT_FOUND,
1278 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1280 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1281 SysTryReturn(NID_IO, pRegSection != null, empty, E_SECTION_NOT_FOUND,
1282 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1284 // read the entries for this section
1285 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1286 SysTryReturn(NID_IO, pEntryEnum != null, empty, GetLastResult(), "[%s] Entry list is empty.",
1287 GetErrorMessage(GetLastResult()));
1289 tmpIndex = entryIndex;
1290 while (pEntryEnum->MoveNext() == E_SUCCESS)
1294 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1295 SysTryReturn(NID_IO, pRegEntry != null, empty, GetLastResult(), "[%s] Propagated.",
1296 GetErrorMessage(GetLastResult()));
1303 SysTryReturn(NID_IO, pRegEntry != null, empty, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] entry was not found.");
1305 SetLastResult(E_SUCCESS);
1306 return pRegEntry->entryName;
1310 _RegistryCore::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType type, void* pValue, int* pSize)
1312 _RegistrySection* pRegSection = null;
1313 _RegistryEntry* pRegEntry = null;
1314 IEnumerator* pEntryEnum = null;
1315 result r = E_SUCCESS;
1317 int tmpEntryIndex = -1;
1318 int entryListSize = 0;
1319 String strValueEncoded;
1321 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1322 SysTryReturnVoidResult(NID_IO, sectionIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1323 SysTryReturnVoidResult(NID_IO, type >= 0 && type <= REG_VALUE_TYPE_MAX, E_INVALID_ARG,
1324 "[E_INVALID_ARG] invalid registry type.");
1325 SysTryReturnVoidResult(NID_IO, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG]pValue is null.");
1326 SysTryReturnVoidResult(NID_IO, pSize != null, E_INVALID_ARG, "[E_INVALID_ARG]pSize is null.");
1327 SysTryReturnVoidResult(NID_IO, *pSize > 0, E_INVALID_ARG, "[E_INVALID_ARG] *pSize < 0.");
1329 listSize = _sectionList.GetCount();
1330 SysTryReturnVoidResult(NID_IO, listSize > 0, E_SECTION_NOT_FOUND,
1331 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1332 SysTryReturnVoidResult(NID_IO, sectionIndex < listSize, E_SECTION_NOT_FOUND,
1333 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1335 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1336 SysTryReturnVoidResult(NID_IO, pRegSection != null, E_SECTION_NOT_FOUND,
1337 "[E_SECTION_NOT_FOUND] section at index (%d) is not available.", sectionIndex);
1339 entryListSize = pRegSection->__entryList.GetCount();
1340 SysTryReturnVoidResult(NID_IO, entryListSize > 0, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry list is empty.");
1341 SysTryReturnVoidResult(NID_IO, entryIndex < entryListSize, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry not found.");
1343 // read the entries for this section
1344 pEntryEnum = pRegSection->__entryList.GetEnumeratorN();
1345 SysTryReturnVoidResult(NID_IO, pEntryEnum != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND]Entry list is empty.");
1347 tmpEntryIndex = entryIndex;
1348 while (pEntryEnum->MoveNext() == E_SUCCESS)
1350 if (entryIndex == 0)
1352 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1353 SysTryReturnVoidResult(NID_IO, pRegEntry != null, GetLastResult(), "[%s] Propagated.",
1354 GetErrorMessage(GetLastResult()));
1362 SysTryReturnVoidResult(NID_IO, pRegEntry != null, E_KEY_NOT_FOUND,
1363 "[E_KEY_NOT_FOUND] Entry was not found.");
1365 strValueEncoded = pRegEntry->entryValue;
1367 DecodeData(strValueEncoded, type, pValue, pSize);
1368 r = GetLastResult();
1369 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1371 SetLastResult(E_SUCCESS);
1376 _RegistryCore::GetSectionName(int sectionIndex)
1378 _RegistrySection* pRegSection = null;
1379 result r = E_SUCCESS;
1381 String sectionName("");
1383 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1384 SysTryReturn(NID_IO, sectionIndex >= 0, sectionName, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1386 listSize = _sectionList.GetCount();
1387 SysTryReturn(NID_IO, listSize > 0, sectionName, E_SECTION_NOT_FOUND,
1388 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1389 SysTryReturn(NID_IO, sectionIndex < listSize, sectionName, E_SECTION_NOT_FOUND,
1390 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1392 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1393 SysTryReturn(NID_IO, pRegSection != null, sectionName, E_SECTION_NOT_FOUND,
1394 "[E_SECTION_NOT_FOUND] sectionIndex is not available.");
1396 r = GetLastResult();
1397 SysTryReturn(NID_IO, !IsFailed(r), sectionName, r, "[%s] Propagated.", GetErrorMessage(r));
1399 SetLastResult(E_SUCCESS);
1400 return pRegSection->__sectionName;
1404 _RegistryCore::GetSectionByIndex(int sectionIndex)
1406 _RegistrySection* pRegSection = null;
1407 int index = sectionIndex;
1409 // Get section information
1410 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
1411 SysTryReturn(NID_IO, pEnum != null, null, GetLastResult(),
1412 "[%s] section list is empty.", GetErrorMessage(GetLastResult()));
1414 while (pEnum->MoveNext() == E_SUCCESS)
1418 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
1419 SysTryReturn(NID_IO, pRegSection != null, null, GetLastResult(), "[%s] Propagated.",
1420 GetErrorMessage(GetLastResult()));
1427 SysTryReturn(NID_IO, pRegSection != null, null, E_SECTION_NOT_FOUND,
1428 "[E_SECTION_NOT_FOUND] Section was not found.");
1430 SetLastResult(E_SUCCESS);
1436 _RegistryCore::DecodeData(const String& strValueEncoded, _RegValueType type, void* pValue, int* pSize)
1438 SysAssert(strValueEncoded.GetLength() > 0);
1441 SysAssert(pSize && *pSize > 0);
1443 SysTryReturnVoidResult(NID_IO, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument.");
1444 SysTryReturnVoidResult(NID_IO, pSize != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument.");
1448 case REG_VALUE_TYPE_INT: // (-2147483647 - 1) ~ 2147483647
1451 result r = E_SUCCESS;
1453 if (*pSize != sizeof(int))
1455 SetLastResult(E_INVALID_ARG);
1459 r = Integer::Parse(strValueEncoded, retIntVal);
1460 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1462 *(int*) pValue = retIntVal;
1466 case REG_VALUE_TYPE_REAL: // Holds signed IEEE 64-bit (8-byte) double-precision floating-point numbers
1467 // ranging in value from -1.79769313486231570E+308 through -4.94065645841246544E-324
1468 // for negative values and from 4.94065645841246544E-324 through 1.79769313486231570E+308
1469 // for positive values.
1471 double retDoubleVal = 0;
1472 result r = E_SUCCESS;
1474 if (*pSize != sizeof(double))
1476 SetLastResult(E_INVALID_ARG);
1480 r = Double::Parse(strValueEncoded, retDoubleVal);
1481 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1483 *(double*) pValue = retDoubleVal;
1487 case REG_VALUE_TYPE_STRING:
1489 // if(*pSize != sizeof(String))
1491 // SetLastResult(E_INVALID_ARG);
1494 *(String*) pValue = strValueEncoded;
1498 case REG_VALUE_TYPE_BINARY:
1500 SetLastResult(E_PARSING_FAILED);
1504 case REG_VALUE_TYPE_UUID:
1507 result r = E_SUCCESS;
1509 r = UuId::Parse(strValueEncoded, retUuIdVal);
1510 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1512 *(UuId*) pValue = retUuIdVal;
1516 case REG_VALUE_TYPE_UUIDSET:
1518 SetLastResult(E_PARSING_FAILED);
1523 SysAssert(0); // should not happen!
1527 SetLastResult(E_SUCCESS);
1532 _RegistryCore::AddSection(LinkedList& sectionList, const String& sectionName)
1534 result r = E_SUCCESS;
1535 bool sectionFound = false;
1537 // Get section information
1538 unique_ptr<IEnumerator> pEnum(sectionList.GetEnumeratorN());
1541 return GetLastResult();
1544 while (pEnum->MoveNext() == E_SUCCESS)
1546 _RegistrySection* pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1547 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1548 GetErrorMessage(GetLastResult()));
1550 if (pRegSection->__sectionName == sectionName)
1552 sectionFound = true;
1557 if (sectionFound == true)
1559 return E_SECTION_ALREADY_EXIST;
1562 // create a key with section name
1563 unique_ptr<_RegistrySection> pRegSection(new (std::nothrow) _RegistrySection());
1565 SysTryReturnResult(NID_IO, pRegSection != null, E_OUT_OF_MEMORY,
1566 "The memory is insufficient.");
1568 pRegSection->__sectionName = sectionName;
1570 // add new section and section-entry map
1571 r = sectionList.Add(*(pRegSection.release()));
1572 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1578 _RegistryCore::AddValue(LinkedList& sectionList, const String& sectionName, const String& entryName, const String& valStr)
1580 result r = E_SUCCESS;
1581 bool sectionFound = false;
1582 bool entryFound = false;
1583 _RegistrySection* pRegSection = null;
1585 // Get section information
1586 unique_ptr<IEnumerator> pEnum(sectionList.GetEnumeratorN());
1589 return GetLastResult();
1592 while (pEnum->MoveNext() == E_SUCCESS)
1594 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1595 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1596 GetErrorMessage(GetLastResult()));
1598 if (pRegSection->__sectionName == sectionName)
1600 sectionFound = true;
1605 if (sectionFound == false)
1607 return E_SECTION_NOT_FOUND;
1610 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
1613 return GetLastResult();
1616 while (pEnum->MoveNext() == E_SUCCESS)
1618 _RegistryEntry* pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
1619 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1620 GetErrorMessage(GetLastResult()));
1622 if (pRegEntry->entryName == entryName)
1629 if (entryFound == true)
1631 return E_KEY_ALREADY_EXIST;
1634 unique_ptr<_RegistryEntry> pRegEntry(new (std::nothrow) _RegistryEntry());
1635 SysTryReturnResult(NID_IO, pRegEntry != null, E_OUT_OF_MEMORY,
1636 "The memory is insufficient.");
1638 pRegEntry->entryName = entryName;
1639 pRegEntry->entryValue = valStr;
1641 r = pRegSection->__entryList.Add(*(pRegEntry.release()));
1642 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1648 _RegistryCore::CheckSectionName(char* pSectionName)
1655 if (pSectionName == null)
1660 pFrom = pSectionName;
1661 length = strlen(pSectionName);
1662 pTo = pSectionName + (length - 1);
1664 if (!pFrom || !pTo || pFrom > pTo)
1669 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1671 if (*pTemp == '\0' || *pTemp == '\n' || *pTemp == '#' || *pTemp == '=')
1681 _RegistryCore::CheckEntryName(char* pEntryName)
1688 if (pEntryName == null)
1694 length = strlen(pEntryName);
1695 pTo = pEntryName + (length - 1);
1697 if (!pFrom || !pTo || pFrom > pTo)
1702 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1704 if (*pTemp == '\0' || *pTemp == '\n' || *pTemp == '#' || *pTemp == '=')
1714 _RegistryCore::CheckEntryValue(char* pEntryVal)
1721 if (pEntryVal == null)
1727 length = strlen(pEntryVal);
1728 pTo = pEntryVal + (length - 1);
1730 if (!pFrom || !pTo || pFrom > pTo)
1735 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1737 if (*pTemp == '\0' || *pTemp == '\n')
1747 _RegistryCore::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer* pKey)
1749 result r = E_SUCCESS;
1751 if (File::IsFileExist(secureRegPath))
1753 r = GetLastResult();
1756 r = E_FILE_ALREADY_EXIST;
1757 SysLog(NID_IO, "[E_FILE_ALREADY_EXIST] The secure registry already exists.");
1761 SysLog(NID_IO, "[%s]Propagated.", GetErrorMessage(r));
1766 unique_ptr<_NormalRegistry> pNormalReg(new (std::nothrow) _NormalRegistry());
1767 SysTryReturnResult(NID_IO, pNormalReg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1769 r = pNormalReg->Load(plainRegPath, "r");
1770 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1772 unique_ptr<_SecureRegistry> pSecureReg(new (std::nothrow) _SecureRegistry());
1773 SysTryReturnResult(NID_IO, pSecureReg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1775 r = pSecureReg->Construct(secureRegPath, "w+", pKey);
1776 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1778 if (pNormalReg->_pBuffer != null && pNormalReg->_length > 0)
1780 pSecureReg->_pBuffer = pNormalReg->_pBuffer;
1781 pSecureReg->_length = pNormalReg->_length;
1783 pNormalReg->_pBuffer = null;
1784 pNormalReg->_length = 0;
1786 r = pSecureReg->Parse();
1787 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));