| 1 | /* |
| 2 | * LegacyClonk |
| 3 | * |
| 4 | * Copyright (c) RedWolf Design |
| 5 | * Copyright (c) 2005, Sven2 |
| 6 | * Copyright (c) 2017-2022, 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 | // RTF file parsing functionality |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #include "StdBuf.h" |
| 23 | |
| 24 | class C4RTFFile |
| 25 | { |
| 26 | public: |
| 27 | struct KeywordTableEntry |
| 28 | { |
| 29 | const char *szKeyword; |
| 30 | int iDefaultParam; |
| 31 | bool fForceDefaultParam; |
| 32 | enum { kwdChars, kwdDest, kwdProp, kwdSpec } eType; |
| 33 | const char *szChars; |
| 34 | int idx; // index into property table |
| 35 | }; |
| 36 | |
| 37 | struct CharProps {}; |
| 38 | struct ParagraphProps {}; |
| 39 | struct SectionProps {}; |
| 40 | struct DocumentProps {}; |
| 41 | |
| 42 | enum DestState { dsNormal, dsSkip, }; |
| 43 | enum ParserState { psNormal, psBinary, psHex, }; |
| 44 | enum SpecialKeyword { specBin, specHex, specSkipDest, }; |
| 45 | |
| 46 | // RTF parser state; states may be nested in definition blocks |
| 47 | struct PropertyState |
| 48 | { |
| 49 | PropertyState *pNext; // linked list |
| 50 | CharProps cp; |
| 51 | ParagraphProps pp; |
| 52 | SectionProps sp; |
| 53 | DocumentProps dp; |
| 54 | DestState dest; |
| 55 | ParserState eState; |
| 56 | char bHex; // used by hex parser |
| 57 | int iHexBinCnt; // used by hex and binary parser |
| 58 | |
| 59 | PropertyState() : pNext(nullptr), cp(), pp(), sp(), dp(), dest(dsNormal), eState(psNormal), bHex(0), iHexBinCnt(0) {} |
| 60 | }; |
| 61 | |
| 62 | class ParserError |
| 63 | { |
| 64 | public: |
| 65 | StdStrBuf ErrorText; |
| 66 | ParserError(const char *szErr) { ErrorText.Copy(pnData: szErr); } |
| 67 | }; |
| 68 | |
| 69 | private: |
| 70 | StdBuf sRTF; // rtf formatted text |
| 71 | |
| 72 | PropertyState *pState; |
| 73 | bool fSkipDestIfUnknownKeyword; |
| 74 | |
| 75 | public: |
| 76 | C4RTFFile(); |
| 77 | ~C4RTFFile(); |
| 78 | |
| 79 | private: |
| 80 | void ClearState(); |
| 81 | void EndGroupAction() {} |
| 82 | void AssertNoEOF(size_t iPos); |
| 83 | void ApplyPropChange(int iProp, int iParam) {} |
| 84 | void ChangeDest(StdStrBuf &sResult, int iDest); |
| 85 | void SpecialKeyword(StdStrBuf &sResult, int iKeyw, int iParam); |
| 86 | void TranslateKeyword(StdStrBuf &sResult, const char *szKeyword, int iParam, bool fHasIntParam); |
| 87 | void ParseKeyword(StdStrBuf &sResult, size_t &iPos); |
| 88 | void ParseChars(StdStrBuf &sResult, const char *szChars); |
| 89 | void ParseChar(StdStrBuf &sResult, char c); |
| 90 | void ParseHexChar(StdStrBuf &sResult, char c); |
| 91 | void PushState(); |
| 92 | void PopState(); |
| 93 | |
| 94 | public: |
| 95 | void Load(const StdBuf &sContents) // load RTF text from file |
| 96 | { |
| 97 | sRTF.Copy(Buf2: sContents); |
| 98 | } |
| 99 | |
| 100 | StdStrBuf GetPlainText(); // convert to plain text |
| 101 | |
| 102 | }; |
| 103 | |
| 104 | std::string RtfEscape(std::string_view plainText); |
| 105 | |