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 inline bool hasPrefix(const std::string& prefix, const std::string& path)
48 return std::mismatch(prefix.begin(), prefix.end(), path.begin()).first == prefix.end();
51 inline std::string ConvertToAssetsInternalPath(const std::string& path, int offset)
53 std::string internalPath = std::string(path.c_str() + offset);
56 while ((i = internalPath.find("//", i)) != std::string::npos)
58 internalPath.replace(i, 2, "/");
64 int ReadFile(const std::string& filename, std::streampos& fileSize, Dali::Vector<char>& memblock, Dali::FileLoader::FileType fileType)
68 char mode[3] = { 'r', 0, 0 };
70 if( fileType == Dali::FileLoader::BINARY )
74 else if( fileType != Dali::FileLoader::TEXT )
79 const std::string assetsPrefix = "assets/";
80 if( hasPrefix( assetsPrefix, filename ) )
82 std::string internalPath = ConvertToAssetsInternalPath( filename, assetsPrefix.length() );
83 AAssetManager* assetManager = Dali::Integration::AndroidFramework::Get().GetApplicationAssets();
84 AAsset* asset = AAssetManager_open( assetManager, internalPath.c_str(), AASSET_MODE_BUFFER );
87 length = AAsset_getLength( asset );
88 memblock.Resize( length + 1 ); // 1 for extra zero at the end
90 char* buffer = &memblock[0];
91 errorCode = ( AAsset_read( asset, buffer, length ) != length ) ? 0 : 1;
94 AAsset_close( asset );
98 DALI_LOG_ERROR( "Asset not found %s\n", internalPath.c_str() );
103 FILE* file = fopen( filename.c_str(), mode );
106 fseek( file, 0, SEEK_END );
107 length = ftell( file );
108 memblock.Resize( length + 1 ); // 1 for extra zero at the end
110 char* buffer = &memblock[0];
111 fseek( file, 0, SEEK_SET );
112 errorCode = ( fread( buffer, 1, length, file ) != length ) ? 0 : 1;
119 DALI_LOG_ERROR( "File not found %s\n", filename.c_str() );
126 std::streampos GetFileSize(const std::string& filename)
128 std::streampos size = 0;
130 const std::string assetsPrefix = "assets/";
131 if( hasPrefix( assetsPrefix, filename ) )
133 std::string internalPath = ConvertToAssetsInternalPath( filename, assetsPrefix.length() );
134 AAssetManager* assetManager = Dali::Integration::AndroidFramework::Get().GetApplicationAssets();
135 AAsset* asset = AAssetManager_open( assetManager, internalPath.c_str(), AASSET_MODE_BUFFER );
138 size = AAsset_getLength( asset );
139 AAsset_close( asset );
143 DALI_LOG_ERROR( "Asset not found %s\n", internalPath.c_str() );
148 FILE* file = fopen( filename.c_str(), "r" );
151 fseek( file, 0, SEEK_END );
152 size = ftell( file );
157 DALI_LOG_ERROR( "File not found %s\n", filename.c_str() );