For example, In android, ios or encrypted file, A way is required to read or decrypt the files.
I do it like this:
public:
/// Helper function for loading a file
std::string load_file(const std::string &t_filename) {
if (loader)
{
return loader(t_filename);
}
std::ifstream infile(t_filename.c_str(), std::ios::in | std::ios::ate | std::ios::binary );
if (!infile.is_open()) {
throw exception::file_not_found_error(t_filename);
}
std::streampos size = infile.tellg();
infile.seekg(0, std::ios::beg);
assert(size >= 0);
if (size == std::streampos(0))
{
return std::string();
} else {
std::vector<char> v(static_cast<unsigned int>(size));
infile.read(&v[0], size);
return std::string(v.begin(), v.end());
}
}
void set_file_reader(const std::function<std::string (const std::string &)>& func)
{
loader = func;
}
private:
std::function<std::string (const std::string &)> loader;
For example, In android, ios or encrypted file, A way is required to read or decrypt the files.
I do it like this: