diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/common/CharReader.cpp | 40 | ||||
-rw-r--r-- | src/core/common/CharReader.hpp | 56 |
2 files changed, 81 insertions, 15 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 */ diff --git a/src/core/common/CharReader.hpp b/src/core/common/CharReader.hpp index 7be5e08..fd3186c 100644 --- a/src/core/common/CharReader.hpp +++ b/src/core/common/CharReader.hpp @@ -32,7 +32,7 @@ #include <memory> #include <vector> -#include "TextCursor.hpp" +#include "Location.hpp" namespace ousia { @@ -375,20 +375,21 @@ protected: /** * Current line the cursor is in. */ - TextCursor::PosType line; + int line; /** * Current column the cursor is in. */ - TextCursor::PosType column; + int column; /** * Constructor of the Cursor class. * * @param cursor is the underlying cursor in the Buffer instance. + * @param line is the line at which the cursor is positioned. + * @param column is the column at which the cursor is positioned. */ - Cursor(Buffer::CursorId cursor, TextCursor::PosType line, - TextCursor::PosType column) + Cursor(Buffer::CursorId cursor, int line, int column) : cursor(cursor), line(line), column(column) { } @@ -423,6 +424,28 @@ private: */ bool readAtCursor(Cursor &cursor, char &c); + /** + * Returns the line the given cursor currently is in, but at most the + * given number of characters in the form of a Context structure. + * + * @param maxSize is the maximum length of the extracted context + * @param referenceCursor is a cursor in the internal buffer pointing at the + * location at which the context should be read. + */ + SourceContext getContextAt(ssize_t maxSize, + Buffer::CursorId referenceCursor); + + /** + * Returns the line the at the given byte offset, but at most the + * given number of characters in the form of a Context structure. + * + * @param maxSize is the maximum length of the extracted context + * @param offs is the byte offset for which the context should be read. + * @return the context at the specified position or an empty (invalid) + * context if the context could not be read. + */ + SourceContext getContextAtOffs(ssize_t maxSize, size_t offs); + protected: /** * Reference pointing at the underlying buffer. @@ -555,19 +578,19 @@ public: /** * Returns the line number the read cursor currently is at. */ - TextCursor::PosType getLine() const { return readCursor.line; } + int getLine() const { return readCursor.line; } /** * Returns the column the read cursor currently is at. */ - TextCursor::PosType getColumn() const { return readCursor.column; } + int getColumn() const { return readCursor.column; } /** * Returns the current position of the read cursor (line and column). */ - TextCursor::Position getPosition() const + SourceLocation getLocation() const { - return TextCursor::Position(getLine(), getColumn(), getOffset()); + return SourceLocation(getLine(), getColumn(), getOffset()); } /** @@ -576,7 +599,20 @@ public: * * @param maxSize is the maximum length of the extracted context */ - TextCursor::Context getContext(ssize_t maxSize = 60); + SourceContext getContext(ssize_t maxSize = 60); + + /** + * Function that can be used to provide the context for a certain source + * location. A pointer to this function can be supplied to a Logger instance + * in the pushFile() method. The data should be set to a pointer to the + * CharReader instance. + * + * @param location is the location for which the context should be returned. + * Only the "offs" field within the location is used. + * @param data is a pointer pointing at a CharReader instance. + */ + static SourceContext contextCallback(const SourceLocation &location, + void *data); }; /** |