Debian Codegraph @

src/lib/RagTime5ClusterManager.hxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #ifndef RAG_TIME_5_CLUSTER_MANAGER
35 # define RAG_TIME_5_CLUSTER_MANAGER
36
37 #include <map>
38 #include <ostream>
39 #include <sstream>
40 #include <string>
41 #include <vector>
42
43 #include "libmwaw_internal.hxx"
44 #include "MWAWDebug.hxx"
45 #include "MWAWEntry.hxx"
46
47 #include "RagTime5StructManager.hxx"
48
49 class RagTime5Document;
50 class RagTime5StructManager;
51
52 namespace RagTime5ClusterManagerInternal
53 {
54 struct State;
55 }
56
57 //! basic class used to manage RagTime 5/6 zones
58 class RagTime5ClusterManager
59 {
60 public:
61 struct Link;
62 struct NameLink;
63
64 struct Cluster;
65 struct ClusterRoot;
66 struct ClusterParser;
67
68 friend struct ClusterParser;
69
70 //! constructor
71 explicit RagTime5ClusterManager(RagTime5Document &doc);
72 //! destructor
73 ~RagTime5ClusterManager();
74
75 //! try to send the root cluster zone
76 bool sendClusterMainList();
77
78 //! try to read a cluster zone
79 bool readCluster(RagTime5Zone &zone, ClusterParser &parser, bool warnForUnparsed=true);
80 //! try to read a cluster zone
81 bool readCluster(RagTime5Zone &zone, std::shared_ptr<Cluster> &cluster, int type=-1);
82 //! try to read the root cluster zone
83 std::shared_ptr<Cluster> readRootCluster(RagTime5Zone &zone);
84 //! try to read the cluster root list (in general Data14)
85 bool readClusterMainList(ClusterRoot &root, std::vector<int> &list, std::vector<int> const &clusterIdList);
86
87 //! try to read a level 2 child of a cluster (picture resizing, ...)
88 bool readClusterGObjProperties(RagTime5Zone &zone);
89 //! try to read some unknown cluster
90 bool readUnknownClusterC(Link const &link);
91 //! try to find a cluster zone type ( heuristic when the cluster type is unknown )
92 int getClusterFileType(RagTime5Zone &zone);
93 //! returns the local zone type
94 int getClusterType(RagTime5Zone &zone, int fileType);
95 //! try to return basic information about the header cluster's zone
96 bool getClusterBasicHeaderInfo(RagTime5Zone &zone, long &N, long &fSz, long &debHeaderPos);
97
98 // low level
99
100 //! try to read a field header, if ok set the endDataPos positions
101 bool readFieldHeader(RagTime5Zone &zone, long endPos, std::string const &headerName, long &endDataPos, long expectedLVal=-99999);
102 //! returns "data"+id+"A" ( followed by the cluster type and name if know)
103 std::string getClusterDebugName(int id);
104 //! define a cluster name (used to associate graph name)
105 void setClusterName(int id, librevenge::RVNGString const &name);
106 //! debug: print a file type
107 static std::string printType(unsigned long fileType)
108 {
109 return RagTime5StructManager::printType(fileType);
110 }
111
112 //! a link to a small zone (or set of zones) in RagTime 5/6 documents
113 struct Link {
114 //! the link type
115 enum Type { L_ClusterLink,
116 L_LongList, L_UnicodeList,
117 L_FieldsList, L_List,
118 L_UnknownClusterC,
119 L_Unknown
120 };
121 //! constructor
122 explicit Link(Type type=L_Unknown)
123 : m_type(type)
124 , m_name("")
125 , m_ids()
126 , m_N(0)
127 , m_fieldSize(0)
128 , m_longList()
129 {
130 for (auto &typ : m_fileType) typ=0;
131 }
132 //! returns true if all link are empty
133 bool empty() const
134 {
135 if (m_type==L_LongList && !m_longList.empty())
136 return false;
137 for (auto id : m_ids)
138 if (id>0) return false;
139 return true;
140 }
141 //! returns the zone name
142 std::string getZoneName() const
143 {
144 switch (m_type) {
145 case L_ClusterLink:
146 return "clustLink";
147 case L_LongList:
148 if (!m_name.empty())
149 return m_name;
150 else {
151 std::stringstream s;
152 s << "longList" << m_fieldSize;
153 return s.str();
154 }
155 case L_UnicodeList:
156 return "unicodeListLink";
157 case L_UnknownClusterC:
158 return "unknownClusterC";
159 case L_FieldsList:
160 if (!m_name.empty())
161 return m_name;
162 return "fieldsList[unkn]";
163 case L_List:
164 if (!m_name.empty())
165 return m_name;
166 break;
167 case L_Unknown:
168 #if !defined(__clang__)
169 default:
170 #endif
171 break;
172 }
173 std::stringstream s;
174 if (m_type==L_List)
175 s << "ListZone";
176 else
177 s << "FixZone";
178 s << std::hex << m_fileType[0] << "_" << m_fileType[1] << std::dec;
179 if (m_fieldSize)
180 s << "_" << m_fieldSize;
181 s << "A";
182 return s.str();
183 }
184 //! operator<<
185 friend std::ostream &operator<<(std::ostream &o, Link const &z)
186 {
187 if (z.empty()) return o;
188 o << z.getZoneName() << ":";
189 size_t numLinks=z.m_ids.size();
190 if (numLinks>1) o << "[";
191 for (size_t i=0; i<numLinks; ++i) {
192 if (z.m_ids[i]<=0)
193 o << "_";
194 else
195 o << "data" << z.m_ids[i] << "A";
196 if (i+1!=numLinks) o << ",";
197 }
198 if (numLinks>1) o << "]";
199 if (z.m_fieldSize&0x8000)
200 o << "[" << std::hex << z.m_fieldSize << std::dec << ":" << z.m_N << "]";
201 else
202 o << "[" << z.m_fieldSize << ":" << z.m_N << "]";
203 return o;
204 }
205 //! the link type
206 Type m_type;
207 //! the link name
208 std::string m_name;
209 //! the data ids
210 std::vector<int> m_ids;
211 //! the number of data ( or some flag if m_N & 0x8020)
212 int m_N;
213 //! the field size
214 int m_fieldSize;
215 //! the zone type in file
216 unsigned long m_fileType[2];
217 //! a list of long used to store decal
218 std::vector<long> m_longList;
219 };
220
221 //! a link to a name zone in RagTime 5/6 documents
222 struct NameLink {
223 //! default constructor
224 NameLink()
225 : m_ids()
226 , m_N(0)
227 , m_decalList()
228 {
229 }
230 //! constructor from list
231 explicit NameLink(Link const &lnk)
232 : m_ids(lnk.m_ids)
233 , m_N(lnk.m_N)
234 , m_decalList(lnk.m_longList)
235 {
236 }
237 //! returns true if all link are empty
238 bool empty() const
239 {
240 for (auto id : m_ids)
241 if (id>0) return false;
242 return true;
243 }
244 //! the data ids
245 std::vector<int> m_ids;
246 //! the number of data
247 int m_N;
248 //! a list of long used to store decal
249 std::vector<long> m_decalList;
250 //! for unicode list field ids and field id to name
251 std::vector<long> m_posToNames[2];
252 //! the corresponding link (for big list)
253 Link m_posToNamesLinks[2];
254 };
255
256 ////////////////////////////////////////////////////////////
257 // cluster classes
258 ////////////////////////////////////////////////////////////
259
260 //! the cluster data
261 struct Cluster {
262 //! the cluster type
263 enum Type {
264 C_ColorPattern, C_FormulaDef, C_FormulaPos, C_Layout, C_Pipeline,
265 C_Root, C_ClusterGProp, C_Sound,
266
267 // the main zones
268 C_ButtonZone, C_ChartZone, C_GraphicZone, C_PictureZone, C_SpreadsheetZone, C_TextZone,
269 // group zones: 6.6
270 C_GroupZone,
271 // the styles
272 C_ColorStyles, C_FormatStyles, C_GraphicStyles, C_TextStyles, C_UnitStyles,
273 // unknown clusters
274 C_ClusterC,
275
276 C_Empty, C_Unknown
277 };
278 //! constructor
279 explicit Cluster(Type type)
280 : m_type(type)
281 , m_zoneId(0)
282 , m_hiLoEndian(true)
283 , m_name("")
284 , m_childLink()
285 , m_parentLink()
286 , m_dataLink()
287 , m_nameLink()
288 , m_formulaLink()
289 , m_settingLinks()
290 , m_linksList()
291 , m_clusterIdsList()
292 , m_isSent(false)
293 {
294 }
295 //! destructor
296 virtual ~Cluster();
297 //! the cluster type
298 Type m_type;
299 //! the zone id
300 int m_zoneId;
301 //! the cluster hiLo endian
302 bool m_hiLoEndian;
303 //! the cluster name (if know)
304 librevenge::RVNGString m_name;
305 //! the child link
306 Link m_childLink;
307 //! the parent link
308 Link m_parentLink;
309 //! the main data link
310 Link m_dataLink;
311 //! the name link
312 NameLink m_nameLink;
313 //! the formula cluster links (def and pos)
314 Link m_formulaLink;
315 //! the settings links
316 std::vector<Link> m_settingLinks;
317 //! the link list
318 std::vector<Link> m_linksList;
319 //! the cluster ids
320 std::vector<int> m_clusterIdsList;
321 //! true if the cluster was send
322 bool m_isSent;
323 };
324
325 /** returns the cluster type corresponding to zone id or C_Unknown (if the zone is not a cluster or was not parsed) */
326 Cluster::Type getClusterType(int zId) const;
327
328 //! the cluster for root
329 struct ClusterRoot final : public Cluster {
330 //! constructor
331 ClusterRoot()
332 : Cluster(C_Root)
333 , m_docInfoLink()
334 , m_functionNameLink()
335 , m_graphicTypeLink()
336 , m_listUnicodeLink()
337 , m_listClusterId(0)
338 , m_listClusterName()
339 , m_linkUnknown()
340 , m_fileName("")
341 {
342 for (auto &id : m_styleClusterIds) id=0;
343 for (auto &id : m_clusterIds) id=0;
344 }
345 //! destructor
346 ~ClusterRoot() final;
347 //! the list of style cluster ( graph, units, unitsbis, text, format, unknown, graphcolor, col/pattern id)
348 int m_styleClusterIds[8];
349
350 //! other cluster id (unknown cluster b, )
351 int m_clusterIds[1];
352
353 //! the doc info link
354 Link m_docInfoLink;
355 //! the function name links
356 Link m_functionNameLink;
357 //! the graphic type id
358 Link m_graphicTypeLink;
359
360 //! a link to a list of unknown index+unicode string
361 Link m_listUnicodeLink;
362 //! the cluster list id
363 int m_listClusterId;
364 //! the cluster list id name zone link
365 NameLink m_listClusterName;
366 //! first the main cluster link, second list of field definition link, third in header
367 Link m_listClusterLink[3];
368
369 //! other link: scripts and field 6
370 Link m_linkUnknown;
371
372 //! the filename if known
373 librevenge::RVNGString m_fileName;
374 };
375
376 ////////////////////////////////////////////////////////////
377 // parser class
378 ////////////////////////////////////////////////////////////
379
380 //! virtual class use to parse the cluster data
381 struct ClusterParser {
382 //! constructor
383 ClusterParser(RagTime5ClusterManager &parser, int type, std::string const &zoneName)
384 : m_parser(parser)
385 , m_type(type)
386 , m_hiLoEndian(true)
387 , m_name(zoneName)
388 , m_dataId(0)
389 , m_link()
390 {
391 }
392 //! destructor
393 virtual ~ClusterParser();
394 //! return the current cluster
395 virtual std::shared_ptr<Cluster> getCluster()=0;
396 //! return the debug name corresponding to a zone
397 virtual std::string getZoneName() const
398 {
399 return m_name;
400 }
401 //! return the debug name corresponding to a cluster
402 virtual std::string getZoneName(int n, int m=-1) const
403 {
404 std::stringstream s;
405 s << m_name << "-" << n;
406 if (m>=0)
407 s << "-B" << m;
408 return s.str();
409 }
410 //! start a new zone
411 virtual void startZone()
412 {
413 }
414 //! parse a zone
415 virtual bool parseZone(MWAWInputStreamPtr &/*input*/, long /*fSz*/, int /*N*/, int /*flag*/, libmwaw::DebugStream &/*f*/)
416 {
417 return false;
418 }
419 //! end of a start zone call
420 virtual void endZone()
421 {
422 }
423 //! parse a the data of a zone, n_dataId:m
424 virtual bool parseField(RagTime5StructManager::Field const &/*field*/, int /*m*/, libmwaw::DebugStream &/*f*/)
425 {
426 return false;
427 }
428 /** returns to new zone to parse. -1: means no preference, 0: means first zone, ...
429 */
430 virtual int getNewZoneToParse()
431 {
432 return -1;
433 }
434 //
435 // some tools
436 //
437
438 //! return true if N correspond to a file/script name
439 bool isANameHeader(long N) const
440 {
441 return (m_hiLoEndian && N==int(0x80000000)) || (!m_hiLoEndian && N==0x8000);
442 }
443
444 //! try to read a link header
445 bool readLinkHeader(MWAWInputStreamPtr &input, long fSz, Link &link, long(&values)[4], std::string &message);
446 //! returns "data"+id+"A" ( followed by the cluster type and name if know)
447 std::string getClusterDebugName(int id);
448 //! the main parser
449 RagTime5ClusterManager &m_parser;
450 //! the cluster type
451 int m_type;
452 //! zone endian
453 bool m_hiLoEndian;
454 //! the cluster name
455 std::string m_name;
456 //! the actual zone id
457 int m_dataId;
458 //! the actual link
459 Link m_link;
460 private:
461 explicit ClusterParser(ClusterParser const &orig) = delete;
462 ClusterParser &operator=(ClusterParser const &orig) = delete;
463 };
464 protected:
465 //! the state
466 std::shared_ptr<RagTime5ClusterManagerInternal::State> m_state;
467 //! the main parser
468 RagTime5Document &m_document;
469 //! the structure manager
470 std::shared_ptr<RagTime5StructManager> m_structManager;
471 private:
472 RagTime5ClusterManager(RagTime5ClusterManager const &orig) = delete;
473 RagTime5ClusterManager operator=(RagTime5ClusterManager const &orig) = delete;
474 };
475
476 #endif
477 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: