#include <algorithm>
#include <cstring>
-#include "except.h"
+#include "handle.h"
#include "memory.h"
using namespace std;
unsigned Memory::do_write(const char *buf, unsigned size)
{
- check_mode(M_WRITE);
+ check_access(M_WRITE);
size = min<unsigned>(size, end-pos);
memcpy(pos, buf, size);
{
if(pos==end)
{
- eof_flag = true;
+ set_eof();
return 0;
}
unsigned Memory::put(char c)
{
- check_mode(M_WRITE);
+ check_access(M_WRITE);
*pos++ = c;
return 1;
}
bool Memory::getline(string &line)
{
+ 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()
{
if(pos==end)
{
- eof_flag = true;
+ set_eof();
return -1;
}
return static_cast<unsigned char>(*pos++);
}
-unsigned Memory::seek(int off, SeekType type)
+SeekOffset Memory::seek(SeekOffset off, SeekType type)
{
char *new_pos;
if(type==S_BEG)
else if(type==S_END)
new_pos = end+off;
else
- throw InvalidParameterValue("Invalid seek type");
+ throw invalid_argument("Memory::seek");
if(new_pos<begin || new_pos>end)
- throw InvalidParameterValue("Invalid seek offset");
+ throw out_of_range("Memory::seek");
pos = new_pos;
return pos-begin;
}
-Handle Memory::get_event_handle()
-{
- throw Exception("Memory doesn't support events");
-}
-
-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