]> git.tdb.fi Git - libs/core.git/blob - source/memory.cpp
Drop copyright and license notices from files
[libs/core.git] / source / memory.cpp
1 #include <algorithm>
2 #include <cstring>
3 #include "except.h"
4 #include "memory.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace IO {
10
11 Memory::Memory(char *d, unsigned s)
12 {
13         init(d, d+s, M_RDWR);
14 }
15
16 Memory::Memory(char *b, char *e)
17 {
18         init(b, e, M_RDWR);
19 }
20
21 Memory::Memory(const char *cd, unsigned s)
22 {
23         char *d = const_cast<char *>(cd);
24         init(d, d+s, M_READ);
25 }
26
27 Memory::Memory(const char *b, const char *e)
28 {
29         init(const_cast<char *>(b), const_cast<char *>(e), M_READ);
30 }
31
32 void Memory::init(char *b, char *e, Mode m)
33 {
34         begin = b;
35         end = e;
36         pos = begin;
37         mode = m;
38 }
39
40 unsigned Memory::put(char c)
41 {
42         check_mode(M_WRITE);
43         *pos++ = c;
44         return 1;
45 }
46
47 bool Memory::getline(string &line)
48 {
49         char *nl = find(pos, end, '\n');
50         line.assign(pos, nl);
51         bool result = (nl!=pos);
52         pos = nl;
53         return result;
54 }
55
56 int Memory::get()
57 {
58         if(pos==end)
59         {
60                 eof_flag = true;
61                 return -1;
62         }
63
64         return static_cast<unsigned char>(*pos++);
65 }
66
67 unsigned Memory::seek(int off, SeekType type)
68 {
69         char *new_pos;
70         if(type==S_BEG)
71                 new_pos = begin+off;
72         else if(type==S_CUR)
73                 new_pos = pos+off;
74         else if(type==S_END)
75                 new_pos = end+off;
76         else
77                 throw InvalidParameterValue("Invalid seek type");
78
79         if(new_pos<begin || new_pos>end)
80                 throw InvalidParameterValue("Invalid seek offset");
81
82         pos = new_pos;
83         return pos-begin;
84 }
85
86 Handle Memory::get_event_handle()
87 {
88         throw Exception("Memory doesn't support events");
89 }
90
91 unsigned Memory::do_write(const char *buf, unsigned size)
92 {
93         check_mode(M_WRITE);
94
95         size = min<unsigned>(size, end-pos);
96         memcpy(pos, buf, size);
97         pos += size;
98         return size;
99 }
100
101 unsigned Memory::do_read(char *buf, unsigned size)
102 {
103         if(pos==end)
104         {
105                 eof_flag = true;
106                 return 0;
107         }
108
109         size = min<unsigned>(size, end-pos);
110         memcpy(buf, pos, size);
111         pos += size;
112         return size;
113 }
114
115 void Memory::check_mode(Mode m) const
116 {
117         if(m==M_WRITE)
118         {
119                 if(!(mode&M_WRITE))
120                         throw InvalidState("Memory is not writable");
121                 if(pos==end)
122                         throw InvalidState("Attempt to write past end of Memory");
123         }
124 }
125
126 } // namespace IO
127 } // namespace Msp