X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fio%2Fmemory.cpp;h=37954cca5bea6bb95a62a5afdfc094009584ba83;hb=8a3d4f05f46bcd6dda9fc28794d4ac84d0d96f08;hp=6ccf84654764cbe8086a3ac841330580d302dcd1;hpb=df5ab3d867c51d72344e443e3adb05bfa29a2b53;p=libs%2Fcore.git diff --git a/source/io/memory.cpp b/source/io/memory.cpp index 6ccf846..37954cc 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) @@ -49,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; } @@ -64,24 +66,37 @@ unsigned Memory::do_read(char *buf, unsigned size) unsigned Memory::put(char c) { 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; } @@ -101,9 +116,10 @@ SeekOffset Memory::seek(SeekOffset 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; }