2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FIo_RegistryCore.cpp
19 * @brief This is the implementation file for _RegistryCore class.
24 #include <unique_ptr.h>
28 #include <FBaseInteger.h>
29 #include <FBaseDouble.h>
30 #include <FBaseFloat.h>
31 #include <FBaseUuId.h>
32 #include <FBaseByteBuffer.h>
33 #include <FBaseColIEnumerator.h>
34 #include <FBaseUtilStringTokenizer.h>
35 #include <FBaseResult.h>
36 #include <FBaseSysLog.h>
37 #include <FBaseColAllElementsDeleter.h>
39 #include <FIoRegistry.h>
41 #include <FBase_StringConverter.h>
42 #include <FBase_LocalizedNumParser.h>
43 #include <FBase_NativeError.h>
44 #include <FApp_AppInfo.h>
45 #include "FIo_FileImpl.h"
46 #include "FIo_NormalRegistry.h"
47 #include "FIo_SecureRegistry.h"
48 #include "FIo_RegistryCore.h"
49 #include "FIo_SecureIoUtil.h"
52 using namespace Tizen::Base;
53 using namespace Tizen::Base::Utility;
54 using namespace Tizen::Base::Collection;
56 namespace Tizen { namespace Io
59 static const size_t _MAX_REG_OPENMODE_LENGTH = 2;
61 #define _REGISTRY_SECTION_MARKER_CHAR L'#'
62 #define _REGISTRY_SECTION_VALUE_ASSIGNMENT_MARKER_CHAR L'='
64 class _RegistrySection
68 ~_RegistrySection(void);
71 LinkedList __entryList;
72 friend class _RegistryCore;
75 _RegistrySection::~_RegistrySection(void)
77 __entryList.RemoveAll(true);
88 _RegistryCore::_RegistryCore(void)
101 _RegistryCore::~_RegistryCore(void)
107 _RegistryCore::VerifyRegistryOpenMode(const char* pOpenMode)
109 if (pOpenMode == null)
111 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode is null.");
115 if (strlen(pOpenMode) > _MAX_REG_OPENMODE_LENGTH)
117 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
121 switch (pOpenMode[0])
135 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
139 switch (pOpenMode[1])
144 if (pOpenMode[2] == '\0')
152 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
156 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
164 _RegistryCore::Parse(void)
166 result r = E_SUCCESS;
168 bool sectionFound = false;
171 unique_ptr<char[]> pEntryName(null);
172 unique_ptr<char[]> pEntryValue(null);
173 int firstTokenPos = 0;
176 if (_pBuffer == null)
178 SysLog(NID_IO, "The buffer is empty.");
182 String inputStr = String((char*)_pBuffer);
183 StringTokenizer lineTok(inputStr, L"\n");
185 r = lineTok.GetNextToken(line);
187 while (r == E_SUCCESS)
190 if (line.GetLength() == 0)
192 r = lineTok.GetNextToken(line);
196 // find registry section marker
197 line.GetCharAt(0, ch);
198 if (ch == _REGISTRY_SECTION_MARKER_CHAR)
202 // skip '#' and "\n' at the end
203 r = line.SubString(1, line.GetLength() - 1, sectionName);
204 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
206 // if End of the document follows section name without newline character.
207 sectionName.GetCharAt(sectionName.GetLength() - 1, ch);
210 sectionName.SetLength(sectionName.GetLength() - 1); //Remove '\n'
213 // for compatibility check if line contains '\r' at the end
214 sectionName.GetCharAt(sectionName.GetLength() - 1, ch);
217 sectionName.SetLength(sectionName.GetLength() - 1); //Remove '\r'
220 //TODO: check if remaining data in sectionName is valid or not
221 //after removing '#', '\n', and '\r', sectionName should contain at least 1 valid character
223 sectionName.GetLength() > 0, r = E_PARSING_FAILED, E_PARSING_FAILED,
224 "[E_PARSING_FAILED] Section length cannot be <2.");
226 unique_ptr<char[]> pSectionName(_StringConverter::CopyToCharArrayN(sectionName));
227 SysTryCatch(NID_IO, pSectionName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
228 "[E_OUT_OF_MEMORY] The memory is insufficient.");
230 if (!_RegistryCore::CheckSectionName(pSectionName.get()))
232 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
233 "[E_PARSING_FAILED] Section name could not be parsed.");
236 r = _RegistryCore::AddSection(_sectionList, sectionName);
237 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
239 else // not a section.. but may belongs to a section (entry)
241 // ignores blank lines
242 line.GetCharAt(0, ch);
243 if ((ch == L'\n') || (ch == L'\r'))
245 r = lineTok.GetNextToken(line);
250 // Need to check for tabs, spaces in the beginning of each line
252 // if no section found till now.. ignore
253 if (sectionFound == false)
255 r = lineTok.GetNextToken(line);
259 // if End of the document follows entry value without newline character.
260 line.GetCharAt(line.GetLength() - 1, ch);
263 line.SetLength(line.GetLength() - 1); //Remove '\n'
266 line.GetCharAt(line.GetLength() - 1, ch);
269 line.SetLength(line.GetLength() - 1); //Remove '\r'
272 String entryName("");
275 // we will have sectionitem=value (or) sectionitem= (or) sectionitem
276 // as our string. Break this into entry and value
277 StringTokenizer strTok(line, L"=");
279 // extract entry name
280 if (strTok.HasMoreTokens())
282 line.IndexOf(L'=', 0, firstTokenPos); // position of first occurance of '=' in a line
283 if (firstTokenPos == 0)
285 // entryName should not be empty. i.e., "=value" or just '=' is invalid case
286 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
287 "[E_PARSING_FAILED] Entry name cannot be empty.");
291 strTok.GetNextToken(entryName);
294 else // missing '=' in entry line or entry line does not seem to contain "xxx=xxx" format
296 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED, "[E_PARSING_FAILED] invalid entry Line found.");
299 // check if entry name contains invalid chars
300 pEntryName.reset(_StringConverter::CopyToCharArrayN(entryName));
301 SysTryCatch(NID_IO, pEntryName, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
302 "[E_OUT_OF_MEMORY] The memory is insufficient.");
304 if (!_RegistryCore::CheckEntryName(pEntryName.get()))
306 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
307 "[E_PARSING_FAILED] Entry name could not be parsed.");
310 if (line.GetLength() > firstTokenPos + 1)
312 line.SubString(firstTokenPos + 1, entryVal); // extract entry value
315 // check if entry value contains invalid chars
316 pEntryValue.reset(_StringConverter::CopyToCharArrayN(entryVal));
317 SysTryCatch(NID_IO, pEntryValue, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
318 "[E_OUT_OF_MEMORY] The memory is insufficient.");
320 length = strlen(pEntryValue.get());
323 if (!_RegistryCore::CheckEntryValue(pEntryValue.get()))
325 SysTryCatch(NID_IO, false, r = E_PARSING_FAILED, E_PARSING_FAILED,
326 "[E_PARSING_FAILED] Entry value could not be parsed.");
330 // add the entry and value
331 r = _RegistryCore::AddValue(_sectionList, sectionName, entryName, entryVal);
332 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
334 r = lineTok.GetNextToken(line);
337 if (r == E_OUT_OF_RANGE)
345 if (r == E_PARSING_FAILED && Tizen::App::_AppInfo::IsOspCompat() == false)
347 r = E_INVALID_FORMAT;
350 SysLog(NID_IO, "[%s] exception occurred.", GetErrorMessage(r));
355 _RegistryCore::Load(const String& regPath, const char* pOpenMode)
357 result r = E_SUCCESS;
358 delete[] _pBuffer; //clear existing buffer
362 // open registry file
363 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl());
364 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
366 r = pFileImpl->Construct(regPath, pOpenMode, null);
367 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Constructing the file has failed.", GetErrorMessage(r));
369 // reset the section list to be sure that its empty
370 _sectionList.RemoveAll(true);
372 if (_truncate == false)
374 // load the registry file
375 r = pFileImpl->ReadN((char**)&_pBuffer, _length);
382 __pFileImpl = pFileImpl.release();
388 _RegistryCore::AddSection(const String& sectionName)
390 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
392 result r = E_SUCCESS;
393 bool sectionFound = false;
395 // Get section information
396 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
399 return GetLastResult();
402 while (pEnum->MoveNext() == E_SUCCESS)
404 _RegistrySection* pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
405 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
406 GetErrorMessage(GetLastResult()));
408 if (pRegSection->__sectionName == sectionName)
415 if (sectionFound == true)
417 return E_SECTION_ALREADY_EXIST;
420 // create a key with section name
421 unique_ptr<_RegistrySection> pRegSection(new (std::nothrow) _RegistrySection());
422 SysTryReturnResult(NID_IO, pRegSection != null, E_OUT_OF_MEMORY,
423 "The memory is insufficient.");
425 pRegSection->__sectionName = sectionName;
427 // add new section and section-entry map
428 r = _sectionList.Add(*(pRegSection.release()));
429 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
436 _RegistryCore::RemoveSection(const String& sectionName)
438 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
440 result r = E_SUCCESS;
441 _RegistrySection* pRegSection = null;
442 bool sectionFound = false;
444 if (_sectionList.GetCount() == 0)
446 return E_SECTION_NOT_FOUND;
449 // Get section information
450 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
453 return GetLastResult();
456 while (pEnum->MoveNext() == E_SUCCESS)
458 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
459 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagating.",
460 GetErrorMessage(GetLastResult()));
462 if (pRegSection->__sectionName == sectionName)
469 if (sectionFound == false)
471 return E_SECTION_NOT_FOUND;
474 r = _sectionList.Remove(*pRegSection, true);
475 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
482 _RegistryCore::GetSectionListN(IList** pRetList)
484 result r = E_SUCCESS;
485 unique_ptr<IEnumerator> pEnum(null);
486 _RegistrySection* pSection = null;
488 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
489 SysTryReturnResult(NID_IO, pRetList != null, E_INVALID_ARG, "pList cannot be null.");
491 // construct an array list to be returned
492 unique_ptr<ArrayList, AllElementsDeleter> pSectionList(new (std::nothrow) ArrayList());
493 SysTryReturnResult(NID_IO, pSectionList != null, E_OUT_OF_MEMORY,
494 "The memory is insufficient.");
496 r = pSectionList->Construct();
497 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
499 // if no sectons, return empty list
500 if (_sectionList.GetCount() == 0)
502 *pRetList = pSectionList.release();
506 // copy from class section list to the new list and return
507 pEnum.reset(_sectionList.GetEnumeratorN());
508 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.", GetErrorMessage(GetLastResult()));
510 while (pEnum->MoveNext() == E_SUCCESS)
512 pSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
513 SysTryReturn(NID_IO, pSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
514 GetErrorMessage(GetLastResult()));
516 unique_ptr<String> pSectionName(new (std::nothrow) String(pSection->__sectionName));
517 SysTryReturnResult(NID_IO, pSectionName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
519 r = pSectionList->Add(*(pSectionName.release()));
520 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
523 *pRetList = pSectionList.release();
529 _RegistryCore::GetEntryList(const String& sectionName, Collection::HashMap& retMap)
531 result r = E_SUCCESS;
532 unique_ptr<IEnumerator> pEnum(null);
533 unique_ptr<String> pKeyEntryStr(null);
534 unique_ptr<String> pKeyValStr(null);
535 bool sectionFound = false;
536 _RegistrySection* pRegSection = null;
537 _RegistryEntry* pRegEntry = null;
539 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
541 if (_sectionList.GetCount() == 0)
543 return E_SECTION_NOT_FOUND;
546 // Get section information
547 pEnum.reset(_sectionList.GetEnumeratorN());
548 SysTryReturnResult(NID_IO, pEnum != null, E_SYSTEM, "Section list is empty.");
550 while (pEnum->MoveNext() == E_SUCCESS)
552 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
553 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
554 GetErrorMessage(GetLastResult()));
556 if (pRegSection->__sectionName == sectionName)
563 if (sectionFound == false)
565 return E_SECTION_NOT_FOUND;
568 // copy from item list to the new list and return
569 pEnum.reset((pRegSection->__entryList).GetEnumeratorN());
570 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
571 GetErrorMessage(GetLastResult()));
573 while (pEnum->MoveNext() == E_SUCCESS)
575 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
576 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
577 GetErrorMessage(GetLastResult()));
579 pKeyEntryStr.reset(new (std::nothrow) String((pRegEntry->entryName)));
580 SysTryCatch(NID_IO, pKeyEntryStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
581 "[E_OUT_OF_MEMORY] The memory is insufficient.");
582 pKeyValStr.reset(new (std::nothrow) String((pRegEntry->entryValue)));
583 SysTryCatch(NID_IO, pKeyValStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
584 "[E_OUT_OF_MEMORY] The memory is insufficient.");
586 r = retMap.Add(*(pKeyEntryStr.release()), *(pKeyValStr.release()));
587 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
593 retMap.RemoveAll(true);
599 _RegistryCore::GetEntryListN(const String& sectionName, HashMap** pRetList)
601 result r = E_SUCCESS;
603 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
604 SysTryReturnResult(NID_IO, pRetList != null, E_INVALID_ARG, "pRetList cannot be null.");
606 // construct an array list to be returned
607 unique_ptr<HashMap, AllElementsDeleter> pEntryList(new (std::nothrow) HashMap());
608 SysTryReturnResult(NID_IO, pEntryList != null, E_OUT_OF_MEMORY,
609 "The memory is insufficient.");
610 r = pEntryList->Construct();
611 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
612 r = GetEntryList(sectionName, *(pEntryList.get()));
613 *pRetList = pEntryList.release();
619 _RegistryCore::GetAllEntriesN(const String& sectionName)
621 result r = E_SUCCESS;
622 HashMap* pMap = null;
624 r = GetEntryListN(sectionName, &pMap);
625 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
627 SetLastResult(E_SUCCESS);
636 _RegistryCore::GetAllEntryNamesN(const String& sectionName)
638 result r = E_SUCCESS;
640 unique_ptr<ArrayList, AllElementsDeleter> pEntryList(new (std::nothrow) ArrayList());
641 SysTryReturn(NID_IO, pEntryList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
643 r = pEntryList->Construct();
644 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
646 unique_ptr<IMap, AllElementsDeleter> pMap(this->GetAllEntriesN(sectionName));
647 SysTryReturn(NID_IO, pMap != null, null, GetLastResult(), "[%s] Getting all entries was failed.",
648 GetErrorMessage(GetLastResult()));
650 unique_ptr<IMapEnumerator> pMapEnum(pMap->GetMapEnumeratorN());
651 SysTryReturn(NID_IO, pMapEnum, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
652 while (pMapEnum->MoveNext() == E_SUCCESS)
654 String* pKey = dynamic_cast< String* >(pMapEnum->GetKey());
655 SysTryReturn(NID_IO, pKey != null, null, E_IO, "[E_OUT_OF_MEMORY] The system error occurred.");
657 String* pEntryName = new (std::nothrow) String(*pKey);
658 SysTryReturn(NID_IO, pEntryName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
660 r = pEntryList->Add(*pEntryName);
661 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
664 SetLastResult(E_SUCCESS);
665 return pEntryList.release();
669 _RegistryCore::GetValue(const String& sectionName, const String& entryName, String& valStr)
671 bool sectionFound = false;
672 bool entryFound = false;
673 _RegistrySection* pRegSection = null;
674 _RegistryEntry* pRegEntry = null;
676 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
678 if (_sectionList.GetCount() == 0)
680 return E_SECTION_NOT_FOUND;
683 // Get section information
684 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
687 return GetLastResult();
690 while (pEnum->MoveNext() == E_SUCCESS)
692 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
693 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
694 GetErrorMessage(GetLastResult()));
696 if (pRegSection->__sectionName == sectionName)
703 if (sectionFound == false)
705 return E_SECTION_NOT_FOUND;
708 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
711 return GetLastResult();
714 while (pEnum->MoveNext() == E_SUCCESS)
716 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
717 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
718 GetErrorMessage(GetLastResult()));
720 if (pRegEntry->entryName == entryName)
722 valStr = pRegEntry->entryValue;
728 SysTryLog(NID_IO, entryFound, "Entry not found");
730 return((entryFound == true) ? E_SUCCESS : E_KEY_NOT_FOUND);
734 _RegistryCore::AddValue(const String& sectionName, const String& entryName, const String& valStr)
736 result r = E_SUCCESS;
737 bool sectionFound = false;
738 bool entryFound = false;
739 _RegistrySection* pRegSection = null;
742 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
744 // Get section information
745 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
748 return GetLastResult();
751 while (pEnum->MoveNext() == E_SUCCESS)
753 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
754 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
755 GetErrorMessage(GetLastResult()));
757 if (pRegSection->__sectionName == sectionName)
764 if (sectionFound == false)
766 return E_SECTION_NOT_FOUND;
769 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
772 return GetLastResult();
775 while (pEnum->MoveNext() == E_SUCCESS)
777 _RegistryEntry* pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
778 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
779 GetErrorMessage(GetLastResult()));
781 if (pRegEntry->entryName == entryName)
788 if (entryFound == true)
790 return E_KEY_ALREADY_EXIST;
793 unique_ptr<_RegistryEntry> pRegEntry(new (std::nothrow) _RegistryEntry());
794 SysTryReturnResult(NID_IO, pRegEntry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
796 unique_ptr<char[]> pEntryName(_StringConverter::CopyToCharArrayN(entryName));
797 SysTryReturnResult(NID_IO, pEntryName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
799 length = strlen(pEntryName.get());
800 if (!_RegistryCore::CheckEntryName(pEntryName.get()))
802 SysTryReturnResult(NID_IO, false, E_PARSING_FAILED, "Entry name could not be parsed.");
805 unique_ptr<char[]> pEntryValue(_StringConverter::CopyToCharArrayN(valStr));
806 SysTryReturnResult(NID_IO, pEntryValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
808 length = strlen(pEntryValue.get());
809 if (length > 0 && !_RegistryCore::CheckEntryValue(pEntryValue.get()))
811 SysTryReturnResult(NID_IO, false, E_PARSING_FAILED, "Entry value could not be parsed.");
814 pRegEntry->entryName = entryName;
815 pRegEntry->entryValue = valStr;
817 r = pRegSection->__entryList.Add(*(pRegEntry.release()));
818 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
825 _RegistryCore::SetValue(const String& sectionName, const String& entryName, const String& valStr)
827 result r = E_SUCCESS;
828 bool sectionFound = false;
829 bool entryFound = false;
830 _RegistryEntry* pRegEntry = null;
831 _RegistrySection* pRegSection = null;
835 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
837 if (_sectionList.GetCount() == 0)
839 return E_SECTION_NOT_FOUND;
842 // Get section information
843 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
846 return GetLastResult();
849 while (pEnum->MoveNext() == E_SUCCESS)
851 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
852 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
853 GetErrorMessage(GetLastResult()));
855 if (pRegSection->__sectionName == sectionName)
862 if (sectionFound == false)
864 return E_SECTION_NOT_FOUND;
867 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
870 return GetLastResult();
873 while (pEnum->MoveNext() == E_SUCCESS)
875 pRegEntry = dynamic_cast< _RegistryEntry* >(pEnum->GetCurrent());
876 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
877 GetErrorMessage(GetLastResult()));
879 if (pRegEntry->entryName == entryName)
887 if (entryFound == false)
889 return E_KEY_NOT_FOUND;
892 if (valStr.GetLength() > 0)
894 unique_ptr<char[]> pEntryValue(_StringConverter::CopyToCharArrayN(valStr));
895 SysTryReturnResult(NID_IO, pEntryValue != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
897 length = strlen(pEntryValue.get());
898 if (!_RegistryCore::CheckEntryValue(pEntryValue.get()))
900 return E_PARSING_FAILED;
904 pRegEntry->entryValue = valStr;
905 r = pRegSection->__entryList.SetAt(*pRegEntry, entryIndex);
906 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
913 _RegistryCore::RemoveValue(const String& sectionName, const String& entryName)
915 result r = E_SUCCESS;
916 bool sectionFound = false;
917 bool entryFound = false;
918 _RegistryEntry* pRegEntry = null;
919 _RegistrySection* pRegSection = null;
921 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
923 if (_sectionList.GetCount() <= 0)
925 return E_SECTION_NOT_FOUND;
928 // Get section information
929 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
932 return E_SECTION_NOT_FOUND;
935 while (pEnum->MoveNext() == E_SUCCESS)
937 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
938 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
939 GetErrorMessage(GetLastResult()));
941 if (pRegSection->__sectionName == sectionName)
948 if (sectionFound == false)
950 return E_SECTION_NOT_FOUND;
953 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
956 return E_SECTION_NOT_FOUND;
959 while (pEnum->MoveNext() == E_SUCCESS)
961 pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
962 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
963 GetErrorMessage(GetLastResult()));
965 if (pRegEntry->entryName == entryName)
972 if (entryFound == false)
974 return E_KEY_NOT_FOUND;
977 r = pRegSection->__entryList.Remove(*pRegEntry, true);
978 SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
985 _RegistryCore::Flush(void)
991 _RegistryCore::Write(void)
993 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
995 if (_pBuffer == null)
997 SysLog(NID_IO, "The buffer is empty.");
1001 result r = __pFileImpl->Seek(FILESEEKPOSITION_BEGIN, 0);
1002 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1004 r = __pFileImpl->Truncate(0);
1005 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1007 r = __pFileImpl->Write(_pBuffer, _length);
1008 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1010 r = __pFileImpl->Flush();
1011 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1017 _RegistryCore::PrepareToWrite(void)
1019 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1021 result r = E_SUCCESS;
1022 _RegistryEntry* pRegEntry = null;
1023 _RegistrySection* pRegSection = null;
1026 delete[] _pBuffer; //clear existing buffer
1030 if (_write == false)
1032 SysLog(NID_IO, "[E_ILLEGAL_ACCESS] Registry cannot be flushed as it was opened in read only mode.");
1033 return E_ILLEGAL_ACCESS;
1036 // if no sections, do nothing
1037 if (_sectionList.GetCount() == 0)
1039 unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(_regPath));
1040 int res = truncate(pFilePath.get(), 0);
1041 SysTryReturnResult(NID_IO, res == 0, __ConvertNativeErrorToResult(errno),
1042 "Failed to truncate. errno: %d (%s)", errno, strerror(errno));
1046 // write the registry map to a file
1047 // Get section information
1048 unique_ptr<IEnumerator> pSectionEnum(_sectionList.GetEnumeratorN());
1049 SysTryReturn(NID_IO, pSectionEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.",
1050 GetErrorMessage(GetLastResult()));
1052 while (pSectionEnum->MoveNext() == E_SUCCESS)
1055 pRegSection = dynamic_cast <_RegistrySection*>(pSectionEnum->GetCurrent());
1056 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1057 GetErrorMessage(GetLastResult()));
1059 // write section name to file
1060 r = inputBuffer.Append(String(L"#" + pRegSection->__sectionName + String(L'\n')));
1061 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1063 // read the entries for this section
1064 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1065 SysTryReturn(NID_IO, pSectionEnum != null, GetLastResult(), GetLastResult(), "[%s] Entry list is empty.",
1066 GetErrorMessage(GetLastResult()));
1068 while (pEntryEnum->MoveNext() == E_SUCCESS)
1070 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1071 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1072 GetErrorMessage(GetLastResult()));
1074 // write entry name to file
1075 r = inputBuffer.Append(pRegEntry->entryName + String(L"=" + pRegEntry->entryValue + String(L'\n')));
1076 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1081 _pBuffer = (byte*)_StringConverter::CopyToCharArrayN(inputBuffer);
1082 SysTryReturn(NID_IO, _pBuffer != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1083 GetErrorMessage(GetLastResult()));
1085 _length = strlen((char*)_pBuffer);
1091 _RegistryCore::GetSectionIndex(const String& sectionName)
1093 bool sectionFound = false;
1094 _RegistrySection* pRegSection = null;
1097 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1098 SysTryReturn(NID_IO, sectionName.GetLength() > 0, -1, E_INVALID_ARG,
1099 "[E_INVALID_ARG] sectionName.GetLength() <= 0");
1101 SysTryReturn(NID_IO, _sectionList.GetCount() > 0, -1, E_SECTION_NOT_FOUND, "[E_SECTION_NOT_FOUND] Section not found.");
1103 // Get section information
1104 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
1105 SysTryReturn(NID_IO, pEnum != null, GetLastResult(), GetLastResult(), "[%s] Section list is empty.",
1106 GetErrorMessage(GetLastResult()));
1108 while (pEnum->MoveNext() == E_SUCCESS)
1110 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1111 SysTryReturn(NID_IO, pRegSection != null, -1, GetLastResult(), "[%s] Propagated.",
1112 GetErrorMessage(GetLastResult()));
1114 if (pRegSection->__sectionName == sectionName)
1116 sectionFound = true;
1122 if (sectionFound == false)
1124 return E_SECTION_NOT_FOUND;
1127 SetLastResult(E_SUCCESS);
1132 _RegistryCore::Removekey(const String& sectionName, const String& keyName)
1134 result r = E_SUCCESS;
1135 int sectionIndex = -1;
1136 int entryIndex = -1;
1138 sectionIndex = GetSectionIndex(sectionName);
1139 r = GetLastResult();
1140 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1142 entryIndex = GetEntryIndex(sectionIndex, keyName);
1143 r = GetLastResult();
1144 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1146 RemoveEntry(sectionIndex, entryIndex);
1147 r = GetLastResult();
1148 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1150 SetLastResult(E_SUCCESS);
1155 _RegistryCore::GetAllSectionCount(void)
1157 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1159 SetLastResult(E_SUCCESS);
1160 return _sectionList.GetCount();
1164 _RegistryCore::GetAllEntryCount(int sectionIndex)
1166 _RegistrySection* pRegSection = null;
1169 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1170 SysTryReturn(NID_IO, sectionIndex >= 0, 0, E_INVALID_ARG, "[E_INVALID_ARG] sectionIndex < 0.");
1171 listSize = _sectionList.GetCount();
1172 SysTryReturn(NID_IO, listSize > 0, 0, E_SECTION_NOT_FOUND, "[E_INVALID_ARG] section listSize <= 0.");
1173 SysTryReturn(NID_IO, sectionIndex >= listSize, 0, E_SECTION_NOT_FOUND,
1174 "[E_INVALID_ARG] sectionIndex > listSize.");
1176 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1177 SysTryReturn(NID_IO, pRegSection != null, 0, E_SECTION_NOT_FOUND,
1178 "[E_SECTION_NOT_FOUND] section was not found.");
1180 SetLastResult(E_SUCCESS);
1181 return pRegSection->__entryList.GetCount();
1185 _RegistryCore::RemoveEntry(int sectionIndex, int entryIndex)
1187 result r = E_SUCCESS;
1189 int entryListSize = 0;
1190 _RegistrySection* pRegSection = null;
1192 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1193 SysTryReturnVoidResult(NID_IO, sectionIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG] sectionIndex < 0.");
1194 SysTryReturnVoidResult(NID_IO, entryIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG] entryIndex < 0.");
1196 listSize = _sectionList.GetCount();
1197 SysTryReturnVoidResult(NID_IO, listSize > 0, E_SECTION_NOT_FOUND,
1198 "[E_SECTION_NOT_FOUND] section listSize <= 0.");
1199 SysTryReturnVoidResult(NID_IO, sectionIndex < listSize, E_SECTION_NOT_FOUND,
1200 "[E_SECTION_NOT_FOUND] sectionIndex >= listSize.");
1202 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1203 SysTryReturnVoidResult(NID_IO, pRegSection != null, E_SECTION_NOT_FOUND,
1204 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1206 r = GetLastResult();
1207 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r,"[%s] Propagated.", GetErrorMessage(r));
1209 entryListSize = pRegSection->__entryList.GetCount();
1210 SysTryReturnVoidResult(NID_IO, (entryListSize > 0 || entryIndex < entryListSize), E_KEY_NOT_FOUND,
1211 "[E_KEY_NOT_FOUND] entryListSize is 0 or entryIndex >= entryListSize.");
1213 r = pRegSection->__entryList.RemoveAt(entryIndex, true);
1214 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagated.", GetErrorMessage(r));
1216 SetLastResult(E_SUCCESS);
1220 _RegistryCore::ModifyEntryValue(int sectionIndex, int entryIndex, _RegValueType type, const void* pValue, int size)
1222 SetLastResult(E_UNSUPPORTED_OPERATION);
1226 _RegistryCore::GetEntryIndex(int sectionIndex, const String& entryName)
1228 _RegistrySection* pRegSection = null;
1229 _RegistryEntry* pRegEntry = null;
1231 bool entryFound = false;
1232 int entryIndex = -1;
1234 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1235 SysTryReturn(NID_IO, sectionIndex >= 0, -1, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1236 SysTryReturn(NID_IO, entryName.GetLength() > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG]entryName length < 0.");
1238 listSize = _sectionList.GetCount();
1239 SysTryReturn(NID_IO, listSize > 0, -1, E_SECTION_NOT_FOUND,
1240 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1241 SysTryReturn(NID_IO, sectionIndex < listSize, -1, E_SECTION_NOT_FOUND,
1242 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1244 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1245 SysTryReturn(NID_IO, pRegSection != null, -1, E_SECTION_NOT_FOUND,
1246 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1248 // read the entries for this section
1249 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1250 SysTryReturn(NID_IO, pEntryEnum != null, -1, GetLastResult(), "[%s] Entry list is empty.",
1251 GetErrorMessage(GetLastResult()));
1254 while (pEntryEnum->MoveNext() == E_SUCCESS)
1256 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1257 SysTryReturn(NID_IO, pRegEntry != null, -1, GetLastResult(), "[%s] Propagated.",
1258 GetErrorMessage(GetLastResult()));
1260 if (entryName == pRegEntry->entryName)
1268 SysTryReturn(NID_IO, entryFound != false, -1, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] entry was not found.");
1270 SetLastResult(E_SUCCESS);
1275 _RegistryCore::GetEntryName(int sectionIndex, int entryIndex)
1277 _RegistrySection* pRegSection = null;
1278 _RegistryEntry* pRegEntry = null;
1283 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1284 SysTryReturn(NID_IO, sectionIndex >= 0, empty, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1285 SysTryReturn(NID_IO, entryIndex >= 0, empty, E_INVALID_ARG, "[E_INVALID_ARG]entryIndex < 0.");
1287 listSize = _sectionList.GetCount();
1288 SysTryReturn(NID_IO, listSize > 0, empty, E_SECTION_NOT_FOUND,
1289 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1290 SysTryReturn(NID_IO, sectionIndex < listSize, empty, E_SECTION_NOT_FOUND,
1291 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1293 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1294 SysTryReturn(NID_IO, pRegSection != null, empty, E_SECTION_NOT_FOUND,
1295 "[E_SECTION_NOT_FOUND] Section at index (%d) not found.", sectionIndex);
1297 // read the entries for this section
1298 unique_ptr<IEnumerator> pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1299 SysTryReturn(NID_IO, pEntryEnum != null, empty, GetLastResult(), "[%s] Entry list is empty.",
1300 GetErrorMessage(GetLastResult()));
1302 tmpIndex = entryIndex;
1303 while (pEntryEnum->MoveNext() == E_SUCCESS)
1307 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1308 SysTryReturn(NID_IO, pRegEntry != null, empty, GetLastResult(), "[%s] Propagated.",
1309 GetErrorMessage(GetLastResult()));
1316 SysTryReturn(NID_IO, pRegEntry != null, empty, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] entry was not found.");
1318 SetLastResult(E_SUCCESS);
1319 return pRegEntry->entryName;
1323 _RegistryCore::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType type, void* pValue, int* pSize)
1325 _RegistrySection* pRegSection = null;
1326 _RegistryEntry* pRegEntry = null;
1327 result r = E_SUCCESS;
1329 int tmpEntryIndex = -1;
1330 int entryListSize = 0;
1331 String strValueEncoded;
1333 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1334 SysTryReturnVoidResult(NID_IO, sectionIndex >= 0, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1335 SysTryReturnVoidResult(NID_IO, type >= 0 && type <= REG_VALUE_TYPE_MAX, E_INVALID_ARG,
1336 "[E_INVALID_ARG] invalid registry type.");
1337 SysTryReturnVoidResult(NID_IO, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG]pValue is null.");
1338 SysTryReturnVoidResult(NID_IO, pSize != null, E_INVALID_ARG, "[E_INVALID_ARG]pSize is null.");
1339 SysTryReturnVoidResult(NID_IO, *pSize > 0, E_INVALID_ARG, "[E_INVALID_ARG] *pSize < 0.");
1341 listSize = _sectionList.GetCount();
1342 SysTryReturnVoidResult(NID_IO, listSize > 0, E_SECTION_NOT_FOUND,
1343 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1344 SysTryReturnVoidResult(NID_IO, sectionIndex < listSize, E_SECTION_NOT_FOUND,
1345 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1347 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1348 SysTryReturnVoidResult(NID_IO, pRegSection != null, E_SECTION_NOT_FOUND,
1349 "[E_SECTION_NOT_FOUND] section at index (%d) is not available.", sectionIndex);
1351 entryListSize = pRegSection->__entryList.GetCount();
1352 SysTryReturnVoidResult(NID_IO, entryListSize > 0, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry list is empty.");
1353 SysTryReturnVoidResult(NID_IO, entryIndex < entryListSize, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry not found.");
1355 // read the entries for this section
1356 unique_ptr< IEnumerator > pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
1357 SysTryReturnVoidResult(NID_IO, pEntryEnum != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND]Entry list is empty.");
1359 tmpEntryIndex = entryIndex;
1360 while (pEntryEnum->MoveNext() == E_SUCCESS)
1362 if (entryIndex == 0)
1364 pRegEntry = dynamic_cast <_RegistryEntry*>(pEntryEnum->GetCurrent());
1365 SysTryReturnVoidResult(NID_IO, pRegEntry != null, GetLastResult(), "[%s] Propagated.",
1366 GetErrorMessage(GetLastResult()));
1374 SysTryReturnVoidResult(NID_IO, pRegEntry != null, E_KEY_NOT_FOUND,
1375 "[E_KEY_NOT_FOUND] Entry was not found.");
1377 strValueEncoded = pRegEntry->entryValue;
1379 DecodeData(strValueEncoded, type, pValue, pSize);
1380 r = GetLastResult();
1381 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1383 SetLastResult(E_SUCCESS);
1388 _RegistryCore::GetSectionName(int sectionIndex)
1390 _RegistrySection* pRegSection = null;
1391 result r = E_SUCCESS;
1393 String sectionName("");
1395 SysAssertf(_constructed == true, "Not yet constructed. Construct() should be called before use.\n");
1396 SysTryReturn(NID_IO, sectionIndex >= 0, sectionName, E_INVALID_ARG, "[E_INVALID_ARG]sectionIndex < 0.");
1398 listSize = _sectionList.GetCount();
1399 SysTryReturn(NID_IO, listSize > 0, sectionName, E_SECTION_NOT_FOUND,
1400 "[E_SECTION_NOT_FOUND]Registry section list is empty.");
1401 SysTryReturn(NID_IO, sectionIndex < listSize, sectionName, E_SECTION_NOT_FOUND,
1402 "[E_SECTION_NOT_FOUND]sectionIndex >= listSize.");
1404 pRegSection = (_RegistrySection*) GetSectionByIndex(sectionIndex);
1405 SysTryReturn(NID_IO, pRegSection != null, sectionName, E_SECTION_NOT_FOUND,
1406 "[E_SECTION_NOT_FOUND] sectionIndex is not available.");
1408 r = GetLastResult();
1409 SysTryReturn(NID_IO, !IsFailed(r), sectionName, r, "[%s] Propagated.", GetErrorMessage(r));
1411 SetLastResult(E_SUCCESS);
1412 return pRegSection->__sectionName;
1416 _RegistryCore::GetSectionByIndex(int sectionIndex)
1418 _RegistrySection* pRegSection = null;
1419 int index = sectionIndex;
1421 // Get section information
1422 unique_ptr<IEnumerator> pEnum(_sectionList.GetEnumeratorN());
1423 SysTryReturn(NID_IO, pEnum != null, null, GetLastResult(),
1424 "[%s] section list is empty.", GetErrorMessage(GetLastResult()));
1426 while (pEnum->MoveNext() == E_SUCCESS)
1430 pRegSection = dynamic_cast< _RegistrySection* >(pEnum->GetCurrent());
1431 SysTryReturn(NID_IO, pRegSection != null, null, GetLastResult(), "[%s] Propagated.",
1432 GetErrorMessage(GetLastResult()));
1439 SysTryReturn(NID_IO, pRegSection != null, null, E_SECTION_NOT_FOUND,
1440 "[E_SECTION_NOT_FOUND] Section was not found.");
1442 SetLastResult(E_SUCCESS);
1448 _RegistryCore::DecodeData(const String& strValueEncoded, _RegValueType type, void* pValue, int* pSize)
1450 SysAssert(strValueEncoded.GetLength() > 0);
1453 SysAssert(pSize && *pSize > 0);
1455 SysTryReturnVoidResult(NID_IO, pValue != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument.");
1456 SysTryReturnVoidResult(NID_IO, pSize != null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument.");
1460 case REG_VALUE_TYPE_INT: // (-2147483647 - 1) ~ 2147483647
1463 result r = E_SUCCESS;
1465 if (*pSize != sizeof(int))
1467 SetLastResult(E_INVALID_ARG);
1471 r = Integer::Parse(strValueEncoded, retIntVal);
1472 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1474 *(int*) pValue = retIntVal;
1478 case REG_VALUE_TYPE_REAL: // Holds signed IEEE 64-bit (8-byte) double-precision floating-point numbers
1479 // ranging in value from -1.79769313486231570E+308 through -4.94065645841246544E-324
1480 // for negative values and from 4.94065645841246544E-324 through 1.79769313486231570E+308
1481 // for positive values.
1483 double retDoubleVal = 0;
1485 if (*pSize != sizeof(double))
1487 SetLastResult(E_INVALID_ARG);
1491 retDoubleVal = _LocalizedNumParser::ToDouble(strValueEncoded, "C");
1492 result r = GetLastResult();
1493 SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1495 *(double*) pValue = retDoubleVal;
1500 case REG_VALUE_TYPE_STRING:
1502 // if(*pSize != sizeof(String))
1504 // SetLastResult(E_INVALID_ARG);
1507 *(String*) pValue = strValueEncoded;
1511 case REG_VALUE_TYPE_BINARY:
1513 SetLastResult(E_PARSING_FAILED);
1517 case REG_VALUE_TYPE_UUID:
1520 result r = E_SUCCESS;
1522 r = UuId::Parse(strValueEncoded, retUuIdVal);
1523 SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
1525 *(UuId*) pValue = retUuIdVal;
1529 case REG_VALUE_TYPE_UUIDSET:
1531 SetLastResult(E_PARSING_FAILED);
1536 SysAssert(0); // should not happen!
1540 SetLastResult(E_SUCCESS);
1545 _RegistryCore::AddSection(LinkedList& sectionList, const String& sectionName)
1547 result r = E_SUCCESS;
1548 bool sectionFound = false;
1550 // Get section information
1551 unique_ptr<IEnumerator> pEnum(sectionList.GetEnumeratorN());
1554 return GetLastResult();
1557 while (pEnum->MoveNext() == E_SUCCESS)
1559 _RegistrySection* pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1560 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1561 GetErrorMessage(GetLastResult()));
1563 if (pRegSection->__sectionName == sectionName)
1565 sectionFound = true;
1570 if (sectionFound == true)
1572 return E_SECTION_ALREADY_EXIST;
1575 // create a key with section name
1576 unique_ptr<_RegistrySection> pRegSection(new (std::nothrow) _RegistrySection());
1578 SysTryReturnResult(NID_IO, pRegSection != null, E_OUT_OF_MEMORY,
1579 "The memory is insufficient.");
1581 pRegSection->__sectionName = sectionName;
1583 // add new section and section-entry map
1584 r = sectionList.Add(*(pRegSection.release()));
1585 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1591 _RegistryCore::AddValue(LinkedList& sectionList, const String& sectionName, const String& entryName, const String& valStr)
1593 result r = E_SUCCESS;
1594 bool sectionFound = false;
1595 bool entryFound = false;
1596 _RegistrySection* pRegSection = null;
1598 // Get section information
1599 unique_ptr<IEnumerator> pEnum(sectionList.GetEnumeratorN());
1602 return GetLastResult();
1605 while (pEnum->MoveNext() == E_SUCCESS)
1607 pRegSection = dynamic_cast <_RegistrySection*>(pEnum->GetCurrent());
1608 SysTryReturn(NID_IO, pRegSection != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1609 GetErrorMessage(GetLastResult()));
1611 if (pRegSection->__sectionName == sectionName)
1613 sectionFound = true;
1618 if (sectionFound == false)
1620 return E_SECTION_NOT_FOUND;
1623 pEnum.reset(pRegSection->__entryList.GetEnumeratorN());
1626 return GetLastResult();
1629 while (pEnum->MoveNext() == E_SUCCESS)
1631 _RegistryEntry* pRegEntry = dynamic_cast <_RegistryEntry*>(pEnum->GetCurrent());
1632 SysTryReturn(NID_IO, pRegEntry != null, GetLastResult(), GetLastResult(), "[%s] Propagated.",
1633 GetErrorMessage(GetLastResult()));
1635 if (pRegEntry->entryName == entryName)
1642 if (entryFound == true)
1644 return E_KEY_ALREADY_EXIST;
1647 unique_ptr<_RegistryEntry> pRegEntry(new (std::nothrow) _RegistryEntry());
1648 SysTryReturnResult(NID_IO, pRegEntry != null, E_OUT_OF_MEMORY,
1649 "The memory is insufficient.");
1651 pRegEntry->entryName = entryName;
1652 pRegEntry->entryValue = valStr;
1654 r = pRegSection->__entryList.Add(*(pRegEntry.release()));
1655 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1661 _RegistryCore::CheckSectionName(char* pSectionName)
1668 if (pSectionName == null)
1673 pFrom = pSectionName;
1674 length = strlen(pSectionName);
1675 pTo = pSectionName + (length - 1);
1677 if (!pFrom || !pTo || pFrom > pTo)
1682 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1684 if (*pTemp == '\0' || *pTemp == '\n' || *pTemp == '#' || *pTemp == '=')
1694 _RegistryCore::CheckEntryName(char* pEntryName)
1701 if (pEntryName == null)
1707 length = strlen(pEntryName);
1708 pTo = pEntryName + (length - 1);
1710 if (!pFrom || !pTo || pFrom > pTo)
1715 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1717 if (*pTemp == '\0' || *pTemp == '\n' || *pTemp == '#' || *pTemp == '=')
1727 _RegistryCore::CheckEntryValue(char* pEntryVal)
1734 if (pEntryVal == null)
1740 length = strlen(pEntryVal);
1741 pTo = pEntryVal + (length - 1);
1743 if (!pFrom || !pTo || pFrom > pTo)
1748 for (pTemp = pFrom; pTemp <= pTo; pTemp++)
1750 if (*pTemp == '\0' || *pTemp == '\n')
1760 _RegistryCore::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer* pKey)
1762 result r = E_SUCCESS;
1764 if (File::IsFileExist(secureRegPath))
1766 r = GetLastResult();
1769 r = E_FILE_ALREADY_EXIST;
1770 SysLog(NID_IO, "[E_FILE_ALREADY_EXIST] The secure registry already exists.");
1774 SysLog(NID_IO, "[%s]Propagated.", GetErrorMessage(r));
1779 unique_ptr<_NormalRegistry> pNormalReg(new (std::nothrow) _NormalRegistry());
1780 SysTryReturnResult(NID_IO, pNormalReg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1782 r = pNormalReg->Load(plainRegPath, "r");
1783 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1785 unique_ptr<_SecureRegistry> pSecureReg(new (std::nothrow) _SecureRegistry());
1786 SysTryReturnResult(NID_IO, pSecureReg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1788 r = pSecureReg->Construct(secureRegPath, "w+", pKey);
1789 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1791 if (pNormalReg->_pBuffer != null && pNormalReg->_length > 0)
1793 pSecureReg->_pBuffer = pNormalReg->_pBuffer;
1794 pSecureReg->_length = pNormalReg->_length;
1796 pNormalReg->_pBuffer = null;
1797 pNormalReg->_length = 0;
1799 r = pSecureReg->Parse();
1800 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1803 r = pSecureReg->Flush();
1804 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
1810 _RegistryCore::LockN(FileLockType lockType)
1812 SysAssertf(_constructed == true && __pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
1813 return __pFileImpl->LockN(lockType);
1817 _RegistryCore::TryToLockN(FileLockType lockType)
1819 SysAssertf(_constructed == true && __pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
1820 return __pFileImpl->TryToLockN(lockType);