summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/common/CharReader.cpp14
-rw-r--r--src/core/common/CharReader.hpp15
2 files changed, 25 insertions, 4 deletions
diff --git a/src/core/common/CharReader.cpp b/src/core/common/CharReader.cpp
index db9bb2e..6966b97 100644
--- a/src/core/common/CharReader.cpp
+++ b/src/core/common/CharReader.cpp
@@ -516,6 +516,18 @@ CharReaderFork CharReader::fork()
return CharReaderFork(buffer, readCursor, peekCursor, coherent);
}
+size_t CharReader::readRaw(char *buf, size_t size)
+{
+ // TODO: This is inefficient, implement ranged read in the Buffer class and
+ // use it
+ size_t res = 0;
+ while (res < size && read(*buf)) {
+ buf++;
+ res++;
+ }
+ return res;
+}
+
SourceContext CharReader::getContextAt(ssize_t maxSize,
Buffer::CursorId referenceCursor)
{
@@ -622,7 +634,7 @@ SourceContext CharReader::getContextAtOffs(ssize_t maxSize, size_t offs)
// Create a new cursor and calculate how far it has to be moved to reach
// the position specified in the location instance
Buffer::CursorId cur = buffer->createCursor();
- ssize_t moveOffs = buffer->offset(cur) - offs;
+ ssize_t moveOffs = offs - buffer->offset(cur);
// Try to move the cursor to the specified position and read the context
SourceContext res;
diff --git a/src/core/common/CharReader.hpp b/src/core/common/CharReader.hpp
index fd3186c..134d9d9 100644
--- a/src/core/common/CharReader.hpp
+++ b/src/core/common/CharReader.hpp
@@ -38,9 +38,8 @@ namespace ousia {
/**
* A chunked ring buffer used in CharReader to provide access to an input stream
- * with multiple read cursors. The Buffer automatically expands to the
- * size of the spanned by the read cursors while reusing already allocated
- * memory.
+ * with multiple read cursors. The Buffer automatically expands to the size of
+ * the spanned by the read cursors while reusing already allocated memory.
*/
class Buffer {
public:
@@ -563,6 +562,16 @@ public:
CharReaderFork fork();
/**
+ * Reads raw data from the CharReader without any processing. Data is always
+ * read from the read cursor.
+ *
+ * @param buf is the target memory buffer.
+ * @param size is the number of bytes to be read.
+ * @return the number of bytes read.
+ */
+ size_t readRaw(char *buf, size_t size);
+
+ /**
* Returns true if there are no more characters as the stream was
* closed.
*