OpenClonk
C4Markup Class Reference

#include <C4Markup.h>

Public Member Functions

 C4Markup (bool fDoClr)
 
 ~C4Markup ()
 
bool Read (const char **ppText, bool fSkip=false)
 
bool SkipTags (const char **ppText)
 
void Apply (C4BltTransform &rBltTrf, DWORD &dwClr)
 
bool Clean ()
 
std::string ClosingTags () const
 
std::string OpeningTags () const
 

Static Public Member Functions

static bool StripMarkup (char *szText)
 
static bool StripMarkup (StdStrBuf *sText)
 

Detailed Description

Definition at line 60 of file C4Markup.h.

Constructor & Destructor Documentation

◆ C4Markup()

C4Markup::C4Markup ( bool  fDoClr)
inline

Definition at line 71 of file C4Markup.h.

71 { pTags=pLast=nullptr; this->fDoClr=fDoClr; }; // ctor

◆ ~C4Markup()

C4Markup::~C4Markup ( )
inline

Definition at line 72 of file C4Markup.h.

References C4MarkupTag::pNext.

73  { C4MarkupTag *pTag=pTags,*pNext; while (pTag) { pNext=pTag->pNext; delete pTag; pTag=pNext; } }
C4MarkupTag * pNext
Definition: C4Markup.h:25

Member Function Documentation

◆ Apply()

void C4Markup::Apply ( C4BltTransform rBltTrf,
DWORD dwClr 
)
inline

Definition at line 77 of file C4Markup.h.

References C4MarkupTag::Apply(), and C4MarkupTag::pNext.

Referenced by CStdFont::DrawText().

78  { for (C4MarkupTag *pTag=pTags; pTag; pTag=pTag->pNext) pTag->Apply(rBltTrf, fDoClr, dwClr); }
C4MarkupTag * pNext
Definition: C4Markup.h:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Clean()

bool C4Markup::Clean ( )
inline

Definition at line 79 of file C4Markup.h.

Referenced by CStdFont::DrawText().

79 { return !pTags; } // empty?
Here is the caller graph for this function:

◆ ClosingTags()

std::string C4Markup::ClosingTags ( ) const

Definition at line 132 of file C4Markup.cpp.

Referenced by C4LogBuffer::AppendLines().

133 {
134  std::string result;
135  for (auto tag = pLast; tag; tag = tag->pPrev)
136  result += tag->ClosingTag();
137  return result;
138 }
Here is the caller graph for this function:

◆ OpeningTags()

std::string C4Markup::OpeningTags ( ) const

Definition at line 140 of file C4Markup.cpp.

Referenced by C4LogBuffer::AppendLines().

141 {
142  std::string result;
143  for (auto tag = pTags; tag; tag = tag->pNext)
144  result += tag->OpeningTag();
145  return result;
146 }
Here is the caller graph for this function:

◆ Read()

bool C4Markup::Read ( const char **  ppText,
bool  fSkip = false 
)

Definition at line 52 of file C4Markup.cpp.

References b, SCharPos(), SCopyEnclosed(), SEqual(), and SLen().

Referenced by C4LogBuffer::AppendLines(), and CStdFont::DrawText().

53 {
54  char Tag[50]; C4MarkupTag *pNewTag=nullptr; int iTagLen,iParLen;
55  // get tag
56  if (!SCopyEnclosed(*ppText, '<', '>', Tag, 49)) return false;
57  iTagLen=SLen(Tag);
58  // split tag to name and pars
59  char *szPars=nullptr; int iSPos;
60  if ((iSPos=SCharPos(' ', Tag))>-1)
61  {
62  Tag[iSPos]=0;
63  szPars=Tag+iSPos+1;
64  }
65  // closing tag?
66  if (Tag[0]=='/')
67  {
68  // no parameters
69  if (szPars) return false;
70  if (!fSkip)
71  {
72  // is this the tag to be closed?
73  if (!pLast) return false;
74  if (!SEqual(pLast->TagName(), Tag+1)) return false;
75  // close it
76  delete Pop();
77  }
78  }
79  // italic
80  else if (SEqual(Tag, "i"))
81  {
82  // no parameters
83  if (szPars) return false;
84  // create italic tag
85  if (!fSkip) pNewTag=new C4MarkupTagItalic();
86  }
87  // colored
88  else if (SEqual(Tag, "c"))
89  {
90  // no parameters?
91  if (!szPars) return false;
92  if ((iParLen=SLen(szPars))>8) return false;
93  if (!fSkip)
94  {
95  // get color value by parameter
96  DWORD dwClr=0;
97  for (int i=0; i<iParLen; ++i)
98  {
99  BYTE b;
100  if (szPars[i]>='0' && szPars[i]<='9') b=szPars[i]-'0';
101  else if (szPars[i]>='a' && szPars[i]<='f') b=szPars[i]-'a'+10;
102  else return false;
103  dwClr|=(b<<((iParLen-i-1)*4));
104  }
105  // adjust alpha if not given
106  if (iParLen<=6) dwClr|=0xff000000;
107  // create color tag
108  pNewTag=new C4MarkupTagColor(dwClr);
109  }
110  }
111  // unknown tag
112  else return false;
113  // add created tag
114  if (pNewTag) Push(pNewTag);
115  // advance past tag
116  *ppText+=iTagLen+2;
117  // success
118  return true;
119 }
#define b
bool SCopyEnclosed(const char *szSource, char cOpen, char cClose, char *sTarget, int iSize)
Definition: Standard.cpp:523
uint8_t BYTE
size_t SLen(const char *sptr)
Definition: Standard.h:74
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
int SCharPos(char cTarget, const char *szInStr, int iIndex)
Definition: Standard.cpp:233
uint32_t DWORD
virtual const char * TagName() const =0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SkipTags()

bool C4Markup::SkipTags ( const char **  ppText)

Definition at line 123 of file C4Markup.cpp.

Referenced by CStdFont::BreakMessage(), C4MenuItem::DoTextProgress(), CStdFont::GetTextExtent(), LogSilent(), and StripMarkup().

124 {
125  // read tags as long as found
126  while (**ppText=='<') if (!Read(ppText, true)) break;
127  // return whether end is reached
128  return !**ppText;
129 }
bool Read(const char **ppText, bool fSkip=false)
Definition: C4Markup.cpp:52
Here is the caller graph for this function:

◆ StripMarkup() [1/2]

bool C4Markup::StripMarkup ( char *  szText)
static

Definition at line 148 of file C4Markup.cpp.

References SkipTags(), and SSearch().

Referenced by C4PlayerInfoCore::Load(), and C4InVal::ValidateString().

149 {
150  // skip any tags and inline-images
151  C4Markup mkup(false);
152  const char *szRead = szText, *szPos2;
153  do
154  {
155  mkup.SkipTags(&szRead);
156  if (szRead[0] == '{' && szRead[1] == '{' && szRead[2] != '{') // skip at {{{, because {{{id}} should be parsed as { {{id}} }.
157  {
158  if ((szPos2 = SSearch(szRead+2, "}}")))
159  // valid {{blub}}-tag
160  szRead = szPos2;
161  else
162  // invalid {{-tag
163  szRead += 2;
164  }
165  else if (szRead[0] == '}' && szRead[1] == '}')
166  // invalid }}-tag
167  szRead += 2;
168  }
169  while ((*szText++ = *szRead++));
170  return szText != szRead;
171 }
const char * SSearch(const char *szString, const char *szIndex)
Definition: Standard.cpp:363
Here is the call graph for this function:
Here is the caller graph for this function:

◆ StripMarkup() [2/2]

bool C4Markup::StripMarkup ( StdStrBuf sText)
static

Definition at line 173 of file C4Markup.cpp.

References StdStrBuf::GrabPointer(), and StdStrBuf::Take().

174 {
175  // strip any markup codes from given text buffer
176  char *buf = sText->GrabPointer();
177  if (!buf) return false;
178  bool fSuccess = StripMarkup(buf);
179  sText->Take(buf);
180  return fSuccess;
181 }
char * GrabPointer()
Definition: StdBuf.h:459
void Take(char *pnData)
Definition: StdBuf.h:457
static bool StripMarkup(char *szText)
Definition: C4Markup.cpp:148
Here is the call graph for this function:

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