From ae5f98d6e18fe65eb4013e55663119855c556287 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 1 Sep 2003 07:43:14 +0000 Subject: [PATCH] Added a memory IO callback class. Not yet tested, though. --- src/mm_io.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/mm_io.h | 20 +++++++++++++++-- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/mm_io.cpp b/src/mm_io.cpp index 3dfd7de71..b46366f6a 100644 --- a/src/mm_io.cpp +++ b/src/mm_io.cpp @@ -393,6 +393,68 @@ size_t mm_null_io_c::write(const void *buffer, size_t size) { void mm_null_io_c::close() { } +/* + * IO callback class working on memory + */ +mm_mem_io_c::mm_mem_io_c(unsigned char *nmem, uint64_t nsize) { + mem = nmem; + mem_size = nsize; + pos = 0; +} + +uint64_t mm_mem_io_c::getFilePointer() { + return pos; +} + +void mm_mem_io_c::setFilePointer(int64 offset, seek_mode mode) { + int64_t npos; + + if ((mem == NULL) || (mem_size == 0)) + throw exception(); + + if (mode == seek_beginning) + npos = offset; + else if (mode == seek_end) + npos = mem_size - offset; + else + npos = pos + offset; + + if ((npos < 0) || (npos >= mem_size)) + throw exception(); + + pos = npos; +} + +uint32 mm_mem_io_c::read(void *buffer, size_t size) { + int64_t rbytes; + + rbytes = (pos + size) >= mem_size ? mem_size - pos : size; + memcpy(buffer, &mem[pos], rbytes); + pos += rbytes; + + return rbytes; +} + +size_t mm_mem_io_c::write(const void *buffer, size_t size) { + int64_t wbytes; + + wbytes = (pos + size) >= mem_size ? mem_size - pos : size; + memcpy(&mem[pos], buffer, wbytes); + pos += wbytes; + + return wbytes; +} + +void mm_mem_io_c::close() { + mem = NULL; + mem_size = 0; + pos = 0; +} + +bool mm_mem_io_c::eof() { + return pos >= mem_size; +} + /* * Class for handling UTF-8/UTF-16/UTF-32 text files. */ diff --git a/src/mm_io.h b/src/mm_io.h index c08126646..e4e81d6b7 100644 --- a/src/mm_io.h +++ b/src/mm_io.h @@ -48,7 +48,7 @@ public: virtual ~mm_io_c(); virtual uint64 getFilePointer(); - virtual void setFilePointer(int64 offset, seek_mode mode=seek_beginning); + virtual void setFilePointer(int64 offset, seek_mode mode = seek_beginning); virtual uint32 read(void *buffer, size_t size); virtual unsigned char read_uint8(); virtual uint16_t read_uint16(); @@ -79,12 +79,28 @@ public: mm_null_io_c(); virtual uint64 getFilePointer(); - virtual void setFilePointer(int64 offset, seek_mode mode=seek_beginning); + virtual void setFilePointer(int64 offset, seek_mode mode = seek_beginning); virtual uint32 read(void *buffer, size_t size); virtual size_t write(const void *buffer, size_t size); virtual void close(); }; +class mm_mem_io_c: public mm_io_c { +protected: + int64_t pos, mem_size; + unsigned char *mem; + +public: + mm_mem_io_c(unsigned char *nmem, uint64_t nsize); + + virtual uint64 getFilePointer(); + virtual void setFilePointer(int64 offset, seek_mode mode = seek_beginning); + virtual uint32 read(void *buffer, size_t size); + virtual size_t write(const void *buffer, size_t size); + virtual void close(); + virtual bool eof(); +}; + enum byte_order_e {BO_UTF8, BO_UTF16_LE, BO_UTF16_BE, BO_UTF32_LE, BO_UTF32_BE, BO_NONE};