X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2FKM_memio.h;h=caf4fc0aca0f48a1517a8a6bbd1c93822baa0c90;hb=80490136d3f872d162670e616827033fdd1be09d;hp=8416b5729193fe97d4ee463aa0add395db481aeb;hpb=bfedf725dac9d13f3a02fe69f45c302ab29d2b1e;p=asdcplib.git diff --git a/src/KM_memio.h b/src/KM_memio.h index 8416b57..caf4fc0 100755 --- a/src/KM_memio.h +++ b/src/KM_memio.h @@ -1,5 +1,5 @@ /* -Copyright (c) 2006, John Hurst +Copyright (c) 2006-2011, John Hurst All rights reserved. Redistribution and use in source and binary forms, with or without @@ -33,7 +33,8 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define _KM_MEMIO_H_ #include -#include +#include +#include namespace Kumu { @@ -60,9 +61,10 @@ namespace Kumu inline void Reset() { m_size = 0; } inline byte_t* Data() { return m_p; } + inline const byte_t* RoData() const { return m_p; } inline byte_t* CurrentData() { return m_p + m_size; } - inline ui32_t Length() { return m_size; } - inline ui32_t Remainder() { return m_capacity - m_size; } + inline ui32_t Length() const { return m_size; } + inline ui32_t Remainder() const { return m_capacity - m_size; } inline bool AddOffset(ui32_t offset) { if ( ( m_size + offset ) > m_capacity ) @@ -119,7 +121,14 @@ namespace Kumu m_size += sizeof(ui64_t); return true; } - }; + + inline bool WriteString(const std::string& str) { + ui32_t len = static_cast(str.length()); + if ( ! WriteUi32BE(len) ) return false; + if ( ! WriteRaw((const byte_t*)str.c_str(), len) ) return false; + return true; + } + }; // class MemIOReader @@ -142,10 +151,10 @@ namespace Kumu ~MemIOReader() {} inline void Reset() { m_size = 0; } - inline const byte_t* Data() { return m_p; } - inline const byte_t* CurrentData() { return m_p + m_size; } - inline ui32_t Offset() { return m_size; } - inline ui32_t Remainder() { return m_capacity - m_size; } + inline const byte_t* Data() const { return m_p; } + inline const byte_t* CurrentData() const { return m_p + m_size; } + inline ui32_t Offset() const { return m_size; } + inline ui32_t Remainder() const { return m_capacity - m_size; } inline bool SkipOffset(ui32_t offset) { if ( ( m_size + offset ) > m_capacity ) @@ -205,8 +214,37 @@ namespace Kumu m_size += sizeof(ui64_t); return true; } + + inline bool ReadString(std::string& str) + { + ui32_t str_length = 0; + if ( ! ReadUi32BE(&str_length) ) return false; + + if ( str_length > 0 ) + { + if ( ( m_size + str_length ) > m_capacity ) return false; + str.assign((const char*)CurrentData(), str_length); + if ( ! SkipOffset(str_length) ) return false; + } + + return true; + } }; + // + inline bool + UnarchiveString(MemIOReader& Reader, std::string& str) { + return Reader.ReadString(str); + } + + // + inline bool + ArchiveString(MemIOWriter& Writer, const std::string& str) + { + return Writer.WriteString(str); + } + + } // namespace Kumu #endif // _KM_MEMIO_H_