7e065cdb8be7c3ad7af5593fbc1ce71325061b3a
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor-framework / android / file-loader-impl-android.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 <cstdio>
22 #include <string>
23 #include <fstream>
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
27 #include <dali/integration-api/adaptor-framework/android/android-framework.h>
28 #include <dali/internal/adaptor/common/framework.h>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 namespace Adaptor
37 {
38
39 int ReadFile(const std::string& filename, Dali::Vector<char>& memblock, Dali::FileLoader::FileType fileType)
40 {
41   std::streampos size;
42
43   return Dali::Internal::Adaptor::ReadFile( filename, size, memblock, fileType);
44 }
45
46 int ReadFile(const std::string& filename, std::streampos& fileSize, Dali::Vector<char>& memblock, Dali::FileLoader::FileType fileType)
47 {
48   int errorCode = 0;
49   int length = 0;
50   char mode[3] = { 'r', 0, 0 };
51
52   if( fileType == Dali::FileLoader::BINARY )
53   {
54     mode[1] = 'b';
55   }
56   else if( fileType != Dali::FileLoader::TEXT )
57   {
58     return errorCode;
59   }
60
61   const char* path = filename.c_str();
62   const int assetsOffset = ( sizeof("assets/") - sizeof( char ) ) / sizeof( char );
63   if( !strncmp( path, "assets/", assetsOffset ) )
64   {
65     AAssetManager* assetManager = Dali::Integration::AndroidFramework::Get().GetApplicationAssets();
66     AAsset* asset = AAssetManager_open( assetManager, path + assetsOffset, AASSET_MODE_BUFFER );
67     if( asset )
68     {
69       length = AAsset_getLength( asset );
70       memblock.Resize( length + 1 ); // 1 for extra zero at the end
71
72       char* buffer = &memblock[0];
73       errorCode = ( AAsset_read( asset, buffer, length ) != length ) ? 0 : 1;
74       fileSize = length;
75
76       AAsset_close( asset );
77     }
78     else
79     {
80       DALI_LOG_ERROR( "Asset not found %s\n", path );
81     }
82   }
83   else
84   {
85     FILE* file = fopen( path,  mode );
86     if( file )
87     {
88       fseek( file, 0, SEEK_END );
89       length = ftell( file );
90       memblock.Resize( length + 1 ); // 1 for extra zero at the end
91
92       char* buffer = &memblock[0];
93       fseek( file, 0, SEEK_SET );
94       errorCode = ( fread( buffer, 1, length, file ) != length ) ? 0 : 1;
95       fileSize = length;
96
97       fclose( file );
98     }
99     else
100     {
101       DALI_LOG_ERROR( "File not found %s\n", path );
102     }
103   }
104
105   return errorCode;
106 }
107
108 std::streampos GetFileSize(const std::string& filename)
109 {
110   std::streampos size = 0;
111
112   const char* path = filename.c_str();
113   const int assetsOffset = ( sizeof("assets/") - sizeof( char ) ) / sizeof( char );
114   if( !strncmp( path, "assets/", assetsOffset ) )
115   {
116     AAssetManager* assetManager = Dali::Integration::AndroidFramework::Get().GetApplicationAssets();
117     AAsset* asset = AAssetManager_open( assetManager, path + assetsOffset, AASSET_MODE_BUFFER );
118     if( asset )
119     {
120       size = AAsset_getLength( asset );
121       AAsset_close( asset );
122     }
123     else
124     {
125       DALI_LOG_ERROR( "Asset not found %s\n", path );
126     }
127   }
128   else
129   {
130     FILE* file = fopen( path, "r" );
131     if( file )
132     {
133       fseek( file, 0, SEEK_END );
134       size = ftell( file );
135       fclose( file );
136     }
137     else
138     {
139       DALI_LOG_ERROR( "File not found %s\n", path );
140     }
141   }
142
143   return size;
144 }
145
146 } // Adaptor
147
148 } // Internal
149
150 } // Dali