]> git.sesse.net Git - nageru/blob - shared/read_file.cpp
Add a hack to FFmpegCapture for decoding Futatabi's Y'CbCr streams correctly.
[nageru] / shared / read_file.cpp
1 #include "shared/read_file.h"
2
3 #include <stdio.h>
4
5 using namespace std;
6
7 string read_file(const string &filename, const unsigned char *start, const size_t size)
8 {
9         FILE *fp = fopen(filename.c_str(), "r");
10         if (fp == nullptr) {
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
13                 // all the time.)
14                 if (start != nullptr) {
15                         return string(reinterpret_cast<const char *>(start),
16                                 reinterpret_cast<const char *>(start) + size);
17                 }
18
19                 perror(filename.c_str());
20                 exit(1);
21         }
22
23         int ret = fseek(fp, 0, SEEK_END);
24         if (ret == -1) {
25                 perror("fseek(SEEK_END)");
26                 exit(1);
27         }
28
29         int disk_size = ftell(fp);
30
31         ret = fseek(fp, 0, SEEK_SET);
32         if (ret == -1) {
33                 perror("fseek(SEEK_SET)");
34                 exit(1);
35         }
36
37         string str;
38         str.resize(disk_size);
39         ret = fread(&str[0], disk_size, 1, fp);
40         if (ret == -1) {
41                 perror("fread");
42                 exit(1);
43         }
44         if (ret == 0) {
45                 fprintf(stderr, "Short read when trying to read %d bytes from %s\n",
46                         disk_size, filename.c_str());
47                 exit(1);
48         }
49         fclose(fp);
50
51         return str;
52 }
53