Tidy up; more/better use of BOOST_FOREACH.
[libdcp.git] / src / util.cc
index 569eb9fb3270b3b19106aac4825f1a714e516b09..856bc9bd0001d0d958cbae4334df0824b6cc0a25 100644 (file)
@@ -31,6 +31,7 @@
 #include "KM_util.h"
 #include "KM_fileio.h"
 #include "AS_DCP.h"
+#include <openjpeg.h>
 #include <xmlsec/xmldsig.h>
 #include <xmlsec/dl.h>
 #include <xmlsec/app.h>
@@ -402,18 +403,17 @@ dcp::file_to_string (boost::filesystem::path p, uintmax_t max_length)
                throw MiscError ("Unexpectedly long file");
        }
 
-       char* c = new char[len + 1];
-
        FILE* f = fopen_boost (p, "r");
        if (!f) {
-               return "";
+               throw FileError ("could not open file", p, errno);
        }
 
-       fread (c, 1, len, f);
+       char* c = new char[len];
+       /* This may read less than `len' if we are on Windows and we have CRLF in the file */
+       int const N = fread (c, 1, len, f);
        fclose (f);
-       c[len] = '\0';
 
-       string s (c);
+       string s (c, N);
        delete[] c;
 
        return s;