Added android version too
This commit is contained in:
1 parent
8089bcc0f3
commit
a08ee412a6
54 files changed
+7146
-488
No files matched your search
+72
-21
@@ -10,40 +10,91 @@
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include <SDL.h>
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include <android/log.h>
|
||||
#endif
|
||||
|
||||
namespace ZL
|
||||
{
|
||||
std::string readTextFile(const std::string &filename) {
|
||||
#ifdef __ANDROID__
|
||||
SDL_RWops* file = SDL_RWFromFile(filename.c_str(), "rb");
|
||||
if (!file) {
|
||||
#ifdef DEBUG
|
||||
__android_log_print(ANDROID_LOG_ERROR, "Utils",
|
||||
"Failed to open: %s", filename.c_str());
|
||||
#endif
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string readTextFile(const std::string& filename)
|
||||
{
|
||||
std::ifstream f(filename);
|
||||
Sint64 size = SDL_RWsize(file);
|
||||
if (size <= 0) {
|
||||
SDL_RWclose(file);
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
std::string content(size, '\0');
|
||||
if (SDL_RWread(file, &content[0], size, 1) != 1) {
|
||||
SDL_RWclose(file);
|
||||
return "";
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
SDL_RWclose(file);
|
||||
return content;
|
||||
#else
|
||||
std::ifstream f(filename);
|
||||
if (!f.is_open()) {
|
||||
return "";
|
||||
}
|
||||
return std::string((std::istreambuf_iterator<char>(f)),
|
||||
std::istreambuf_iterator<char>());
|
||||
#endif
|
||||
}
|
||||
|
||||
std::vector<char> readFile(const std::string& filename)
|
||||
{
|
||||
std::ifstream file(filename, std::ios::binary);
|
||||
std::vector<char> readFile(const std::string &filename) {
|
||||
#ifdef __ANDROID__
|
||||
SDL_RWops* file = SDL_RWFromFile(filename.c_str(), "rb");
|
||||
if (!file) {
|
||||
return {};
|
||||
}
|
||||
|
||||
file.unsetf(std::ios::skipws);
|
||||
Sint64 size = SDL_RWsize(file);
|
||||
if (size <= 0) {
|
||||
SDL_RWclose(file);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::streampos fileSize;
|
||||
std::vector<char> data(size);
|
||||
if (SDL_RWread(file, data.data(), size, 1) != 1) {
|
||||
SDL_RWclose(file);
|
||||
return {};
|
||||
}
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
fileSize = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
SDL_RWclose(file);
|
||||
return data;
|
||||
#else
|
||||
std::ifstream file(filename, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<char> vec;
|
||||
vec.reserve(static_cast<size_t>(fileSize));
|
||||
std::streamsize size = file.tellg();
|
||||
if (size <= 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
vec.insert(vec.begin(),
|
||||
std::istream_iterator<char>(file),
|
||||
std::istream_iterator<char>());
|
||||
file.seekg(0, std::ios::beg);
|
||||
std::vector<char> buffer(size);
|
||||
|
||||
return vec;
|
||||
}
|
||||
if (!file.read(buffer.data(), size)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return buffer;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::vector<char> readFileFromZIP(const std::string& filename, const std::string& zipfilename) {
|
||||
#ifdef EMSCRIPTEN
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#include <stack>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <array>
|
||||
|
||||
namespace ZL
|
||||
{
|
||||
std::string readTextFile(const std::string& filename);
|
||||
|
||||
Reference in new issue
Block a user