f81d01f744ad0bfb9ce22991e3095f3ed60ff23d
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor-framework / generic / file-loader-impl-generic.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // CLASS HEADER
18 #include <dali/internal/adaptor-framework/common/file-loader-impl.h>
19
20 // EXTERNAL INCLUDES
21 #include <string>
22 #include <fstream>
23
24 #include <dali/integration-api/debug.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 namespace Adaptor
33 {
34
35 int ReadFile(const std::string& filename, Dali::Vector<char>& memblock, Dali::FileLoader::FileType fileType)
36 {
37   std::streampos size;
38
39   return Dali::Internal::Adaptor::ReadFile( filename, size, memblock, fileType);
40 }
41
42 int ReadFile(const std::string& filename, Dali::Vector<uint8_t>& memblock, Dali::FileLoader::FileType fileType)
43 {
44   std::streampos size;
45
46   return Dali::Internal::Adaptor::ReadFile(filename, size, memblock, fileType);
47 }
48
49 template<typename T>
50 int ReadFile(const std::string& filename, std::streampos& fileSize, Dali::Vector<T>& memblock, Dali::FileLoader::FileType fileType)
51 {
52   int errorCode = 0;
53   std::ifstream * file;
54
55   if( fileType == Dali::FileLoader::BINARY )
56   {
57     file = new std::ifstream (filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
58   }
59   else if( fileType == Dali::FileLoader::TEXT )
60   {
61     file = new std::ifstream (filename.c_str(), std::ios::in|std::ios::ate);
62   }
63   else
64   {
65     return errorCode;
66   }
67
68   if( file->is_open() )
69   {
70     fileSize = file->tellg();
71
72     memblock.Resize( fileSize );
73
74     file->seekg (0, std::ios::beg);
75     file->read( reinterpret_cast<char*>(memblock.Begin()), fileSize );
76     file->close();
77
78     delete file;
79
80     errorCode = 1;
81   }
82   else
83   {
84     DALI_LOG_ERROR( "file open failed for: \"%s\"\n", filename.c_str() );
85   }
86
87   return errorCode;
88 }
89
90 std::streampos GetFileSize(const std::string& filename)
91 {
92   std::streampos size = 0;
93
94   std::ifstream file( filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate );
95   if( file.is_open() )
96   {
97     size = file.tellg();
98     file.close();
99   }
100
101   return size;
102 }
103
104 } // Adaptor
105
106 } // Internal
107
108 } // Dali