21 #include "hx3d/utils/file.hpp" 23 #include <SDL2/SDL_system.h> 27 #include "hx3d/core/platforms.hpp" 28 #include "hx3d/utils/log.hpp" 33 data(nullptr), size(0)
53 #if defined(__ANDROID__) 54 return loadAsciiFileAndroid(path);
55 #elif defined(__IOS__) 56 return loadAsciiFileiOS(
"assets/" + path);
58 return loadAsciiFileDesktop(
"assets/" + path);
63 #if defined(__ANDROID__) 64 return loadBinaryFileAndroid(path);
65 #elif defined(__IOS__) 66 return loadBinaryFileiOS(
"assets/" + path);
68 return loadBinaryFileDesktop(
"assets/" + path);
73 #if defined(__ANDROID__) 74 path = getInternalPath() +
"/" + path;
77 return loadAsciiFileDesktop(path);
81 #if defined(__ANDROID__) 82 path = getInternalPath() +
"/" + path;
85 std::ofstream file(path);
91 return std::string(data, size);
96 Ptr<File> File::loadAsciiFileDesktop(std::string path) {
97 std::ifstream file(path);
99 std::ostringstream oss(
"");
102 std::string content = oss.str();
105 fileptr->size = content.size() + 1;
106 fileptr->data =
new char[fileptr->size];
107 std::copy(content.begin(), content.end(), fileptr->data);
108 fileptr->data[content.size()] =
'\0';
113 Log.
Error(
"Bad path: %s", path.c_str());
118 Ptr<File> File::loadBinaryFileDesktop(std::string path) {
119 std::ifstream file(path, std::ios::binary);
122 unsigned int size = 0;
124 file.seekg(0, std::ios::end);
126 file.seekg(0, std::ios::beg);
128 fileptr->size = size;
129 fileptr->data =
new char[fileptr->size];
130 file.read(fileptr->data, fileptr->size);
136 Log.
Error(
"Bad path: %s", path.c_str());
147 #include "TargetConditionals.h" 148 #ifdef TARGET_OS_IPHONE 150 #include <CoreFoundation/CoreFoundation.h> 154 Ptr<File> File::loadAsciiFileiOS(std::string path) {
155 return loadAsciiFileDesktop(path);
158 Ptr<File> File::loadBinaryFileiOS(std::string path) {
159 return loadBinaryFileDesktop(path);
171 #include <android/log.h> 172 #include <android/asset_manager.h> 173 #include <android/asset_manager_jni.h> 177 std::string File::getInternalPath() {
178 return std::string(SDL_AndroidGetInternalStoragePath());
181 std::string File::readAsString(std::string path) {
184 JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
186 jobject sdlActivity = (jobject)SDL_AndroidGetActivity();
187 jclass sdlActivityClass = env->GetObjectClass(sdlActivity);
188 env->DeleteLocalRef(sdlActivity);
193 jmethodID getContext = env->GetStaticMethodID(sdlActivityClass,
"getContext",
"()Landroid/content/Context;");
194 jobject context = env->CallStaticObjectMethod(sdlActivityClass, getContext);
199 jmethodID getAssets = env->GetMethodID(env->GetObjectClass(context),
"getAssets",
"()Landroid/content/res/AssetManager;");
200 jobject assetManager = env->CallObjectMethod(context, getAssets);
201 AAssetManager* mgr = AAssetManager_fromJava(env, assetManager);
209 AAsset* asset = AAssetManager_open(mgr, path.c_str(), AASSET_MODE_STREAMING);
210 std::string fileContent;
217 __android_log_print(ANDROID_LOG_WARN,
"SDL",
"File \"%s\" does not exists", path.c_str());
224 while (AAsset_read(asset, &buffer, 1) > 0){
225 fileContent += buffer;
234 Ptr<File> File::loadBinaryFileAndroid(std::string path) {
236 std::string content = File::readAsString(path);
239 file->size = content.size();
240 file->data =
new char[file->size];
241 std::copy(content.begin(), content.end(), file->data);
246 Ptr<File> File::loadAsciiFileAndroid(std::string path) {
247 std::string content = File::readAsString(path);
250 file->size = content.size();
251 file->data =
new char[file->size];
252 std::copy(content.begin(), content.end(), file->data);
std::string toString()
Return the file content as a string.
char * getData() const
Get the file data as an 8-bit array.
static Ptr< File > loadAsciiFile(std::string path)
Load an ascii file from a path.
static Ptr< File > loadInternalAsciiFile(std::string path)
Load an internal ascii file.
void Error(const std::string fmt,...)
Write an error message.
static hx3d::LogImpl Log
Current log implementation.
static void writeInternalAsciiFile(std::string path, std::string content)
Write to an internal ascii file.
size_t getSize() const
Get the file size.
static Ptr< File > loadBinaryFile(std::string path)
Load a binary file from a path.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.