2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali/internal/adaptor-framework/common/file-loader-impl.h>
24 #include <dali/integration-api/debug.h>
27 #include <dali/integration-api/adaptor-framework/android/android-framework.h>
28 #include <dali/internal/adaptor/common/framework.h>
39 int ReadFile(const std::string& filename, Dali::Vector<char>& memblock, Dali::FileLoader::FileType fileType)
43 return Dali::Internal::Adaptor::ReadFile( filename, size, memblock, fileType);
46 int ReadFile(const std::string& filename, std::streampos& fileSize, Dali::Vector<char>& memblock, Dali::FileLoader::FileType fileType)
50 char mode[3] = { 'r', 0, 0 };
52 if( fileType == Dali::FileLoader::BINARY )
56 else if( fileType != Dali::FileLoader::TEXT )
61 const char* path = filename.c_str();
62 const int assetsOffset = ( sizeof("assets/") - sizeof( char ) ) / sizeof( char );
63 if( !strncmp( path, "assets/", assetsOffset ) )
65 AAssetManager* assetManager = Dali::Integration::AndroidFramework::Get().GetApplicationAssets();
66 AAsset* asset = AAssetManager_open( assetManager, path + assetsOffset, AASSET_MODE_BUFFER );
69 length = AAsset_getLength( asset );
70 memblock.Resize( length + 1 ); // 1 for extra zero at the end
72 char* buffer = &memblock[0];
73 errorCode = ( AAsset_read( asset, buffer, length ) != length ) ? 0 : 1;
76 AAsset_close( asset );
80 DALI_LOG_ERROR( "Asset not found %s\n", path );
85 FILE* file = fopen( path, mode );
88 fseek( file, 0, SEEK_END );
89 length = ftell( file );
90 memblock.Resize( length + 1 ); // 1 for extra zero at the end
92 char* buffer = &memblock[0];
93 fseek( file, 0, SEEK_SET );
94 errorCode = ( fread( buffer, 1, length, file ) != length ) ? 0 : 1;
101 DALI_LOG_ERROR( "File not found %s\n", path );
108 std::streampos GetFileSize(const std::string& filename)
110 std::streampos size = 0;
112 const char* path = filename.c_str();
113 const int assetsOffset = ( sizeof("assets/") - sizeof( char ) ) / sizeof( char );
114 if( !strncmp( path, "assets/", assetsOffset ) )
116 AAssetManager* assetManager = Dali::Integration::AndroidFramework::Get().GetApplicationAssets();
117 AAsset* asset = AAssetManager_open( assetManager, path + assetsOffset, AASSET_MODE_BUFFER );
120 size = AAsset_getLength( asset );
121 AAsset_close( asset );
125 DALI_LOG_ERROR( "Asset not found %s\n", path );
130 FILE* file = fopen( path, "r" );
133 fseek( file, 0, SEEK_END );
134 size = ftell( file );
139 DALI_LOG_ERROR( "File not found %s\n", path );