From 97beeb72a4a0fe144dc88839a288140679902b2c Mon Sep 17 00:00:00 2001 From: Anton Obzhirov Date: Tue, 2 Jul 2019 11:16:46 +0100 Subject: [PATCH] All file read operations should be done through FileLoader. Change-Id: I8709b060504f76a35d3abc5ef6500fa29320f897 --- build/tizen/adaptor/Makefile.am | 6 ++ build/tizen/adaptor/module.list | 4 + dali/devel-api/adaptor-framework/file-loader.cpp | 58 ++---------- .../adaptor-framework/common/file-loader-impl.h | 45 ++++++++++ dali/internal/adaptor-framework/file.list | 3 + .../generic/file-loader-impl-generic.cpp | 100 +++++++++++++++++++++ .../haptics/common/feedback-player-impl.cpp | 14 +-- .../internal/styling/common/style-monitor-impl.cpp | 17 ++-- dali/internal/system/common/file-closer.h | 43 +++++++-- 9 files changed, 218 insertions(+), 72 deletions(-) create mode 100644 dali/internal/adaptor-framework/common/file-loader-impl.h create mode 100644 dali/internal/adaptor-framework/file.list create mode 100644 dali/internal/adaptor-framework/generic/file-loader-impl-generic.cpp diff --git a/build/tizen/adaptor/Makefile.am b/build/tizen/adaptor/Makefile.am index ef77c19..e5cbc8e 100644 --- a/build/tizen/adaptor/Makefile.am +++ b/build/tizen/adaptor/Makefile.am @@ -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) \ diff --git a/build/tizen/adaptor/module.list b/build/tizen/adaptor/module.list index 97727a4..2e73325 100644 --- a/build/tizen/adaptor/module.list +++ b/build/tizen/adaptor/module.list @@ -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 diff --git a/dali/devel-api/adaptor-framework/file-loader.cpp b/dali/devel-api/adaptor-framework/file-loader.cpp index 9403781..b63b5c7 100644 --- a/dali/devel-api/adaptor-framework/file-loader.cpp +++ b/dali/devel-api/adaptor-framework/file-loader.cpp @@ -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. @@ -17,10 +17,13 @@ // CLASS HEADER #include - +// EXTERNAL INCLUDES #include #include +// INTERNAL INCLUDES +#include + namespace Dali { @@ -29,60 +32,17 @@ namespace FileLoader int ReadFile(const std::string& filename, Dali::Vector & 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 & memblock, FileLoader::FileType fileType) +int ReadFile(const std::string& filename, std::streampos& fileSize, Dali::Vector& 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 index 0000000..8319716 --- /dev/null +++ b/dali/internal/adaptor-framework/common/file-loader-impl.h @@ -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 + +// INTERNAL INCLUDES + +namespace Dali +{ + +namespace Internal +{ + +namespace Adaptor +{ + +int ReadFile(const std::string& filename, Dali::Vector& memblock, Dali::FileLoader::FileType fileType = Dali::FileLoader::BINARY); + +int ReadFile(const std::string& filename, std::streampos& fileSize, Dali::Vector& 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 index 0000000..443056e --- /dev/null +++ b/dali/internal/adaptor-framework/file.list @@ -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 index 0000000..45422e7 --- /dev/null +++ b/dali/internal/adaptor-framework/generic/file-loader-impl-generic.cpp @@ -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 + +// EXTERNAL INCLUDES +#include +#include + +#include + +namespace Dali +{ + +namespace Internal +{ + +namespace Adaptor +{ + +int ReadFile(const std::string& filename, Dali::Vector & 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 & 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 diff --git a/dali/internal/haptics/common/feedback-player-impl.cpp b/dali/internal/haptics/common/feedback-player-impl.cpp index 738e96f..ea4a5f0 100644 --- a/dali/internal/haptics/common/feedback-player-impl.cpp +++ b/dali/internal/haptics/common/feedback-player-impl.cpp @@ -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 // EXTERNAL INCLUDES -#include #include +#include +#include // INTERNAL INCLUDES #include @@ -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 fileBuffer; + if( Dali::FileLoader::ReadFile( filename, bufferSize, fileBuffer, FileLoader::FileType::TEXT ) ) { - data.assign((std::istreambuf_iterator(stream)), - std::istreambuf_iterator()); + data.assign( &fileBuffer[0], bufferSize ); loaded = true; } diff --git a/dali/internal/styling/common/style-monitor-impl.cpp b/dali/internal/styling/common/style-monitor-impl.cpp index badca0c..5b316b4 100644 --- a/dali/internal/styling/common/style-monitor-impl.cpp +++ b/dali/internal/styling/common/style-monitor-impl.cpp @@ -19,9 +19,8 @@ #include // EXTERNAL INCLUDES +#include #include -#include -#include #include // 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 fileBuffer; + if( Dali::FileLoader::ReadFile( filename, bufferSize, fileBuffer, FileLoader::FileType::TEXT ) ) + { + output.assign( &fileBuffer[0], bufferSize ); retval = true; } + return retval; } diff --git a/dali/internal/system/common/file-closer.h b/dali/internal/system/common/file-closer.h index 96b03b0..27c30ac 100755 --- a/dali/internal/system/common/file-closer.h +++ b/dali/internal/system/common/file-closer.h @@ -18,6 +18,7 @@ */ // INTERNAL INCLUDES +#include // EXTERNAL INCLUDES #include @@ -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 mFileBuffer; }; } // namespace Platform -- 2.7.4