Tidy up; more/better use of BOOST_FOREACH.
[libdcp.git] / src / util.cc
index 139118ca427e660a2d4c35b4ecc98ca0d7f0e6dd..856bc9bd0001d0d958cbae4334df0824b6cc0a25 100644 (file)
 #include "util.h"
 #include "exceptions.h"
 #include "types.h"
-#include "certificates.h"
+#include "certificate.h"
 #include "openjpeg_image.h"
 #include "dcp_assert.h"
 #include "compose.hpp"
 #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;