| 1 | /* |
| 2 | * LegacyClonk |
| 3 | * |
| 4 | * Copyright (c) RedWolf Design |
| 5 | * Copyright (c) 2001, Sven2 |
| 6 | * Copyright (c) 2017-2020, The LegacyClonk Team and contributors |
| 7 | * |
| 8 | * Distributed under the terms of the ISC license; see accompanying file |
| 9 | * "COPYING" for details. |
| 10 | * |
| 11 | * "Clonk" is a registered trademark of Matthes Bender, used with permission. |
| 12 | * See accompanying file "TRADEMARK" for details. |
| 13 | * |
| 14 | * To redistribute this file separately, substitute the full license texts |
| 15 | * for the above references. |
| 16 | */ |
| 17 | |
| 18 | // a buffer holding a log history |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #include "C4ForwardDeclarations.h" |
| 23 | |
| 24 | #include <cstddef> |
| 25 | #include <cstdint> |
| 26 | |
| 27 | // circular log buffer to holding line-wise log data |
| 28 | class C4LogBuffer |
| 29 | { |
| 30 | private: |
| 31 | struct LineData |
| 32 | { |
| 33 | CStdFont *pFont; // line font |
| 34 | uint32_t dwClr; // line clr |
| 35 | bool fNewParagraph; // if set, this line marks a new paragraph (and is not the wrapped line of a previous par) |
| 36 | }; |
| 37 | |
| 38 | char *szBuf; // string buffer |
| 39 | LineData *pLineDataBuf; // line data buffer |
| 40 | size_t iBufSize; // size of string buffer |
| 41 | int iFirstLinePos, iAfterLastLinePos; // current string buffer positions |
| 42 | int iLineDataPos, iNextLineDataPos; // current line data buffer positions |
| 43 | size_t iMaxLineCount; // max number of valid lines - size of line data buffer |
| 44 | size_t iLineCount; // number of valid lines in buffer |
| 45 | int iLineBreakWidth; // line breaking width |
| 46 | char *szIndent; // chars inserted as indent space |
| 47 | bool fDynamicGrow; // if true, lines are always added to the buffer. If false, the buffer is used circular and old lines removed |
| 48 | bool fMarkup; // if set, '|' is treated as linebreak |
| 49 | |
| 50 | void GrowLineCountBuffer(size_t iGrowBy); |
| 51 | void GrowTextBuffer(size_t iGrowBy); |
| 52 | void DiscardFirstLine(); // discard oldest line in buffer |
| 53 | void AppendSingleLine(const char *szLine, size_t iLineLength, const char *szIndent, CStdFont *pFont, uint32_t dwClr, bool fNewParagraph); // append given string as single line |
| 54 | |
| 55 | public: |
| 56 | C4LogBuffer(size_t iSize, size_t iMaxLines, int iLBWidth, const char *szIndentChars = " " , bool fDynamicGrow = false, bool fMarkup = true); |
| 57 | ~C4LogBuffer(); |
| 58 | |
| 59 | void AppendLines(const char *szLine, CStdFont *pFont, uint32_t dwClr, CStdFont *pFirstLineFont = nullptr); // append message line to buffer; overwriting old lines if necessary |
| 60 | const char *GetLine(int iLineIndex, CStdFont **ppFont, uint32_t *pdwClr, bool *pNewParagraph) const; // get indexed line - negative indices -n return last-n'th-line |
| 61 | void Clear(); // clear all lines |
| 62 | |
| 63 | void SetLBWidth(int iToWidth); |
| 64 | }; |
| 65 | |