[Tizen] Implement partial update
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / file-closer.h
index 8a8a37d..27c30ac 100755 (executable)
@@ -1,7 +1,7 @@
-#ifndef _DALI_INTERNAL_PLATFORM_FILECLOSER_H__
-#define _DALI_INTERNAL_PLATFORM_FILECLOSER_H__
+#ifndef DALI_INTERNAL_PLATFORM_FILECLOSER_H
+#define DALI_INTERNAL_PLATFORM_FILECLOSER_H
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
  */
 
 // INTERNAL INCLUDES
+#include <dali/devel-api/adaptor-framework/file-loader.h>
 
 // EXTERNAL INCLUDES
 #include <cstdio>
@@ -28,23 +29,7 @@ namespace Internal
 {
 namespace Platform
 {
-namespace InternalFile
-{
-  FILE *fmemopen( void *__s, size_t __len, const char *__modes );
-
-  size_t fread( void*  _Buffer, size_t _ElementSize, size_t _ElementCount, FILE*  _Stream );
-  int fclose( FILE *__stream );
-
-  size_t fread( void* buf, size_t eleSize, size_t count, const FILE *fp );
-
-  void fwrite( void *buf, int size, int count, FILE *fp );
-
-  int fseek( FILE *fp, int offset, int origin );
 
-  int ftell( FILE *fp );
-
-  bool feof( FILE *fp );
-};
 /**
  * Opens files and closes them later even if an exception is thrown.
  */
@@ -55,23 +40,52 @@ protected: // prevent this class being directly instantiated
   /**
    * @brief Construct a FileCloser guarding a new FILE* for accessing the path passed in.
    */
-  FileCloser( const char * const filename, const char * const mode )
-  : mFile(fopen(filename, mode))
+  FileCloser( const char* const filename, const char* const mode )
   {
-    DALI_ASSERT_DEBUG( filename != 0 && "Cant open a null filename." );
+    DALI_ASSERT_DEBUG( filename != 0 && "Can't open a null filename." );
     DALI_ASSERT_DEBUG( mode != 0 && "Null mode is undefined behaviour in spec." );
 
-    if( mFile == 0 )
+    Dali::FileLoader::FileType fileType = Dali::FileLoader::FileType::TEXT;
+
+    const char* modeStr = mode;
+    while( *modeStr )
     {
-      DALI_LOG_WARNING( "File open failed for: \"%s\" in mode: \"%s\".\n", filename, mode );
+      switch ( *modeStr )
+      {
+      case 'r':
+        break;
+      case 'b':
+        fileType = FileLoader::FileType::BINARY;
+        break;
+      // Still has to use fopen for append and write modes
+      case 'a':
+      case 'w':
+      case '+':
+        mFile = fopen( filename, mode );
+        return;
+      default:
+        break;
+      }
+
+      ++modeStr;
+    }
+
+    std::streampos bufferSize = 0;
+    if( !Dali::FileLoader::ReadFile( filename, bufferSize, mFileBuffer, fileType ) )
+    {
+      mFile = nullptr;
+    }
+    else
+    {
+      mFile = fmemopen( &mFileBuffer[0], bufferSize, mode );
     }
   }
 
   /**
    * @brief Construct a FileCloser guarding a FILE* for reading out of the memory buffer passed in.
    */
-  FileCloser( uint8_t* buffer, size_t dataSize, const char * const mode )
-  : mFile( InternalFile::fmemopen( buffer, dataSize, mode) )
+  FileCloser( uint8_t* buffer, size_t dataSize, const char* const mode )
+  : mFile( fmemopen( buffer, dataSize, mode) )
   {
   }
 
@@ -87,7 +101,7 @@ protected: // prevent this class being directly instantiated
     vector.Resize( bufferSize );
 
     void * const buffer = &vector[0];
-    mFile = InternalFile::fmemopen( buffer, bufferSize, mode );
+    mFile = fmemopen( buffer, bufferSize, mode );
 
     DALI_ASSERT_DEBUG( buffer != 0 && "Cant open file on null buffer." );
     DALI_ASSERT_DEBUG( dataSize > 0 && "Pointless to open file on empty buffer." );
@@ -106,7 +120,7 @@ protected: // prevent this class being directly instantiated
   {
     if( mFile != 0 )
     {
-      const int closeFailed = InternalFile::fclose( mFile );
+      const int closeFailed = fclose( mFile );
 
       if ( closeFailed )
       {
@@ -135,10 +149,13 @@ private:
 
 private:
   FILE* mFile;
+  Dali::Vector<char> mFileBuffer;
 };
 
-} /* namespace Platform */
-} /* namespace Internal */
-} /* namespace Dali */
+} // namespace Platform
+
+} // namespace Internal
+
+} // namespace Dali
 
-#endif /* _DALI_INTERNAL_PLATFORM_FILECLOSER_H__ */
+#endif // DALI_INTERNAL_PLATFORM_FILECLOSER_H