X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fmemory.cpp;h=6ea130b54ae134478ab40851f4c49cf1d68ad439;hp=c44554ae5a4f9b59f7347befabc552c3fe190bf8;hb=b97d4e9f86e90254ab9edef7ee62a910f6333c78;hpb=7c6c7369eb6a2e63c466e3823ea729c72d5c55d3 diff --git a/source/memory.cpp b/source/memory.cpp index c44554a..6ea130b 100644 --- a/source/memory.cpp +++ b/source/memory.cpp @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of libmspio -Copyright © 2009 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include #include #include "except.h" @@ -27,7 +20,7 @@ Memory::Memory(char *b, char *e) Memory::Memory(const char *cd, unsigned s) { - char *d=const_cast(cd); + char *d = const_cast(cd); init(d, d+s, M_READ); } @@ -38,25 +31,49 @@ Memory::Memory(const char *b, const char *e) void Memory::init(char *b, char *e, Mode m) { - begin=b; - end=e; - pos=begin; - mode=m; + begin = b; + end = e; + pos = begin; + mode = m; +} + +unsigned Memory::do_write(const char *buf, unsigned size) +{ + check_mode(M_WRITE); + + size = min(size, end-pos); + memcpy(pos, buf, size); + pos += size; + return size; +} + +unsigned Memory::do_read(char *buf, unsigned size) +{ + if(pos==end) + { + eof_flag = true; + return 0; + } + + size = min(size, end-pos); + memcpy(buf, pos, size); + pos += size; + return size; } unsigned Memory::put(char c) { check_mode(M_WRITE); - *pos++=c; + *pos++ = c; return 1; } bool Memory::getline(string &line) { - char *nl=find(pos, end, '\n'); + char *nl = find(pos, end, '\n'); line.assign(pos, nl); - bool result=(nl!=pos); - pos=nl; + bool result = (nl!=pos); + pos = nl; return result; } @@ -64,7 +81,7 @@ int Memory::get() { if(pos==end) { - eof_flag=true; + eof_flag = true; return -1; } @@ -75,18 +92,18 @@ unsigned Memory::seek(int off, SeekType type) { char *new_pos; if(type==S_BEG) - new_pos=begin+off; + new_pos = begin+off; else if(type==S_CUR) - new_pos=pos+off; + new_pos = pos+off; else if(type==S_END) - new_pos=end+off; + new_pos = end+off; else throw InvalidParameterValue("Invalid seek type"); if(new_posend) throw InvalidParameterValue("Invalid seek offset"); - pos=new_pos; + pos = new_pos; return pos-begin; } @@ -95,30 +112,6 @@ Handle Memory::get_event_handle() throw Exception("Memory doesn't support events"); } -unsigned Memory::do_write(const char *buf, unsigned size) -{ - check_mode(M_WRITE); - - size=min(size, end-pos); - memcpy(pos, buf, size); - pos+=size; - return size; -} - -unsigned Memory::do_read(char *buf, unsigned size) -{ - if(pos==end) - { - eof_flag=true; - return 0; - } - - size=min(size, end-pos); - memcpy(buf, pos, size); - pos+=size; - return size; -} - void Memory::check_mode(Mode m) const { if(m==M_WRITE)