Avoid execessive Pushback when Dali::Vector<char> used as file buffer container
[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 inline bool hasPrefix(const std::string& prefix, const std::string& path)
47 {
48   return std::mismatch(prefix.begin(), prefix.end(), path.begin()).first == prefix.end();
49 }
50
51 inline std::string ConvertToAssetsInternalPath(const std::string& path, int offset)
52 {
53   std::string internalPath = std::string(path.c_str() + offset);
54
55   int i = 0;
56   while ((i = internalPath.find("//", i)) != std::string::npos)
57   {
58     internalPath.replace(i, 2, "/");
59   }
60
61   return internalPath;
62 }
63
64 int ReadFile(const std::string& filename, std::streampos& fileSize, Dali::Vector<char>& memblock, Dali::FileLoader::FileType fileType)
65 {
66   int errorCode = 0;
67   int length = 0;
68   char mode[3] = { 'r', 0, 0 };
69
70   if( fileType == Dali::FileLoader::BINARY )
71   {
72     mode[1] = 'b';
73   }
74   else if( fileType != Dali::FileLoader::TEXT )
75   {
76     return errorCode;
77   }
78
79   const std::string assetsPrefix = "assets/";
80   if( hasPrefix( assetsPrefix, filename ) )
81   {
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 );
85     if( asset )
86     {
87       length = AAsset_getLength( asset );
88       memblock.Resize( length + 1 ); // 1 for extra zero at the end
89
90       char* buffer = &memblock[0];
91       errorCode = ( AAsset_read( asset, buffer, length ) != length ) ? 0 : 1;
92       fileSize = length;
93
94       AAsset_close( asset );
95     }
96     else
97     {
98       DALI_LOG_ERROR( "Asset not found %s\n", internalPath.c_str() );
99     }
100   }
101   else
102   {
103     FILE* file = fopen( filename.c_str(),  mode );
104     if( file )
105     {
106       fseek( file, 0, SEEK_END );
107       length = ftell( file );
108       //Dali::Vector.Resize would lead to calling PushBack for each byte, waste of CPU resource
109       memblock.ResizeUninitialized( length + 1 );
110       //put last byte as 0, in case this is a text file without null-terminator
111       memblock[length] = 0;
112
113       char* buffer = &memblock[0];
114       fseek( file, 0, SEEK_SET );
115       errorCode = ( fread( buffer, 1, length, file ) != length ) ? 0 : 1;
116       fileSize = length;
117
118       fclose( file );
119     }
120     else
121     {
122       DALI_LOG_ERROR( "File not found %s\n", filename.c_str() );
123     }
124   }
125
126   return errorCode;
127 }
128
129 std::streampos GetFileSize(const std::string& filename)
130 {
131   std::streampos size = 0;
132
133   const std::string assetsPrefix = "assets/";
134   if( hasPrefix( assetsPrefix, filename ) )
135   {
136     std::string internalPath = ConvertToAssetsInternalPath( filename, assetsPrefix.length() );
137     AAssetManager* assetManager = Dali::Integration::AndroidFramework::Get().GetApplicationAssets();
138     AAsset* asset = AAssetManager_open( assetManager, internalPath.c_str(), AASSET_MODE_BUFFER );
139     if( asset )
140     {
141       size = AAsset_getLength( asset );
142       AAsset_close( asset );
143     }
144     else
145     {
146       DALI_LOG_ERROR( "Asset not found %s\n", internalPath.c_str() );
147     }
148   }
149   else
150   {
151     FILE* file = fopen( filename.c_str(), "r" );
152     if( file )
153     {
154       fseek( file, 0, SEEK_END );
155       size = ftell( file );
156       fclose( file );
157     }
158     else
159     {
160       DALI_LOG_ERROR( "File not found %s\n", filename.c_str() );
161     }
162   }
163
164   return size;
165 }
166
167 } // Adaptor
168
169 } // Internal
170
171 } // Dali