Commit e6d7f93d authored by Davis King's avatar Davis King

Added seekg() and made vectorstream more robust.

parent 45d21205
...@@ -29,6 +29,12 @@ namespace dlib ...@@ -29,6 +29,12 @@ namespace dlib
buffer(buffer_) buffer(buffer_)
{} {}
void seekg(size_type pos)
{
read_pos = pos;
}
// ------------------------ OUTPUT FUNCTIONS ------------------------ // ------------------------ OUTPUT FUNCTIONS ------------------------
int_type overflow ( int_type c) int_type overflow ( int_type c)
...@@ -85,14 +91,14 @@ namespace dlib ...@@ -85,14 +91,14 @@ namespace dlib
std::streamsize n std::streamsize n
) )
{ {
const size_type num = std::min<size_type>(n, buffer.size()-read_pos); if (read_pos < buffer.size())
if (num > 0)
{ {
const size_type num = std::min<size_type>(n, buffer.size()-read_pos);
std::memcpy(s, &buffer[read_pos], num); std::memcpy(s, &buffer[read_pos], num);
read_pos += num; read_pos += num;
return num;
} }
return 0;
return num;
} }
}; };
...@@ -106,6 +112,14 @@ namespace dlib ...@@ -106,6 +112,14 @@ namespace dlib
buf(buffer) buf(buffer)
{} {}
std::istream& seekg (
std::streampos pos
)
{
buf.seekg(pos);
return *this;
}
private: private:
vector_streambuf buf; vector_streambuf buf;
}; };
......
...@@ -43,6 +43,17 @@ namespace dlib ...@@ -43,6 +43,17 @@ namespace dlib
immediately show up in the buffer. immediately show up in the buffer.
!*/ !*/
std::istream& seekg (
std::streampos pos
);
/*!
ensures
- The next read from this object will read from the position buffer[pos],
where buffer is the std::vector given to this object's constructor. Note
that if pos >= buffer.size() then the next read will simply return EOF.
- returns *this
!*/
}; };
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment