Debian Codegraph @

plugins/dm.gui/XData.h

raw
1 #pragma once
2
3 #include <string>
4 #include <vector>
5 #include <iostream>
6 #include "i18n.h"
7 #include <memory>
8
9 #include "parser/DefTokeniser.h"
10
11 namespace XData
12 {
13
14 namespace
15 {
16 // All vectors of XData-objects are initialized with this size so that no sorting is necessary, which
17 // would otherwise be necessary when e.g. page2_body was defined before page1_body and a simple
18 // vector.push_back(..) was used to store the data instead of a direct access using an Index.
19 const std::size_t MAX_PAGE_COUNT = 20;
20
21 const char* DEFAULT_TWOSIDED_GUI = "guis/readables/books/book_calig_mac_humaine.gui";
22 const char* DEFAULT_ONESIDED_GUI = "guis/readables/sheets/sheet_paper_hand_nancy.gui";
23 const char* DEFAULT_SNDPAGETURN = "readable_page_turn";
24 }
25
26 typedef std::vector<std::string> StringList;
27
28 enum FileStatus
29 {
30 FileExists,
31 DefinitionExists,
32 MultipleDefinitions,
33 DefinitionMismatch,
34 MergeFailed,
35 OpenFailed,
36 AllOk
37 };
38 enum ExporterCommand
39 {
40 Normal,
41 Merge,
42 MergeOverwriteExisting,
43 Overwrite,
44 OverwriteMultDef
45 };
46 enum Side
47 {
48 Left,
49 Right
50 };
51 enum ContentType
52 {
53 Title,
54 Body
55 };
56 enum PageLayout
57 {
58 TwoSided,
59 OneSided
60 };
61
62 class XData;
63 typedef std::shared_ptr<XData> XDataPtr;
64
65 ///////////////////////////// XData:
66 // XData base-class for exporting, storing and managing XData.
67 class XData
68 {
69 public:
70 //Methods:
71 // Returns the PageLayout of the object: TwoSided or OneSided
72 virtual const PageLayout getPageLayout() const = 0;
73
74 // Exports the XData class formated properly into the File specified in
75 // Filename (absolute Filepath). If the file already exists this function can overwrite the
76 // file or merge. If the file cannot be opened, OpenFailed is returned.
77 // -Merge: If the definition already exists, DefinitionExists is returned (can also return OpenFailed).
78 // This definition can be overwritten using the command MergeOverWriteExisting AFTER using Merge-cmd.
79 // Otherwise the definition is appended. MergeOverwriteExisting can fail and returns MergeFailed or OpenFailed.
80 // -Overwrite: Returns DefinitionMismatch if the definition in the target-file does not match
81 // the name of the current definition or returns MultipleDefinitions. If a DefinitionMatch
82 // is the case, the file is overwritten. Use the command OverwriteMultDef to overwrite the
83 // file no matter what. With both commands OpenFailed can be returned.
84 // If the target-file has Syntax errors, it is overwritten...
85 FileStatus xport(const std::string& filename, ExporterCommand cmd);
86
87 // Creates a OneSided XData object of an TwoSided XData object and vice versa without discarding any actual page
88 // contents. numPages is adjusted accordingly and default guiPage-statements are applied. The result is stored
89 // in target.
90 virtual void togglePageLayout(XDataPtr& target) const = 0;
91
92 virtual ~XData() {};
93
94 //Getters and Setters for Attributes:
95
96 // The page-contents of the Two- and OneSided XData-objects. cc defines whether Title or Body shall be accessed.
97 // The Side-parameter is discarded on OneSidedXData-objects. Throws std::runtime_error when index out of bounds.
98 virtual const std::string& getPageContent(ContentType cc, std::size_t pageIndex, Side side) const = 0;
99 virtual void setPageContent(ContentType cc, std::size_t pageIndex, Side side, const std::string& content) = 0;
100
101 // The name of the XData-Definition
102 const std::string& getName() const { return _name; }
103 void setName(const std::string& name) { _name = name; }
104
105 // numPages-statement. Resizes vectors accordingly. Attention: If numPages is lowered, data will obviously be discarded.
106 const std::size_t getNumPages() const { return _numPages; }
107 void setNumPages(std::size_t numPages)
108 {
109 _numPages = numPages;
110 resizeVectors(numPages);
111 }
112
113 // guiPage-statement. Methods for accessing a whole StringList or single VectorElements.
114 const StringList& getGuiPage() const { return _guiPage; }
115 const std::string& getGuiPage(std::size_t index) const
116 {
117 if (index >= _numPages)
118 throw std::runtime_error(_("GUI Page Index out of bounds."));
119 return _guiPage[index];
120 }
121 void setGuiPage(const StringList& guiPage) { _guiPage = guiPage; }
122 void setGuiPage(const std::string& guiPage, std::size_t index)
123 {
124 if (index >= _numPages)
125 throw std::runtime_error(_("GUI Page Index out of bounds."));
126 _guiPage[index] = guiPage;
127 }
128
129 // sndPageTurn-statement.
130 const std::string& getSndPageTurn() const{ return _sndPageTurn; }
131 void setSndPageTurn(const std::string& sndPageTurn) { _sndPageTurn = sndPageTurn; }
132
133 private:
134 //Methods for export:
135
136 // Generates the XData-Definition for the XData-object.
137 const std::string generateXDataDef() const;
138
139 // Returns the length of the current definition. The get-pointer has to be at the beginning of that definition. Returns 0 on Syntax errors.
140 const std::size_t getDefLength(const std::string& def) const;
141
142 // Returns the definition-name found in the file or "" if multiple definitions where found.
143 // Used by the xport()-method in the overwrite-command for checking for a DefinitionMatch or MultipleDefinitions.
144 const std::string getDefinitionNameFromXD(std::ifstream& file) const;
145
146 // Used to jump out of a definition. Can lead to undefined behavior on Syntax-errors.
147 void jumpOutOfBrackets(parser::DefTokeniser& tok, int currentDepth) const;
148
149 protected:
150 // Resizes all vectors to TargetSize.
151 virtual void resizeVectors(std::size_t targetSize);
152
153 // Returns the OneSided or TwoSided Page-content-definition of an XData object.
154 virtual const std::string getContentDef() const = 0;
155
156 // Generates the XData-compatible definition for Multi-line strings and adds escape-characters before quotes.
157 const std::string generateTextDef(const std::string& rawString) const;
158
159 //Attributes:
160 std::string _name; //name of the XData-Definition
161 std::size_t _numPages; //numPages-statement
162 StringList _guiPage; //guiPage-statements
163 std::string _sndPageTurn; //sndPageTurn-statement
164 std::size_t _definitionStart; //Marks the start of a definition in an .xd-File. Used by the xport()-method.
165 };
166
167
168 class OneSidedXData : public XData
169 {
170 private:
171 //page contents
172 StringList _pageTitle;
173 StringList _pageBody;
174 //end of page contents
175 const std::string getContentDef() const;
176 void resizeVectors(std::size_t targetSize);
177 public:
178
179 void setPageContent(ContentType cc, std::size_t pageIndex, Side side, const std::string& content);
180
181 const std::string& getPageContent(ContentType cc, std::size_t pageIndex, Side side) const;
182
183 const PageLayout getPageLayout() const { return OneSided; }
184
185 void togglePageLayout(XDataPtr& target) const;
186
187 OneSidedXData(const std::string& name)
188 {
189 _name = name;
190 setNumPages(MAX_PAGE_COUNT);
191 }
192 ~OneSidedXData()
193 {
194 _guiPage.clear();
195 _pageTitle.clear();
196 _pageBody.clear();
197 }
198 };
199 typedef std::shared_ptr<OneSidedXData> OneSidedXDataPtr;
200
201 class TwoSidedXData : public XData
202 {
203 private:
204 //page contents:
205 StringList _pageLeftTitle;
206 StringList _pageRightTitle;
207 StringList _pageLeftBody;
208 StringList _pageRightBody;
209 //end of page contents
210 const std::string getContentDef() const;
211 void resizeVectors(std::size_t targetSize);
212 public:
213
214 void setPageContent(ContentType cc, std::size_t pageIndex, Side side, const std::string& content);
215
216 const std::string& getPageContent(ContentType cc, std::size_t pageIndex, Side side) const;
217
218 const PageLayout getPageLayout() const { return TwoSided; }
219
220 void togglePageLayout(XDataPtr& target) const;
221
222 TwoSidedXData(const std::string& name)
223 {
224 _name=name;
225 setNumPages(MAX_PAGE_COUNT);
226 }
227 ~TwoSidedXData()
228 {
229 _pageLeftTitle.clear();
230 _pageLeftBody.clear();
231 _pageRightBody.clear();
232 _pageRightTitle.clear();
233 _guiPage.clear();
234 }
235 };
236 typedef std::shared_ptr<TwoSidedXData> TwoSidedXDataPtr;
237
238 } //namespace XData