From 72766c21b2889ac13c94f4645638fe043dfa35d7 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 29 Feb 2016 15:26:25 +0100 Subject: [PATCH] Streams set to iostream now --- include/stdex/idrec.h | 68 ++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/include/stdex/idrec.h b/include/stdex/idrec.h index ac6b08190..98e9cf266 100644 --- a/include/stdex/idrec.h +++ b/include/stdex/idrec.h @@ -22,6 +22,8 @@ #include "common.h" #include +#include +#include namespace stdex { @@ -35,8 +37,8 @@ namespace stdex { /// - true when successful /// - false otherwise /// - template - inline bool ignore(T_STREAM& stream) + template + inline bool ignore(std::istream& stream) { T_SIZE size; @@ -64,8 +66,8 @@ namespace stdex { /// - true when found /// - false otherwise /// - template - inline bool find(T_STREAM& stream, T_ID id, std::streamoff end = (std::streamoff)-1) + template + inline bool find(std::istream& stream, T_ID id, std::streamoff end = (std::streamoff)-1) { T_ID _id; @@ -77,7 +79,7 @@ namespace stdex { // The record was found. return true; } else - ignore(stream); + ignore(stream); } return false; @@ -92,18 +94,19 @@ namespace stdex { /// /// \returns Position of the record header start in \p stream. Save for later \c close call. /// - template - inline std::streamoff open(T_STREAM& stream, T_ID id) + template + inline std::streamoff open(std::ostream& stream, T_ID id) { std::streamoff start = stream.tellp(); // Write ID. if (stream.fail()) return (std::streamoff)-1; - stream.write(&id, sizeof(id)); + stream.write((const char*)&id, sizeof(id)); // Write 0 as a placeholder for data size. if (stream.fail()) return (std::streamoff)-1; - stream.write(&(T_SIZE)0, sizeof(T_SIZE)); + T_SIZE size = 0; + stream.write((const char*)&size, sizeof(size)); return start; } @@ -117,8 +120,8 @@ namespace stdex { /// /// \returns Position of the record end in \p stream /// - template - inline std::streamoff close(T_STREAM& stream, std::streamoff start) + template + inline std::streamoff close(std::ostream& stream, std::streamoff start) { std::streamoff end = stream.tellp(); T_SIZE @@ -127,7 +130,7 @@ namespace stdex { if (remainder) { // Append padding. - static const unsigned char padding[ALIGN] = {}; + static const char padding[ALIGN] = {}; stream.write(padding, remainder); end += remainder; } @@ -135,7 +138,7 @@ namespace stdex { // Update the data size. if (stream.fail()) return (std::streamoff)-1; stream.seekp(start + sizeof(T_ID)); - stream.write(&size, sizeof(size)); + stream.write((const char*)&size, sizeof(size)); stream.seekp(end); return end; @@ -176,10 +179,9 @@ namespace stdex { /// /// \returns Position of the record header start in \p stream. Save for later \c close call. /// - template - static inline std::streamoff open(T_STREAM& stream) + static inline std::streamoff open(std::ostream& stream) { - return open(stream, id); + return open(stream, id); } @@ -191,10 +193,9 @@ namespace stdex { /// /// \returns Position of the record end in \p stream /// - template - static inline std::streamoff close(T_STREAM& stream, std::streamoff start) + static inline std::streamoff close(std::ostream& stream, std::streamoff start) { - return close(stream, start); + return close(stream, start); } @@ -208,10 +209,9 @@ namespace stdex { /// - true when found /// - false otherwise /// - template - static inline bool find(T_STREAM& stream, std::streamoff end = (std::streamoff)-1) + static inline bool find(std::istream& stream, std::streamoff end = (std::streamoff)-1) { - return find(stream, id, end); + return find(stream, id, end); } static const T_ID id; ///< Record id @@ -221,8 +221,16 @@ namespace stdex { }; -template -inline T_STREAM& operator <<(T_STREAM& stream, const stdex::idrec::record r) +/// +/// Writes record to a stream +/// +/// \param[in] stream Output stream +/// \param[in] r Record +/// +/// \returns The stream \p stream +/// +template +inline std::ostream& operator <<(std::ostream& stream, const stdex::idrec::record r) { // Parameter r does not need to be passed by reference. It has only one field (data), which is a reference itself already. The id field is static anyway. @@ -239,8 +247,16 @@ inline T_STREAM& operator <<(T_STREAM& stream, const stdex::idrec::record -inline T_STREAM& operator >>(T_STREAM& stream, stdex::idrec::record r) +/// +/// Reads record from a stream +/// +/// \param[in] stream Input stream +/// \param[out] r Record +/// +/// \returns The stream \p stream +/// +template +inline std::istream& operator >>(std::istream& stream, stdex::idrec::record r) { // Parameter r does not need to be passed by reference. It has only one field (data), which is a reference itself already. The id field is static anyway.