Merge "Optimising GetHashCode()" into devel_3.0_main
[platform/framework/native/appfw.git] / src / io / FIo_RegistryCore.cpp
index 28b443a..6cee81a 100644 (file)
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
@@ -23,6 +22,8 @@
 #include <unistd.h>
 #include <new>
 #include <unique_ptr.h>
+#include <string.h>
+#include <errno.h>
 
 #include <FBaseInteger.h>
 #include <FBaseDouble.h>
@@ -38,6 +39,8 @@
 #include <FIoRegistry.h>
 
 #include <FBase_StringConverter.h>
+#include <FBase_LocalizedNumParser.h>
+#include <FBase_NativeError.h>
 #include <FApp_AppInfo.h>
 #include "FIo_FileImpl.h"
 #include "FIo_NormalRegistry.h"
@@ -88,13 +91,16 @@ _RegistryCore::_RegistryCore(void)
        , _write(false)
        , _truncate(false)
        , _append(false)
+       , _update(false)
        , _pBuffer(null)
        , _length(0)
+       , __pFileImpl(null)
 {
 }
 
 _RegistryCore::~_RegistryCore(void)
 {
+       delete __pFileImpl;
 }
 
 bool
@@ -301,7 +307,10 @@ _RegistryCore::Parse(void)
                                                   "[E_PARSING_FAILED] Entry name could not be parsed.");
                        }
 
-                       line.SubString(firstTokenPos + 1, entryVal); // extract entry value
+                       if (line.GetLength() > firstTokenPos + 1)
+                       {
+                               line.SubString(firstTokenPos + 1, entryVal); // extract entry value
+                       }
 
                        // check if entry value contains invalid chars
                        pEntryValue.reset(_StringConverter::CopyToCharArrayN(entryVal));
@@ -370,6 +379,8 @@ _RegistryCore::Load(const String& regPath, const char* pOpenMode)
                }
        }
 
+       __pFileImpl = pFileImpl.release();
+
        return r;
 }
 
@@ -417,6 +428,7 @@ _RegistryCore::AddSection(const String& sectionName)
        r = _sectionList.Add(*(pRegSection.release()));
        SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
 
+       _update = true;
        return E_SUCCESS;
 }
 
@@ -462,6 +474,7 @@ _RegistryCore::RemoveSection(const String& sectionName)
        r = _sectionList.Remove(*pRegSection, true);
        SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
 
+       _update = true;
        return E_SUCCESS;
 }
 
@@ -804,6 +817,7 @@ _RegistryCore::AddValue(const String& sectionName, const String& entryName, cons
        r = pRegSection->__entryList.Add(*(pRegEntry.release()));
        SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
 
+       _update = true;
        return E_SUCCESS;
 }
 
@@ -891,6 +905,7 @@ _RegistryCore::SetValue(const String& sectionName, const String& entryName, cons
        r = pRegSection->__entryList.SetAt(*pRegEntry, entryIndex);
        SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
 
+       _update = true;
        return E_SUCCESS;
 }
 
@@ -962,6 +977,7 @@ _RegistryCore::RemoveValue(const String& sectionName, const String& entryName)
        r = pRegSection->__entryList.Remove(*pRegEntry, true);
        SysTryReturnResult(NID_IO, !IsFailed(r), E_IO, "system error");
 
+       _update = true;
        return E_SUCCESS;
 }
 
@@ -982,20 +998,16 @@ _RegistryCore::Write(void)
                return E_SUCCESS;
        }
 
-       result r = E_SUCCESS;
-
-       String openMode(L"w+");
-       unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
-       SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
+       result r = __pFileImpl->Seek(FILESEEKPOSITION_BEGIN, 0);
+       SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
 
-       //TODO: Secure mode is to be handled
-       r = pFileImpl->Construct(_regPath, "w+", null);
+       r = __pFileImpl->Truncate(0);
        SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
 
-       r = pFileImpl->Write(_pBuffer, _length);
+       r = __pFileImpl->Write(_pBuffer, _length);
        SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
 
-       r = pFileImpl->Flush();
+       r = __pFileImpl->Flush();
        SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
 
        return r;
@@ -1025,8 +1037,9 @@ _RegistryCore::PrepareToWrite(void)
        if (_sectionList.GetCount() == 0)
        {
                unique_ptr<char[]> pFilePath(_StringConverter::CopyToCharArrayN(_regPath));
-               truncate(pFilePath.get(), 0);
-
+               int res = truncate(pFilePath.get(), 0);
+               SysTryReturnResult(NID_IO, res == 0, __ConvertNativeErrorToResult(errno),
+                               "Failed to truncate. errno: %d (%s)", errno, strerror(errno));
                return E_SUCCESS;
        }
 
@@ -1311,7 +1324,6 @@ _RegistryCore::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType typ
 {
        _RegistrySection* pRegSection = null;
        _RegistryEntry* pRegEntry = null;
-       IEnumerator* pEntryEnum = null;
        result r = E_SUCCESS;
        int listSize = 0;
        int tmpEntryIndex = -1;
@@ -1341,7 +1353,7 @@ _RegistryCore::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType typ
        SysTryReturnVoidResult(NID_IO, entryIndex < entryListSize, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND]Entry not found.");
 
        // read the entries for this section
-       pEntryEnum = pRegSection->__entryList.GetEnumeratorN();
+       unique_ptr< IEnumerator > pEntryEnum(pRegSection->__entryList.GetEnumeratorN());
        SysTryReturnVoidResult(NID_IO, pEntryEnum != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND]Entry list is empty.");
 
        tmpEntryIndex = entryIndex;
@@ -1469,7 +1481,6 @@ _RegistryCore::DecodeData(const String& strValueEncoded, _RegValueType type, voi
                                        // for positive values.
        {
                double retDoubleVal = 0;
-               result r = E_SUCCESS;
 
                if (*pSize != sizeof(double))
                {
@@ -1477,10 +1488,12 @@ _RegistryCore::DecodeData(const String& strValueEncoded, _RegValueType type, voi
                        return;
                }
 
-               r = Double::Parse(strValueEncoded, retDoubleVal);
-               SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
+               retDoubleVal = _LocalizedNumParser::ToDouble(strValueEncoded, "C");
+               result r = GetLastResult();
+               SysTryReturnVoidResult(NID_IO, r == E_SUCCESS, E_PARSING_FAILED, "[%s] Propagated.", GetErrorMessage(r));
 
                *(double*) pValue = retDoubleVal;
+
                break;
        }
 
@@ -1787,8 +1800,25 @@ _RegistryCore::ConvertToSecureRegistry(const String& plainRegPath, const String&
                SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
        }
 
+       r = pSecureReg->Flush();
+       SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
+
        return E_SUCCESS;
 }
 
+FileLock*
+_RegistryCore::LockN(FileLockType lockType)
+{
+       SysAssertf(_constructed == true && __pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
+       return __pFileImpl->LockN(lockType);
+}
+
+FileLock*
+_RegistryCore::TryToLockN(FileLockType lockType)
+{
+       SysAssertf(_constructed == true && __pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
+       return __pFileImpl->TryToLockN(lockType);
+}
+
 }} // Tizen::Io