X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fdata.cc;h=e843736100012e562805596dcdcda60d0de0bb2f;hb=f0a6d7b5b8d16b8b47917cd30b340917f5c62fc5;hp=a326ed3b7e3b3fcb2dc958425ed55b368679b075;hpb=faa03caeb082459e0283be39198464f50bef780a;p=libdcp.git diff --git a/src/data.cc b/src/data.cc index a326ed3b..e8437361 100644 --- a/src/data.cc +++ b/src/data.cc @@ -17,33 +17,79 @@ */ -/** @file src/data.cc - * @brief Data class. - */ - #include "data.h" #include "util.h" #include "exceptions.h" #include +#include +using boost::shared_array; using namespace dcp; -/** Construct a Data object from the contents of a file. - * @param file File to read. - */ +Data::Data () + : _size (0) +{ + +} + +Data::Data (int size) + : _data (new uint8_t[size]) + , _size (size) +{ + +} + +Data::Data (uint8_t const * data, int size) + : _data (new uint8_t[size]) + , _size (size) +{ + memcpy (_data.get(), data, size); +} + +Data::Data (shared_array data, int size) + : _data (data) + , _size (size) +{ + +} + Data::Data (boost::filesystem::path file) { + _size = boost::filesystem::file_size (file); + _data.reset (new uint8_t[_size]); + FILE* f = fopen_boost (file, "rb"); if (!f) { throw FileError ("could not open file for reading", file, errno); } - size = boost::filesystem::file_size (file); - data.reset (new uint8_t[size]); - size_t const read = fread (data.get(), 1, size, f); + size_t const r = fread (_data.get(), 1, _size, f); + if (r != size_t (_size)) { + fclose (f); + throw FileError ("could not read from file", file, errno); + } + fclose (f); +} - if (read != size) { - throw FileError ("could not read file", file, -1); +void +Data::write (boost::filesystem::path file) const +{ + FILE* f = fopen_boost (file, "wb"); + if (!f) { + throw FileError ("could not write to file", file, errno); + } + size_t const r = fwrite (_data.get(), 1, _size, f); + if (r != size_t (_size)) { + fclose (f); + throw FileError ("could not write to file", file, errno); } + fclose (f); +} + +void +Data::write_via_temp (boost::filesystem::path temp, boost::filesystem::path final) const +{ + write (temp); + boost::filesystem::rename (temp, final); }