Skip to content

Need using load_file, a callback is needed to provide special implementation. #116

@yindongfei

Description

@yindongfei

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions