Added a memory IO callback class. Not yet tested, though.

This commit is contained in:
Moritz Bunkus 2003-09-01 07:43:14 +00:00
parent 8a1f0fbd80
commit ae5f98d6e1
2 changed files with 80 additions and 2 deletions

View File

@ -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.
*/

View File

@ -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};