| 123456789101112131415161718192021222324252627282930313233343536 |
- //
- // files.cxx
- // gameutils
- //
- // Created by Sam Jaffe on 5/19/19.
- // Copyright © 2019 Sam Jaffe. All rights reserved.
- //
- #include "game/util/files.hpp"
- #include "scope_guard/scope_guard.hpp"
- namespace files {
- std::unique_ptr<char[]> load(std::string const & absolute_path) {
- FILE * fp = fopen(absolute_path.c_str(), "r");
- if (!fp) { return nullptr; }
- scope(exit) { fclose(fp); };
- // Determine file size
- fseek(fp, 0, SEEK_END);
- long size = ftell(fp);
- if (size < 0) { return nullptr; }
- std::unique_ptr<char[]> buffer{new char[size + 1]};
- rewind(fp);
- size_t read = fread(buffer.get(), sizeof(char), size, fp);
- if (read != static_cast<size_t>(size)) {
- fputs("Error reading file", stderr);
- return nullptr;
- } else {
- buffer[read + 1] = '\0';
- }
- return buffer;
- }
- }
|