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 <FBase_LocalizedNumParser.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)
104 _RegistryCore::VerifyRegistryOpenMode(const char* pOpenMode)
106 if (pOpenMode == null)
108 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode is null.");
112 if (strlen(pOpenMode) > _MAX_REG_OPENMODE_LENGTH)
114 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
118 switch (pOpenMode[0])
132 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
136 switch (pOpenMode[1])
141 if (pOpenMode[2] == '\0')
149 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
153 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
161 _RegistryCore::Parse(void)
163 result r = E_SUCCESS;
165 bool sectionFound = false;
168 unique_ptr<char[]> pEntryName(null);
169 unique_ptr<char[]> pEntryValue(null);
170 int firstTokenPos = 0;
173 if (_pBuffer == null)
175 SysLog(NID_IO, "The buffer is empty.");
179 String inputStr = String((char*)_pBuffer);
180 StringTokenizer lineTok(inputStr, L"\n");
182 r = lineTok.GetNextToken(line);
184 while (r == E_SUCCESS)
187 if (line.GetLength() == 0)
189 r = lineTok.GetNextToken(line);
193 // find registry section marker
194 line.GetCharAt(0, ch);
195 if (ch == _REGISTRY_SECTION_MARKER_CHAR)
199 // skip '#' and "\n' at the end
200 r = line.SubString(1, line.GetLength() - 1, sectionName);
201 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
203 // if End of the document follows section name without newline character.
204 sectionName.GetCharAt(sectionName.GetLength() - 1, ch);
207 sectionName.SetLength(sectionName.GetLength() - 1); //Remove '\n'
210 // for compatibility check if line contains '\r' at the end
211 sectionName.GetCharAt(sectionName.GetLength() - 1, ch);
214 sectionName.SetLength(sectionName.GetLength() - 1); //Remove '\r'
217 //TODO: check if remaining data in sectionName is valid or not
218 //after removing '#', '\n', and '\r', sectionName should contain at least 1 valid character
220 sectionName.GetLength() > 0, r = E_PARSING_FAILED, E_PARSING_FAILED,
221 "[E_PARSING_FAILED] Section length cannot be <2.");
223 unique_ptr<char[]> pSectionName(_StringConverter::CopyToCharArrayN(sectionName));
224 SysTryCatch(NID_IO, pSectionName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
225 "[E_OUT_OF_MEMORY] The memory is insufficient.");
227 if (!_RegistryCore::CheckSectionName(pSectionName.get()))
229 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
230 "[E_PARSING_FAILED] Section name could not be parsed.");
233 r = _RegistryCore::AddSection(_sectionList, sectionName);
234 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
236 else // not a section.. but may belongs to a section (entry)
238 // ignores blank lines
239 line.GetCharAt(0, ch);
240 if ((ch == L'\n') || (ch == L'\r'))
242 r = lineTok.GetNextToken(line);
247 // Need to check for tabs, spaces in the beginning of each line
249 // if no section found till now.. ignore
250 if (sectionFound == false)
252 r = lineTok.GetNextToken(line);
256 // if End of the document follows entry value without newline character.
257 line.GetCharAt(line.GetLength() - 1, ch);
260 line.SetLength(line.GetLength() - 1); //Remove '\n'
263 line.GetCharAt(line.GetLength() - 1, ch);
266 line.SetLength(line.GetLength() - 1); //Remove '\r'
269 String entryName("");
272 // we will have sectionitem=value (or) sectionitem= (or) sectionitem
273 // as our string. Break this into entry and value
274 StringTokenizer strTok(line, L"=");
276 // extract entry name
277 if (strTok.HasMoreTokens())
279 line.IndexOf(L'=', 0, firstTokenPos); // position of first occurance of '=' in a line
280 if (firstTokenPos == 0)
282 // entryName should not be empty. i.e., "=value" or just '=' is invalid case
283 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
284 "[E_PARSING_FAILED] Entry name cannot be empty.");
288 strTok.GetNextToken(entryName);
291 else // missing '=' in entry line or entry line does not seem to contain "xxx=xxx" format
293 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED, "[E_PARSING_FAILED] invalid entry Line found.");
296 // check if entry name contains invalid chars
297 pEntryName.reset(_StringConverter::CopyToCharArrayN(entryName));
298 SysTryCatch(NID_IO, pEntryName, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
299 "[E_OUT_OF_MEMORY] The memory is insufficient.");
301 if (!_RegistryCore::CheckEntryName(pEntryName.get()))
303 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
304 "[E_PARSING_FAILED] Entry name could not be parsed.");
307 line.SubString(firstTokenPos + 1, entryVal); // extract entry value
309 // check if entry value contains invalid chars
310 pEntryValue.reset(_StringConverter::CopyToCharArrayN(entryVal));
311 SysTryCatch(NID_IO, pEntryValue, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
312 "[E_OUT_OF_MEMORY] The memory is insufficient.");
314 length = strlen(pEntryValue.get());
317 if (!_RegistryCore::CheckEntryValue(pEntryValue.get()))
319 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
320 "[E_PARSING_FAILED] Entry value could not be parsed.");
324 // add the entry and value
325 r = _RegistryCore::AddValue(_sectionList, sectionName, entryName, entryVal);
326 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
328 r = lineTok.GetNextToken(line);
331 if (r == E_OUT_OF_RANGE)
339 if (r == E_PARSING_FAILED && Tizen::App::_AppInfo::IsOspCompat() == false)
341 r = E_INVALID_FORMAT;
344 SysLog(NID_IO, "[%s] exception occurred.", GetErrorMessage(r));
349 _RegistryCore::Load(const String& regPath, const char* pOpenMode)
351 result r = E_SUCCESS;
352 delete[] _pBuffer; //clear existing buffer
356 // open registry file
357 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl());
358 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
360 r = pFileImpl->Construct(regPath, pOpenMode, null);
361 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Constructing the file has failed.", GetErrorMessage(r));
363 // reset the section list to be sure that its empty
364 _sectionList.RemoveAll(true);
366 if (_truncate == false)
368 // load the registry file
369 r = pFileImpl->ReadN((char**)&_pBuffer, _length);
376 __pFileImpl = pFileImpl.release();
382 _RegistryCore::AddSection(const String& sectionName)
384 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
386 result r = E_SUCCESS;
387 bool sectionFound = false;
389 // Get section information
390 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
393 return GetLastResult();
396 while (pEnum->MoveNext() == E_SUCCESS)
398 _RegistrySection* pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
399 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
400 GetErrorMessage(GetLastResult()));
402 if (pRegSection->__sectionName == sectionName)
409 if (sectionFound == true)
411 return E_SECTION_ALREADY_EXIST;
414 // create a key with section name
415 unique_ptr<_RegistrySection> pRegSection(new (std::nothrow) _RegistrySection());
416 SysTryReturnResult(NID_IO, pRegSection != null, E_OUT_OF_MEMORY,
417 "The memory is insufficient.");
419 pRegSection->__sectionName = sectionName;
421 // add new section and section-entry map
422 r = _sectionList.Add(*(pRegSection.release()));
423 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
430 _RegistryCore::RemoveSection(const String& sectionName)
432 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
434 result r = E_SUCCESS;
435 _RegistrySection* pRegSection = null;
436 bool sectionFound = false;
438 if (_sectionList.GetCount() == 0)
440 return E_SECTION_NOT_FOUND;
443 // Get section information
444 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
447 return GetLastResult();
450 while (pEnum->MoveNext() == E_SUCCESS)
452 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
453 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagating.",
454 GetErrorMessage(GetLastResult()));
456 if (pRegSection->__sectionName == sectionName)
463 if (sectionFound == false)
465 return E_SECTION_NOT_FOUND;
468 r = _sectionList.Remove(*pRegSection, true);
469 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
476 _RegistryCore::GetSectionListN(IList** pRetList)
478 result r = E_SUCCESS;
479 unique_ptr<IEnumerator> pEnum(null);
480 _RegistrySection* pSection = null;
482 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
483 SysTryReturnResult(NID_IO, pRetList != null, E_INVALID_ARG, "pList cannot be null.");
485 // construct an array list to be returned
486 unique_ptr<ArrayList, AllElementsDeleter> pSectionList(new (std::nothrow) ArrayList());
487 SysTryReturnResult(NID_IO, pSectionList != null, E_OUT_OF_MEMORY,
488 "The memory is insufficient.");
490 r = pSectionList->Construct();
491 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
493 // if no sectons, return empty list
494 if (_sectionList.GetCount() == 0)
496 *pRetList = pSectionList.release();
500 // copy from class section list to the new list and return
501 pEnum.reset(_sectionList.GetEnumeratorN());
502 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.", GetErrorMessage(GetLastResult()));
504 while (pEnum->MoveNext() == E_SUCCESS)
506 pSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
507 SysTryReturn(NID_IO, pSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
508 GetErrorMessage(GetLastResult()));
510 unique_ptr<String> pSectionName(new (std::nothrow) String(pSection->__sectionName));
511 SysTryReturnResult(NID_IO, pSectionName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
513 r = pSectionList->Add(*(pSectionName.release()));
514 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
517 *pRetList = pSectionList.release();
523 _RegistryCore::GetEntryList(const String& sectionName, Collection::HashMap& retMap)
525 result r = E_SUCCESS;
526 unique_ptr<IEnumerator> pEnum(null);
527 unique_ptr<String> pKeyEntryStr(null);
528 unique_ptr<String> pKeyValStr(null);
529 bool sectionFound = false;
530 _RegistrySection* pRegSection = null;
531 _RegistryEntry* pRegEntry = null;
533 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
535 if (_sectionList.GetCount() == 0)
537 return E_SECTION_NOT_FOUND;
540 // Get section information
541 pEnum.reset(_sectionList.GetEnumeratorN());
542 SysTryReturnResult(NID_IO, pEnum != null, E_SYSTEM, "Section list is empty.");
544 while (pEnum->MoveNext() == E_SUCCESS)
546 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
547 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
548 GetErrorMessage(GetLastResult()));
550 if (pRegSection->__sectionName == sectionName)
557 if (sectionFound == false)
559 return E_SECTION_NOT_FOUND;
562 // copy from item list to the new list and return
563 pEnum.reset((pRegSection->__entryList).GetEnumeratorN());
564 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
565 GetErrorMessage(GetLastResult()));
567 while (pEnum->MoveNext() == E_SUCCESS)
569 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
570 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
571 GetErrorMessage(GetLastResult()));
573 pKeyEntryStr.reset(new (std::nothrow) String((pRegEntry->entryName)));
574 SysTryCatch(NID_IO, pKeyEntryStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
575 "[E_OUT_OF_MEMORY] The memory is insufficient.");
576 pKeyValStr.reset(new (std::nothrow) String((pRegEntry->entryValue)));
577 SysTryCatch(NID_IO, pKeyValStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
578 "[E_OUT_OF_MEMORY] The memory is insufficient.");
580 r = retMap.Add(*(pKeyEntryStr.release()), *(pKeyValStr.release()));
581 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
587 retMap.RemoveAll(true);
593 _RegistryCore::GetEntryListN(const String& sectionName, HashMap** pRetList)
595 result r = E_SUCCESS;
597 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
598 SysTryReturnResult(NID_IO, pRetList != null, E_INVALID_ARG, "pRetList cannot be null.");
600 // construct an array list to be returned
601 unique_ptr<HashMap, AllElementsDeleter> pEntryList(new (std::nothrow) HashMap());
602 SysTryReturnResult(NID_IO, pEntryList != null, E_OUT_OF_MEMORY,
603 "The memory is insufficient.");
604 r = pEntryList->Construct();
605 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
606 r = GetEntryList(sectionName, *(pEntryList.get()));
607 *pRetList = pEntryList.release();
613 _RegistryCore::GetAllEntriesN(const String& sectionName)
615 result r = E_SUCCESS;
616 HashMap* pMap = null;
618 r = GetEntryListN(sectionName, &pMap);
619 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
621 SetLastResult(E_SUCCESS);
630 _RegistryCore::GetAllEntryNamesN(const String& sectionName)
632 result r = E_SUCCESS;
634 unique_ptr<ArrayList, AllElementsDeleter> pEntryList(new (std::nothrow) ArrayList());
635 SysTryReturn(NID_IO, pEntryList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
637 r = pEntryList->Construct();
638 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
640 unique_ptr<IMap, AllElementsDeleter> pMap(this->GetAllEntriesN(sectionName));
641 SysTryReturn(NID_IO, pMap != null, null, GetLastResult(), "[%s] Getting all entries was failed.",
642 GetErrorMessage(GetLastResult()));
644 unique_ptr<IMapEnumerator> pMapEnum(pMap->GetMapEnumeratorN());
645 SysTryReturn(NID_IO, pMapEnum, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
646 while (pMapEnum->MoveNext() == E_SUCCESS)
648 String* pKey = dynamic_cast< String* >(pMapEnum->GetKey());
649 SysTryReturn(NID_IO, pKey != null, null, E_IO, "[E_OUT_OF_MEMORY] The system error occurred.");
651 String* pEntryName = new (std::nothrow) String(*pKey);
652 SysTryReturn(NID_IO, pEntryName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
654 r = pEntryList->Add(*pEntryName);
655 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
658 SetLastResult(E_SUCCESS);
659 return pEntryList.release();
663 _RegistryCore::GetValue(const String& sectionName, const String& entryName, String& valStr)
665 bool sectionFound = false;
666 bool entryFound = false;
667 _RegistrySection* pRegSection = null;
668 _RegistryEntry* pRegEntry = null;
670 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
672 if (_sectionList.GetCount() == 0)
674 return E_SECTION_NOT_FOUND;
677 // Get section information
678 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
681 return GetLastResult();
684 while (pEnum->MoveNext() == E_SUCCESS)
686 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
687 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
688 GetErrorMessage(GetLastResult()));
690 if (pRegSection->__sectionName == sectionName)
697 if (sectionFound == false)
699 return E_SECTION_NOT_FOUND;
702 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
705 return GetLastResult();
708 while (pEnum->MoveNext() == E_SUCCESS)
710 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
711 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
712 GetErrorMessage(GetLastResult()));
714 if (pRegEntry->entryName == entryName)
716 valStr = pRegEntry->entryValue;
722 SysTryLog(NID_IO, entryFound, "Entry not found");
724 return((entryFound == true) ? E_SUCCESS : E_KEY_NOT_FOUND);
728 _RegistryCore::AddValue(const String& sectionName, const String& entryName, const String& valStr)
730 result r = E_SUCCESS;
731 bool sectionFound = false;
732 bool entryFound = false;
733 _RegistrySection* pRegSection = null;
736 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
738 // Get section information
739 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
742 return GetLastResult();
745 while (pEnum->MoveNext() == E_SUCCESS)
747 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
748 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
749 GetErrorMessage(GetLastResult()));
751 if (pRegSection->__sectionName == sectionName)
758 if (sectionFound == false)
760 return E_SECTION_NOT_FOUND;
763 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
766 return GetLastResult();
769 while (pEnum->MoveNext() == E_SUCCESS)
771 _RegistryEntry* pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
772 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
773 GetErrorMessage(GetLastResult()));
775 if (pRegEntry->entryName == entryName)
782 if (entryFound == true)
784 return E_KEY_ALREADY_EXIST;
787 unique_ptr<_RegistryEntry> pRegEntry(new (std::nothrow) _RegistryEntry());
788 SysTryReturnResult(NID_IO, pRegEntry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
790 unique_ptr<char[]> pEntryName(_StringConverter::CopyToCharArrayN(entryName));
791 SysTryReturnResult(NID_IO, pEntryName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
793 length = strlen(pEntryName.get());
794 if (!_RegistryCore::CheckEntryName(pEntryName.get()))
796 SysTryReturnResult(NID_IO, false, E_PARSING_FAILED, "Entry name could not be parsed.");
799 unique_ptr<char[]> pEntryValue(_StringConverter::CopyToCharArrayN(valStr));
800 SysTryReturnResult(NID_IO, pEntryValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
802 length = strlen(pEntryValue.get());
803 if (length > 0 && !_RegistryCore::CheckEntryValue(pEntryValue.get()))
805 SysTryReturnResult(NID_IO, false, E_PARSING_FAILED, "Entry value could not be parsed.");
808 pRegEntry->entryName = entryName;
809 pRegEntry->entryValue = valStr;
811 r = pRegSection->__entryList.Add(*(pRegEntry.release()));
812 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
819 _RegistryCore::SetValue(const String& sectionName, const String& entryName, const String& valStr)
821 result r = E_SUCCESS;
822 bool sectionFound = false;
823 bool entryFound = false;
824 _RegistryEntry* pRegEntry = null;
825 _RegistrySection* pRegSection = null;
829 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
831 if (_sectionList.GetCount() == 0)
833 return E_SECTION_NOT_FOUND;
836 // Get section information
837 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
840 return GetLastResult();
843 while (pEnum->MoveNext() == E_SUCCESS)
845 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
846 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
847 GetErrorMessage(GetLastResult()));
849 if (pRegSection->__sectionName == sectionName)
856 if (sectionFound == false)
858 return E_SECTION_NOT_FOUND;
861 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
864 return GetLastResult();
867 while (pEnum->MoveNext() == E_SUCCESS)
869 pRegEntry = dynamic_cast< _RegistryEntry* >(pEnum->GetCurrent());
870 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
871 GetErrorMessage(GetLastResult()));
873 if (pRegEntry->entryName == entryName)
881 if (entryFound == false)
883 return E_KEY_NOT_FOUND;
886 if (valStr.GetLength() > 0)
888 unique_ptr<char[]> pEntryValue(_StringConverter::CopyToCharArrayN(valStr));
889 SysTryReturnResult(NID_IO, pEntryValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
891 length = strlen(pEntryValue.get());
892 if (!_RegistryCore::CheckEntryValue(pEntryValue.get()))
894 return E_PARSING_FAILED;
898 pRegEntry->entryValue = valStr;
899 r = pRegSection->__entryList.SetAt(*pRegEntry, entryIndex);
900 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
907 _RegistryCore::RemoveValue(const String& sectionName, const String& entryName)
909 result r = E_SUCCESS;
910 bool sectionFound = false;
911 bool entryFound = false;
912 _RegistryEntry* pRegEntry = null;
913 _RegistrySection* pRegSection = null;
915 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
917 if (_sectionList.GetCount() <= 0)
919 return E_SECTION_NOT_FOUND;
922 // Get section information
923 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
926 return E_SECTION_NOT_FOUND;
929 while (pEnum->MoveNext() == E_SUCCESS)
931 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
932 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
933 GetErrorMessage(GetLastResult()));
935 if (pRegSection->__sectionName == sectionName)
942 if (sectionFound == false)
944 return E_SECTION_NOT_FOUND;
947 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
950 return E_SECTION_NOT_FOUND;
953 while (pEnum->MoveNext() == E_SUCCESS)
955 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
956 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
957 GetErrorMessage(GetLastResult()));
959 if (pRegEntry->entryName == entryName)
966 if (entryFound == false)
968 return E_KEY_NOT_FOUND;
971 r = pRegSection->__entryList.Remove(*pRegEntry, true);
972 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
979 _RegistryCore::Flush(void)
985 _RegistryCore::Write(void)
987 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
989 if (_pBuffer == null)
991 SysLog(NID_IO, "The buffer is empty.");
995 result r = E_SUCCESS;
997 String openMode(L"w+");
998 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
999 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1001 //TODO: Secure mode is to be handled
1002 r = pFileImpl->Construct(_regPath, "w+", null);
1003 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1005 r = pFileImpl->Write(_pBuffer, _length);
1006 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1008 r = pFileImpl->Flush();
1009 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1015 _RegistryCore::PrepareToWrite(void)
1017 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1019 result r = E_SUCCESS;
1020 _RegistryEntry* pRegEntry = null;
1021 _RegistrySection* pRegSection = null;
1024 delete[] _pBuffer; //clear existing buffer
1028 if (_write == false)
1030 SysLog(NID_IO, "[E_ILLEGAL_ACCESS] Registry cannot be flushed as it was opened in read only mode.");
1031 return E_ILLEGAL_ACCESS;
1034 // if no sections, do nothing
1035 if (_sectionList.GetCount() == 0)
1037 unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(_regPath));
1038 truncate(pFilePath.get(), 0);
1043 // write the registry map to a file
1044 // Get section information
1045 unique_ptr<IEnumerator> pSectionEnum(_sectionList.GetEnumeratorN());
1046 SysTryReturn(NID_IO, pSectionEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.",
1047 GetErrorMessage(GetLastResult()));
1049 while (pSectionEnum->MoveNext() == E_SUCCESS)
1052 pRegSection = dynamic_cast <_RegistrySection*>(pSectionEnum->GetCurrent());
1053 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1054 GetErrorMessage(GetLastResult()));
1056 // write section name to file
1057 r = inputBuffer.Append(String(L"#" + pRegSection->__sectionName + String(L'\n')));
1058 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1060 // read the entries for this section
1061 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1062 SysTryReturn(NID_IO, pSectionEnum != null, GetLastResult(), GetLastResult(), "[%s] Entry list is empty.",
1063 GetErrorMessage(GetLastResult()));
1065 while (pEntryEnum->MoveNext() == E_SUCCESS)
1067 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1068 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1069 GetErrorMessage(GetLastResult()));
1071 // write entry name to file
1072 r = inputBuffer.Append(pRegEntry->entryName + String(L"=" + pRegEntry->entryValue + String(L'\n')));
1073 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1078 _pBuffer = (byte*)_StringConverter::CopyToCharArrayN(inputBuffer);
1079 SysTryReturn(NID_IO, _pBuffer != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1080 GetErrorMessage(GetLastResult()));
1082 _length = strlen((char*)_pBuffer);
1088 _RegistryCore::GetSectionIndex(const String& sectionName)
1090 bool sectionFound = false;
1091 _RegistrySection* pRegSection = null;
1094 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1095 SysTryReturn(NID_IO, sectionName.GetLength() > 0, -1, E_INVALID_ARG,
1096 "[E_INVALID_ARG] sectionName.GetLength() <= 0");
1098 SysTryReturn(NID_IO, _sectionList.GetCount() > 0, -1, E_SECTION_NOT_FOUND, "[E_SECTION_NOT_FOUND] Section not found.");
1100 // Get section information
1101 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
1102 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.",
1103 GetErrorMessage(GetLastResult()));
1105 while (pEnum->MoveNext() == E_SUCCESS)
1107 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1108 SysTryReturn(NID_IO, pRegSection != null, -1, GetLastResult(), "[%s] Propagated.",
1109 GetErrorMessage(GetLastResult()));
1111 if (pRegSection->__sectionName == sectionName)
1113 sectionFound = true;
1119 if (sectionFound == false)
1121 return E_SECTION_NOT_FOUND;
1124 SetLastResult(E_SUCCESS);
1129 _RegistryCore::Removekey(const String& sectionName, const String& keyName)
1131 result r = E_SUCCESS;
1132 int sectionIndex = -1;
1133 int entryIndex = -1;
1135 sectionIndex = GetSectionIndex(sectionName);
1136 r = GetLastResult();
1137 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1139 entryIndex = GetEntryIndex(sectionIndex, keyName);
1140 r = GetLastResult();
1141 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1143 RemoveEntry(sectionIndex, entryIndex);
1144 r = GetLastResult();
1145 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1147 SetLastResult(E_SUCCESS);
1152 _RegistryCore::GetAllSectionCount(void)
1154 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1156 SetLastResult(E_SUCCESS);
1157 return _sectionList.GetCount();
1161 _RegistryCore::GetAllEntryCount(int sectionIndex)
1163 _RegistrySection* pRegSection = null;
1166 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1167 SysTryReturn(NID_IO, sectionIndex >= 0, 0, E_INVALID_ARG, "[E_INVALID_ARG] sectionIndex < 0.");
1168 listSize = _sectionList.GetCount();
1169 SysTryReturn(NID_IO, listSize > 0, 0, E_SECTION_NOT_FOUND, "[E_INVALID_ARG] section listSize <= 0.");
1170 SysTryReturn(NID_IO, sectionIndex >= listSize, 0, E_SECTION_NOT_FOUND,
1171 "[E_INVALID_ARG] sectionIndex > listSize.");
1173 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1174 SysTryReturn(NID_IO, pRegSection != null, 0, E_SECTION_NOT_FOUND,
1175 "[E_SECTION_NOT_FOUND] section was not found.");
1177 SetLastResult(E_SUCCESS);
1178 return pRegSection->__entryList.GetCount();
1182 _RegistryCore::RemoveEntry(int sectionIndex, int entryIndex)
1184 result r = E_SUCCESS;
1186 int entryListSize = 0;
1187 _RegistrySection* pRegSection = null;
1189 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1190 SysTryReturnVoidResult(NID_IO, sectionIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG] sectionIndex < 0.");
1191 SysTryReturnVoidResult(NID_IO, entryIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG] entryIndex < 0.");
1193 listSize = _sectionList.GetCount();
1194 SysTryReturnVoidResult(NID_IO, listSize > 0, E_SECTION_NOT_FOUND,
1195 "[E_SECTION_NOT_FOUND] section listSize <= 0.");
1196 SysTryReturnVoidResult(NID_IO, sectionIndex < listSize, E_SECTION_NOT_FOUND,
1197 "[E_SECTION_NOT_FOUND] sectionIndex >= listSize.");
1199 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1200 SysTryReturnVoidResult(NID_IO, pRegSection != null, E_SECTION_NOT_FOUND,
1201 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1203 r = GetLastResult();
1204 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r,"[%s] Propagated.", GetErrorMessage(r));
1206 entryListSize = pRegSection->__entryList.GetCount();
1207 SysTryReturnVoidResult(NID_IO, (entryListSize > 0 || entryIndex < entryListSize), E_KEY_NOT_FOUND,
1208 "[E_KEY_NOT_FOUND] entryListSize is 0 or entryIndex >= entryListSize.");
1210 r = pRegSection->__entryList.RemoveAt(entryIndex, true);
1211 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1213 SetLastResult(E_SUCCESS);
1217 _RegistryCore::ModifyEntryValue(int sectionIndex, int entryIndex, _RegValueType type, const void* pValue, int size)
1219 SetLastResult(E_UNSUPPORTED_OPERATION);
1223 _RegistryCore::GetEntryIndex(int sectionIndex, const String& entryName)
1225 _RegistrySection* pRegSection = null;
1226 _RegistryEntry* pRegEntry = null;
1228 bool entryFound = false;
1229 int entryIndex = -1;
1231 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1232 SysTryReturn(NID_IO, sectionIndex >= 0, -1, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1233 SysTryReturn(NID_IO, entryName.GetLength() > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG]entryName length < 0.");
1235 listSize = _sectionList.GetCount();
1236 SysTryReturn(NID_IO, listSize > 0, -1, E_SECTION_NOT_FOUND,
1237 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1238 SysTryReturn(NID_IO, sectionIndex < listSize, -1, E_SECTION_NOT_FOUND,
1239 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1241 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1242 SysTryReturn(NID_IO, pRegSection != null, -1, E_SECTION_NOT_FOUND,
1243 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1245 // read the entries for this section
1246 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1247 SysTryReturn(NID_IO, pEntryEnum != null, -1, GetLastResult(), "[%s] Entry list is empty.",
1248 GetErrorMessage(GetLastResult()));
1251 while (pEntryEnum->MoveNext() == E_SUCCESS)
1253 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1254 SysTryReturn(NID_IO, pRegEntry != null, -1, GetLastResult(), "[%s] Propagated.",
1255 GetErrorMessage(GetLastResult()));
1257 if (entryName == pRegEntry->entryName)
1265 SysTryReturn(NID_IO, entryFound != false, -1, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] entry was not found.");
1267 SetLastResult(E_SUCCESS);
1272 _RegistryCore::GetEntryName(int sectionIndex, int entryIndex)
1274 _RegistrySection* pRegSection = null;
1275 _RegistryEntry* pRegEntry = null;
1280 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1281 SysTryReturn(NID_IO, sectionIndex >= 0, empty, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1282 SysTryReturn(NID_IO, entryIndex >= 0, empty, E_INVALID_ARG, "[E_INVALID_ARG]entryIndex < 0.");
1284 listSize = _sectionList.GetCount();
1285 SysTryReturn(NID_IO, listSize > 0, empty, E_SECTION_NOT_FOUND,
1286 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1287 SysTryReturn(NID_IO, sectionIndex < listSize, empty, E_SECTION_NOT_FOUND,
1288 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1290 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1291 SysTryReturn(NID_IO, pRegSection != null, empty, E_SECTION_NOT_FOUND,
1292 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1294 // read the entries for this section
1295 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1296 SysTryReturn(NID_IO, pEntryEnum != null, empty, GetLastResult(), "[%s] Entry list is empty.",
1297 GetErrorMessage(GetLastResult()));
1299 tmpIndex = entryIndex;
1300 while (pEntryEnum->MoveNext() == E_SUCCESS)
1304 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1305 SysTryReturn(NID_IO, pRegEntry != null, empty, GetLastResult(), "[%s] Propagated.",
1306 GetErrorMessage(GetLastResult()));
1313 SysTryReturn(NID_IO, pRegEntry != null, empty, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] entry was not found.");
1315 SetLastResult(E_SUCCESS);
1316 return pRegEntry->entryName;
1320 _RegistryCore::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType type, void* pValue, int* pSize)
1322 _RegistrySection* pRegSection = null;
1323 _RegistryEntry* pRegEntry = null;
1324 result r = E_SUCCESS;
1326 int tmpEntryIndex = -1;
1327 int entryListSize = 0;
1328 String strValueEncoded;
1330 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1331 SysTryReturnVoidResult(NID_IO, sectionIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1332 SysTryReturnVoidResult(NID_IO, type >= 0 && type <= REG_VALUE_TYPE_MAX, E_INVALID_ARG,
1333 "[E_INVALID_ARG] invalid registry type.");
1334 SysTryReturnVoidResult(NID_IO, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG]pValue is null.");
1335 SysTryReturnVoidResult(NID_IO, pSize != null, E_INVALID_ARG, "[E_INVALID_ARG]pSize is null.");
1336 SysTryReturnVoidResult(NID_IO, *pSize > 0, E_INVALID_ARG, "[E_INVALID_ARG] *pSize < 0.");
1338 listSize = _sectionList.GetCount();
1339 SysTryReturnVoidResult(NID_IO, listSize > 0, E_SECTION_NOT_FOUND,
1340 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1341 SysTryReturnVoidResult(NID_IO, sectionIndex < listSize, E_SECTION_NOT_FOUND,
1342 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1344 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1345 SysTryReturnVoidResult(NID_IO, pRegSection != null, E_SECTION_NOT_FOUND,
1346 "[E_SECTION_NOT_FOUND] section at index (%d) is not available.", sectionIndex);
1348 entryListSize = pRegSection->__entryList.GetCount();
1349 SysTryReturnVoidResult(NID_IO, entryListSize > 0, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry list is empty.");
1350 SysTryReturnVoidResult(NID_IO, entryIndex < entryListSize, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry not found.");
1352 // read the entries for this section
1353 unique_ptr< IEnumerator > pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1354 SysTryReturnVoidResult(NID_IO, pEntryEnum != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND]Entry list is empty.");
1356 tmpEntryIndex = entryIndex;
1357 while (pEntryEnum->MoveNext() == E_SUCCESS)
1359 if (entryIndex == 0)
1361 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1362 SysTryReturnVoidResult(NID_IO, pRegEntry != null, GetLastResult(), "[%s] Propagated.",
1363 GetErrorMessage(GetLastResult()));
1371 SysTryReturnVoidResult(NID_IO, pRegEntry != null, E_KEY_NOT_FOUND,
1372 "[E_KEY_NOT_FOUND] Entry was not found.");
1374 strValueEncoded = pRegEntry->entryValue;
1376 DecodeData(strValueEncoded, type, pValue, pSize);
1377 r = GetLastResult();
1378 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1380 SetLastResult(E_SUCCESS);
1385 _RegistryCore::GetSectionName(int sectionIndex)
1387 _RegistrySection* pRegSection = null;
1388 result r = E_SUCCESS;
1390 String sectionName("");
1392 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1393 SysTryReturn(NID_IO, sectionIndex >= 0, sectionName, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1395 listSize = _sectionList.GetCount();
1396 SysTryReturn(NID_IO, listSize > 0, sectionName, E_SECTION_NOT_FOUND,
1397 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1398 SysTryReturn(NID_IO, sectionIndex < listSize, sectionName, E_SECTION_NOT_FOUND,
1399 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1401 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1402 SysTryReturn(NID_IO, pRegSection != null, sectionName, E_SECTION_NOT_FOUND,
1403 "[E_SECTION_NOT_FOUND] sectionIndex is not available.");
1405 r = GetLastResult();
1406 SysTryReturn(NID_IO, !IsFailed(r), sectionName, r, "[%s] Propagated.", GetErrorMessage(r));
1408 SetLastResult(E_SUCCESS);
1409 return pRegSection->__sectionName;
1413 _RegistryCore::GetSectionByIndex(int sectionIndex)
1415 _RegistrySection* pRegSection = null;
1416 int index = sectionIndex;
1418 // Get section information
1419 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
1420 SysTryReturn(NID_IO, pEnum != null, null, GetLastResult(),
1421 "[%s] section list is empty.", GetErrorMessage(GetLastResult()));
1423 while (pEnum->MoveNext() == E_SUCCESS)
1427 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
1428 SysTryReturn(NID_IO, pRegSection != null, null, GetLastResult(), "[%s] Propagated.",
1429 GetErrorMessage(GetLastResult()));
1436 SysTryReturn(NID_IO, pRegSection != null, null, E_SECTION_NOT_FOUND,
1437 "[E_SECTION_NOT_FOUND] Section was not found.");
1439 SetLastResult(E_SUCCESS);
1445 _RegistryCore::DecodeData(const String& strValueEncoded, _RegValueType type, void* pValue, int* pSize)
1447 SysAssert(strValueEncoded.GetLength() > 0);
1450 SysAssert(pSize && *pSize > 0);
1452 SysTryReturnVoidResult(NID_IO, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument.");
1453 SysTryReturnVoidResult(NID_IO, pSize != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument.");
1457 case REG_VALUE_TYPE_INT: // (-2147483647 - 1) ~ 2147483647
1460 result r = E_SUCCESS;
1462 if (*pSize != sizeof(int))
1464 SetLastResult(E_INVALID_ARG);
1468 r = Integer::Parse(strValueEncoded, retIntVal);
1469 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1471 *(int*) pValue = retIntVal;
1475 case REG_VALUE_TYPE_REAL: // Holds signed IEEE 64-bit (8-byte) double-precision floating-point numbers
1476 // ranging in value from -1.79769313486231570E+308 through -4.94065645841246544E-324
1477 // for negative values and from 4.94065645841246544E-324 through 1.79769313486231570E+308
1478 // for positive values.
1480 double retDoubleVal = 0;
1482 if (*pSize != sizeof(double))
1484 SetLastResult(E_INVALID_ARG);
1488 retDoubleVal = _LocalizedNumParser::ToDouble(strValueEncoded, "C");
1489 result r = GetLastResult();
1490 SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1492 *(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);