summaryrefslogtreecommitdiff
path: root/src/core/common
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-02-08 17:53:15 +0100
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-02-08 17:53:15 +0100
commitfb0922e57f1a5e1fb8bfbe153dc381d5778e3137 (patch)
tree5ae5197acad0c086ba2a3d14926dedf3b5cfa246 /src/core/common
parentc2b9597c49abeef3f333b1bf7221a51019d53668 (diff)
Added seekPeekCursor function to CharReader, improved how seeking is handled by adding seekCursor method to Buffer
Diffstat (limited to 'src/core/common')
-rw-r--r--src/core/common/CharReader.cpp32
-rw-r--r--src/core/common/CharReader.hpp20
2 files changed, 44 insertions, 8 deletions
diff --git a/src/core/common/CharReader.cpp b/src/core/common/CharReader.cpp
index edcaf76..5b9b1d4 100644
--- a/src/core/common/CharReader.cpp
+++ b/src/core/common/CharReader.cpp
@@ -329,6 +329,19 @@ ssize_t Buffer::moveCursor(CursorId cursor, ssize_t relativeOffs)
}
}
+size_t Buffer::seekCursor(CursorId cursor, size_t offs)
+{
+ // Fetch the current offset
+ const ssize_t currentOffs = offset(cursor);
+ const ssize_t relativeOffs = offs - currentOffs;
+
+ // Perform the actual seeking, move the peek cursor to the read cursor
+ const ssize_t reachedOffs = currentOffs + moveCursor(cursor, relativeOffs);
+
+ // Clamp to values larger or equal to zero
+ return reachedOffs < 0 ? 0 : reachedOffs;
+}
+
bool Buffer::atEnd(Buffer::CursorId cursor) const
{
const Cursor &c = cursors[cursor];
@@ -504,17 +517,17 @@ size_t CharReader::readRaw(char *buf, size_t size)
size_t CharReader::seek(size_t requestedOffset)
{
- // Fetch the current offset
- const ssize_t currentOffs = getOffset();
- const ssize_t relativeOffs = requestedOffset - currentOffs;
-
- // Perform the actual seeking, move the peek cursor to the read cursor
- const ssize_t reachedOffs = currentOffs + buffer->moveCursor(readCursor, relativeOffs);
+ const size_t res = buffer->seekCursor(readCursor, requestedOffset);
buffer->copyCursor(readCursor, peekCursor);
coherent = true;
+ return res;
+}
- // Clamp to values larger or equal to zero
- return reachedOffs < 0 ? 0 : reachedOffs;
+size_t CharReader::seekPeekCursor(size_t requestedOffset)
+{
+ const size_t res = buffer->seekCursor(peekCursor, requestedOffset);
+ coherent = (res == getOffset());
+ return res;
}
bool CharReader::atEnd() const { return buffer->atEnd(readCursor); }
@@ -526,6 +539,9 @@ size_t CharReader::getOffset() const
size_t CharReader::getPeekOffset() const
{
+ if (coherent) {
+ return getOffset();
+ }
return buffer->offset(peekCursor) + offs;
}
diff --git a/src/core/common/CharReader.hpp b/src/core/common/CharReader.hpp
index cbd7b74..cbfeaf2 100644
--- a/src/core/common/CharReader.hpp
+++ b/src/core/common/CharReader.hpp
@@ -302,6 +302,15 @@ public:
ssize_t moveCursor(CursorId cursor, ssize_t relativeOffs);
/**
+ * Moves the cursor to the given position.
+ *
+ * @param cursor is the cursor that should be moved.
+ * @param offs is the offset to which the cursor should be moved.
+ * @return the actual location that was reached.
+ */
+ size_t seekCursor(CursorId cursor, size_t offs);
+
+ /**
* Returns the current byte offset of the given cursor relative to the
* beginning of the stream.
*
@@ -533,6 +542,17 @@ public:
size_t seek(size_t requestedOffset);
/**
+ * Moves the peek cursor to the requested offset. Returns the offse that wa
+ * actually reached.
+ *
+ * @param requestedOffset is the requested offset. This offset may no longer
+ * be reachable by the CharReader.
+ * @return the actually reached offset. The operation was successful, if
+ * the requested and reached offset are equal.
+ */
+ size_t seekPeekCursor(size_t requestedOffset);
+
+ /**
* Returns true if there are no more characters as the stream was closed.
*
* @return true if there is no more data.