]> git.tdb.fi Git - libs/core.git/commitdiff
Add Memory class
authorMikko Rasa <tdb@tdb.fi>
Tue, 17 Mar 2009 11:58:07 +0000 (11:58 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 17 Mar 2009 11:58:07 +0000 (11:58 +0000)
source/memory.cpp [new file with mode: 0644]
source/memory.h [new file with mode: 0644]

diff --git a/source/memory.cpp b/source/memory.cpp
new file mode 100644 (file)
index 0000000..c44554a
--- /dev/null
@@ -0,0 +1,134 @@
+/* $Id$
+
+This file is part of libmspio
+Copyright © 2009 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <algorithm>
+#include <cstring>
+#include "except.h"
+#include "memory.h"
+
+using namespace std;
+
+namespace Msp {
+namespace IO {
+
+Memory::Memory(char *d, unsigned s)
+{
+       init(d, d+s, M_RDWR);
+}
+
+Memory::Memory(char *b, char *e)
+{
+       init(b, e, M_RDWR);
+}
+
+Memory::Memory(const char *cd, unsigned s)
+{
+       char *d=const_cast<char *>(cd);
+       init(d, d+s, M_READ);
+}
+
+Memory::Memory(const char *b, const char *e)
+{
+       init(const_cast<char *>(b), const_cast<char *>(e), M_READ);
+}
+
+void Memory::init(char *b, char *e, Mode m)
+{
+       begin=b;
+       end=e;
+       pos=begin;
+       mode=m;
+}
+
+unsigned Memory::put(char c)
+{
+       check_mode(M_WRITE);
+       *pos++=c;
+       return 1;
+}
+
+bool Memory::getline(string &line)
+{
+       char *nl=find(pos, end, '\n');
+       line.assign(pos, nl);
+       bool result=(nl!=pos);
+       pos=nl;
+       return result;
+}
+
+int Memory::get()
+{
+       if(pos==end)
+       {
+               eof_flag=true;
+               return -1;
+       }
+
+       return static_cast<unsigned char>(*pos++);
+}
+
+unsigned Memory::seek(int off, SeekType type)
+{
+       char *new_pos;
+       if(type==S_BEG)
+               new_pos=begin+off;
+       else if(type==S_CUR)
+               new_pos=pos+off;
+       else if(type==S_END)
+               new_pos=end+off;
+       else
+               throw InvalidParameterValue("Invalid seek type");
+
+       if(new_pos<begin || new_pos>end)
+               throw InvalidParameterValue("Invalid seek offset");
+
+       pos=new_pos;
+       return pos-begin;
+}
+
+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<unsigned>(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<unsigned>(size, end-pos);
+       memcpy(buf, pos, size);
+       pos+=size;
+       return size;
+}
+
+void Memory::check_mode(Mode m) const
+{
+       if(m==M_WRITE)
+       {
+               if(!(mode&M_WRITE))
+                       throw InvalidState("Memory is not writable");
+               if(pos==end)
+                       throw InvalidState("Attempt to write past end of Memory");
+       }
+}
+
+} // namespace IO
+} // namespace Msp
diff --git a/source/memory.h b/source/memory.h
new file mode 100644 (file)
index 0000000..7ff5316
--- /dev/null
@@ -0,0 +1,48 @@
+/* $Id$
+
+This file is part of libmspio
+Copyright © 2009 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifndef MSP_IO_MEMORY_H_
+#define MSP_IO_MEMORY_H_
+
+#include "base.h"
+#include "seek.h"
+
+namespace Msp {
+namespace IO {
+
+class Memory: public Base
+{
+private:
+       char *begin;
+       char *end;
+       char *pos;
+
+public:
+       Memory(char *, unsigned);
+       Memory(char *, char *);
+       Memory(const char *, unsigned);
+       Memory(const char *, const char *);
+private:
+       void init(char *, char *, Mode);
+
+public:
+       virtual unsigned put(char);
+       virtual bool getline(std::string &);
+       virtual int get();
+       unsigned seek(int, SeekType);
+       unsigned tell() const { return pos-begin; }
+       virtual Handle get_event_handle();
+private:
+       virtual unsigned do_write(const char *, unsigned);
+       virtual unsigned do_read(char *, unsigned);
+       void check_mode(Mode) const;
+};
+
+} // namespace IO
+} // namespace Msp
+
+#endif