2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FIo_RegistryCore.cpp
19 * @brief This is the implementation file for _RegistryCore class.
24 #include <unique_ptr.h>
26 #include <FBaseInteger.h>
27 #include <FBaseDouble.h>
28 #include <FBaseFloat.h>
29 #include <FBaseUuId.h>
30 #include <FBaseByteBuffer.h>
31 #include <FBaseColIEnumerator.h>
32 #include <FBaseUtilStringTokenizer.h>
33 #include <FBaseResult.h>
34 #include <FBaseSysLog.h>
35 #include <FBaseColAllElementsDeleter.h>
37 #include <FIoRegistry.h>
39 #include <FBase_StringConverter.h>
40 #include <FApp_AppInfo.h>
41 #include "FIo_FileImpl.h"
42 #include "FIo_NormalRegistry.h"
43 #include "FIo_SecureRegistry.h"
44 #include "FIo_RegistryCore.h"
45 #include "FIo_SecureIoUtil.h"
48 using namespace Tizen::Base;
49 using namespace Tizen::Base::Utility;
50 using namespace Tizen::Base::Collection;
52 namespace Tizen { namespace Io
55 static const size_t _MAX_REG_OPENMODE_LENGTH = 2;
57 #define _REGISTRY_SECTION_MARKER_CHAR L'#'
58 #define _REGISTRY_SECTION_VALUE_ASSIGNMENT_MARKER_CHAR L'='
60 class _RegistrySection
64 ~_RegistrySection(void);
67 LinkedList __entryList;
68 friend class _RegistryCore;
71 _RegistrySection::~_RegistrySection(void)
73 __entryList.RemoveAll(true);
84 _RegistryCore::_RegistryCore(void)
97 _RegistryCore::~_RegistryCore(void)
103 _RegistryCore::VerifyRegistryOpenMode(const char* pOpenMode)
105 if (pOpenMode == null)
107 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode is null.");
111 if (strlen(pOpenMode) > _MAX_REG_OPENMODE_LENGTH)
113 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
117 switch (pOpenMode[0])
131 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
135 switch (pOpenMode[1])
140 if (pOpenMode[2] == '\0')
148 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
152 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
160 _RegistryCore::Parse(void)
162 result r = E_SUCCESS;
164 bool sectionFound = false;
167 unique_ptr<char[]> pEntryName(null);
168 unique_ptr<char[]> pEntryValue(null);
169 int firstTokenPos = 0;
172 if (_pBuffer == null)
174 SysLog(NID_IO, "The buffer is empty.");
178 String inputStr = String((char*)_pBuffer);
179 StringTokenizer lineTok(inputStr, L"\n");
181 r = lineTok.GetNextToken(line);
183 while (r == E_SUCCESS)
186 if (line.GetLength() == 0)
188 r = lineTok.GetNextToken(line);
192 // find registry section marker
193 line.GetCharAt(0, ch);
194 if (ch == _REGISTRY_SECTION_MARKER_CHAR)
198 // skip '#' and "\n' at the end
199 r = line.SubString(1, line.GetLength() - 1, sectionName);
200 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
202 // if End of the document follows section name without newline character.
203 sectionName.GetCharAt(sectionName.GetLength() - 1, ch);
206 sectionName.SetLength(sectionName.GetLength() - 1); //Remove '\n'
209 // for compatibility check if line contains '\r' at the end
210 sectionName.GetCharAt(sectionName.GetLength() - 1, ch);
213 sectionName.SetLength(sectionName.GetLength() - 1); //Remove '\r'
216 //TODO: check if remaining data in sectionName is valid or not
217 //after removing '#', '\n', and '\r', sectionName should contain at least 1 valid character
219 sectionName.GetLength() > 0, r = E_PARSING_FAILED, E_PARSING_FAILED,
220 "[E_PARSING_FAILED] Section length cannot be <2.");
222 unique_ptr<char[]> pSectionName(_StringConverter::CopyToCharArrayN(sectionName));
223 SysTryCatch(NID_IO, pSectionName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
224 "[E_OUT_OF_MEMORY] The memory is insufficient.");
226 if (!_RegistryCore::CheckSectionName(pSectionName.get()))
228 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
229 "[E_PARSING_FAILED] Section name could not be parsed.");
232 r = _RegistryCore::AddSection(_sectionList, sectionName);
233 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
235 else // not a section.. but may belongs to a section (entry)
237 // ignores blank lines
238 line.GetCharAt(0, ch);
239 if ((ch == L'\n') || (ch == L'\r'))
241 r = lineTok.GetNextToken(line);
246 // Need to check for tabs, spaces in the beginning of each line
248 // if no section found till now.. ignore
249 if (sectionFound == false)
251 r = lineTok.GetNextToken(line);
255 // if End of the document follows entry value without newline character.
256 line.GetCharAt(line.GetLength() - 1, ch);
259 line.SetLength(line.GetLength() - 1); //Remove '\n'
262 line.GetCharAt(line.GetLength() - 1, ch);
265 line.SetLength(line.GetLength() - 1); //Remove '\r'
268 String entryName("");
271 // we will have sectionitem=value (or) sectionitem= (or) sectionitem
272 // as our string. Break this into entry and value
273 StringTokenizer strTok(line, L"=");
275 // extract entry name
276 if (strTok.HasMoreTokens())
278 line.IndexOf(L'=', 0, firstTokenPos); // position of first occurance of '=' in a line
279 if (firstTokenPos == 0)
281 // entryName should not be empty. i.e., "=value" or just '=' is invalid case
282 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
283 "[E_PARSING_FAILED] Entry name cannot be empty.");
287 strTok.GetNextToken(entryName);
290 else // missing '=' in entry line or entry line does not seem to contain "xxx=xxx" format
292 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED, "[E_PARSING_FAILED] invalid entry Line found.");
295 // check if entry name contains invalid chars
296 pEntryName.reset(_StringConverter::CopyToCharArrayN(entryName));
297 SysTryCatch(NID_IO, pEntryName, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
298 "[E_OUT_OF_MEMORY] The memory is insufficient.");
300 if (!_RegistryCore::CheckEntryName(pEntryName.get()))
302 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
303 "[E_PARSING_FAILED] Entry name could not be parsed.");
306 line.SubString(firstTokenPos + 1, entryVal); // extract entry value
308 // check if entry value contains invalid chars
309 pEntryValue.reset(_StringConverter::CopyToCharArrayN(entryVal));
310 SysTryCatch(NID_IO, pEntryValue, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
311 "[E_OUT_OF_MEMORY] The memory is insufficient.");
313 length = strlen(pEntryValue.get());
316 if (!_RegistryCore::CheckEntryValue(pEntryValue.get()))
318 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
319 "[E_PARSING_FAILED] Entry value could not be parsed.");
323 // add the entry and value
324 r = _RegistryCore::AddValue(_sectionList, sectionName, entryName, entryVal);
325 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
327 r = lineTok.GetNextToken(line);
330 if (r == E_OUT_OF_RANGE)
338 if (r == E_PARSING_FAILED && Tizen::App::_AppInfo::IsOspCompat() == false)
340 r = E_INVALID_FORMAT;
343 SysLog(NID_IO, "[%s] exception occurred.", GetErrorMessage(r));
348 _RegistryCore::Load(const String& regPath, const char* pOpenMode)
350 result r = E_SUCCESS;
351 delete[] _pBuffer; //clear existing buffer
355 // open registry file
356 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl());
357 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
359 r = pFileImpl->Construct(regPath, pOpenMode, null);
360 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Constructing the file has failed.", GetErrorMessage(r));
362 // reset the section list to be sure that its empty
363 _sectionList.RemoveAll(true);
365 if (_truncate == false)
367 // load the registry file
368 r = pFileImpl->ReadN((char**)&_pBuffer, _length);
375 __pFileImpl = pFileImpl.release();
381 _RegistryCore::AddSection(const String& sectionName)
383 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
385 result r = E_SUCCESS;
386 bool sectionFound = false;
388 // Get section information
389 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
392 return GetLastResult();
395 while (pEnum->MoveNext() == E_SUCCESS)
397 _RegistrySection* pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
398 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
399 GetErrorMessage(GetLastResult()));
401 if (pRegSection->__sectionName == sectionName)
408 if (sectionFound == true)
410 return E_SECTION_ALREADY_EXIST;
413 // create a key with section name
414 unique_ptr<_RegistrySection> pRegSection(new (std::nothrow) _RegistrySection());
415 SysTryReturnResult(NID_IO, pRegSection != null, E_OUT_OF_MEMORY,
416 "The memory is insufficient.");
418 pRegSection->__sectionName = sectionName;
420 // add new section and section-entry map
421 r = _sectionList.Add(*(pRegSection.release()));
422 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
429 _RegistryCore::RemoveSection(const String& sectionName)
431 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
433 result r = E_SUCCESS;
434 _RegistrySection* pRegSection = null;
435 bool sectionFound = false;
437 if (_sectionList.GetCount() == 0)
439 return E_SECTION_NOT_FOUND;
442 // Get section information
443 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
446 return GetLastResult();
449 while (pEnum->MoveNext() == E_SUCCESS)
451 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
452 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagating.",
453 GetErrorMessage(GetLastResult()));
455 if (pRegSection->__sectionName == sectionName)
462 if (sectionFound == false)
464 return E_SECTION_NOT_FOUND;
467 r = _sectionList.Remove(*pRegSection, true);
468 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
475 _RegistryCore::GetSectionListN(IList** pRetList)
477 result r = E_SUCCESS;
478 unique_ptr<IEnumerator> pEnum(null);
479 _RegistrySection* pSection = null;
481 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
482 SysTryReturnResult(NID_IO, pRetList != null, E_INVALID_ARG, "pList cannot be null.");
484 // construct an array list to be returned
485 unique_ptr<ArrayList, AllElementsDeleter> pSectionList(new (std::nothrow) ArrayList());
486 SysTryReturnResult(NID_IO, pSectionList != null, E_OUT_OF_MEMORY,
487 "The memory is insufficient.");
489 r = pSectionList->Construct();
490 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
492 // if no sectons, return empty list
493 if (_sectionList.GetCount() == 0)
495 *pRetList = pSectionList.release();
499 // copy from class section list to the new list and return
500 pEnum.reset(_sectionList.GetEnumeratorN());
501 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.", GetErrorMessage(GetLastResult()));
503 while (pEnum->MoveNext() == E_SUCCESS)
505 pSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
506 SysTryReturn(NID_IO, pSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
507 GetErrorMessage(GetLastResult()));
509 unique_ptr<String> pSectionName(new (std::nothrow) String(pSection->__sectionName));
510 SysTryReturnResult(NID_IO, pSectionName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
512 r = pSectionList->Add(*(pSectionName.release()));
513 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
516 *pRetList = pSectionList.release();
522 _RegistryCore::GetEntryList(const String& sectionName, Collection::HashMap& retMap)
524 result r = E_SUCCESS;
525 unique_ptr<IEnumerator> pEnum(null);
526 unique_ptr<String> pKeyEntryStr(null);
527 unique_ptr<String> pKeyValStr(null);
528 bool sectionFound = false;
529 _RegistrySection* pRegSection = null;
530 _RegistryEntry* pRegEntry = null;
532 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
534 if (_sectionList.GetCount() == 0)
536 return E_SECTION_NOT_FOUND;
539 // Get section information
540 pEnum.reset(_sectionList.GetEnumeratorN());
541 SysTryReturnResult(NID_IO, pEnum != null, E_SYSTEM, "Section list is empty.");
543 while (pEnum->MoveNext() == E_SUCCESS)
545 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
546 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
547 GetErrorMessage(GetLastResult()));
549 if (pRegSection->__sectionName == sectionName)
556 if (sectionFound == false)
558 return E_SECTION_NOT_FOUND;
561 // copy from item list to the new list and return
562 pEnum.reset((pRegSection->__entryList).GetEnumeratorN());
563 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
564 GetErrorMessage(GetLastResult()));
566 while (pEnum->MoveNext() == E_SUCCESS)
568 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
569 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
570 GetErrorMessage(GetLastResult()));
572 pKeyEntryStr.reset(new (std::nothrow) String((pRegEntry->entryName)));
573 SysTryCatch(NID_IO, pKeyEntryStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
574 "[E_OUT_OF_MEMORY] The memory is insufficient.");
575 pKeyValStr.reset(new (std::nothrow) String((pRegEntry->entryValue)));
576 SysTryCatch(NID_IO, pKeyValStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
577 "[E_OUT_OF_MEMORY] The memory is insufficient.");
579 r = retMap.Add(*(pKeyEntryStr.release()), *(pKeyValStr.release()));
580 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
586 retMap.RemoveAll(true);
592 _RegistryCore::GetEntryListN(const String& sectionName, HashMap** pRetList)
594 result r = E_SUCCESS;
596 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
597 SysTryReturnResult(NID_IO, pRetList != null, E_INVALID_ARG, "pRetList cannot be null.");
599 // construct an array list to be returned
600 unique_ptr<HashMap, AllElementsDeleter> pEntryList(new (std::nothrow) HashMap());
601 SysTryReturnResult(NID_IO, pEntryList != null, E_OUT_OF_MEMORY,
602 "The memory is insufficient.");
603 r = pEntryList->Construct();
604 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
605 r = GetEntryList(sectionName, *(pEntryList.get()));
606 *pRetList = pEntryList.release();
612 _RegistryCore::GetAllEntriesN(const String& sectionName)
614 result r = E_SUCCESS;
615 HashMap* pMap = null;
617 r = GetEntryListN(sectionName, &pMap);
618 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
620 SetLastResult(E_SUCCESS);
629 _RegistryCore::GetAllEntryNamesN(const String& sectionName)
631 result r = E_SUCCESS;
633 unique_ptr<ArrayList, AllElementsDeleter> pEntryList(new (std::nothrow) ArrayList());
634 SysTryReturn(NID_IO, pEntryList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
636 r = pEntryList->Construct();
637 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
639 unique_ptr<IMap, AllElementsDeleter> pMap(this->GetAllEntriesN(sectionName));
640 SysTryReturn(NID_IO, pMap != null, null, GetLastResult(), "[%s] Getting all entries was failed.",
641 GetErrorMessage(GetLastResult()));
643 unique_ptr<IMapEnumerator> pMapEnum(pMap->GetMapEnumeratorN());
644 SysTryReturn(NID_IO, pMapEnum, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
645 while (pMapEnum->MoveNext() == E_SUCCESS)
647 String* pKey = dynamic_cast< String* >(pMapEnum->GetKey());
648 SysTryReturn(NID_IO, pKey != null, null, E_IO, "[E_OUT_OF_MEMORY] The system error occurred.");
650 String* pEntryName = new (std::nothrow) String(*pKey);
651 SysTryReturn(NID_IO, pEntryName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
653 r = pEntryList->Add(*pEntryName);
654 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
657 SetLastResult(E_SUCCESS);
658 return pEntryList.release();
662 _RegistryCore::GetValue(const String& sectionName, const String& entryName, String& valStr)
664 bool sectionFound = false;
665 bool entryFound = false;
666 _RegistrySection* pRegSection = null;
667 _RegistryEntry* pRegEntry = null;
669 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
671 if (_sectionList.GetCount() == 0)
673 return E_SECTION_NOT_FOUND;
676 // Get section information
677 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
680 return GetLastResult();
683 while (pEnum->MoveNext() == E_SUCCESS)
685 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
686 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
687 GetErrorMessage(GetLastResult()));
689 if (pRegSection->__sectionName == sectionName)
696 if (sectionFound == false)
698 return E_SECTION_NOT_FOUND;
701 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
704 return GetLastResult();
707 while (pEnum->MoveNext() == E_SUCCESS)
709 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
710 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
711 GetErrorMessage(GetLastResult()));
713 if (pRegEntry->entryName == entryName)
715 valStr = pRegEntry->entryValue;
721 SysTryLog(NID_IO, entryFound, "Entry not found");
723 return((entryFound == true) ? E_SUCCESS : E_KEY_NOT_FOUND);
727 _RegistryCore::AddValue(const String& sectionName, const String& entryName, const String& valStr)
729 result r = E_SUCCESS;
730 bool sectionFound = false;
731 bool entryFound = false;
732 _RegistrySection* pRegSection = null;
735 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
737 // Get section information
738 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
741 return GetLastResult();
744 while (pEnum->MoveNext() == E_SUCCESS)
746 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
747 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
748 GetErrorMessage(GetLastResult()));
750 if (pRegSection->__sectionName == sectionName)
757 if (sectionFound == false)
759 return E_SECTION_NOT_FOUND;
762 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
765 return GetLastResult();
768 while (pEnum->MoveNext() == E_SUCCESS)
770 _RegistryEntry* pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
771 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
772 GetErrorMessage(GetLastResult()));
774 if (pRegEntry->entryName == entryName)
781 if (entryFound == true)
783 return E_KEY_ALREADY_EXIST;
786 unique_ptr<_RegistryEntry> pRegEntry(new (std::nothrow) _RegistryEntry());
787 SysTryReturnResult(NID_IO, pRegEntry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
789 unique_ptr<char[]> pEntryName(_StringConverter::CopyToCharArrayN(entryName));
790 SysTryReturnResult(NID_IO, pEntryName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
792 length = strlen(pEntryName.get());
793 if (!_RegistryCore::CheckEntryName(pEntryName.get()))
795 SysTryReturnResult(NID_IO, false, E_PARSING_FAILED, "Entry name could not be parsed.");
798 unique_ptr<char[]> pEntryValue(_StringConverter::CopyToCharArrayN(valStr));
799 SysTryReturnResult(NID_IO, pEntryValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
801 length = strlen(pEntryValue.get());
802 if (length > 0 && !_RegistryCore::CheckEntryValue(pEntryValue.get()))
804 SysTryReturnResult(NID_IO, false, E_PARSING_FAILED, "Entry value could not be parsed.");
807 pRegEntry->entryName = entryName;
808 pRegEntry->entryValue = valStr;
810 r = pRegSection->__entryList.Add(*(pRegEntry.release()));
811 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
818 _RegistryCore::SetValue(const String& sectionName, const String& entryName, const String& valStr)
820 result r = E_SUCCESS;
821 bool sectionFound = false;
822 bool entryFound = false;
823 _RegistryEntry* pRegEntry = null;
824 _RegistrySection* pRegSection = null;
828 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
830 if (_sectionList.GetCount() == 0)
832 return E_SECTION_NOT_FOUND;
835 // Get section information
836 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
839 return GetLastResult();
842 while (pEnum->MoveNext() == E_SUCCESS)
844 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
845 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
846 GetErrorMessage(GetLastResult()));
848 if (pRegSection->__sectionName == sectionName)
855 if (sectionFound == false)
857 return E_SECTION_NOT_FOUND;
860 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
863 return GetLastResult();
866 while (pEnum->MoveNext() == E_SUCCESS)
868 pRegEntry = dynamic_cast< _RegistryEntry* >(pEnum->GetCurrent());
869 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
870 GetErrorMessage(GetLastResult()));
872 if (pRegEntry->entryName == entryName)
880 if (entryFound == false)
882 return E_KEY_NOT_FOUND;
885 if (valStr.GetLength() > 0)
887 unique_ptr<char[]> pEntryValue(_StringConverter::CopyToCharArrayN(valStr));
888 SysTryReturnResult(NID_IO, pEntryValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
890 length = strlen(pEntryValue.get());
891 if (!_RegistryCore::CheckEntryValue(pEntryValue.get()))
893 return E_PARSING_FAILED;
897 pRegEntry->entryValue = valStr;
898 r = pRegSection->__entryList.SetAt(*pRegEntry, entryIndex);
899 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
906 _RegistryCore::RemoveValue(const String& sectionName, const String& entryName)
908 result r = E_SUCCESS;
909 bool sectionFound = false;
910 bool entryFound = false;
911 _RegistryEntry* pRegEntry = null;
912 _RegistrySection* pRegSection = null;
914 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
916 if (_sectionList.GetCount() <= 0)
918 return E_SECTION_NOT_FOUND;
921 // Get section information
922 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
925 return E_SECTION_NOT_FOUND;
928 while (pEnum->MoveNext() == E_SUCCESS)
930 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
931 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
932 GetErrorMessage(GetLastResult()));
934 if (pRegSection->__sectionName == sectionName)
941 if (sectionFound == false)
943 return E_SECTION_NOT_FOUND;
946 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
949 return E_SECTION_NOT_FOUND;
952 while (pEnum->MoveNext() == E_SUCCESS)
954 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
955 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
956 GetErrorMessage(GetLastResult()));
958 if (pRegEntry->entryName == entryName)
965 if (entryFound == false)
967 return E_KEY_NOT_FOUND;
970 r = pRegSection->__entryList.Remove(*pRegEntry, true);
971 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
978 _RegistryCore::Flush(void)
984 _RegistryCore::Write(void)
986 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
988 if (_pBuffer == null)
990 SysLog(NID_IO, "The buffer is empty.");
994 result r = E_SUCCESS;
996 String openMode(L"w+");
997 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
998 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1000 //TODO: Secure mode is to be handled
1001 r = pFileImpl->Construct(_regPath, "w+", null);
1002 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1004 r = pFileImpl->Write(_pBuffer, _length);
1005 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1007 r = pFileImpl->Flush();
1008 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1014 _RegistryCore::PrepareToWrite(void)
1016 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1018 result r = E_SUCCESS;
1019 _RegistryEntry* pRegEntry = null;
1020 _RegistrySection* pRegSection = null;
1023 delete[] _pBuffer; //clear existing buffer
1027 if (_write == false)
1029 SysLog(NID_IO, "[E_ILLEGAL_ACCESS] Registry cannot be flushed as it was opened in read only mode.");
1030 return E_ILLEGAL_ACCESS;
1033 // if no sections, do nothing
1034 if (_sectionList.GetCount() == 0)
1036 unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(_regPath));
1037 truncate(pFilePath.get(), 0);
1042 // write the registry map to a file
1043 // Get section information
1044 unique_ptr<IEnumerator> pSectionEnum(_sectionList.GetEnumeratorN());
1045 SysTryReturn(NID_IO, pSectionEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.",
1046 GetErrorMessage(GetLastResult()));
1048 while (pSectionEnum->MoveNext() == E_SUCCESS)
1051 pRegSection = dynamic_cast <_RegistrySection*>(pSectionEnum->GetCurrent());
1052 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1053 GetErrorMessage(GetLastResult()));
1055 // write section name to file
1056 r = inputBuffer.Append(String(L"#" + pRegSection->__sectionName + String(L'\n')));
1057 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1059 // read the entries for this section
1060 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1061 SysTryReturn(NID_IO, pSectionEnum != null, GetLastResult(), GetLastResult(), "[%s] Entry list is empty.",
1062 GetErrorMessage(GetLastResult()));
1064 while (pEntryEnum->MoveNext() == E_SUCCESS)
1066 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1067 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1068 GetErrorMessage(GetLastResult()));
1070 // write entry name to file
1071 r = inputBuffer.Append(pRegEntry->entryName + String(L"=" + pRegEntry->entryValue + String(L'\n')));
1072 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1077 _pBuffer = (byte*)_StringConverter::CopyToCharArrayN(inputBuffer);
1078 SysTryReturn(NID_IO, _pBuffer != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1079 GetErrorMessage(GetLastResult()));
1081 _length = strlen((char*)_pBuffer);
1087 _RegistryCore::GetSectionIndex(const String& sectionName)
1089 bool sectionFound = false;
1090 _RegistrySection* pRegSection = null;
1093 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1094 SysTryReturn(NID_IO, sectionName.GetLength() > 0, -1, E_INVALID_ARG,
1095 "[E_INVALID_ARG] sectionName.GetLength() <= 0");
1097 SysTryReturn(NID_IO, _sectionList.GetCount() > 0, -1, E_SECTION_NOT_FOUND, "[E_SECTION_NOT_FOUND] Section not found.");
1099 // Get section information
1100 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
1101 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.",
1102 GetErrorMessage(GetLastResult()));
1104 while (pEnum->MoveNext() == E_SUCCESS)
1106 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1107 SysTryReturn(NID_IO, pRegSection != null, -1, GetLastResult(), "[%s] Propagated.",
1108 GetErrorMessage(GetLastResult()));
1110 if (pRegSection->__sectionName == sectionName)
1112 sectionFound = true;
1118 if (sectionFound == false)
1120 return E_SECTION_NOT_FOUND;
1123 SetLastResult(E_SUCCESS);
1128 _RegistryCore::Removekey(const String& sectionName, const String& keyName)
1130 result r = E_SUCCESS;
1131 int sectionIndex = -1;
1132 int entryIndex = -1;
1134 sectionIndex = GetSectionIndex(sectionName);
1135 r = GetLastResult();
1136 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1138 entryIndex = GetEntryIndex(sectionIndex, keyName);
1139 r = GetLastResult();
1140 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1142 RemoveEntry(sectionIndex, entryIndex);
1143 r = GetLastResult();
1144 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1146 SetLastResult(E_SUCCESS);
1151 _RegistryCore::GetAllSectionCount(void)
1153 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1155 SetLastResult(E_SUCCESS);
1156 return _sectionList.GetCount();
1160 _RegistryCore::GetAllEntryCount(int sectionIndex)
1162 _RegistrySection* pRegSection = null;
1165 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1166 SysTryReturn(NID_IO, sectionIndex >= 0, 0, E_INVALID_ARG, "[E_INVALID_ARG] sectionIndex < 0.");
1167 listSize = _sectionList.GetCount();
1168 SysTryReturn(NID_IO, listSize > 0, 0, E_SECTION_NOT_FOUND, "[E_INVALID_ARG] section listSize <= 0.");
1169 SysTryReturn(NID_IO, sectionIndex >= listSize, 0, E_SECTION_NOT_FOUND,
1170 "[E_INVALID_ARG] sectionIndex > listSize.");
1172 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1173 SysTryReturn(NID_IO, pRegSection != null, 0, E_SECTION_NOT_FOUND,
1174 "[E_SECTION_NOT_FOUND] section was not found.");
1176 SetLastResult(E_SUCCESS);
1177 return pRegSection->__entryList.GetCount();
1181 _RegistryCore::RemoveEntry(int sectionIndex, int entryIndex)
1183 result r = E_SUCCESS;
1185 int entryListSize = 0;
1186 _RegistrySection* pRegSection = null;
1188 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1189 SysTryReturnVoidResult(NID_IO, sectionIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG] sectionIndex < 0.");
1190 SysTryReturnVoidResult(NID_IO, entryIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG] entryIndex < 0.");
1192 listSize = _sectionList.GetCount();
1193 SysTryReturnVoidResult(NID_IO, listSize > 0, E_SECTION_NOT_FOUND,
1194 "[E_SECTION_NOT_FOUND] section listSize <= 0.");
1195 SysTryReturnVoidResult(NID_IO, sectionIndex < listSize, E_SECTION_NOT_FOUND,
1196 "[E_SECTION_NOT_FOUND] sectionIndex >= listSize.");
1198 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1199 SysTryReturnVoidResult(NID_IO, pRegSection != null, E_SECTION_NOT_FOUND,
1200 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1202 r = GetLastResult();
1203 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r,"[%s] Propagated.", GetErrorMessage(r));
1205 entryListSize = pRegSection->__entryList.GetCount();
1206 SysTryReturnVoidResult(NID_IO, (entryListSize > 0 || entryIndex < entryListSize), E_KEY_NOT_FOUND,
1207 "[E_KEY_NOT_FOUND] entryListSize is 0 or entryIndex >= entryListSize.");
1209 r = pRegSection->__entryList.RemoveAt(entryIndex, true);
1210 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1212 SetLastResult(E_SUCCESS);
1216 _RegistryCore::ModifyEntryValue(int sectionIndex, int entryIndex, _RegValueType type, const void* pValue, int size)
1218 SetLastResult(E_UNSUPPORTED_OPERATION);
1222 _RegistryCore::GetEntryIndex(int sectionIndex, const String& entryName)
1224 _RegistrySection* pRegSection = null;
1225 _RegistryEntry* pRegEntry = null;
1227 bool entryFound = false;
1228 int entryIndex = -1;
1230 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1231 SysTryReturn(NID_IO, sectionIndex >= 0, -1, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1232 SysTryReturn(NID_IO, entryName.GetLength() > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG]entryName length < 0.");
1234 listSize = _sectionList.GetCount();
1235 SysTryReturn(NID_IO, listSize > 0, -1, E_SECTION_NOT_FOUND,
1236 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1237 SysTryReturn(NID_IO, sectionIndex < listSize, -1, E_SECTION_NOT_FOUND,
1238 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1240 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1241 SysTryReturn(NID_IO, pRegSection != null, -1, E_SECTION_NOT_FOUND,
1242 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1244 // read the entries for this section
1245 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1246 SysTryReturn(NID_IO, pEntryEnum != null, -1, GetLastResult(), "[%s] Entry list is empty.",
1247 GetErrorMessage(GetLastResult()));
1250 while (pEntryEnum->MoveNext() == E_SUCCESS)
1252 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1253 SysTryReturn(NID_IO, pRegEntry != null, -1, GetLastResult(), "[%s] Propagated.",
1254 GetErrorMessage(GetLastResult()));
1256 if (entryName == pRegEntry->entryName)
1264 SysTryReturn(NID_IO, entryFound != false, -1, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] entry was not found.");
1266 SetLastResult(E_SUCCESS);
1271 _RegistryCore::GetEntryName(int sectionIndex, int entryIndex)
1273 _RegistrySection* pRegSection = null;
1274 _RegistryEntry* pRegEntry = null;
1279 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1280 SysTryReturn(NID_IO, sectionIndex >= 0, empty, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1281 SysTryReturn(NID_IO, entryIndex >= 0, empty, E_INVALID_ARG, "[E_INVALID_ARG]entryIndex < 0.");
1283 listSize = _sectionList.GetCount();
1284 SysTryReturn(NID_IO, listSize > 0, empty, E_SECTION_NOT_FOUND,
1285 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1286 SysTryReturn(NID_IO, sectionIndex < listSize, empty, E_SECTION_NOT_FOUND,
1287 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1289 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1290 SysTryReturn(NID_IO, pRegSection != null, empty, E_SECTION_NOT_FOUND,
1291 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1293 // read the entries for this section
1294 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1295 SysTryReturn(NID_IO, pEntryEnum != null, empty, GetLastResult(), "[%s] Entry list is empty.",
1296 GetErrorMessage(GetLastResult()));
1298 tmpIndex = entryIndex;
1299 while (pEntryEnum->MoveNext() == E_SUCCESS)
1303 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1304 SysTryReturn(NID_IO, pRegEntry != null, empty, GetLastResult(), "[%s] Propagated.",
1305 GetErrorMessage(GetLastResult()));
1312 SysTryReturn(NID_IO, pRegEntry != null, empty, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] entry was not found.");
1314 SetLastResult(E_SUCCESS);
1315 return pRegEntry->entryName;
1319 _RegistryCore::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType type, void* pValue, int* pSize)
1321 _RegistrySection* pRegSection = null;
1322 _RegistryEntry* pRegEntry = null;
1323 result r = E_SUCCESS;
1325 int tmpEntryIndex = -1;
1326 int entryListSize = 0;
1327 String strValueEncoded;
1329 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1330 SysTryReturnVoidResult(NID_IO, sectionIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1331 SysTryReturnVoidResult(NID_IO, type >= 0 && type <= REG_VALUE_TYPE_MAX, E_INVALID_ARG,
1332 "[E_INVALID_ARG] invalid registry type.");
1333 SysTryReturnVoidResult(NID_IO, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG]pValue is null.");
1334 SysTryReturnVoidResult(NID_IO, pSize != null, E_INVALID_ARG, "[E_INVALID_ARG]pSize is null.");
1335 SysTryReturnVoidResult(NID_IO, *pSize > 0, E_INVALID_ARG, "[E_INVALID_ARG] *pSize < 0.");
1337 listSize = _sectionList.GetCount();
1338 SysTryReturnVoidResult(NID_IO, listSize > 0, E_SECTION_NOT_FOUND,
1339 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1340 SysTryReturnVoidResult(NID_IO, sectionIndex < listSize, E_SECTION_NOT_FOUND,
1341 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1343 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1344 SysTryReturnVoidResult(NID_IO, pRegSection != null, E_SECTION_NOT_FOUND,
1345 "[E_SECTION_NOT_FOUND] section at index (%d) is not available.", sectionIndex);
1347 entryListSize = pRegSection->__entryList.GetCount();
1348 SysTryReturnVoidResult(NID_IO, entryListSize > 0, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry list is empty.");
1349 SysTryReturnVoidResult(NID_IO, entryIndex < entryListSize, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry not found.");
1351 // read the entries for this section
1352 unique_ptr< IEnumerator > pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1353 SysTryReturnVoidResult(NID_IO, pEntryEnum != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND]Entry list is empty.");
1355 tmpEntryIndex = entryIndex;
1356 while (pEntryEnum->MoveNext() == E_SUCCESS)
1358 if (entryIndex == 0)
1360 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1361 SysTryReturnVoidResult(NID_IO, pRegEntry != null, GetLastResult(), "[%s] Propagated.",
1362 GetErrorMessage(GetLastResult()));
1370 SysTryReturnVoidResult(NID_IO, pRegEntry != null, E_KEY_NOT_FOUND,
1371 "[E_KEY_NOT_FOUND] Entry was not found.");
1373 strValueEncoded = pRegEntry->entryValue;
1375 DecodeData(strValueEncoded, type, pValue, pSize);
1376 r = GetLastResult();
1377 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1379 SetLastResult(E_SUCCESS);
1384 _RegistryCore::GetSectionName(int sectionIndex)
1386 _RegistrySection* pRegSection = null;
1387 result r = E_SUCCESS;
1389 String sectionName("");
1391 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1392 SysTryReturn(NID_IO, sectionIndex >= 0, sectionName, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1394 listSize = _sectionList.GetCount();
1395 SysTryReturn(NID_IO, listSize > 0, sectionName, E_SECTION_NOT_FOUND,
1396 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1397 SysTryReturn(NID_IO, sectionIndex < listSize, sectionName, E_SECTION_NOT_FOUND,
1398 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1400 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1401 SysTryReturn(NID_IO, pRegSection != null, sectionName, E_SECTION_NOT_FOUND,
1402 "[E_SECTION_NOT_FOUND] sectionIndex is not available.");
1404 r = GetLastResult();
1405 SysTryReturn(NID_IO, !IsFailed(r), sectionName, r, "[%s] Propagated.", GetErrorMessage(r));
1407 SetLastResult(E_SUCCESS);
1408 return pRegSection->__sectionName;
1412 _RegistryCore::GetSectionByIndex(int sectionIndex)
1414 _RegistrySection* pRegSection = null;
1415 int index = sectionIndex;
1417 // Get section information
1418 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
1419 SysTryReturn(NID_IO, pEnum != null, null, GetLastResult(),
1420 "[%s] section list is empty.", GetErrorMessage(GetLastResult()));
1422 while (pEnum->MoveNext() == E_SUCCESS)
1426 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
1427 SysTryReturn(NID_IO, pRegSection != null, null, GetLastResult(), "[%s] Propagated.",
1428 GetErrorMessage(GetLastResult()));
1435 SysTryReturn(NID_IO, pRegSection != null, null, E_SECTION_NOT_FOUND,
1436 "[E_SECTION_NOT_FOUND] Section was not found.");
1438 SetLastResult(E_SUCCESS);
1444 _RegistryCore::DecodeData(const String& strValueEncoded, _RegValueType type, void* pValue, int* pSize)
1446 SysAssert(strValueEncoded.GetLength() > 0);
1449 SysAssert(pSize && *pSize > 0);
1451 SysTryReturnVoidResult(NID_IO, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument.");
1452 SysTryReturnVoidResult(NID_IO, pSize != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument.");
1456 case REG_VALUE_TYPE_INT: // (-2147483647 - 1) ~ 2147483647
1459 result r = E_SUCCESS;
1461 if (*pSize != sizeof(int))
1463 SetLastResult(E_INVALID_ARG);
1467 r = Integer::Parse(strValueEncoded, retIntVal);
1468 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1470 *(int*) pValue = retIntVal;
1474 case REG_VALUE_TYPE_REAL: // Holds signed IEEE 64-bit (8-byte) double-precision floating-point numbers
1475 // ranging in value from -1.79769313486231570E+308 through -4.94065645841246544E-324
1476 // for negative values and from 4.94065645841246544E-324 through 1.79769313486231570E+308
1477 // for positive values.
1479 double retDoubleVal = 0;
1480 result r = E_SUCCESS;
1482 if (*pSize != sizeof(double))
1484 SetLastResult(E_INVALID_ARG);
1488 r = Double::Parse(strValueEncoded, retDoubleVal);
1489 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1491 *(double*) pValue = retDoubleVal;
1495 case REG_VALUE_TYPE_STRING:
1497 // if(*pSize != sizeof(String))
1499 // SetLastResult(E_INVALID_ARG);
1502 *(String*) pValue = strValueEncoded;
1506 case REG_VALUE_TYPE_BINARY:
1508 SetLastResult(E_PARSING_FAILED);
1512 case REG_VALUE_TYPE_UUID:
1515 result r = E_SUCCESS;
1517 r = UuId::Parse(strValueEncoded, retUuIdVal);
1518 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1520 *(UuId*) pValue = retUuIdVal;
1524 case REG_VALUE_TYPE_UUIDSET:
1526 SetLastResult(E_PARSING_FAILED);
1531 SysAssert(0); // should not happen!
1535 SetLastResult(E_SUCCESS);
1540 _RegistryCore::AddSection(LinkedList& sectionList, const String& sectionName)
1542 result r = E_SUCCESS;
1543 bool sectionFound = false;
1545 // Get section information
1546 unique_ptr<IEnumerator> pEnum(sectionList.GetEnumeratorN());
1549 return GetLastResult();
1552 while (pEnum->MoveNext() == E_SUCCESS)
1554 _RegistrySection* pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1555 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1556 GetErrorMessage(GetLastResult()));
1558 if (pRegSection->__sectionName == sectionName)
1560 sectionFound = true;
1565 if (sectionFound == true)
1567 return E_SECTION_ALREADY_EXIST;
1570 // create a key with section name
1571 unique_ptr<_RegistrySection> pRegSection(new (std::nothrow) _RegistrySection());
1573 SysTryReturnResult(NID_IO, pRegSection != null, E_OUT_OF_MEMORY,
1574 "The memory is insufficient.");
1576 pRegSection->__sectionName = sectionName;
1578 // add new section and section-entry map
1579 r = sectionList.Add(*(pRegSection.release()));
1580 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1586 _RegistryCore::AddValue(LinkedList& sectionList, const String& sectionName, const String& entryName, const String& valStr)
1588 result r = E_SUCCESS;
1589 bool sectionFound = false;
1590 bool entryFound = false;
1591 _RegistrySection* pRegSection = null;
1593 // Get section information
1594 unique_ptr<IEnumerator> pEnum(sectionList.GetEnumeratorN());
1597 return GetLastResult();
1600 while (pEnum->MoveNext() == E_SUCCESS)
1602 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1603 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1604 GetErrorMessage(GetLastResult()));
1606 if (pRegSection->__sectionName == sectionName)
1608 sectionFound = true;
1613 if (sectionFound == false)
1615 return E_SECTION_NOT_FOUND;
1618 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
1621 return GetLastResult();
1624 while (pEnum->MoveNext() == E_SUCCESS)
1626 _RegistryEntry* pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
1627 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1628 GetErrorMessage(GetLastResult()));
1630 if (pRegEntry->entryName == entryName)
1637 if (entryFound == true)
1639 return E_KEY_ALREADY_EXIST;
1642 unique_ptr<_RegistryEntry> pRegEntry(new (std::nothrow) _RegistryEntry());
1643 SysTryReturnResult(NID_IO, pRegEntry != null, E_OUT_OF_MEMORY,
1644 "The memory is insufficient.");
1646 pRegEntry->entryName = entryName;
1647 pRegEntry->entryValue = valStr;
1649 r = pRegSection->__entryList.Add(*(pRegEntry.release()));
1650 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1656 _RegistryCore::CheckSectionName(char* pSectionName)
1663 if (pSectionName == null)
1668 pFrom = pSectionName;
1669 length = strlen(pSectionName);
1670 pTo = pSectionName + (length - 1);
1672 if (!pFrom || !pTo || pFrom > pTo)
1677 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1679 if (*pTemp == '\0' || *pTemp == '\n' || *pTemp == '#' || *pTemp == '=')
1689 _RegistryCore::CheckEntryName(char* pEntryName)
1696 if (pEntryName == null)
1702 length = strlen(pEntryName);
1703 pTo = pEntryName + (length - 1);
1705 if (!pFrom || !pTo || pFrom > pTo)
1710 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1712 if (*pTemp == '\0' || *pTemp == '\n' || *pTemp == '#' || *pTemp == '=')
1722 _RegistryCore::CheckEntryValue(char* pEntryVal)
1729 if (pEntryVal == null)
1735 length = strlen(pEntryVal);
1736 pTo = pEntryVal + (length - 1);
1738 if (!pFrom || !pTo || pFrom > pTo)
1743 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1745 if (*pTemp == '\0' || *pTemp == '\n')
1755 _RegistryCore::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer* pKey)
1757 result r = E_SUCCESS;
1759 if (File::IsFileExist(secureRegPath))
1761 r = GetLastResult();
1764 r = E_FILE_ALREADY_EXIST;
1765 SysLog(NID_IO, "[E_FILE_ALREADY_EXIST] The secure registry already exists.");
1769 SysLog(NID_IO, "[%s]Propagated.", GetErrorMessage(r));
1774 unique_ptr<_NormalRegistry> pNormalReg(new (std::nothrow) _NormalRegistry());
1775 SysTryReturnResult(NID_IO, pNormalReg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1777 r = pNormalReg->Load(plainRegPath, "r");
1778 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1780 unique_ptr<_SecureRegistry> pSecureReg(new (std::nothrow) _SecureRegistry());
1781 SysTryReturnResult(NID_IO, pSecureReg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1783 r = pSecureReg->Construct(secureRegPath, "w+", pKey);
1784 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1786 if (pNormalReg->_pBuffer != null && pNormalReg->_length > 0)
1788 pSecureReg->_pBuffer = pNormalReg->_pBuffer;
1789 pSecureReg->_length = pNormalReg->_length;
1791 pNormalReg->_pBuffer = null;
1792 pNormalReg->_length = 0;
1794 r = pSecureReg->Parse();
1795 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1798 r = pSecureReg->Flush();
1799 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1805 _RegistryCore::LockN(FileLockType lockType)
1807 SysAssertf(_constructed == true && __pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
1808 return __pFileImpl->LockN(lockType);
1812 _RegistryCore::TryToLockN(FileLockType lockType)
1814 SysAssertf(_constructed == true && __pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
1815 return __pFileImpl->TryToLockN(lockType);