From d4a2e054dee6ee80b578500e47f4eaf84731f140 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sun, 29 Nov 2015 23:45:59 +0000 Subject: Take DCP-o-matic's version of Data class. --- src/data.cc | 71 +++++++++++++++++++++++++++++++++---------- src/data.h | 32 ++++++++++--------- src/interop_subtitle_asset.cc | 2 +- src/j2k.cc | 4 +-- src/smpte_subtitle_asset.cc | 4 +-- 5 files changed, 78 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/data.cc b/src/data.cc index 66970c91..e8437361 100644 --- a/src/data.cc +++ b/src/data.cc @@ -17,40 +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); } -Data::Data (uint8_t const * data_, boost::uintmax_t size_) - : data (new uint8_t[size]) - , size (size_) +void +Data::write_via_temp (boost::filesystem::path temp, boost::filesystem::path final) const { - memcpy (data.get(), data_, size); + write (temp); + boost::filesystem::rename (temp, final); } diff --git a/src/data.h b/src/data.h index 12949fe4..60689031 100644 --- a/src/data.h +++ b/src/data.h @@ -20,33 +20,37 @@ #ifndef LIBDCP_DATA_H #define LIBDCP_DATA_H -/** @file src/data.h - * @brief Data class. - */ - #include #include #include namespace dcp { -/** A block of arbitrary data */ class Data { public: - Data () {} + Data (); + Data (int size); + Data (uint8_t const * data, int size); + Data (boost::shared_array data, int size); + Data (boost::filesystem::path file); - Data (boost::shared_array data_, boost::uintmax_t size_) - : data (data_) - , size (size_) - {} + virtual ~Data () {} - Data (uint8_t const * data, boost::uintmax_t size_); + void write (boost::filesystem::path file) const; + void write_via_temp (boost::filesystem::path temp, boost::filesystem::path final) const; - Data (boost::filesystem::path file); + boost::shared_array data () const { + return _data; + } + + int size () const { + return _size; + } - boost::shared_array data; - boost::uintmax_t size; +private: + boost::shared_array _data; + int _size; }; } diff --git a/src/interop_subtitle_asset.cc b/src/interop_subtitle_asset.cc index 8b88ce6b..4ba24a19 100644 --- a/src/interop_subtitle_asset.cc +++ b/src/interop_subtitle_asset.cc @@ -173,7 +173,7 @@ InteropSubtitleAsset::write (boost::filesystem::path p) const ++j; } if (j != _fonts.end ()) { - fwrite (j->data.data.get(), 1, j->data.size, f); + fwrite (j->data.data().get(), 1, j->data.size(), f); j->file = file; } fclose (f); diff --git a/src/j2k.cc b/src/j2k.cc index 5318f0c2..c5502ec3 100644 --- a/src/j2k.cc +++ b/src/j2k.cc @@ -35,7 +35,7 @@ using namespace dcp; shared_ptr dcp::decompress_j2k (Data data, int reduce) { - return dcp::decompress_j2k (data.data.get(), data.size, reduce); + return dcp::decompress_j2k (data.data().get(), data.size(), reduce); } class ReadBuffer @@ -153,7 +153,7 @@ public: OPJ_SIZE_T write (void* buffer, OPJ_SIZE_T nb_bytes) { DCP_ASSERT ((_offset + nb_bytes) < MAX_J2K_SIZE); - memcpy (_data.data.get() + _offset, buffer, nb_bytes); + memcpy (_data.data().get() + _offset, buffer, nb_bytes); _offset += nb_bytes; return nb_bytes; } diff --git a/src/smpte_subtitle_asset.cc b/src/smpte_subtitle_asset.cc index 08462a4a..00a9299a 100644 --- a/src/smpte_subtitle_asset.cc +++ b/src/smpte_subtitle_asset.cc @@ -273,8 +273,8 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const } if (j != _fonts.end ()) { ASDCP::TimedText::FrameBuffer buffer; - buffer.SetData (j->data.data.get(), j->data.size); - buffer.Size (j->data.size); + buffer.SetData (j->data.data().get(), j->data.size()); + buffer.Size (j->data.size()); r = writer.WriteAncillaryResource (buffer); if (ASDCP_FAILURE (r)) { boost::throw_exception (MXFFileError ("could not write font to timed text resource", p.string(), r)); -- cgit v1.2.3