All file read operations should be done through FileLoader. 27/209127/6
authorAnton Obzhirov <a.obzhirov@samsung.com>
Tue, 2 Jul 2019 10:16:46 +0000 (11:16 +0100)
committerAnton Obzhirov <a.obzhirov@samsung.com>
Fri, 5 Jul 2019 10:59:24 +0000 (10:59 +0000)
Change-Id: I8709b060504f76a35d3abc5ef6500fa29320f897

build/tizen/adaptor/Makefile.am
build/tizen/adaptor/module.list
dali/devel-api/adaptor-framework/file-loader.cpp
dali/internal/adaptor-framework/common/file-loader-impl.h [new file with mode: 0644]
dali/internal/adaptor-framework/file.list [new file with mode: 0644]
dali/internal/adaptor-framework/generic/file-loader-impl-generic.cpp [new file with mode: 0644]
dali/internal/haptics/common/feedback-player-impl.cpp
dali/internal/styling/common/style-monitor-impl.cpp
dali/internal/system/common/file-closer.h

index ef77c19..e5cbc8e 100644 (file)
@@ -35,6 +35,7 @@ LIBDALI_ADAPTOR_LA_SOURCES = \
                   $(adaptor_adaptor_ubuntu_src_files) \
                   $(adaptor_clipboard_common_src_files) \
                   $(adaptor_clipboard_ubuntu_x11_src_files) \
+                  $(adaptor_framework_generic_src_files) \
                   $(devel_api_src_files) \
                   $(adaptor_devel_api_text_abstraction_src_files) \
                   $(adaptor_graphics_common_src_files) \
@@ -91,6 +92,7 @@ LIBDALI_ADAPTOR_LA_SOURCES = \
                   $(adaptor_adaptor_tizen_wayland_src_files) \
                   $(adaptor_clipboard_common_src_files) \
                   $(adaptor_clipboard_tizen_wayland_src_files) \
+                  $(adaptor_framework_generic_src_files) \
                   $(devel_api_src_files) \
                   $(adaptor_devel_api_text_abstraction_src_files) \
                   $(adaptor_graphics_common_src_files) \
@@ -155,6 +157,7 @@ LIBDALI_ADAPTOR_LA_SOURCES = \
                   $(adaptor_adaptor_tizen_wayland_src_files) \
                   $(adaptor_clipboard_common_src_files) \
                   $(adaptor_clipboard_tizen_wayland_src_files) \
+                  $(adaptor_framework_generic_src_files) \
                   $(devel_api_src_files) \
                   $(adaptor_devel_api_text_abstraction_src_files) \
                   $(adaptor_graphics_common_src_files) \
@@ -220,6 +223,7 @@ LIBDALI_ADAPTOR_LA_SOURCES = \
                   $(adaptor_adaptor_tizen_wayland_src_files) \
                   $(adaptor_clipboard_common_src_files) \
                   $(adaptor_clipboard_tizen_wayland_src_files) \
+                  $(adaptor_framework_generic_src_files) \
                   $(devel_api_src_files) \
                   $(adaptor_devel_api_text_abstraction_src_files) \
                   $(adaptor_graphics_common_src_files) \
@@ -283,6 +287,7 @@ LIBDALI_ADAPTOR_LA_SOURCES = \
                   $(adaptor_adaptor_tizen_wayland_src_files) \
                   $(adaptor_clipboard_common_src_files) \
                   $(adaptor_clipboard_tizen_wayland_src_files) \
+                  $(adaptor_framework_generic_src_files) \
                   $(devel_api_src_files) \
                   $(adaptor_devel_api_text_abstraction_src_files) \
                   $(adaptor_graphics_common_src_files) \
@@ -349,6 +354,7 @@ LIBDALI_ADAPTOR_LA_SOURCES = \
                   $(adaptor_adaptor_tizen_wearable_src_files) \
                   $(adaptor_clipboard_common_src_files) \
                   $(adaptor_clipboard_tizen_wayland_src_files) \
+                  $(adaptor_framework_generic_src_files) \
                   $(devel_api_src_files) \
                   $(adaptor_devel_api_text_abstraction_src_files) \
                   $(adaptor_graphics_common_src_files) \
index 97727a4..2e73325 100644 (file)
@@ -11,6 +11,10 @@ include ../../../dali/internal/adaptor/file.list
 adaptor_clipboard_dir = ../../../dali/internal/clipboard
 include ../../../dali/internal/clipboard/file.list
 
+# module: adaptor-framework
+adaptor_framework_dir = ../../../dali/internal/adaptor-framework
+include ../../../dali/internal/adaptor-framework/file.list
+
 # Module: graphics
 adaptor_graphics_dir = ../../../dali/internal/graphics
 include ../../../dali/internal/graphics/file.list
index 9403781..b63b5c7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 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.
 // CLASS HEADER
 #include <dali/devel-api/adaptor-framework/file-loader.h>
 
-
+// EXTERNAL INCLUDES
 #include <iostream>
 #include <fstream>
 
+// INTERNAL INCLUDES
+#include <dali/internal/adaptor-framework/common/file-loader-impl.h>
+
 namespace Dali
 {
 
@@ -29,60 +32,17 @@ namespace FileLoader
 
 int ReadFile(const std::string& filename, Dali::Vector<char> & memblock, FileLoader::FileType fileType)
 {
-  std::streampos size;
-
-  return ReadFile( filename, size, memblock, fileType);
+  return Dali::Internal::Adaptor::ReadFile( filename, memblock, fileType );
 }
 
-int ReadFile(const std::string& filename, std::streampos& fileSize, Dali::Vector<char> & memblock, FileLoader::FileType fileType)
+int ReadFile(const std::string& filename, std::streampos& fileSize, Dali::Vector<char>& memblock, FileLoader::FileType fileType)
 {
-  int errorCode = 0;
-  std::ifstream * file;
-
-
-  if( fileType == BINARY )
-  {
-    file = new std::ifstream (filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
-  }
-  else if( fileType == TEXT )
-  {
-    file = new std::ifstream (filename.c_str(), std::ios::in|std::ios::ate);
-  }
-  else
-  {
-    return errorCode;
-  }
-
-  if( file->is_open() )
-  {
-    fileSize = file->tellg();
-
-    memblock.Resize( fileSize );
-
-    file->seekg (0, std::ios::beg);
-    file->read( memblock.Begin(), fileSize );
-    file->close();
-
-    delete file;
-
-    errorCode = 1;
-  }
-
-  return errorCode;
+  return Dali::Internal::Adaptor::ReadFile( filename, fileSize, memblock, fileType );;
 }
 
-
 std::streampos GetFileSize(const std::string& filename)
 {
-  std::streampos size = 0;
-
-  std::ifstream file (filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
-  if( file.is_open() )
-  {
-    size = file.tellg();
-    file.close();
-  }
-  return size;
+  return Dali::Internal::Adaptor::GetFileSize(filename);
 }
 
 } //FileLoader
diff --git a/dali/internal/adaptor-framework/common/file-loader-impl.h b/dali/internal/adaptor-framework/common/file-loader-impl.h
new file mode 100644 (file)
index 0000000..8319716
--- /dev/null
@@ -0,0 +1,45 @@
+#ifndef DALI_FILE_LOADER_IMPL_GENERIC_H
+#define DALI_FILE_LOADER_IMPL_GENERIC_H
+
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// EXTERNAL INCLUDES
+#include <dali/devel-api/adaptor-framework/file-loader.h>
+
+// INTERNAL INCLUDES
+
+namespace Dali
+{
+
+namespace Internal
+{
+
+namespace Adaptor
+{
+
+int ReadFile(const std::string& filename, Dali::Vector<char>& memblock, Dali::FileLoader::FileType fileType = Dali::FileLoader::BINARY);
+
+int ReadFile(const std::string& filename, std::streampos& fileSize, Dali::Vector<char>& memblock, Dali::FileLoader::FileType fileType = Dali::FileLoader::BINARY);
+
+std::streampos GetFileSize(const std::string& filename);
+
+} // Adaptor
+
+} // Internal
+
+} // Dali
+#endif // DALI_FILE_LOADER_IMPL_GENERIC_H
diff --git a/dali/internal/adaptor-framework/file.list b/dali/internal/adaptor-framework/file.list
new file mode 100644 (file)
index 0000000..443056e
--- /dev/null
@@ -0,0 +1,3 @@
+# module: adaptor-framework, backend: generic
+adaptor_framework_generic_src_files=\
+    ${adaptor_framework_dir}/generic/file-loader-impl-generic.cpp
diff --git a/dali/internal/adaptor-framework/generic/file-loader-impl-generic.cpp b/dali/internal/adaptor-framework/generic/file-loader-impl-generic.cpp
new file mode 100644 (file)
index 0000000..45422e7
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// CLASS HEADER
+#include <dali/internal/adaptor-framework/common/file-loader-impl.h>
+
+// EXTERNAL INCLUDES
+#include <string>
+#include <fstream>
+
+#include <dali/integration-api/debug.h>
+
+namespace Dali
+{
+
+namespace Internal
+{
+
+namespace Adaptor
+{
+
+int ReadFile(const std::string& filename, Dali::Vector<char> & memblock, Dali::FileLoader::FileType fileType)
+{
+  std::streampos size;
+
+  return Dali::Internal::Adaptor::ReadFile( filename, size, memblock, fileType);
+}
+
+int ReadFile(const std::string& filename, std::streampos& fileSize, Dali::Vector<char> & memblock, Dali::FileLoader::FileType fileType)
+{
+  int errorCode = 0;
+  std::ifstream * file;
+
+  if( fileType == Dali::FileLoader::BINARY )
+  {
+    file = new std::ifstream (filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
+  }
+  else if( fileType == Dali::FileLoader::TEXT )
+  {
+    file = new std::ifstream (filename.c_str(), std::ios::in|std::ios::ate);
+  }
+  else
+  {
+    return errorCode;
+  }
+
+  if( file->is_open() )
+  {
+    fileSize = file->tellg();
+
+    memblock.Resize( fileSize );
+
+    file->seekg (0, std::ios::beg);
+    file->read( memblock.Begin(), fileSize );
+    file->close();
+
+    delete file;
+
+    errorCode = 1;
+  }
+  else
+  {
+    DALI_LOG_WARNING( "file open failed for: \"%s\"", filename );
+  }
+
+  return errorCode;
+}
+
+std::streampos GetFileSize(const std::string& filename)
+{
+  std::streampos size = 0;
+
+  std::ifstream file( filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate );
+  if( file.is_open() )
+  {
+    size = file.tellg();
+    file.close();
+  }
+
+  return size;
+}
+
+} // Adaptor
+
+} // Internal
+
+} // Dali
index 738e96f..ea4a5f0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
@@ -19,8 +19,9 @@
 #include <dali/internal/haptics/common/feedback-player-impl.h>
 
 // EXTERNAL INCLUDES
-#include <fstream>
 #include <dali/public-api/object/type-registry.h>
+#include <dali/devel-api/adaptor-framework/file-loader.h>
+#include <dali/integration-api/debug.h>
 
 // INTERNAL INCLUDES
 #include <dali/internal/system/common/singleton-service-impl.h>
@@ -111,12 +112,11 @@ bool FeedbackPlayer::LoadFile(const std::string& filename, std::string& data)
 {
   bool loaded = false;
 
-  std::ifstream stream(filename.c_str());
-
-  if( stream.is_open() )
+  std::streampos bufferSize = 0;
+  Dali::Vector<char> fileBuffer;
+  if( Dali::FileLoader::ReadFile( filename, bufferSize, fileBuffer, FileLoader::FileType::TEXT ) )
   {
-    data.assign((std::istreambuf_iterator<char>(stream)),
-                std::istreambuf_iterator<char>());
+    data.assign( &fileBuffer[0], bufferSize );
     loaded = true;
   }
 
index badca0c..5b316b4 100644 (file)
@@ -19,9 +19,8 @@
 #include <dali/internal/styling/common/style-monitor-impl.h>
 
 // EXTERNAL INCLUDES
+#include <dali/devel-api/adaptor-framework/file-loader.h>
 #include <dali/public-api/object/type-registry.h>
-#include <fstream>
-#include <sstream>
 #include <dali/integration-api/debug.h>
 
 // INTERNAL INCLUDES
@@ -157,17 +156,15 @@ void StyleMonitor::SetTheme(const std::string& path)
 bool StyleMonitor::LoadThemeFile( const std::string& filename, std::string& output )
 {
   bool retval( false );
-  std::ifstream in( filename.c_str(), std::ios::in );
-  if( in )
-  {
-    std::stringstream buffer;
-    buffer << in.rdbuf();
-
-    output = buffer.str();
 
-    in.close();
+  std::streampos bufferSize = 0;
+  Dali::Vector<char> fileBuffer;
+  if( Dali::FileLoader::ReadFile( filename, bufferSize, fileBuffer, FileLoader::FileType::TEXT ) )
+  {
+    output.assign( &fileBuffer[0], bufferSize );
     retval = true;
   }
+
   return retval;
 }
 
index 96b03b0..27c30ac 100755 (executable)
@@ -18,6 +18,7 @@
  */
 
 // INTERNAL INCLUDES
+#include <dali/devel-api/adaptor-framework/file-loader.h>
 
 // EXTERNAL INCLUDES
 #include <cstdio>
@@ -39,22 +40,51 @@ 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 )
+    {
+      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
     {
-      DALI_LOG_WARNING( "File open failed for: \"%s\" in mode: \"%s\".\n", filename, mode );
+      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 )
+  FileCloser( uint8_t* buffer, size_t dataSize, const char* const mode )
   : mFile( fmemopen( buffer, dataSize, mode) )
   {
   }
@@ -119,6 +149,7 @@ private:
 
 private:
   FILE* mFile;
+  Dali::Vector<char> mFileBuffer;
 };
 
 } // namespace Platform