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)
98 _RegistryCore::~_RegistryCore(void)
105 _RegistryCore::VerifyRegistryOpenMode(const char* pOpenMode)
107 if (pOpenMode == null)
109 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode is null.");
113 if (strlen(pOpenMode) > _MAX_REG_OPENMODE_LENGTH)
115 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
119 switch (pOpenMode[0])
133 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
137 switch (pOpenMode[1])
142 if (pOpenMode[2] == '\0')
150 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
154 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
162 _RegistryCore::Parse(void)
164 result r = E_SUCCESS;
166 bool sectionFound = false;
169 unique_ptr<char[]> pEntryName(null);
170 unique_ptr<char[]> pEntryValue(null);
171 int firstTokenPos = 0;
174 if (_pBuffer == null)
176 SysLog(NID_IO, "The buffer is empty.");
180 String inputStr = String((char*)_pBuffer);
181 StringTokenizer lineTok(inputStr, L"\n");
183 r = lineTok.GetNextToken(line);
185 while (r == E_SUCCESS)
188 if (line.GetLength() == 0)
190 r = lineTok.GetNextToken(line);
194 // find registry section marker
195 line.GetCharAt(0, ch);
196 if (ch == _REGISTRY_SECTION_MARKER_CHAR)
200 // skip '#' and "\n' at the end
201 r = line.SubString(1, line.GetLength() - 1, sectionName);
202 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
204 // if End of the document follows section name without newline character.
205 sectionName.GetCharAt(sectionName.GetLength() - 1, ch);
208 sectionName.SetLength(sectionName.GetLength() - 1); //Remove '\n'
211 // for compatibility check if line contains '\r' at the end
212 sectionName.GetCharAt(sectionName.GetLength() - 1, ch);
215 sectionName.SetLength(sectionName.GetLength() - 1); //Remove '\r'
218 //TODO: check if remaining data in sectionName is valid or not
219 //after removing '#', '\n', and '\r', sectionName should contain at least 1 valid character
221 sectionName.GetLength() > 0, r = E_PARSING_FAILED, E_PARSING_FAILED,
222 "[E_PARSING_FAILED] Section length cannot be <2.");
224 unique_ptr<char[]> pSectionName(_StringConverter::CopyToCharArrayN(sectionName));
225 SysTryCatch(NID_IO, pSectionName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
226 "[E_OUT_OF_MEMORY] The memory is insufficient.");
228 if (!_RegistryCore::CheckSectionName(pSectionName.get()))
230 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
231 "[E_PARSING_FAILED] Section name could not be parsed.");
234 r = _RegistryCore::AddSection(_sectionList, sectionName);
235 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
237 else // not a section.. but may belongs to a section (entry)
239 // ignores blank lines
240 line.GetCharAt(0, ch);
241 if ((ch == L'\n') || (ch == L'\r'))
243 r = lineTok.GetNextToken(line);
248 // Need to check for tabs, spaces in the beginning of each line
250 // if no section found till now.. ignore
251 if (sectionFound == false)
253 r = lineTok.GetNextToken(line);
257 // if End of the document follows entry value without newline character.
258 line.GetCharAt(line.GetLength() - 1, ch);
261 line.SetLength(line.GetLength() - 1); //Remove '\n'
264 line.GetCharAt(line.GetLength() - 1, ch);
267 line.SetLength(line.GetLength() - 1); //Remove '\r'
270 String entryName("");
273 // we will have sectionitem=value (or) sectionitem= (or) sectionitem
274 // as our string. Break this into entry and value
275 StringTokenizer strTok(line, L"=");
277 // extract entry name
278 if (strTok.HasMoreTokens())
280 line.IndexOf(L'=', 0, firstTokenPos); // position of first occurance of '=' in a line
281 if (firstTokenPos == 0)
283 // entryName should not be empty. i.e., "=value" or just '=' is invalid case
284 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
285 "[E_PARSING_FAILED] Entry name cannot be empty.");
289 strTok.GetNextToken(entryName);
292 else // missing '=' in entry line or entry line does not seem to contain "xxx=xxx" format
294 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED, "[E_PARSING_FAILED] invalid entry Line found.");
297 // check if entry name contains invalid chars
298 pEntryName.reset(_StringConverter::CopyToCharArrayN(entryName));
299 SysTryCatch(NID_IO, pEntryName, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
300 "[E_OUT_OF_MEMORY] The memory is insufficient.");
302 if (!_RegistryCore::CheckEntryName(pEntryName.get()))
304 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
305 "[E_PARSING_FAILED] Entry name could not be parsed.");
308 line.SubString(firstTokenPos + 1, entryVal); // extract entry value
310 // check if entry value contains invalid chars
311 pEntryValue.reset(_StringConverter::CopyToCharArrayN(entryVal));
312 SysTryCatch(NID_IO, pEntryValue, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
313 "[E_OUT_OF_MEMORY] The memory is insufficient.");
315 length = strlen(pEntryValue.get());
318 if (!_RegistryCore::CheckEntryValue(pEntryValue.get()))
320 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
321 "[E_PARSING_FAILED] Entry value could not be parsed.");
325 // add the entry and value
326 r = _RegistryCore::AddValue(_sectionList, sectionName, entryName, entryVal);
327 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
329 r = lineTok.GetNextToken(line);
332 if (r == E_OUT_OF_RANGE)
340 if (r == E_PARSING_FAILED && Tizen::App::_AppInfo::IsOspCompat() == false)
342 r = E_INVALID_FORMAT;
345 SysLog(NID_IO, "[%s] exception occurred.", GetErrorMessage(r));
350 _RegistryCore::Load(const String& regPath, const char* pOpenMode)
352 result r = E_SUCCESS;
353 delete[] _pBuffer; //clear existing buffer
357 // open registry file
358 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl());
359 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
361 r = pFileImpl->Construct(regPath, pOpenMode, null);
362 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Constructing the file has failed.", GetErrorMessage(r));
364 // reset the section list to be sure that its empty
365 _sectionList.RemoveAll(true);
367 if (_truncate == false)
369 // load the registry file
370 r = pFileImpl->ReadN((char**)&_pBuffer, _length);
377 __pFileImpl = pFileImpl.release();
383 _RegistryCore::AddSection(const String& sectionName)
385 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
387 result r = E_SUCCESS;
388 bool sectionFound = false;
390 // Get section information
391 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
394 return GetLastResult();
397 while (pEnum->MoveNext() == E_SUCCESS)
399 _RegistrySection* pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
400 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
401 GetErrorMessage(GetLastResult()));
403 if (pRegSection->__sectionName == sectionName)
410 if (sectionFound == true)
412 return E_SECTION_ALREADY_EXIST;
415 // create a key with section name
416 unique_ptr<_RegistrySection> pRegSection(new (std::nothrow) _RegistrySection());
417 SysTryReturnResult(NID_IO, pRegSection != null, E_OUT_OF_MEMORY,
418 "The memory is insufficient.");
420 pRegSection->__sectionName = sectionName;
422 // add new section and section-entry map
423 r = _sectionList.Add(*(pRegSection.release()));
424 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
431 _RegistryCore::RemoveSection(const String& sectionName)
433 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
435 result r = E_SUCCESS;
436 _RegistrySection* pRegSection = null;
437 bool sectionFound = false;
439 if (_sectionList.GetCount() == 0)
441 return E_SECTION_NOT_FOUND;
444 // Get section information
445 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
448 return GetLastResult();
451 while (pEnum->MoveNext() == E_SUCCESS)
453 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
454 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagating.",
455 GetErrorMessage(GetLastResult()));
457 if (pRegSection->__sectionName == sectionName)
464 if (sectionFound == false)
466 return E_SECTION_NOT_FOUND;
469 r = _sectionList.Remove(*pRegSection, true);
470 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
477 _RegistryCore::GetSectionListN(IList** pRetList)
479 result r = E_SUCCESS;
480 unique_ptr<IEnumerator> pEnum(null);
481 _RegistrySection* pSection = null;
483 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
484 SysTryReturnResult(NID_IO, pRetList != null, E_INVALID_ARG, "pList cannot be null.");
486 // construct an array list to be returned
487 unique_ptr<ArrayList, AllElementsDeleter> pSectionList(new (std::nothrow) ArrayList());
488 SysTryReturnResult(NID_IO, pSectionList != null, E_OUT_OF_MEMORY,
489 "The memory is insufficient.");
491 r = pSectionList->Construct();
492 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
494 // if no sectons, return empty list
495 if (_sectionList.GetCount() == 0)
497 *pRetList = pSectionList.release();
501 // copy from class section list to the new list and return
502 pEnum.reset(_sectionList.GetEnumeratorN());
503 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.", GetErrorMessage(GetLastResult()));
505 while (pEnum->MoveNext() == E_SUCCESS)
507 pSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
508 SysTryReturn(NID_IO, pSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
509 GetErrorMessage(GetLastResult()));
511 unique_ptr<String> pSectionName(new (std::nothrow) String(pSection->__sectionName));
512 SysTryReturnResult(NID_IO, pSectionName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
514 r = pSectionList->Add(*(pSectionName.release()));
515 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
518 *pRetList = pSectionList.release();
524 _RegistryCore::GetEntryList(const String& sectionName, Collection::HashMap& retMap)
526 result r = E_SUCCESS;
527 unique_ptr<IEnumerator> pEnum(null);
528 unique_ptr<String> pKeyEntryStr(null);
529 unique_ptr<String> pKeyValStr(null);
530 bool sectionFound = false;
531 _RegistrySection* pRegSection = null;
532 _RegistryEntry* pRegEntry = null;
534 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
536 if (_sectionList.GetCount() == 0)
538 return E_SECTION_NOT_FOUND;
541 // Get section information
542 pEnum.reset(_sectionList.GetEnumeratorN());
543 SysTryReturnResult(NID_IO, pEnum != null, E_SYSTEM, "Section list is empty.");
545 while (pEnum->MoveNext() == E_SUCCESS)
547 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
548 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
549 GetErrorMessage(GetLastResult()));
551 if (pRegSection->__sectionName == sectionName)
558 if (sectionFound == false)
560 return E_SECTION_NOT_FOUND;
563 // copy from item list to the new list and return
564 pEnum.reset((pRegSection->__entryList).GetEnumeratorN());
565 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
566 GetErrorMessage(GetLastResult()));
568 while (pEnum->MoveNext() == E_SUCCESS)
570 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
571 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
572 GetErrorMessage(GetLastResult()));
574 pKeyEntryStr.reset(new (std::nothrow) String((pRegEntry->entryName)));
575 SysTryCatch(NID_IO, pKeyEntryStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
576 "[E_OUT_OF_MEMORY] The memory is insufficient.");
577 pKeyValStr.reset(new (std::nothrow) String((pRegEntry->entryValue)));
578 SysTryCatch(NID_IO, pKeyValStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
579 "[E_OUT_OF_MEMORY] The memory is insufficient.");
581 r = retMap.Add(*(pKeyEntryStr.release()), *(pKeyValStr.release()));
582 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
588 retMap.RemoveAll(true);
594 _RegistryCore::GetEntryListN(const String& sectionName, HashMap** pRetList)
596 result r = E_SUCCESS;
598 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
599 SysTryReturnResult(NID_IO, pRetList != null, E_INVALID_ARG, "pRetList cannot be null.");
601 // construct an array list to be returned
602 unique_ptr<HashMap, AllElementsDeleter> pEntryList(new (std::nothrow) HashMap());
603 SysTryReturnResult(NID_IO, pEntryList != null, E_OUT_OF_MEMORY,
604 "The memory is insufficient.");
605 r = pEntryList->Construct();
606 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
607 r = GetEntryList(sectionName, *(pEntryList.get()));
608 *pRetList = pEntryList.release();
614 _RegistryCore::GetAllEntriesN(const String& sectionName)
616 result r = E_SUCCESS;
617 HashMap* pMap = null;
619 r = GetEntryListN(sectionName, &pMap);
620 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
622 SetLastResult(E_SUCCESS);
631 _RegistryCore::GetAllEntryNamesN(const String& sectionName)
633 result r = E_SUCCESS;
635 unique_ptr<ArrayList, AllElementsDeleter> pEntryList(new (std::nothrow) ArrayList());
636 SysTryReturn(NID_IO, pEntryList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
638 r = pEntryList->Construct();
639 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
641 unique_ptr<IMap, AllElementsDeleter> pMap(this->GetAllEntriesN(sectionName));
642 SysTryReturn(NID_IO, pMap != null, null, GetLastResult(), "[%s] Getting all entries was failed.",
643 GetErrorMessage(GetLastResult()));
645 unique_ptr<IMapEnumerator> pMapEnum(pMap->GetMapEnumeratorN());
646 SysTryReturn(NID_IO, pMapEnum, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
647 while (pMapEnum->MoveNext() == E_SUCCESS)
649 String* pKey = dynamic_cast< String* >(pMapEnum->GetKey());
650 SysTryReturn(NID_IO, pKey != null, null, E_IO, "[E_OUT_OF_MEMORY] The system error occurred.");
652 String* pEntryName = new (std::nothrow) String(*pKey);
653 SysTryReturn(NID_IO, pEntryName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
655 r = pEntryList->Add(*pEntryName);
656 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
659 SetLastResult(E_SUCCESS);
660 return pEntryList.release();
664 _RegistryCore::GetValue(const String& sectionName, const String& entryName, String& valStr)
666 bool sectionFound = false;
667 bool entryFound = false;
668 _RegistrySection* pRegSection = null;
669 _RegistryEntry* pRegEntry = null;
671 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
673 if (_sectionList.GetCount() == 0)
675 return E_SECTION_NOT_FOUND;
678 // Get section information
679 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
682 return GetLastResult();
685 while (pEnum->MoveNext() == E_SUCCESS)
687 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
688 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
689 GetErrorMessage(GetLastResult()));
691 if (pRegSection->__sectionName == sectionName)
698 if (sectionFound == false)
700 return E_SECTION_NOT_FOUND;
703 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
706 return GetLastResult();
709 while (pEnum->MoveNext() == E_SUCCESS)
711 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
712 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
713 GetErrorMessage(GetLastResult()));
715 if (pRegEntry->entryName == entryName)
717 valStr = pRegEntry->entryValue;
723 SysTryLog(NID_IO, entryFound, "Entry not found");
725 return((entryFound == true) ? E_SUCCESS : E_KEY_NOT_FOUND);
729 _RegistryCore::AddValue(const String& sectionName, const String& entryName, const String& valStr)
731 result r = E_SUCCESS;
732 bool sectionFound = false;
733 bool entryFound = false;
734 _RegistrySection* pRegSection = null;
737 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
739 // Get section information
740 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
743 return GetLastResult();
746 while (pEnum->MoveNext() == E_SUCCESS)
748 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
749 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
750 GetErrorMessage(GetLastResult()));
752 if (pRegSection->__sectionName == sectionName)
759 if (sectionFound == false)
761 return E_SECTION_NOT_FOUND;
764 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
767 return GetLastResult();
770 while (pEnum->MoveNext() == E_SUCCESS)
772 _RegistryEntry* pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
773 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
774 GetErrorMessage(GetLastResult()));
776 if (pRegEntry->entryName == entryName)
783 if (entryFound == true)
785 return E_KEY_ALREADY_EXIST;
788 unique_ptr<_RegistryEntry> pRegEntry(new (std::nothrow) _RegistryEntry());
789 SysTryReturnResult(NID_IO, pRegEntry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
791 unique_ptr<char[]> pEntryName(_StringConverter::CopyToCharArrayN(entryName));
792 SysTryReturnResult(NID_IO, pEntryName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
794 length = strlen(pEntryName.get());
795 if (!_RegistryCore::CheckEntryName(pEntryName.get()))
797 SysTryReturnResult(NID_IO, false, E_PARSING_FAILED, "Entry name could not be parsed.");
800 unique_ptr<char[]> pEntryValue(_StringConverter::CopyToCharArrayN(valStr));
801 SysTryReturnResult(NID_IO, pEntryValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
803 length = strlen(pEntryValue.get());
804 if (length > 0 && !_RegistryCore::CheckEntryValue(pEntryValue.get()))
806 SysTryReturnResult(NID_IO, false, E_PARSING_FAILED, "Entry value could not be parsed.");
809 pRegEntry->entryName = entryName;
810 pRegEntry->entryValue = valStr;
812 r = pRegSection->__entryList.Add(*(pRegEntry.release()));
813 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
820 _RegistryCore::SetValue(const String& sectionName, const String& entryName, const String& valStr)
822 result r = E_SUCCESS;
823 bool sectionFound = false;
824 bool entryFound = false;
825 _RegistryEntry* pRegEntry = null;
826 _RegistrySection* pRegSection = null;
830 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
832 if (_sectionList.GetCount() == 0)
834 return E_SECTION_NOT_FOUND;
837 // Get section information
838 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
841 return GetLastResult();
844 while (pEnum->MoveNext() == E_SUCCESS)
846 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
847 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
848 GetErrorMessage(GetLastResult()));
850 if (pRegSection->__sectionName == sectionName)
857 if (sectionFound == false)
859 return E_SECTION_NOT_FOUND;
862 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
865 return GetLastResult();
868 while (pEnum->MoveNext() == E_SUCCESS)
870 pRegEntry = dynamic_cast< _RegistryEntry* >(pEnum->GetCurrent());
871 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
872 GetErrorMessage(GetLastResult()));
874 if (pRegEntry->entryName == entryName)
882 if (entryFound == false)
884 return E_KEY_NOT_FOUND;
887 if (valStr.GetLength() > 0)
889 unique_ptr<char[]> pEntryValue(_StringConverter::CopyToCharArrayN(valStr));
890 SysTryReturnResult(NID_IO, pEntryValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
892 length = strlen(pEntryValue.get());
893 if (!_RegistryCore::CheckEntryValue(pEntryValue.get()))
895 return E_PARSING_FAILED;
899 pRegEntry->entryValue = valStr;
900 r = pRegSection->__entryList.SetAt(*pRegEntry, entryIndex);
901 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
908 _RegistryCore::RemoveValue(const String& sectionName, const String& entryName)
910 result r = E_SUCCESS;
911 bool sectionFound = false;
912 bool entryFound = false;
913 _RegistryEntry* pRegEntry = null;
914 _RegistrySection* pRegSection = null;
916 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
918 if (_sectionList.GetCount() <= 0)
920 return E_SECTION_NOT_FOUND;
923 // Get section information
924 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
927 return E_SECTION_NOT_FOUND;
930 while (pEnum->MoveNext() == E_SUCCESS)
932 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
933 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
934 GetErrorMessage(GetLastResult()));
936 if (pRegSection->__sectionName == sectionName)
943 if (sectionFound == false)
945 return E_SECTION_NOT_FOUND;
948 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
951 return E_SECTION_NOT_FOUND;
954 while (pEnum->MoveNext() == E_SUCCESS)
956 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
957 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
958 GetErrorMessage(GetLastResult()));
960 if (pRegEntry->entryName == entryName)
967 if (entryFound == false)
969 return E_KEY_NOT_FOUND;
972 r = pRegSection->__entryList.Remove(*pRegEntry, true);
973 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
980 _RegistryCore::Flush(void)
986 _RegistryCore::Write(void)
988 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
990 if (_pBuffer == null)
992 SysLog(NID_IO, "The buffer is empty.");
996 result r = E_SUCCESS;
998 String openMode(L"w+");
999 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
1000 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1002 //TODO: Secure mode is to be handled
1003 r = pFileImpl->Construct(_regPath, "w+", null);
1004 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1006 r = pFileImpl->Write(_pBuffer, _length);
1007 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1009 r = pFileImpl->Flush();
1010 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1016 _RegistryCore::PrepareToWrite(void)
1018 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1020 result r = E_SUCCESS;
1021 _RegistryEntry* pRegEntry = null;
1022 _RegistrySection* pRegSection = null;
1025 delete[] _pBuffer; //clear existing buffer
1029 if (_write == false)
1031 SysLog(NID_IO, "[E_ILLEGAL_ACCESS] Registry cannot be flushed as it was opened in read only mode.");
1032 return E_ILLEGAL_ACCESS;
1035 // if no sections, do nothing
1036 if (_sectionList.GetCount() == 0)
1038 unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(_regPath));
1039 truncate(pFilePath.get(), 0);
1044 // write the registry map to a file
1045 // Get section information
1046 unique_ptr<IEnumerator> pSectionEnum(_sectionList.GetEnumeratorN());
1047 SysTryReturn(NID_IO, pSectionEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.",
1048 GetErrorMessage(GetLastResult()));
1050 while (pSectionEnum->MoveNext() == E_SUCCESS)
1053 pRegSection = dynamic_cast <_RegistrySection*>(pSectionEnum->GetCurrent());
1054 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1055 GetErrorMessage(GetLastResult()));
1057 // write section name to file
1058 r = inputBuffer.Append(String(L"#" + pRegSection->__sectionName + String(L'\n')));
1059 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1061 // read the entries for this section
1062 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1063 SysTryReturn(NID_IO, pSectionEnum != null, GetLastResult(), GetLastResult(), "[%s] Entry list is empty.",
1064 GetErrorMessage(GetLastResult()));
1066 while (pEntryEnum->MoveNext() == E_SUCCESS)
1068 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1069 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1070 GetErrorMessage(GetLastResult()));
1072 // write entry name to file
1073 r = inputBuffer.Append(pRegEntry->entryName + String(L"=" + pRegEntry->entryValue + String(L'\n')));
1074 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1079 _pBuffer = (byte*)_StringConverter::CopyToCharArrayN(inputBuffer);
1080 SysTryReturn(NID_IO, _pBuffer != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1081 GetErrorMessage(GetLastResult()));
1083 _length = strlen((char*)_pBuffer);
1089 _RegistryCore::GetSectionIndex(const String& sectionName)
1091 bool sectionFound = false;
1092 _RegistrySection* pRegSection = null;
1095 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1096 SysTryReturn(NID_IO, sectionName.GetLength() > 0, -1, E_INVALID_ARG,
1097 "[E_INVALID_ARG] sectionName.GetLength() <= 0");
1099 SysTryReturn(NID_IO, _sectionList.GetCount() > 0, -1, E_SECTION_NOT_FOUND, "[E_SECTION_NOT_FOUND] Section not found.");
1101 // Get section information
1102 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
1103 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.",
1104 GetErrorMessage(GetLastResult()));
1106 while (pEnum->MoveNext() == E_SUCCESS)
1108 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1109 SysTryReturn(NID_IO, pRegSection != null, -1, GetLastResult(), "[%s] Propagated.",
1110 GetErrorMessage(GetLastResult()));
1112 if (pRegSection->__sectionName == sectionName)
1114 sectionFound = true;
1120 if (sectionFound == false)
1122 return E_SECTION_NOT_FOUND;
1125 SetLastResult(E_SUCCESS);
1130 _RegistryCore::Removekey(const String& sectionName, const String& keyName)
1132 result r = E_SUCCESS;
1133 int sectionIndex = -1;
1134 int entryIndex = -1;
1136 sectionIndex = GetSectionIndex(sectionName);
1137 r = GetLastResult();
1138 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1140 entryIndex = GetEntryIndex(sectionIndex, keyName);
1141 r = GetLastResult();
1142 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1144 RemoveEntry(sectionIndex, entryIndex);
1145 r = GetLastResult();
1146 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1148 SetLastResult(E_SUCCESS);
1153 _RegistryCore::GetAllSectionCount(void)
1155 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1157 SetLastResult(E_SUCCESS);
1158 return _sectionList.GetCount();
1162 _RegistryCore::GetAllEntryCount(int sectionIndex)
1164 _RegistrySection* pRegSection = null;
1167 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1168 SysTryReturn(NID_IO, sectionIndex >= 0, 0, E_INVALID_ARG, "[E_INVALID_ARG] sectionIndex < 0.");
1169 listSize = _sectionList.GetCount();
1170 SysTryReturn(NID_IO, listSize > 0, 0, E_SECTION_NOT_FOUND, "[E_INVALID_ARG] section listSize <= 0.");
1171 SysTryReturn(NID_IO, sectionIndex >= listSize, 0, E_SECTION_NOT_FOUND,
1172 "[E_INVALID_ARG] sectionIndex > listSize.");
1174 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1175 SysTryReturn(NID_IO, pRegSection != null, 0, E_SECTION_NOT_FOUND,
1176 "[E_SECTION_NOT_FOUND] section was not found.");
1178 SetLastResult(E_SUCCESS);
1179 return pRegSection->__entryList.GetCount();
1183 _RegistryCore::RemoveEntry(int sectionIndex, int entryIndex)
1185 result r = E_SUCCESS;
1187 int entryListSize = 0;
1188 _RegistrySection* pRegSection = null;
1190 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1191 SysTryReturnVoidResult(NID_IO, sectionIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG] sectionIndex < 0.");
1192 SysTryReturnVoidResult(NID_IO, entryIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG] entryIndex < 0.");
1194 listSize = _sectionList.GetCount();
1195 SysTryReturnVoidResult(NID_IO, listSize > 0, E_SECTION_NOT_FOUND,
1196 "[E_SECTION_NOT_FOUND] section listSize <= 0.");
1197 SysTryReturnVoidResult(NID_IO, sectionIndex < listSize, E_SECTION_NOT_FOUND,
1198 "[E_SECTION_NOT_FOUND] sectionIndex >= listSize.");
1200 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1201 SysTryReturnVoidResult(NID_IO, pRegSection != null, E_SECTION_NOT_FOUND,
1202 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1204 r = GetLastResult();
1205 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r,"[%s] Propagated.", GetErrorMessage(r));
1207 entryListSize = pRegSection->__entryList.GetCount();
1208 SysTryReturnVoidResult(NID_IO, (entryListSize > 0 || entryIndex < entryListSize), E_KEY_NOT_FOUND,
1209 "[E_KEY_NOT_FOUND] entryListSize is 0 or entryIndex >= entryListSize.");
1211 r = pRegSection->__entryList.RemoveAt(entryIndex, true);
1212 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1214 SetLastResult(E_SUCCESS);
1218 _RegistryCore::ModifyEntryValue(int sectionIndex, int entryIndex, _RegValueType type, const void* pValue, int size)
1220 SetLastResult(E_UNSUPPORTED_OPERATION);
1224 _RegistryCore::GetEntryIndex(int sectionIndex, const String& entryName)
1226 _RegistrySection* pRegSection = null;
1227 _RegistryEntry* pRegEntry = null;
1229 bool entryFound = false;
1230 int entryIndex = -1;
1232 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1233 SysTryReturn(NID_IO, sectionIndex >= 0, -1, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1234 SysTryReturn(NID_IO, entryName.GetLength() > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG]entryName length < 0.");
1236 listSize = _sectionList.GetCount();
1237 SysTryReturn(NID_IO, listSize > 0, -1, E_SECTION_NOT_FOUND,
1238 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1239 SysTryReturn(NID_IO, sectionIndex < listSize, -1, E_SECTION_NOT_FOUND,
1240 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1242 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1243 SysTryReturn(NID_IO, pRegSection != null, -1, E_SECTION_NOT_FOUND,
1244 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1246 // read the entries for this section
1247 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1248 SysTryReturn(NID_IO, pEntryEnum != null, -1, GetLastResult(), "[%s] Entry list is empty.",
1249 GetErrorMessage(GetLastResult()));
1252 while (pEntryEnum->MoveNext() == E_SUCCESS)
1254 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1255 SysTryReturn(NID_IO, pRegEntry != null, -1, GetLastResult(), "[%s] Propagated.",
1256 GetErrorMessage(GetLastResult()));
1258 if (entryName == pRegEntry->entryName)
1266 SysTryReturn(NID_IO, entryFound != false, -1, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] entry was not found.");
1268 SetLastResult(E_SUCCESS);
1273 _RegistryCore::GetEntryName(int sectionIndex, int entryIndex)
1275 _RegistrySection* pRegSection = null;
1276 _RegistryEntry* pRegEntry = null;
1281 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1282 SysTryReturn(NID_IO, sectionIndex >= 0, empty, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1283 SysTryReturn(NID_IO, entryIndex >= 0, empty, E_INVALID_ARG, "[E_INVALID_ARG]entryIndex < 0.");
1285 listSize = _sectionList.GetCount();
1286 SysTryReturn(NID_IO, listSize > 0, empty, E_SECTION_NOT_FOUND,
1287 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1288 SysTryReturn(NID_IO, sectionIndex < listSize, empty, E_SECTION_NOT_FOUND,
1289 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1291 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1292 SysTryReturn(NID_IO, pRegSection != null, empty, E_SECTION_NOT_FOUND,
1293 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1295 // read the entries for this section
1296 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1297 SysTryReturn(NID_IO, pEntryEnum != null, empty, GetLastResult(), "[%s] Entry list is empty.",
1298 GetErrorMessage(GetLastResult()));
1300 tmpIndex = entryIndex;
1301 while (pEntryEnum->MoveNext() == E_SUCCESS)
1305 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1306 SysTryReturn(NID_IO, pRegEntry != null, empty, GetLastResult(), "[%s] Propagated.",
1307 GetErrorMessage(GetLastResult()));
1314 SysTryReturn(NID_IO, pRegEntry != null, empty, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] entry was not found.");
1316 SetLastResult(E_SUCCESS);
1317 return pRegEntry->entryName;
1321 _RegistryCore::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType type, void* pValue, int* pSize)
1323 _RegistrySection* pRegSection = null;
1324 _RegistryEntry* pRegEntry = null;
1325 result r = E_SUCCESS;
1327 int tmpEntryIndex = -1;
1328 int entryListSize = 0;
1329 String strValueEncoded;
1331 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1332 SysTryReturnVoidResult(NID_IO, sectionIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1333 SysTryReturnVoidResult(NID_IO, type >= 0 && type <= REG_VALUE_TYPE_MAX, E_INVALID_ARG,
1334 "[E_INVALID_ARG] invalid registry type.");
1335 SysTryReturnVoidResult(NID_IO, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG]pValue is null.");
1336 SysTryReturnVoidResult(NID_IO, pSize != null, E_INVALID_ARG, "[E_INVALID_ARG]pSize is null.");
1337 SysTryReturnVoidResult(NID_IO, *pSize > 0, E_INVALID_ARG, "[E_INVALID_ARG] *pSize < 0.");
1339 listSize = _sectionList.GetCount();
1340 SysTryReturnVoidResult(NID_IO, listSize > 0, E_SECTION_NOT_FOUND,
1341 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1342 SysTryReturnVoidResult(NID_IO, sectionIndex < listSize, E_SECTION_NOT_FOUND,
1343 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1345 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1346 SysTryReturnVoidResult(NID_IO, pRegSection != null, E_SECTION_NOT_FOUND,
1347 "[E_SECTION_NOT_FOUND] section at index (%d) is not available.", sectionIndex);
1349 entryListSize = pRegSection->__entryList.GetCount();
1350 SysTryReturnVoidResult(NID_IO, entryListSize > 0, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry list is empty.");
1351 SysTryReturnVoidResult(NID_IO, entryIndex < entryListSize, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry not found.");
1353 // read the entries for this section
1354 unique_ptr< IEnumerator > pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1355 SysTryReturnVoidResult(NID_IO, pEntryEnum != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND]Entry list is empty.");
1357 tmpEntryIndex = entryIndex;
1358 while (pEntryEnum->MoveNext() == E_SUCCESS)
1360 if (entryIndex == 0)
1362 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1363 SysTryReturnVoidResult(NID_IO, pRegEntry != null, GetLastResult(), "[%s] Propagated.",
1364 GetErrorMessage(GetLastResult()));
1372 SysTryReturnVoidResult(NID_IO, pRegEntry != null, E_KEY_NOT_FOUND,
1373 "[E_KEY_NOT_FOUND] Entry was not found.");
1375 strValueEncoded = pRegEntry->entryValue;
1377 DecodeData(strValueEncoded, type, pValue, pSize);
1378 r = GetLastResult();
1379 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1381 SetLastResult(E_SUCCESS);
1386 _RegistryCore::GetSectionName(int sectionIndex)
1388 _RegistrySection* pRegSection = null;
1389 result r = E_SUCCESS;
1391 String sectionName("");
1393 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1394 SysTryReturn(NID_IO, sectionIndex >= 0, sectionName, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1396 listSize = _sectionList.GetCount();
1397 SysTryReturn(NID_IO, listSize > 0, sectionName, E_SECTION_NOT_FOUND,
1398 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1399 SysTryReturn(NID_IO, sectionIndex < listSize, sectionName, E_SECTION_NOT_FOUND,
1400 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1402 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1403 SysTryReturn(NID_IO, pRegSection != null, sectionName, E_SECTION_NOT_FOUND,
1404 "[E_SECTION_NOT_FOUND] sectionIndex is not available.");
1406 r = GetLastResult();
1407 SysTryReturn(NID_IO, !IsFailed(r), sectionName, r, "[%s] Propagated.", GetErrorMessage(r));
1409 SetLastResult(E_SUCCESS);
1410 return pRegSection->__sectionName;
1414 _RegistryCore::GetSectionByIndex(int sectionIndex)
1416 _RegistrySection* pRegSection = null;
1417 int index = sectionIndex;
1419 // Get section information
1420 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
1421 SysTryReturn(NID_IO, pEnum != null, null, GetLastResult(),
1422 "[%s] section list is empty.", GetErrorMessage(GetLastResult()));
1424 while (pEnum->MoveNext() == E_SUCCESS)
1428 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
1429 SysTryReturn(NID_IO, pRegSection != null, null, GetLastResult(), "[%s] Propagated.",
1430 GetErrorMessage(GetLastResult()));
1437 SysTryReturn(NID_IO, pRegSection != null, null, E_SECTION_NOT_FOUND,
1438 "[E_SECTION_NOT_FOUND] Section was not found.");
1440 SetLastResult(E_SUCCESS);
1446 _RegistryCore::DecodeData(const String& strValueEncoded, _RegValueType type, void* pValue, int* pSize)
1448 SysAssert(strValueEncoded.GetLength() > 0);
1451 SysAssert(pSize && *pSize > 0);
1453 SysTryReturnVoidResult(NID_IO, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument.");
1454 SysTryReturnVoidResult(NID_IO, pSize != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument.");
1458 case REG_VALUE_TYPE_INT: // (-2147483647 - 1) ~ 2147483647
1461 result r = E_SUCCESS;
1463 if (*pSize != sizeof(int))
1465 SetLastResult(E_INVALID_ARG);
1469 r = Integer::Parse(strValueEncoded, retIntVal);
1470 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1472 *(int*) pValue = retIntVal;
1476 case REG_VALUE_TYPE_REAL: // Holds signed IEEE 64-bit (8-byte) double-precision floating-point numbers
1477 // ranging in value from -1.79769313486231570E+308 through -4.94065645841246544E-324
1478 // for negative values and from 4.94065645841246544E-324 through 1.79769313486231570E+308
1479 // for positive values.
1481 double retDoubleVal = 0;
1482 result r = E_SUCCESS;
1484 if (*pSize != sizeof(double))
1486 SetLastResult(E_INVALID_ARG);
1490 r = Double::Parse(strValueEncoded, retDoubleVal);
1491 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1493 *(double*) pValue = retDoubleVal;
1497 case REG_VALUE_TYPE_STRING:
1499 // if(*pSize != sizeof(String))
1501 // SetLastResult(E_INVALID_ARG);
1504 *(String*) pValue = strValueEncoded;
1508 case REG_VALUE_TYPE_BINARY:
1510 SetLastResult(E_PARSING_FAILED);
1514 case REG_VALUE_TYPE_UUID:
1517 result r = E_SUCCESS;
1519 r = UuId::Parse(strValueEncoded, retUuIdVal);
1520 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1522 *(UuId*) pValue = retUuIdVal;
1526 case REG_VALUE_TYPE_UUIDSET:
1528 SetLastResult(E_PARSING_FAILED);
1533 SysAssert(0); // should not happen!
1537 SetLastResult(E_SUCCESS);
1542 _RegistryCore::AddSection(LinkedList& sectionList, const String& sectionName)
1544 result r = E_SUCCESS;
1545 bool sectionFound = false;
1547 // Get section information
1548 unique_ptr<IEnumerator> pEnum(sectionList.GetEnumeratorN());
1551 return GetLastResult();
1554 while (pEnum->MoveNext() == E_SUCCESS)
1556 _RegistrySection* pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1557 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1558 GetErrorMessage(GetLastResult()));
1560 if (pRegSection->__sectionName == sectionName)
1562 sectionFound = true;
1567 if (sectionFound == true)
1569 return E_SECTION_ALREADY_EXIST;
1572 // create a key with section name
1573 unique_ptr<_RegistrySection> pRegSection(new (std::nothrow) _RegistrySection());
1575 SysTryReturnResult(NID_IO, pRegSection != null, E_OUT_OF_MEMORY,
1576 "The memory is insufficient.");
1578 pRegSection->__sectionName = sectionName;
1580 // add new section and section-entry map
1581 r = sectionList.Add(*(pRegSection.release()));
1582 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1588 _RegistryCore::AddValue(LinkedList& sectionList, const String& sectionName, const String& entryName, const String& valStr)
1590 result r = E_SUCCESS;
1591 bool sectionFound = false;
1592 bool entryFound = false;
1593 _RegistrySection* pRegSection = null;
1595 // Get section information
1596 unique_ptr<IEnumerator> pEnum(sectionList.GetEnumeratorN());
1599 return GetLastResult();
1602 while (pEnum->MoveNext() == E_SUCCESS)
1604 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1605 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1606 GetErrorMessage(GetLastResult()));
1608 if (pRegSection->__sectionName == sectionName)
1610 sectionFound = true;
1615 if (sectionFound == false)
1617 return E_SECTION_NOT_FOUND;
1620 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
1623 return GetLastResult();
1626 while (pEnum->MoveNext() == E_SUCCESS)
1628 _RegistryEntry* pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
1629 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1630 GetErrorMessage(GetLastResult()));
1632 if (pRegEntry->entryName == entryName)
1639 if (entryFound == true)
1641 return E_KEY_ALREADY_EXIST;
1644 unique_ptr<_RegistryEntry> pRegEntry(new (std::nothrow) _RegistryEntry());
1645 SysTryReturnResult(NID_IO, pRegEntry != null, E_OUT_OF_MEMORY,
1646 "The memory is insufficient.");
1648 pRegEntry->entryName = entryName;
1649 pRegEntry->entryValue = valStr;
1651 r = pRegSection->__entryList.Add(*(pRegEntry.release()));
1652 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1658 _RegistryCore::CheckSectionName(char* pSectionName)
1665 if (pSectionName == null)
1670 pFrom = pSectionName;
1671 length = strlen(pSectionName);
1672 pTo = pSectionName + (length - 1);
1674 if (!pFrom || !pTo || pFrom > pTo)
1679 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1681 if (*pTemp == '\0' || *pTemp == '\n' || *pTemp == '#' || *pTemp == '=')
1691 _RegistryCore::CheckEntryName(char* pEntryName)
1698 if (pEntryName == null)
1704 length = strlen(pEntryName);
1705 pTo = pEntryName + (length - 1);
1707 if (!pFrom || !pTo || pFrom > pTo)
1712 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1714 if (*pTemp == '\0' || *pTemp == '\n' || *pTemp == '#' || *pTemp == '=')
1724 _RegistryCore::CheckEntryValue(char* pEntryVal)
1731 if (pEntryVal == null)
1737 length = strlen(pEntryVal);
1738 pTo = pEntryVal + (length - 1);
1740 if (!pFrom || !pTo || pFrom > pTo)
1745 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1747 if (*pTemp == '\0' || *pTemp == '\n')
1757 _RegistryCore::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer* pKey)
1759 result r = E_SUCCESS;
1761 if (File::IsFileExist(secureRegPath))
1763 r = GetLastResult();
1766 r = E_FILE_ALREADY_EXIST;
1767 SysLog(NID_IO, "[E_FILE_ALREADY_EXIST] The secure registry already exists.");
1771 SysLog(NID_IO, "[%s]Propagated.", GetErrorMessage(r));
1776 unique_ptr<_NormalRegistry> pNormalReg(new (std::nothrow) _NormalRegistry());
1777 SysTryReturnResult(NID_IO, pNormalReg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1779 r = pNormalReg->Load(plainRegPath, "r");
1780 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1782 unique_ptr<_SecureRegistry> pSecureReg(new (std::nothrow) _SecureRegistry());
1783 SysTryReturnResult(NID_IO, pSecureReg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1785 r = pSecureReg->Construct(secureRegPath, "w+", pKey);
1786 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1788 if (pNormalReg->_pBuffer != null && pNormalReg->_length > 0)
1790 pSecureReg->_pBuffer = pNormalReg->_pBuffer;
1791 pSecureReg->_length = pNormalReg->_length;
1793 pNormalReg->_pBuffer = null;
1794 pNormalReg->_length = 0;
1796 r = pSecureReg->Parse();
1797 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1800 r = pSecureReg->Flush();
1801 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1807 _RegistryCore::LockN(FileLockType lockType)
1809 SysAssertf(_constructed == true && __pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
1810 return __pFileImpl->LockN(lockType);
1814 _RegistryCore::TryToLockN(FileLockType lockType)
1816 SysAssertf(_constructed == true && __pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
1817 return __pFileImpl->TryToLockN(lockType);