OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4LogBuffer Class Reference

#include <C4LogBuf.h>

Public Member Functions

 C4LogBuffer (int iSize, int iMaxLines, int iLBWidth, const char *szIndentChars=" ", bool fDynamicGrow=false, bool fMarkup=true)
 
 ~C4LogBuffer ()
 
void AppendLines (const char *szLine, CStdFont *pFont, DWORD dwClr, CStdFont *pFirstLineFont=nullptr)
 
const char * GetLine (int iLineIndex, CStdFont **ppFont, DWORD *pdwClr, bool *pNewParagraph) const
 
void Clear ()
 
int GetCount () const
 
void SetLBWidth (int iToWidth)
 

Detailed Description

Definition at line 22 of file C4LogBuf.h.

Constructor & Destructor Documentation

C4LogBuffer::C4LogBuffer ( int  iSize,
int  iMaxLines,
int  iLBWidth,
const char *  szIndentChars = "    ",
bool  fDynamicGrow = false,
bool  fMarkup = true 
)

Definition at line 23 of file C4LogBuf.cpp.

24  : iBufSize(iSize), iFirstLinePos(0), iAfterLastLinePos(0), iLineDataPos(0),
25  iNextLineDataPos(0), iMaxLineCount(iMaxLines), iLineCount(0), iLineBreakWidth(iLBWidth), fDynamicGrow(fDynamicGrow), fMarkup(fMarkup)
26 {
27  // copy indent
28  if (szIndentChars && *szIndentChars)
29  {
30  szIndent = new char[strlen(szIndentChars)+1];
31  strcpy(szIndent, szIndentChars);
32  }
33  else szIndent = nullptr;
34  // create buffers, if buffer size is given. Otherwise, create/grow them dynamically
35  if (iBufSize) szBuf = new char[iBufSize]; else szBuf=nullptr;
36  if (iMaxLineCount) pLineDataBuf = new LineData[iMaxLineCount]; else pLineDataBuf=nullptr;
37  assert(fDynamicGrow || (iBufSize && iMaxLineCount));
38 }
int iSize
Definition: TstC4NetIO.cpp:32
C4LogBuffer::~C4LogBuffer ( )

Definition at line 40 of file C4LogBuf.cpp.

41 {
42  // free buffers
43  delete [] pLineDataBuf;
44  delete [] szBuf;
45  // free indent
46  if (szIndent) delete [] szIndent;
47 }

Member Function Documentation

void C4LogBuffer::AppendLines ( const char *  szLine,
CStdFont pFont,
DWORD  dwClr,
CStdFont pFirstLineFont = nullptr 
)

Definition at line 170 of file C4LogBuf.cpp.

References CStdFont::GetMessageBreak(), and CStdFont::GetTextExtent().

Referenced by C4GUI::MultilineLabel::AddLine(), and C4MessageBoard::AddLog().

171 {
172  char LineBreakChars [] = { 0x0D, 0x0A, '|' };
173  int32_t iLineBreakCharCount = 2 + fMarkup;
174  // safety
175  if (!szLine) return;
176  // split '|'/CR/LF-separations first, if there are any
177  bool fAnyLineBreakChar = false;
178  for (int i = 0; i < iLineBreakCharCount; ++i)
179  if (strchr(szLine, LineBreakChars[i]))
180  {
181  fAnyLineBreakChar = true;
182  break;
183  }
184  if (fAnyLineBreakChar)
185  {
186  char *szBuf = new char[strlen(szLine)+1];
187  char *szBufPos, *szPos2 = szBuf, *szBufFind;
188  strcpy(szBuf, szLine);
189  while ((szBufPos = szPos2))
190  {
191  // find first occurance of any line break char
192  szPos2 = nullptr;
193  for (int i = 0; i < iLineBreakCharCount; ++i)
194  if ((szBufFind = strchr(szBufPos, LineBreakChars[i])))
195  if (!szPos2 || szBufFind < szPos2)
196  szPos2 = szBufFind;
197  // split string at linebreak char
198  if (szPos2) *szPos2++ = '\0';
199  // output current line if not empty
200  if (!*szBufPos) continue;
201  // first line in caption font
202  if (pFirstLineFont)
203  {
204  AppendLines(szBufPos, pFirstLineFont, dwClr);
205  pFirstLineFont = nullptr;
206  }
207  else
208  AppendLines(szBufPos, pFont, dwClr);
209  }
210  delete [] szBuf;
211  return;
212  }
213  // no line breaks desired: Output all in one line
214  if (!iLineBreakWidth || !pFont)
215  {
216  AppendSingleLine(szLine, strlen(szLine), nullptr, pFont, dwClr, true);
217  }
218  else
219  {
220  // output broken lines until there are any
221  int iLineIndex = 0;
222  while (*szLine)
223  {
224  // get line width of this line
225  int iBreakWdt = iLineBreakWidth;
226  if (iLineIndex && szIndent)
227  {
228  int32_t iIndentWdt, Q;
229  pFont->GetTextExtent(szIndent, iIndentWdt, Q, true);
230  iBreakWdt -= iIndentWdt;
231  }
232  // get number of characters printable into this line
233  const char *szNextLine;
234  int iNumChars = pFont->GetMessageBreak(szLine, &szNextLine, iBreakWdt);
235  // add them
236  AppendSingleLine(szLine, iNumChars, iLineIndex ? szIndent : nullptr, pFont, dwClr, !iLineIndex);
237  // next line
238  szLine = szNextLine;
239  ++iLineIndex;
240  }
241  }
242 }
int GetMessageBreak(const char *szMsg, const char **ppNewPos, int iBreakWidth, float fZoom=1.0f)
void AppendLines(const char *szLine, CStdFont *pFont, DWORD dwClr, CStdFont *pFirstLineFont=nullptr)
Definition: C4LogBuf.cpp:170
bool GetTextExtent(const char *szText, int32_t &rsx, int32_t &rsy, bool fCheckMarkup=true)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4LogBuffer::Clear ( )

Definition at line 272 of file C4LogBuf.cpp.

Referenced by C4GUI::MultilineLabel::Clear(), C4MessageBoard::ClearLog(), and C4MessageBoard::~C4MessageBoard().

273 {
274  // clear buffer usage
275  iFirstLinePos = iAfterLastLinePos = iLineCount = iNextLineDataPos = iLineDataPos = 0;
276 }

Here is the caller graph for this function:

int C4LogBuffer::GetCount ( ) const
inline

Definition at line 57 of file C4LogBuf.h.

57 { return iLineCount; }// retrieve number of valid lines in buffer
const char * C4LogBuffer::GetLine ( int  iLineIndex,
CStdFont **  ppFont,
DWORD pdwClr,
bool *  pNewParagraph 
) const

Definition at line 244 of file C4LogBuf.cpp.

Referenced by C4LoaderScreen::Draw(), C4MessageBoard::Draw(), C4GUI::MultilineLabel::DrawElement(), C4MessageBoard::Execute(), and C4GUI::MultilineLabel::UpdateHeight().

245 {
246  // evaluate negative indices
247  if (iLineIndex < 0)
248  {
249  iLineIndex += iLineCount;
250  if (iLineIndex < 0) return nullptr;
251  }
252  // range check
253  if (iLineIndex >= iLineCount) return nullptr;
254  // assign data
255  LineData &rData = pLineDataBuf[(iLineDataPos + iLineIndex) % iMaxLineCount];
256  if (ppFont) *ppFont = rData.pFont;
257  if (pdwClr) *pdwClr = rData.dwClr;
258  if (pfNewPar) *pfNewPar = rData.fNewParagraph;
259  // advance in lines until desired line is found
260  char *szResult = szBuf + iFirstLinePos;
261  while (iLineIndex--)
262  {
263  // skip this line
264  while (*szResult++) ;
265  // double delimeter or end of buffer: reset searching to front of buffer
266  if (szResult == (szBuf+iBufSize) || !*szResult) szResult = szBuf;
267  }
268  // return found buffer pos
269  return szResult;
270 }

Here is the caller graph for this function:

void C4LogBuffer::SetLBWidth ( int  iToWidth)

Definition at line 278 of file C4LogBuf.cpp.

Referenced by C4MessageBoard::Init(), C4GUI::MultilineLabel::UpdateSize(), and C4MessageBoard::~C4MessageBoard().

279 {
280  iLineBreakWidth = iToWidth;
281 }

Here is the caller graph for this function:


The documentation for this class was generated from the following files: