From 8fdabca48d74b70a5d35ea31bca09261a587b7f3 Mon Sep 17 00:00:00 2001 From: Chris Johns Date: Mon, 26 Nov 2012 11:04:57 +1100 Subject: Add writing from images as well as streaming operators. Add the ability to write to the compressed stream directly from files::images. Add streaming operator support which is always in a standard byte format in the output image. --- rld-compression.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) (limited to 'rld-compression.h') diff --git a/rld-compression.h b/rld-compression.h index 624ab8a..7e510f1 100644 --- a/rld-compression.h +++ b/rld-compression.h @@ -54,14 +54,25 @@ namespace rld ~compressor (); /** - * Write to the output buffer and the image once full - * and compressed. + * Write the data to the output buffer and once the image buffer is full + * compress and write the compressed data to the image. * * @param data The data to write to the image compressed * @param length The mount of data in bytes to write. */ void write (const void* data, size_t length); + /** + * Write the section of the input image file to the output buffer and + * once the image buffer is full compress and write the compressed data + * to the image. + * + * @param input The input image. + * @param offset The input image offset to read from. + * @param length The mount of data in bytes to write. + */ + void write (files::image& input, off_t offset, size_t length); + /** * Flush the output buffer is data is present. */ @@ -94,9 +105,48 @@ namespace rld // transferred. }; + /** + * Compressor template function for writing data to the compressor.. + */ + template < typename T > + void write (compressor& comp, const T value) + { + uint8_t bytes[sizeof (T)]; + T v = value; + int b = sizeof (T) - 1; + while (b > 0) + { + bytes[b--] = (uint8_t) v; + v >>= 8; + } + comp.write (bytes, sizeof (T)); + } + } +} + +static inline rld::compress::compressor& operator<< (rld::compress::compressor& comp, + const uint64_t value) { + rld::compress::write < uint64_t > (comp, value); + return comp; +} +static inline rld::compress::compressor& operator<< (rld::compress::compressor& comp, + const uint32_t value) { + rld::compress::write < uint32_t > (comp, value); + return comp; +} + +static inline rld::compress::compressor& operator<< (rld::compress::compressor& comp, + const size_t value) { + comp << (uint32_t) value; + return comp; +} +static inline rld::compress::compressor& operator<< (rld::compress::compressor& comp, + const std::string& str) { + comp.write (str.c_str (), str.size ()); + return comp; } #endif -- cgit v1.2.3