X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Fmemory.cpp;h=f64b4be9e52edcb6fcae3bb94c99d69595fd4e80;hp=79956cdafecae37a113b7421d10cb30fadd1d2c1;hb=3f7aa81d9212811b323d89949adcccda212cbed3;hpb=8f2711fba7a2817840038630d9cf9a2060ecbe8e diff --git a/source/io/memory.cpp b/source/io/memory.cpp index 79956cd..f64b4be 100644 --- a/source/io/memory.cpp +++ b/source/io/memory.cpp @@ -1,5 +1,6 @@ #include #include +#include "handle.h" #include "memory.h" using namespace std; @@ -7,14 +8,14 @@ using namespace std; namespace Msp { namespace IO { -Memory::Memory(char *d, unsigned s) +Memory::Memory(char *d, unsigned s, Mode m) { - init(d, d+s, M_RDWR); + init(d, d+s, m); } -Memory::Memory(char *b, char *e) +Memory::Memory(char *b, char *e, Mode m) { - init(b, e, M_RDWR); + init(b, e, m); } Memory::Memory(const char *cd, unsigned s) @@ -38,7 +39,7 @@ void Memory::init(char *b, char *e, Mode m) unsigned Memory::do_write(const char *buf, unsigned size) { - check_mode(M_WRITE); + check_access(M_WRITE); size = min(size, end-pos); memcpy(pos, buf, size); @@ -48,9 +49,11 @@ unsigned Memory::do_write(const char *buf, unsigned size) unsigned Memory::do_read(char *buf, unsigned size) { + check_access(M_READ); + if(pos==end) { - eof_flag = true; + set_eof(); return 0; } @@ -62,32 +65,45 @@ unsigned Memory::do_read(char *buf, unsigned size) unsigned Memory::put(char c) { - check_mode(M_WRITE); + check_access(M_WRITE); + + if(pos==end) + return 0; + *pos++ = c; return 1; } bool Memory::getline(string &line) { + check_access(M_READ); + + if(pos==end) + { + set_eof(); + return false; + } + char *nl = find(pos, end, '\n'); line.assign(pos, nl); - bool result = (nl!=pos); - pos = nl; - return result; + pos = (nl==end ? end : nl+1); + return true; } int Memory::get() { + check_access(M_READ); + if(pos==end) { - eof_flag = true; + set_eof(); return -1; } return static_cast(*pos++); } -unsigned Memory::seek(int off, SeekType type) +SeekOffset Memory::seek(SeekOffset off, SeekType type) { char *new_pos; if(type==S_BEG) @@ -103,19 +119,9 @@ unsigned Memory::seek(int off, SeekType type) throw out_of_range("Memory::seek"); pos = new_pos; + eof_flag = false; return pos-begin; } -Handle Memory::get_event_handle() -{ - throw logic_error("Memory doesn't support events"); -} - -void Memory::check_mode(Mode m) const -{ - if(m==M_WRITE && !(mode&M_WRITE)) - throw invalid_access(M_WRITE); -} - } // namespace IO } // namespace Msp