X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=util.cpp;h=a6175b4dbaf81bcad6fe7d9b6b585a4b2a3b76b6;hp=e08a0859a25ef09ae7ddba9945ef648b26934260;hb=0150ac0cd82ab2b270bc764a6163e52106f45e0e;hpb=d88ed3150376693000665b9016c0350d5d90e9e1 diff --git a/util.cpp b/util.cpp index e08a085..a6175b4 100644 --- a/util.cpp +++ b/util.cpp @@ -87,17 +87,41 @@ string read_file(const string &filename) { const string full_pathname = *movit_data_directory + "/" + filename; - static char buf[131072]; FILE *fp = fopen(full_pathname.c_str(), "r"); if (fp == NULL) { perror(full_pathname.c_str()); exit(1); } - int len = fread(buf, 1, sizeof(buf), fp); + int ret = fseek(fp, 0, SEEK_END); + if (ret == -1) { + perror("fseek(SEEK_END)"); + exit(1); + } + + int size = ftell(fp); + + ret = fseek(fp, 0, SEEK_SET); + if (ret == -1) { + perror("fseek(SEEK_SET)"); + exit(1); + } + + string str; + str.resize(size); + ret = fread(&str[0], size, 1, fp); + if (ret == -1) { + perror("fread"); + exit(1); + } + if (ret == 0) { + fprintf(stderr, "Short read when trying to read %d bytes from %s\n", + size, full_pathname.c_str()); + exit(1); + } fclose(fp); - return string(buf, len); + return str; } string read_version_dependent_file(const string &base, const string &extension) @@ -159,6 +183,16 @@ string output_glsl_mat3(const string &name, const Eigen::Matrix3d &m) return ss.str(); } +string output_glsl_float(const string &name, float x) +{ + // Use stringstream to be independent of the current locale in a thread-safe manner. + stringstream ss; + ss.imbue(locale("C")); + ss.precision(8); + ss << "const float " << name << " = " << x << ";\n"; + return ss.str(); +} + string output_glsl_vec2(const string &name, float x, float y) { // Use stringstream to be independent of the current locale in a thread-safe manner.