Simple file loading devel class 87/44287/11
authorDavid Fumanal <d.fumanal@samsung.com>
Mon, 20 Jul 2015 13:27:28 +0000 (14:27 +0100)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Mon, 3 Aug 2015 14:31:44 +0000 (07:31 -0700)
Change-Id: I8f2c366ab75e41db7ba2384ee1630f6044508182

adaptors/devel-api/adaptor-framework/file-loader.cpp [new file with mode: 0644]
adaptors/devel-api/adaptor-framework/file-loader.h [new file with mode: 0644]
adaptors/devel-api/file.list
automated-tests/resources/test.txt [new file with mode: 0644]
automated-tests/src/dali-adaptor/CMakeLists.txt
automated-tests/src/dali-adaptor/utc-Dali-FileLoader.cpp [new file with mode: 0644]

diff --git a/adaptors/devel-api/adaptor-framework/file-loader.cpp b/adaptors/devel-api/adaptor-framework/file-loader.cpp
new file mode 100644 (file)
index 0000000..8ead026
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2015 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 "file-loader.h"
+
+
+#include <iostream>
+#include <fstream>
+
+namespace Dali
+{
+
+namespace FileLoader
+{
+
+int ReadFile(const std::string& filename, Dali::Vector<char> & memblock, FileLoader::FileType fileType)
+{
+  std::streampos size;
+
+  return ReadFile( filename, size, memblock, 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;
+}
+
+
+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;
+}
+
+} //FileLoader
+
+} //Dali
diff --git a/adaptors/devel-api/adaptor-framework/file-loader.h b/adaptors/devel-api/adaptor-framework/file-loader.h
new file mode 100644 (file)
index 0000000..dc979cf
--- /dev/null
@@ -0,0 +1,73 @@
+#ifndef __DALI_FILE_LOADER_H__
+#define __DALI_FILE_LOADER_H__
+
+/*
+ * Copyright (c) 2015 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.
+ */
+
+#include <string>
+#include <dali/public-api/common/dali-common.h>
+#include <dali/public-api/object/base-handle.h>
+
+namespace Dali
+{
+
+namespace FileLoader
+{
+/**
+ * @brief File type formats
+ * The default format is binary
+ */
+enum FileType           ///< FileType format
+{
+  BINARY,               ///< File will be loaded as a binary
+  TEXT                  ///< File will be loaded as text
+};
+
+/**
+ * @brief Load the file. It will load it either as a binary or as a text
+ *
+ * @param[in] filename  Filename of the file to load.
+ * @param[in] memblock  Dali::Vector containing the buffer loaded
+ * @param[in] fileType  How we want to load the file. Binary or Text. Binary default
+ * @return error code. 0 - Error, 1 - Ok
+ *
+ *
+ */
+DALI_IMPORT_API int ReadFile(const std::string& filename, Dali::Vector<char> & memblock, FileLoader::FileType fileType = BINARY);
+
+/**
+ * @brief Load the file. It will load it either as a binary or as a text
+ *
+ * @param[in] filename  Filename of the file to load.
+ * @param[in] fileSize  Size of the loaded file
+ * @param[in] memblock  Dali::Vector containing the buffer loaded
+ * @param[in] fileType  How we want to load the file. Binary or Text. Binary default
+ * @return error code. 0 - Error, 1 - Ok
+ *
+ */
+DALI_IMPORT_API int ReadFile(const std::string& filename, std::streampos& fileSize, Dali::Vector<char> & memblock, FileLoader::FileType fileType = BINARY);
+
+/**
+ * @brief Get the file size of a file
+ *
+ * @param[in] filename  Filename of the file to load.
+ * @return the size of the file or 0 if file not found
+ */
+DALI_IMPORT_API std::streampos GetFileSize(const std::string& filename);
+};
+
+} // Dali
+#endif // __DALI_FILE_LOADER_H__
index 77443c7..64d4221 100644 (file)
@@ -8,6 +8,7 @@ devel_api_src_files = \
   $(adaptor_devel_api_dir)/adaptor-framework/drag-and-drop-detector.cpp \
   $(adaptor_devel_api_dir)/adaptor-framework/event-feeder.cpp \
   $(adaptor_devel_api_dir)/adaptor-framework/feedback-player.cpp \
+  $(adaptor_devel_api_dir)/adaptor-framework/file-loader.cpp \
   $(adaptor_devel_api_dir)/adaptor-framework/imf-manager.cpp \
   $(adaptor_devel_api_dir)/adaptor-framework/orientation.cpp \
   $(adaptor_devel_api_dir)/adaptor-framework/performance-logger.cpp \
@@ -33,6 +34,7 @@ devel_api_adaptor_framework_header_files = \
   $(adaptor_devel_api_dir)/adaptor-framework/event-feeder.h \
   $(adaptor_devel_api_dir)/adaptor-framework/feedback-plugin.h \
   $(adaptor_devel_api_dir)/adaptor-framework/feedback-player.h \
+  $(adaptor_devel_api_dir)/adaptor-framework/file-loader.h \
   $(adaptor_devel_api_dir)/adaptor-framework/imf-manager.h \
   $(adaptor_devel_api_dir)/adaptor-framework/lifecycle-controller.h \
   $(adaptor_devel_api_dir)/adaptor-framework/orientation.h \
diff --git a/automated-tests/resources/test.txt b/automated-tests/resources/test.txt
new file mode 100644 (file)
index 0000000..524acff
--- /dev/null
@@ -0,0 +1 @@
+Test file
index fe4968d..f711d28 100644 (file)
@@ -12,6 +12,7 @@ SET(TC_SOURCES
     utc-Dali-Timer.cpp
     utc-Dali-TtsPlayer.cpp
     utc-Dali-Application.cpp
+    utc-Dali-FileLoader.cpp
 )
 
 LIST(APPEND TC_SOURCES
@@ -36,6 +37,8 @@ PKG_CHECK_MODULES(${CAPI_LIB} REQUIRED
 SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -O0 -ggdb --coverage -Wall -Werror=return-type")
 SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${${CAPI_LIB}_CFLAGS_OTHER}")
 
+ADD_DEFINITIONS(-DTEST_RESOURCE_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/../../resources\" )
+
 FOREACH(directory ${${CAPI_LIB}_LIBRARY_DIRS})
     SET(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -L${directory}")
 ENDFOREACH(directory ${CAPI_LIB_LIBRARY_DIRS})
diff --git a/automated-tests/src/dali-adaptor/utc-Dali-FileLoader.cpp b/automated-tests/src/dali-adaptor/utc-Dali-FileLoader.cpp
new file mode 100644 (file)
index 0000000..724cbea
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2015 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.
+ *
+ */
+
+#include <dali/dali.h>
+#include <dali-test-suite-utils.h>
+#include <dali/devel-api/adaptor-framework/file-loader.h>
+
+
+
+using namespace Dali;
+
+
+void utc_dali_file_loader_startup(void)
+{
+  test_return_value = TET_UNDEF;
+}
+
+void utc_dali_file_loader_cleanup(void)
+{
+  test_return_value = TET_PASS;
+}
+
+int UtcDaliReadFileNew1(void)
+{
+  int errorCode;
+  Dali::Vector<char> buffer;
+
+  //negative case
+  errorCode = FileLoader::ReadFile( TEST_RESOURCE_DIR "/not_exist.txt", buffer, FileLoader::TEXT );
+
+  DALI_TEST_CHECK( errorCode == 0 );
+
+  DALI_TEST_CHECK( buffer.Size() == 0 );
+
+  //positive case
+  errorCode = FileLoader::ReadFile( TEST_RESOURCE_DIR "/test.txt", buffer, FileLoader::TEXT );
+
+  DALI_TEST_CHECK( errorCode != 0 );
+
+  DALI_TEST_CHECK( buffer.Size() > 0 );
+
+  END_TEST;
+}
+
+int UtcDaliReadFileNew2(void)
+{
+  int errorCode;
+  Dali::Vector<char> buffer;
+  std::streampos fileSize = 0;
+
+  //negative case
+  errorCode = FileLoader::ReadFile( TEST_RESOURCE_DIR "/not_exist.txt", fileSize, buffer, FileLoader::TEXT );
+
+  DALI_TEST_CHECK( errorCode == 0 );
+
+  DALI_TEST_CHECK( buffer.Size() == 0 );
+
+  DALI_TEST_CHECK( fileSize == 0 );
+
+  //positive case
+  errorCode = FileLoader::ReadFile( TEST_RESOURCE_DIR "/test.txt", fileSize, buffer, FileLoader::TEXT );
+
+  DALI_TEST_CHECK( errorCode != 0 );
+
+  DALI_TEST_CHECK( buffer.Size() > 0 );
+
+  DALI_TEST_CHECK( fileSize != 0 );
+
+  END_TEST;
+}
+
+int UtcDaliReadFileNew3(void)
+{
+  std::streampos fileSize = 0;
+
+  //negative case
+  fileSize = FileLoader::GetFileSize( TEST_RESOURCE_DIR "/not_exist.txt" );
+
+  DALI_TEST_CHECK( fileSize == 0 );
+
+  //positive case
+  fileSize = FileLoader::GetFileSize( TEST_RESOURCE_DIR "/test.txt" );
+
+  DALI_TEST_CHECK( fileSize != 0 );
+
+  END_TEST;
+}