1 #include "shared/read_file.h"
7 string read_file(const string &filename, const unsigned char *start, const size_t size)
9 FILE *fp = fopen(filename.c_str(), "r");
11 // Fall back to the version we compiled in. (We prefer disk if we can,
12 // since that makes it possible to work on shaders without recompiling
14 if (start != nullptr) {
15 return string(reinterpret_cast<const char *>(start),
16 reinterpret_cast<const char *>(start) + size);
19 perror(filename.c_str());
23 int ret = fseek(fp, 0, SEEK_END);
25 perror("fseek(SEEK_END)");
29 int disk_size = ftell(fp);
31 ret = fseek(fp, 0, SEEK_SET);
33 perror("fseek(SEEK_SET)");
38 str.resize(disk_size);
39 ret = fread(&str[0], disk_size, 1, fp);
45 fprintf(stderr, "Short read when trying to read %d bytes from %s\n",
46 disk_size, filename.c_str());