X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Fmemory.cpp;h=c5aee1e0536e040e72187172b2c084269596cce9;hp=f89d9fc894c26d207e6e3b8300d9f9dc99c6f7ae;hb=481b844ed7d180ffbf70223075f2fc1ffdb5b444;hpb=31e72f50fbb34d86877e5110401c49ce3fefd4bb diff --git a/source/io/memory.cpp b/source/io/memory.cpp index f89d9fc..c5aee1e 100644 --- a/source/io/memory.cpp +++ b/source/io/memory.cpp @@ -8,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) @@ -37,9 +37,14 @@ void Memory::init(char *b, char *e, Mode m) mode = m; } +void Memory::set_block(bool) +{ + throw logic_error("Memory::set_block"); +} + 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); @@ -49,9 +54,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; } @@ -63,32 +70,50 @@ 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) +const Handle &Memory::get_handle(Mode) +{ + throw logic_error("Memory::get_handle"); +} + +SeekOffset Memory::seek(SeekOffset off, SeekType type) { char *new_pos; if(type==S_BEG) @@ -101,22 +126,12 @@ unsigned Memory::seek(int off, SeekType type) throw invalid_argument("Memory::seek"); if(new_posend) - throw out_of_range("Memory::seek"); + throw bad_seek(off, type); pos = new_pos; + eof_flag = false; return pos-begin; } -const 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