diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-01 15:10:13 +0100 |
---|---|---|
committer | Andreas Stöckel <andreas@somweyr.de> | 2015-01-01 15:10:13 +0100 |
commit | b36c695f216588b444ba511fbe1733fb112ab3d9 (patch) | |
tree | 0a202d736cdcb0495c8cb91ea191cfb1ff9a5b4d /src/core/common/CharReader.cpp | |
parent | 386daea692688c7dd2bf021225c407ad4ded3133 (diff) |
Added new functions to get the context at a certain byte offset
Diffstat (limited to 'src/core/common/CharReader.cpp')
-rw-r--r-- | src/core/common/CharReader.cpp | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/src/core/common/CharReader.cpp b/src/core/common/CharReader.cpp index 1a52f53..db9bb2e 100644 --- a/src/core/common/CharReader.cpp +++ b/src/core/common/CharReader.cpp @@ -516,10 +516,11 @@ CharReaderFork CharReader::fork() return CharReaderFork(buffer, readCursor, peekCursor, coherent); } -TextCursor::Context CharReader::getContext(ssize_t maxSize) +SourceContext CharReader::getContextAt(ssize_t maxSize, + Buffer::CursorId referenceCursor) { - // Clone the current read cursor - Buffer::CursorId cur = buffer->createCursor(readCursor.cursor); + // Clone the given read cursor + Buffer::CursorId cur = buffer->createCursor(referenceCursor); // Fetch the start position of the search ssize_t offs = buffer->offset(cur); @@ -612,8 +613,37 @@ TextCursor::Context CharReader::getContext(ssize_t maxSize) // Delete the newly created cursor buffer->deleteCursor(cur); - return TextCursor::Context{ss.str(), relPos, !foundBegin || tStart != start, - !foundEnd || tEnd != end}; + return SourceContext{ss.str(), relPos, !foundBegin || tStart != start, + !foundEnd || tEnd != end}; +} + +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; + + // Try to move the cursor to the specified position and read the context + SourceContext res; + if (buffer->moveCursor(cur, moveOffs) == moveOffs) { + res = getContextAt(60, cur); + } + + // Delete the read cursor + buffer->deleteCursor(cur); + return res; +} + +SourceContext CharReader::getContext(ssize_t maxSize) +{ + return getContextAt(maxSize, readCursor.cursor); +} + +SourceContext CharReader::contextCallback(const SourceLocation &location, + void *data) +{ + return static_cast<CharReader *>(data)->getContextAtOffs(60, location.offs); } /* Class CharReaderFork */ |