Debian Codegraph @

src/lib/StudentWritingCParser.cxx

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 #include <array>
35 #include <cstring>
36 #include <iomanip>
37 #include <iostream>
38 #include <limits>
39 #include <set>
40 #include <sstream>
41 #include <stack>
42
43 #include <librevenge/librevenge.h>
44
45 #include "MWAWCell.hxx"
46 #include "MWAWTextListener.hxx"
47 #include "MWAWFont.hxx"
48 #include "MWAWFontConverter.hxx"
49 #include "MWAWHeader.hxx"
50 #include "MWAWParagraph.hxx"
51 #include "MWAWPosition.hxx"
52 #include "MWAWPictMac.hxx"
53 #include "MWAWPrinter.hxx"
54 #include "MWAWSection.hxx"
55 #include "MWAWStringStream.hxx"
56 #include "MWAWSubDocument.hxx"
57 #include "MWAWTable.hxx"
58
59 #include "StudentWritingCParser.hxx"
60
61 /** Internal: the structures of a StudentWritingCParser */
62 namespace StudentWritingCParserInternal
63 {
64 typedef std::pair<int,int> ZoneEntry;
65 //! an structure of StudentWritingCParserInternal to store the position of a frame and its entries
66 struct FrameStruct {
67 //! constructor
68 explicit FrameStruct(int type)
69 : m_type(type)
70 , m_page(0)
71 , m_id()
72 {
73 }
74 //! the frame type
75 int m_type;
76 //! the page
77 int m_page;
78 //! the zone id
79 ZoneEntry m_id;
80 //! the bounding boxes
81 MWAWBox2f m_boxes[2];
82 };
83
84 //! an structure of StudentWritingCParserInternal to store the page's data
85 struct PageStruct {
86 //! constructor
87 PageStruct()
88 : m_pageNumber(0)
89 , m_firstChar(0)
90 , m_numColumns(0)
91 {
92 }
93 //! the page number
94 int m_pageNumber;
95 //! the first character
96 int m_firstChar;
97 //! the number of columns
98 int m_numColumns;
99 //! the header/footer id
100 ZoneEntry m_hfIds[2];
101 };
102
103 //! an structure of StudentWritingCParserInternal to store the position of a picture and its entries
104 struct PictureStruct {
105 //! constructor
106 PictureStruct()
107 : m_box()
108 {
109 for (auto &id : m_ids) id=0;
110 }
111 //! the bounding box
112 MWAWBox2f m_box;
113 //! the zone id and the zone sub ids
114 int m_ids[2];
115 };
116
117 //! an structure of StudentWritingCParserInternal to store the position of a zone and its entries
118 struct ZoneStruct {
119 //! constructor
120 ZoneStruct()
121 : m_flags(0)
122 , m_subZones2(1)
123 {
124 for (auto &m : m_margins) m=0;
125 }
126 //! the zone ids
127 ZoneEntry m_ids[9];
128 //! the zone flag
129 int m_flags;
130 //! the number of subzone in zone2
131 int m_subZones2;
132 //! the margins: LTRB
133 float m_margins[4];
134 };
135
136 //! a list of entry of StudentWritingCParser defining a zone
137 struct Zone {
138 //! constructor
139 Zone(int type, int id)
140 : m_type(type)
141 , m_id(id)
142 , m_idToEntryMap()
143
144 , m_idToParagraphMap()
145
146 , m_idToDataMap()
147 , m_idToPageMap()
148 , m_frames()
149 , m_frameDates()
150 , m_idToFrameNoteMap()
151 , m_idToFrameBiblioMap()
152 , m_idToPictureMap()
153 , m_idToObjectMap()
154 {
155 }
156
157 //! insert a new entry
158 bool insert(int id, MWAWEntry const &entry)
159 {
160 if (m_idToEntryMap.find(id)!=m_idToEntryMap.end()) {
161 MWAW_DEBUG_MSG(("StudentWritingCParserInternal::Zone::insert: entry %d already exists\n", id));
162 return false;
163 }
164 m_idToEntryMap[id]=entry;
165 return true;
166 }
167 //! the zone type
168 int m_type;
169 //! the zone id
170 int m_id;
171 //! the id to sub zone map
172 std::map<int, MWAWEntry> m_idToEntryMap;
173
174 // specific data
175
176 //! a map id to paragraph
177 std::map<int, MWAWParagraph> m_idToParagraphMap;
178
179 //! a map id to main zone data, ...
180 std::map<int, ZoneStruct> m_idToDataMap;
181 //! a map id to page positions, ...
182 std::map<int, PageStruct> m_idToPageMap;
183 //! a list of frames
184 std::vector<FrameStruct> m_frames;
185 //! a list of frames date
186 std::vector<std::array<int,3> > m_frameDates;
187 //! a map id to frame note entry
188 std::map<int, ZoneEntry> m_idToFrameNoteMap;
189 //! a map id to biblio text
190 std::map<int,librevenge::RVNGString> m_idToFrameBiblioMap;
191 //! a map id to picture positions, ...
192 std::map<int, PictureStruct> m_idToPictureMap;
193 //! a map id to an embedded object
194 std::map<int, MWAWEmbeddedObject> m_idToObjectMap;
195 };
196
197 ////////////////////////////////////////
198 //! Internal: the state of a StudentWritingCParser
199 struct State {
200 //! constructor
201 State()
202 : m_actPage(0)
203 , m_numPages(0)
204
205 , m_isUncompressed(false)
206 , m_zones()
207 , m_idToZoneMap()
208
209 , m_idToFontNameMap()
210 , m_idToFontNameUsed()
211
212 , m_sendZoneSet()
213 , m_sendBoxesStack()
214 {
215 }
216
217 //! try to return a color corresponding to a color id
218 bool getColor(int id, MWAWColor &color) const
219 {
220 if (id<0 || id>15) {
221 MWAW_DEBUG_MSG(("StudentWritingCParserInternal::State::getColor: unknown id=%d\n", id));
222 return false;
223 }
224 static uint32_t colors[]= { 0, 0xffffff, 0x838300, 0x808080, 0xc9c9c9,
225 0xff0000, 0xff00, 0xff, 0xffff, 0xff00ff,
226 0xffff00, 0x8f8f, 0x8f00, 0x8f0000, 0x8f,
227 0xb000b0
228 };
229 color=MWAWColor(colors[id]);
230 return true;
231 }
232 //! small function to know if a zone with given type exists
233 bool checkIfZone(int id, int type) const
234 {
235 auto const &zId=m_idToZoneMap.find(id);
236 return zId!=m_idToZoneMap.end() && zId->second->m_type==type;
237 }
238 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
239
240 //! a flag to know if we have uncompress the data
241 bool m_isUncompressed;
242 //! the main zone id and the font id
243 ZoneEntry m_ids[2];
244 //! the list of zone
245 std::vector<std::shared_ptr<Zone> > m_zones;
246 //! a map id to zone data
247 std::map<int,std::shared_ptr<Zone> > m_idToZoneMap;
248
249 //! a map id to font name
250 std::map<int,std::string> m_idToFontNameMap;
251 //! a set to store the font name used
252 std::set<int> m_idToFontNameUsed;
253
254 //! a set to keep the list of send zone (to avoid loop)
255 std::set<int> m_sendZoneSet;
256 //! a stack of send bounding box (use to send background picture)
257 std::stack<MWAWBox2f> m_sendBoxesStack;
258 };
259
260 ////////////////////////////////////////
261 //! Internal: the subdocument of a StudentWritingCParser
262 class SubDocument final : public MWAWSubDocument
263 {
264 public:
265 SubDocument(StudentWritingCParser &pars, MWAWInputStreamPtr const &input, int id)
266 : MWAWSubDocument(&pars, input, MWAWEntry())
267 , m_zoneId(id)
268 {
269 }
270
271 //! destructor
272 ~SubDocument() final {}
273
274 //! operator!=
275 bool operator!=(MWAWSubDocument const &doc) const final
276 {
277 if (MWAWSubDocument::operator!=(doc)) return true;
278 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
279 if (!sDoc) return true;
280 if (m_zoneId != sDoc->m_zoneId) return true;
281 return false;
282 }
283
284 //! the parser function
285 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
286
287 protected:
288 //! the zone id
289 int m_zoneId;
290 };
291
292 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
293 {
294 if (!listener.get()) {
295 MWAW_DEBUG_MSG(("StudentWritingCParserInternal::SubDocument::parse: no listener\n"));
296 return;
297 }
298 auto *parser=dynamic_cast<StudentWritingCParser *>(m_parser);
299 if (!parser) {
300 MWAW_DEBUG_MSG(("StudentWritingCParserInternal::SubDocument::parse: no parser\n"));
301 return;
302 }
303
304 long pos = m_input->tell();
305 parser->sendZone(m_zoneId);
306 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
307 }
308 }
309
310 ////////////////////////////////////////////////////////////
311 // constructor/destructor, ...
312 ////////////////////////////////////////////////////////////
313 StudentWritingCParser::StudentWritingCParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
314 : MWAWTextParser(input, rsrcParser, header)
315 , m_state()
316 {
317 setAsciiName("main-1");
318
319 m_state.reset(new StudentWritingCParserInternal::State);
320
321 // reduce the margin (in case, the page is not defined)
322 getPageSpan().setMargins(0.1);
323 }
324
325 StudentWritingCParser::~StudentWritingCParser()
326 {
327 }
328
329 ////////////////////////////////////////////////////////////
330 // the parser
331 ////////////////////////////////////////////////////////////
332 void StudentWritingCParser::parse(librevenge::RVNGTextInterface *docInterface)
333 {
334 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
335 bool ok = true;
336 try {
337 getParserState()->m_input=decode();
338 if (!getInput())
339 throw(libmwaw::ParseException());
340 m_state->m_isUncompressed=true;
341 // create the asciiFile
342 ascii().setStream(getInput());
343 ascii().open(asciiName());
344
345 checkHeader(nullptr);
346 ok = createZones();
347 if (ok) {
348 createDocument(docInterface);
349 sendZone(m_state->m_ids[0].first);
350 }
351
352 ascii().reset();
353 }
354 catch (...) {
355 MWAW_DEBUG_MSG(("StudentWritingCParser::parse: exception catched when parsing\n"));
356 ok = false;
357 }
358
359 resetTextListener();
360 if (!ok) throw(libmwaw::ParseException());
361 }
362
363 ////////////////////////////////////////////////////////////
364 // create the document
365 ////////////////////////////////////////////////////////////
366 void StudentWritingCParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
367 {
368 if (!documentInterface) throw(libmwaw::ParseException());
369 if (getTextListener()) {
370 MWAW_DEBUG_MSG(("StudentWritingCParser::createDocument: listener already exist\n"));
371 return;
372 }
373
374 std::vector<MWAWPageSpan> pageList;
375 // first find the main zone to look for header/footer
376 auto mainIt=m_state->m_idToZoneMap.find(m_state->m_ids[0].first);
377 if (mainIt==m_state->m_idToZoneMap.end() || !mainIt->second || mainIt->second->m_type!=5) {
378 MWAW_DEBUG_MSG(("StudentWritingCParser::createDocument: can not find the main zone\n"));
379 throw (libmwaw::ParseException());
380 }
381
382 auto const &mainZone=*mainIt->second;
383 if (mainZone.m_idToDataMap.find(1)!=mainZone.m_idToDataMap.end()) {
384 auto const &mainStruct=mainZone.m_idToDataMap.find(1)->second;
385 bool hasTitlePage=mainStruct.m_ids[7].first>0;
386 if (hasTitlePage)
387 ++m_state->m_numPages;
388 // look also if we need to add a final page for the biblio at the end
389 if (mainStruct.m_ids[1].first) {
390 auto biblioIt=m_state->m_idToZoneMap.find(mainStruct.m_ids[1].first);
391 if (biblioIt!=m_state->m_idToZoneMap.end() && biblioIt->second && !biblioIt->second->m_idToFrameBiblioMap.empty())
392 ++m_state->m_numPages;
393 }
394 // now look for header/footer
395 auto const &flags=mainStruct.m_flags;
396 bool hasHF[2];
397 for (int i=0; i<2; ++i) hasHF[i]=mainStruct.m_ids[i+4].first>0;
398 bool const hasFirstHF[2]= {hasHF[0] &&(flags&0x800)==0, hasHF[1] &&(flags&0x1000)==0};
399 bool needFirstPage=(hasTitlePage && (hasHF[0] || hasHF[1])) || hasHF[0]!=hasFirstHF[0] || hasHF[1]!=hasFirstHF[1];
400 for (auto st=0; st<2; ++st) {
401 MWAWPageSpan ps(getPageSpan());
402 if (st==0) {
403 if (!needFirstPage)
404 continue;
405 ps.setPageSpan(1);
406 }
407 else
408 ps.setPageSpan(m_state->m_numPages+(needFirstPage ? 0 : 1));
409 ps.setMarginTop(mainStruct.m_margins[1]);
410 ps.setMarginBottom(mainStruct.m_margins[3]);
411 ps.setMarginLeft(mainStruct.m_margins[0]);
412 ps.setMarginRight(mainStruct.m_margins[2]);
413 for (int wh=0; wh<2; ++wh) {
414 if ((st==0 && (hasTitlePage || !hasFirstHF[wh])) || (st==1 && !hasHF[wh]))
415 continue;
416 MWAWHeaderFooter hf(wh==1 ? MWAWHeaderFooter::FOOTER : MWAWHeaderFooter::HEADER, MWAWHeaderFooter::ALL);
417 hf.m_subDocument.reset(new StudentWritingCParserInternal::SubDocument(*this, getInput(),mainStruct.m_ids[wh+4].first));
418 ps.setHeaderFooter(hf);
419 }
420 pageList.push_back(ps);
421 }
422 }
423 else {
424 MWAW_DEBUG_MSG(("StudentWritingCParser::createDocument: can not find the main zone's structure\n"));
425 MWAWPageSpan ps(getPageSpan());
426 ps.setPageSpan(m_state->m_numPages+1);
427 pageList.push_back(ps);
428 }
429
430 //
431 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
432 setTextListener(listen);
433 listen->startDocument();
434 }
435
436
437 ////////////////////////////////////////////////////////////
438 //
439 // Intermediate level
440 //
441 ////////////////////////////////////////////////////////////
442 bool StudentWritingCParser::createZones()
443 {
444 MWAWInputStreamPtr input = getInput();
445 if (!input || !input->checkPosition(352))
446 return false;
447
448 libmwaw::DebugStream f;
449 input->seek(4, librevenge::RVNG_SEEK_SET);
450 int val=int(input->readLong(1));
451 if (val==3)
452 f << "template,";
453 else if (val!=2)
454 f << "unk=" << val << ",";
455 val=int(input->readLong(2));
456 if (val!=0x4646)
457 f << "f0=" << std::hex << val << std::dec << ",";
458 val=int(input->readLong(1));
459 if (val) f << "f1=" << val << ",";
460
461 // first block: 220
462 // then 124?
463 val=int(input->readLong(2)); // 0-4
464 switch (val) {
465 case 0:
466 f << "report,";
467 break;
468 case 1:
469 f << "journal,";
470 break;
471 case 2:
472 f << "sign,";
473 break;
474 case 3:
475 f << "newletter,";
476 break;
477 case 4:
478 f << "letter,";
479 break;
480 default:
481 f << "type=" << val << ",";
482 break;
483 }
484
485 for (int i=0; i<4; ++i) {
486 val=int(input->readLong(2));
487 int const expected[]= {2, 1, 0, 0};
488 if (val==expected[i]) continue;
489 f << "f" << i+2 << "=" << val << ",";
490 }
491 for (int st=0; st<2; ++st) {
492 int cId=int(input->readLong(2));
493 int type=int(input->readLong(2));
494 m_state->m_ids[st]=std::make_pair(cId,type);
495 if (!cId) continue;
496 f << (st==0 ? "main" : "font") << "=Z" << cId << ":" << type << ",";
497 }
498 for (int i=0; i<2; ++i) {
499 val=int(input->readLong(2));
500 if (val==(i==0 ? 1 : 0)) continue;
501 f << "f" << i+6 << "=" << val << ",";
502 }
503 val=int(input->readLong(1));
504 if (val) f << "f7=" << val << ",";
505 for (int st=0; st<2; ++st) {
506 long aPos=input->tell();
507 std::string name;
508 for (int i=0; i<32; ++i) {
509 char c=char(input->readLong(1));
510 if (!c)
511 break;
512 name+=c;
513 }
514 if (!name.empty()) f << "text" << st << "=" << name << ",";
515 input->seek(aPos+32, librevenge::RVNG_SEEK_SET);
516 }
517 ascii().addPos(0);
518 ascii().addNote(f.str().c_str());
519
520 long pos=input->tell();
521 f.str("");
522 f << "FileHeader-A:";
523 for (int i=0; i<60; ++i) {
524 val=int(input->readLong(2));
525 if (val)
526 f << "f" << i << "=" << val << ",";
527 }
528 ascii().addPos(pos);
529 ascii().addNote(f.str().c_str());
530
531 pos=input->tell();
532 f.str("");
533 f << "FileHeader-B:";
534 val=int(input->readLong(1));
535 if (val)
536 f << "f0=" << val << ",";
537 for (int i=0; i<3; ++i) {
538 val=int(input->readLong(2));
539 int const expected[]= {0x28 /* or aa*/, 0, 0x7cb};
540 if (val==expected[i])
541 continue;
542 if (i==0)
543 f << "day=" << val << ",";
544 else if (i==2)
545 f << "year=" << val << ",";
546 else
547 f << "f" << i+1 << "=" << val << ",";
548 }
549 for (int i=0; i<2; ++i) { // f3=0|2|4
550 val=int(input->readLong(1));
551 if (val)
552 f << "f" << i+3 << "=" << val << ",";
553 }
554 for (int i=0; i<18; ++i) {
555 val=int(input->readLong(2));
556 if (val)
557 f << "f" << i+5 << "=" << val << ",";
558 }
559 for (int i=0; i<12; ++i) {
560 val=int(input->readLong(2));
561 int const expected[]= {0x320, 0, 0x7c, 0x78, 0,
562 0x11f, 1, 0, 0xaea, 0x86f,
563 0x64, 1
564 };
565 if (val==expected[i])
566 continue;
567 f << "g" << i << "=" << val << ",";
568 }
569
570 for (int i=0; i<34; ++i) {
571 val=int(input->readLong(2));
572 if (val)
573 f << "h" << i << "=" << val << ",";
574 }
575 ascii().addPos(pos);
576 ascii().addNote(f.str().c_str());
577
578 pos=input->tell();
579 if (!readPrintInfo())
580 return false;
581 input->seek(pos+120, librevenge::RVNG_SEEK_SET);
582
583 while (!input->isEnd()) {
584 pos=input->tell();
585 if (!input->checkPosition(pos+10))
586 break;
587 long dataSize=long(input->readLong(4));
588 long endPos=pos+10+dataSize;
589 if (endPos<pos+10 || !input->checkPosition(endPos)) {
590 input->seek(pos, librevenge::RVNG_SEEK_SET);
591 break;
592 }
593 int ids[3];
594 for (int i=0; i<3; ++i)
595 ids[i]=int(input->readLong(2));
596
597 MWAWEntry entry;
598 entry.setBegin(pos);
599 entry.setEnd(endPos);
600 entry.setId(ids[0]);
601 if (m_state->m_idToZoneMap.find(ids[0])==m_state->m_idToZoneMap.end()) {
602 m_state->m_zones.push_back(std::make_shared<StudentWritingCParserInternal::Zone>(ids[1], ids[0]));
603 m_state->m_idToZoneMap[ids[0]]=m_state->m_zones.back();
604 }
605 auto &zone=*m_state->m_idToZoneMap.find(ids[0])->second;
606 if (zone.m_type!=ids[1] || !zone.insert(ids[2],entry)) {
607 MWAW_DEBUG_MSG(("StudentWritingCParser::createZones: find a bad zone type\n"));
608 ascii().addPos(pos);
609 ascii().addNote("Entries(BadZone):###");
610 }
611 input->seek(endPos, librevenge::RVNG_SEEK_SET);
612 }
613
614 if (!input->isEnd()) {
615 MWAW_DEBUG_MSG(("StudentWritingCParser::createZones: find extra data\n"));
616 ascii().addPos(input->tell());
617 ascii().addNote("Entries(Unknown):###");
618 }
619
620 bool mainZoneFound=false;
621 for (auto const &zIt : m_state->m_idToZoneMap) {
622 int zoneId=zIt.first;
623 auto &zone=*zIt.second;
624
625 switch (zone.m_type) {
626 case 0:
627 for (auto const &dataIt : zone.m_idToEntryMap) {
628 auto const &id=dataIt.first;
629 auto const &entry=dataIt.second;
630 f.str("");
631 f << "Entries(End):";
632 if (zoneId!=0 || id!=0 || entry.length()!=10) {
633 MWAW_DEBUG_MSG(("StudentWritingCParser::createZones: unexpected end zone data\n"));
634 f << "###";
635 }
636 ascii().addPos(entry.begin());
637 ascii().addNote(f.str().c_str());
638 }
639 break;
640 case 1:
641 readTextZone(zone);
642 break;
643 case 2:
644 for (auto const &dataIt : zone.m_idToEntryMap) {
645 auto const &id=dataIt.first;
646 auto const &entry=dataIt.second;
647 input->seek(entry.begin()+10, librevenge::RVNG_SEEK_SET);
648
649 f.str("");
650 f << "Entries(Data2)[Z" << zoneId << "]:id=" << id << ",";
651 if (entry.length()!=10+0x54) {
652 MWAW_DEBUG_MSG(("StudentWritingCParser::createZones[data2]: unexpected end zone size\n"));
653 f << "###";
654 }
655 else {
656 StudentWritingCParserInternal::PageStruct page;
657 for (int i=0; i<7; ++i) {
658 val=int(input->readLong(2));
659 int const expected[]= {1, 0, 0, 0, 1, 1, 0 };
660 if (val!=expected[i])
661 f << "f" << i << "=" << val << ",";
662 }
663 for (int st=0; st<2; ++st) {
664 int cId=int(input->readLong(2));
665 int type=int(input->readLong(2));
666 page.m_hfIds[st]=std::make_pair(cId,type);
667 if (!cId) continue;
668 f << (st==0 ? "pageNumber[header]" : "pageNumber[footer]") << "=Z" << cId << ":" << type << ",";
669 if (!m_state->checkIfZone(cId, type)) {
670 MWAW_DEBUG_MSG(("StudentWritingCParser::createZones[data2]: unexpected zone5 id/size\n"));
671 f << "###";
672 }
673 }
674 val=int(input->readLong(2));
675 if (val!=1)
676 f << "g0=" << val << ",";
677 page.m_pageNumber=int(input->readLong(2));
678 if (page.m_pageNumber!=1)
679 f << "page[number]=" << page.m_pageNumber << ",";
680 val=int(input->readULong(2));
681 page.m_numColumns=1+(val&7);
682 if (page.m_numColumns!=1)
683 f << "num[columns]=" << page.m_numColumns << ",";
684 val &= 0xfff8;
685 if (val)
686 f << "fl=" << std::hex << val << std::dec << ",";
687 for (int i=0; i<9; ++i) { // g2 related to g0 height?
688 val=int(input->readLong(2));
689 int const expected[]= { 0xc8, 0x48, 0, 0, 0, 0, 0, 1, 0};
690 if (val==expected[i])
691 continue;
692 if (i==5) {
693 page.m_firstChar=val;
694 f << "first[char]=" << val << ",";
695 }
696 else
697 f << "g" << i+1 << "=" << val << ",";
698 }
699 for (int i=0; i<12; ++i) { // h1=h4
700 val=int(input->readLong(2));
701 if (val)
702 f << "h" << i << "=" << val << ",";
703 }
704 for (int i=0; i<7; ++i) { // k0=small number
705 val=int(input->readLong(2));
706 int const expected[]= {0, 0x40, 0x78, 0, 0, 0, 0};
707 if (val!=expected[i])
708 f << "k" << i << "=" << val << ",";
709 }
710 zone.m_idToPageMap[id]=page;
711 }
712 ascii().addPos(entry.begin());
713 ascii().addNote(f.str().c_str());
714 }
715 break;
716 case 3:
717 readFrame(zone);
718 break;
719 case 4:
720 readParagraph(zone);
721 break;
722 case 5:
723 if (zoneId==m_state->m_ids[0].first)
724 mainZoneFound=true;
725 for (auto const &dataIt : zone.m_idToEntryMap) {
726 auto const &id=dataIt.first;
727 auto const &entry=dataIt.second;
728 f.str("");
729 f << "Entries(Data5)[Z" << zoneId << "]:id=" << id << ",";
730 input->seek(entry.begin()+10, librevenge::RVNG_SEEK_SET);
731
732 if (id!=1 || entry.length()!=10+0x72) {
733 MWAW_DEBUG_MSG(("StudentWritingCParser::createZones[data5]: unexpected end zone id/size\n"));
734 f << "###";
735 }
736 else {
737 StudentWritingCParserInternal::ZoneStruct data;
738 f << "IDS=[";
739 for (int i=0; i<7; ++i) // big number multiple of 4: change for each save
740 f << std::hex << input->readULong(4) << std::dec << ",";
741 f << "],";
742 for (int i=0; i<2; ++i) { // 0
743 val=int(input->readULong(2));
744 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
745 }
746 for (int i=0; i<8; ++i) { // zone2-zone5, pagenumber:5, next zone5, zone1, title page
747 int cId=int(input->readLong(2));
748 int type=int(input->readLong(2));
749 data.m_ids[i]=std::make_pair(cId, type);
750 if (cId==0)
751 continue;
752 char const *what[]= {"pages", "frames", "paragraph", "Zone5",
753 "header", "footer", "text", "title"
754 };
755 if (what[i])
756 f << what[i] << "=Z" << cId << ":" << type << ",";
757 else
758 f << "unkn" << i << "=Z" << cId << ":" << type << ",";
759 if (!m_state->checkIfZone(cId, type)) {
760 MWAW_DEBUG_MSG(("StudentWritingCParser::createZones[data5]: unexpected child id/type\n"));
761 f << "###";
762 }
763 }
764 data.m_flags=val=int(input->readULong(2));
765 if (val&7)
766 f << "num[columns]=" << 1+int(val&7) << ",";
767 if (val&0x40)
768 f << "has[master,head],";
769 if (val&0x800)
770 f << "header[first,skip],";
771 if (val&0x1000)
772 f << "footer[first,skip],";
773 if (val&0x4000)
774 f << "pagenumber[bottom],";
775 if (val&0x8000)
776 f << "pagenumber[center/right],";
777 val&=0x27b8;
778 if (val)
779 f << "fl0=" << std::hex << val << std::dec << ",";
780 val=int(input->readULong(2)); // 2: header?, 3: footer?
781 if (val)
782 f << "fl1=" << std::hex << val << std::dec << ",";
783 f << "IDS2=" << std::hex << input->readULong(4) << std::dec << ",";
784 f << "margins=["; // L, T, R, B
785 for (auto &m : data.m_margins) {
786 m=float(input->readLong(2))/1000;
787 f << m << "in,";
788 }
789 f << "],";
790 val=int(input->readULong(2));
791 if (val!=0xc8)
792 f << "f2=" << val << ",";
793 val=int(input->readULong(2)); // &1: line between column
794 if (val)
795 f << "fl2=" << std::hex << val << std::dec << ",";
796 for (int i=0; i<12; ++i) { // N8 related to page number align ?
797 val=int(input->readLong(2));
798 int const expected[]= {1,0,0,1,1,
799 0,0,0/*or 402|404*/,0x64,0x78,
800 0 /* 0|2|7|9|14|84|97*/, 0 /*0|2|4|5*/
801 };
802 if (val==expected[i]) continue;
803 if (i==0) {
804 data.m_subZones2=val;
805 f << "N[data2]=" << val << ",";
806 }
807 else
808 f << "N" << i << "=" << val << ",";
809 }
810 int cId=int(input->readLong(2));
811 int type=int(input->readLong(2));
812 data.m_ids[8]=std::make_pair(cId, type);
813 if (cId) {
814 f << "bgPict?=Z" << cId << ":" << type << ",";
815 if (!m_state->checkIfZone(cId, type)) {
816 MWAW_DEBUG_MSG(("StudentWritingCParser::createZones[data5]: unexpected picture id/type\n"));
817 f << "###";
818 }
819 }
820 val=int(input->readLong(2)); // 0
821 if (val)
822 f << "f3=" << val << ",";
823 zone.m_idToDataMap[id]=data;
824 }
825 ascii().addPos(entry.begin());
826 ascii().addNote(f.str().c_str());
827 }
828 break;
829 case 6:
830 readPicture(zone);
831 break;
832 case 7:
833 for (auto const &dataIt : zone.m_idToEntryMap) {
834 auto const &id=dataIt.first;
835 auto const &entry=dataIt.second;
836 f.str("");
837 f << "Entries(Fonts)[Z" << zoneId << "]:";
838 if (id!=1 || !readFontsList(entry)) {
839 MWAW_DEBUG_MSG(("StudentWritingCParser::createZones[fonts]: unexpected id=%d\n", id));
840 f << "###";
841 ascii().addPos(entry.begin());
842 ascii().addNote(f.str().c_str());
843 }
844 }
845 break;
846 default:
847 for (auto const &dataIt : zone.m_idToEntryMap) {
848 auto const &id=dataIt.first;
849 auto const &entry=dataIt.second;
850 f.str("");
851 f << "Entries(Zone" << zone.m_type << "A)[Z" << zoneId << "]:";
852 if (id!=1)
853 f << "id=" << id << ",";
854 ascii().addPos(entry.begin());
855 ascii().addNote(f.str().c_str());
856 }
857 break;
858 }
859 }
860 if (!mainZoneFound) {
861 MWAW_DEBUG_MSG(("StudentWritingCParser::createZone: can not found the main zone\n"));
862 return false;
863 }
864 return mainZoneFound;
865 }
866
867 bool StudentWritingCParser::readTextZone(StudentWritingCParserInternal::Zone const &zone)
868 {
869 auto input=getInput();
870 if (!input || zone.m_type!=1) {
871 MWAW_DEBUG_MSG(("StudentWritingCParser::readTextZone: called will incorrect zone type\n"));
872 return false;
873 }
874 libmwaw::DebugStream f;
875 int val;
876 std::map<int,int> textIdNumChars;
877 std::map<int,std::array<int,4> > styleIdValues;
878 for (auto const &dataIt : zone.m_idToEntryMap) {
879 auto const &id=dataIt.first;
880 auto const &entry=dataIt.second;
881 input->seek(entry.begin()+10, librevenge::RVNG_SEEK_SET);
882 switch (id) {
883 case 1: {
884 f.str("");
885 f << "Entries(TZone)[Z" << zone.m_id << "]:header,";
886 if (entry.length()<10+10) {
887 MWAW_DEBUG_MSG(("StudentWritingCParser::readTextZone[header]: the entry seems too short\n"));
888 f << "###";
889 break;
890 }
891 val=int(input->readLong(2));
892 if (val)
893 f << "f0=" << val << ",";
894
895 int begPos[2];
896 int N[2];
897 f << "zones=[";
898 for (int i=0; i<2; ++i) {
899 f << "[";
900 begPos[i]=int(input->readULong(2));
901 f << "pos=" <<10+begPos[i] << ",";
902 N[i]=int(input->readLong(2));
903 f << "N=" << N[i] << ",";
904 if (begPos[i]<10 || N[i]<0 || (entry.length()-10-begPos[i])/10<N[i] || begPos[i]+10+10*N[i]>entry.length()) {
905 MWAW_DEBUG_MSG(("StudentWritingCParser::readTextZone[header]: a sub zone seems bad\n"));
906 f << "###";
907 N[i]=0;
908 }
909 f << "],";
910 }
911 f << "],";
912 input->seek(entry.begin()+10+begPos[0], librevenge::RVNG_SEEK_SET);
913 f << "text=[";
914 for (int n=0; n<N[0]; ++n) {
915 f << "[";
916 int cId=int(input->readULong(2));
917 f << "id=" << cId << ",";
918 for (int j=0; j<4; ++j) {
919 val=int(input->readLong(2));
920 if (j==1)
921 textIdNumChars[cId]=val;
922 int const expected[]= {0xff0, 0xff0, 0, 0}; // num[max], num[char], 0, 0
923 if (val==expected[j])
924 continue;
925 if (j==0)
926 f << "zone[sz]=" << val << ",";
927 else if (j==1)
928 f << "num[char]=" << val << ",";
929 else
930 f << "f" << j << "=" << val << ",";
931 }
932 f << "],";
933 }
934 f << "],";
935
936 input->seek(entry.begin()+10+begPos[1], librevenge::RVNG_SEEK_SET);
937 f << "style=[";
938 for (int n=0; n<N[1]; ++n) {
939 f << "[";
940 int cId=int(input->readULong(2));
941 f << "id=" << cId << ",";
942 std::array<int,4> values;
943 for (size_t j=0; j<4; ++j) {
944 values[j]=int(input->readLong(2));
945 int const expected[]= {0xff, 0, 0, 0};
946 if (values[j]==expected[j]) continue;
947 switch (j) {
948 case 0:
949 f << "numStyle[max]=" << values[j] << ",";
950 break;
951 case 1:
952 f << "numStyle=" << values[j] << ",";
953 break;
954 case 2:
955 f << "f0=" << values[j] << ",";
956 break;
957 case 3:
958 default:
959 f << "numChar=" << values[j] << ",";
960 break;
961 }
962 }
963 styleIdValues[cId]=values;
964 f << "],";
965 }
966 f << "],";
967 break;
968 }
969 default: {
970 auto tId=textIdNumChars.find(id);
971 if (tId!=textIdNumChars.end()) {
972 int n=tId->second;
973 f.str("");
974 f << "Entries(TZone)[Z" << zone.m_id << "]:text,id=" << id << ",";
975 if (10+n<10 || 10+n>entry.length()) {
976 MWAW_DEBUG_MSG(("StudentWritingCParser::readTextZone: bad number of characters\n"));
977 f << "###";
978 break;
979 }
980 std::string text;
981 for (int i=0; i<n; ++i) {
982 char c=char(input->readLong(1));
983 if (!c)
984 text+="#[0]";
985 else
986 text+=c;
987 }
988 f << text << ",";
989 if (input->tell()!=entry.end()) {
990 ascii().addPos(input->tell());
991 ascii().addNote("_");
992 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
993 }
994 break;
995 }
996 auto sIt=styleIdValues.find(id);
997 if (sIt!=styleIdValues.end()) {
998 f.str("");
999 f << "Entries(TZone)[Z" << zone.m_id << "]:style,id=" << id << ",";
1000 int numStyles=sIt->second[1];
1001 if (numStyles<0 || entry.length()<10+6*numStyles || (entry.length()-10)/6<numStyles) {
1002 MWAW_DEBUG_MSG(("StudentWritingCParser::readTextZone: bad number of style\n"));
1003 f << "###N=" << numStyles << ",";
1004 break;
1005 }
1006 libmwaw::DebugStream f2;
1007 int cPos=0;
1008 for (int i=0; i<numStyles; ++i) {
1009 long pos=input->tell();
1010 f2.str("");
1011 f2 << "TZone-S" << i << ":";
1012 int type=int(input->readULong(2));
1013 int nChar=int(input->readLong(2));
1014 val=int(input->readULong(2));
1015 if (cPos)
1016 f2 << "pos=" << cPos << ",";
1017 cPos+=nChar;
1018 switch (type) {
1019 // 0,2-3
1020 case 0x1:
1021 f2 << "endNote=F" << val << ",";
1022 break;
1023 case 0x2:
1024 f2 << "setDate=F" << val << ",";
1025 break;
1026 // 10-18
1027 case 0x10:
1028 f2 << "font,bold=" << val << ","; // true/false
1029 break;
1030 case 0x11:
1031 f2 << "font,italic=" << val << ","; // true/false
1032 break;
1033 case 0x12:
1034 f2 << "font,underline=" << val << ","; // true/false
1035 break;
1036 case 0x13:
1037 f2 << "font,FN" << val << ",";
1038 break;
1039 case 0x14:
1040 f2 << "font,size=" << float(val)/10 << ",";
1041 break;
1042 case 0x15:
1043 f2 << "font,outline=" << val << ","; // true/false
1044 break;
1045 case 0x16:
1046 f2 << "font,color=" << val << ",";
1047 break;
1048 case 0x17:
1049 f2 << "font,sub/super=" << val << ","; // 0: none 1: super, 2: subs
1050 break;
1051 case 0x18:
1052 f2 << "font,shadow=" << val << ","; // true/false
1053 break;
1054 // 20-22
1055 case 0x20: // checkme
1056 f2 << "page[number],";
1057 if (val) f << "f0=" << val << ",";
1058 break;
1059 case 0x21:
1060 f2 << "date,form=" << std::hex << val << std::dec << ",";
1061 break;
1062 case 0x22:
1063 f2 << "bullet,";
1064 if (val) f << "f0=" << val << ",";
1065 break;
1066
1067 case 0x100:
1068 f2 << "para,P" << val << ",";
1069 break;
1070
1071 case 0x300:
1072 f2 << "col[break],col=" << val << ",";
1073 break;
1074 case 0x500:
1075 ++m_state->m_numPages;
1076 f2 << "page[break],page=" << val << ",";
1077 break;
1078 case 0x700:
1079 f2 << "zone[break]=" << val << ",";
1080 break;
1081
1082 default:
1083 f2 << "type=" << std::hex << type << std::dec << ",";
1084 }
1085 ascii().addPos(pos);
1086 ascii().addNote(f2.str().c_str());
1087 input->seek(pos+6, librevenge::RVNG_SEEK_SET);
1088 }
1089 if (input->tell()!=entry.end()) {
1090 ascii().addPos(input->tell());
1091 ascii().addNote("_");
1092 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1093 }
1094 break;
1095 }
1096 MWAW_DEBUG_MSG(("StudentWritingCParser::readTextZone[header]: find unknown zone\n"));
1097 f.str("");
1098 f << "Entries(TZone)[Z" << zone.m_id << "]:id=" << id << "," << "###";
1099 break;
1100 }
1101 }
1102 if (input->tell()!=entry.end())
1103 ascii().addDelimiter(input->tell(),'|');
1104 ascii().addPos(entry.begin());
1105 ascii().addNote(f.str().c_str());
1106 }
1107 return true;
1108 }
1109
1110 bool StudentWritingCParser::readFrame(StudentWritingCParserInternal::Zone &zone)
1111 {
1112 auto input=getInput();
1113 if (!input || zone.m_type!=3) {
1114 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame: called will incorrect zone type\n"));
1115 return false;
1116 }
1117 libmwaw::DebugStream f;
1118 std::set<int> biblioIds;
1119 for (auto const &dataIt : zone.m_idToEntryMap) {
1120 auto const &id=dataIt.first;
1121 auto const &entry=dataIt.second;
1122 input->seek(entry.begin()+10, librevenge::RVNG_SEEK_SET);
1123 f.str("");
1124 f << "Entries(Frame)[Z" << zone.m_id << "]:";
1125
1126 int val;
1127 if (id==0) {
1128 f << "none,";
1129 if (entry.length()!=10) {
1130 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame[empty]: find some data\n"));
1131 f << "###";
1132 }
1133 }
1134 else if (biblioIds.find(id)!=biblioIds.end()) {
1135 f << "biblio,id=" << id << ",";
1136 if (entry.length()<10+58) {
1137 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame[biblio]: the entry seems too short\n"));
1138 f << "###";
1139 }
1140 else {
1141 // f2=1,f3=1,f7=5,f14=2,f15=0 => 9 data
1142 for (int i=0; i<16; ++i) {
1143 val=int(input->readLong(2));
1144 int const expected[]= { 5, 0x275, 0, 0, 1, 0, 4, 2, 0, 0, 0, 0, 0, 0, 1, 2 };
1145 if (val!=expected[i])
1146 f << "f" << i << "=" << val << ",";
1147 }
1148 f << "IDS=[";
1149 for (int i=0; i<4; ++i) f << std::hex << input->readULong(4) << std::dec << ",";
1150 f << "],";
1151 val=int(input->readLong(2));
1152 if (val) f << "g0=" << val << ",";
1153 // a list of strings, the number probably depends on some f_i values...
1154 // there are added at the end of the files
1155 auto &fontConverter=getFontConverter();
1156 librevenge::RVNGString finalText;
1157 while (input->tell()<entry.end()) {
1158 long actPos=input->tell();
1159 librevenge::RVNGString text;
1160 bool endFound=false;
1161 while (input->tell()<entry.end()) {
1162 char c=char(input->readLong(1));
1163 if (!c) {
1164 endFound=true;
1165 break;
1166 }
1167 else if (c!=0x9 && c<0x1f)
1168 break;
1169 int unicode=fontConverter->unicode(12,(unsigned char) c);
1170 if (unicode!=-1)
1171 libmwaw::appendUnicode(uint32_t(unicode),text);
1172 else
1173 text.append(c);
1174 }
1175 if (!endFound) {
1176 ascii().addDelimiter(actPos, '|');
1177 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame: can not find text\n"));
1178 f << "###";
1179 break;
1180 }
1181 if (text.empty())
1182 continue;
1183 f << text.cstr() << ",";
1184 if (!finalText.empty())
1185 finalText.append(", ");
1186 finalText.append(text);
1187 }
1188 if (!finalText.empty())
1189 zone.m_idToFrameBiblioMap[id]=finalText;
1190 }
1191 }
1192 else {
1193 if (id!=1)
1194 f << "id=" << id << ",";
1195 if (entry.length()<10+4) {
1196 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame[main]: the size seems bad\n"));
1197 f << "###";
1198 }
1199 else {
1200 int zType=int(input->readLong(2));
1201 switch (zType) {
1202 case 2: {
1203 f << "list,";
1204 if (entry.length()<10+16) {
1205 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame[main-2]: the size seems bad\n"));
1206 f << "###";
1207 break;
1208 }
1209 int page=0; // 0: means background?
1210 for (int i=0; i<6; ++i) { // f2=0-1
1211 val=int(input->readLong(2));
1212 if (!val) continue;
1213 if (i==0)
1214 f << "next=" << val << ",";
1215 else if (i==1)
1216 f << "prev=" << val << ",";
1217 else if (i==2) {
1218 page=val;
1219 f << "page=" << val << ",";
1220 }
1221 else
1222 f << "f" << i << "=" << val << ",";
1223 }
1224 int n=int(input->readLong(2));
1225 f << "n=" << n << ",";
1226 if (n<0 || (entry.length()-26)/28<n) {
1227 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame[main-2]: the number of sub zone seems bad\n"));
1228 f << "###";
1229 break;
1230 }
1231 libmwaw::DebugStream f2;
1232 for (int i=0; i<n; ++i) {
1233 long pos=input->tell();
1234 f2.str("");
1235 f2 << "Frame-F" << i << ":";
1236 StudentWritingCParserInternal::FrameStruct frame(2);
1237 frame.m_page=page;
1238 int cId=int(input->readLong(2));
1239 int type=int(input->readLong(2));
1240 frame.m_id=std::make_pair(cId,type);
1241 if (cId) {
1242 f2 << "content=Z" << cId << ":" << type << ",";
1243 if (!m_state->checkIfZone(cId, type)) {
1244 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame: unexpected id/type\n"));
1245 f2 << "###";
1246 }
1247 }
1248 float dim[4];
1249 for (auto &d : dim) d=float(input->readLong(2))/14; // unit ?
1250 frame.m_boxes[0]=MWAWBox2f(MWAWVec2f(dim[0],dim[1]),MWAWVec2f(dim[2],dim[3]));
1251 f2 << "box=" << frame.m_boxes[0] << ",";
1252 for (auto &d : dim) d=float(input->readLong(2));
1253 frame.m_boxes[1]=MWAWBox2f(MWAWVec2f(dim[0],dim[1]),MWAWVec2f(dim[2],dim[3]));
1254 f2 << "box2=" << frame.m_boxes[1] << ",";
1255 for (int j=0; j<2; ++j) { // 0|1
1256 val=int(input->readLong(2));
1257 if (val)
1258 f2 << "f" << i << "=" << val << ",";
1259 }
1260 val=int(input->readULong(2));
1261 f2 << "border=[";
1262 if (val==0)
1263 f << "none,";
1264 else {
1265 if (val&0x80) f2 << "L";
1266 if (val&0x100) f2 << "T";
1267 if (val&0x200) f2 << "R";
1268 if (val&0x400) f2 << "B";
1269 f2 << ":";
1270 if ((val&0x7)!=1) f2 << "style=" << (val&0x7) << ",";
1271 if (((val>>3)&0xf)!=0) f2 << "color=" << ((val>>3)&0xf) << ","; // the color are not in the same order than text color, 0=black, 1=yellow, 2=red .. 15=white
1272 if (val&0x800) f2 << "shade:"; // background=gray
1273 val&=0xf000;
1274 if (val) f2 << "fl=" << std::hex << val << std::dec << ",";
1275 }
1276 f2 << "],";
1277 val=int(input->readLong(2)); // 0
1278 if (val) f2 << "f2=" << val << ",";
1279 ascii().addPos(pos);
1280 ascii().addNote(f2.str().c_str());
1281 input->seek(pos+28, librevenge::RVNG_SEEK_SET);
1282 zone.m_frames.push_back(frame);
1283 }
1284 if (input->tell()<entry.end()) {
1285 ascii().addPos(input->tell());
1286 ascii().addNote("_");
1287 }
1288 break;
1289 }
1290 case 3: {
1291 f << "note,";
1292 if (entry.length()<10+16) {
1293 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame[note]: the zone seems too short\n"));
1294 f << "###,";
1295 break;
1296 }
1297 int cId=int(input->readLong(2));
1298 int type=int(input->readLong(2));
1299 if (cId) {
1300 f << "Z" << cId << ":" << type << ",";
1301 if (!m_state->checkIfZone(cId, type)) {
1302 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame: unexpected type\n"));
1303 f << "###";
1304 }
1305 }
1306 for (int i=0; i<6; ++i) { // f1: small number
1307 val=int(input->readLong(2));
1308 if (!val) continue;
1309 f << "f" << i << "=" << val << ",";
1310 }
1311 zone.m_idToFrameNoteMap[id]=std::make_pair(cId,type);
1312 break;
1313 }
1314 case 5: {
1315 f << "biblio,";
1316 int n=int(input->readLong(2));
1317 if (n<0 || entry.length()<10+4+2*n) {
1318 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame[biblio]: can not find the number of id\n"));
1319 f << "###n=" << n << ",";
1320 break;
1321 }
1322 f << "ids=[";
1323 for (int i=0; i<n; ++i) {
1324 val=int(input->readLong(2));
1325 biblioIds.insert(val);
1326 f << val << ",";
1327 }
1328 f << "],";
1329 break;
1330 }
1331 case 6: {
1332 int n=int(input->readLong(2));
1333 f << "n=" << n << ",";
1334 if (entry.length()<10+8+6*n) {
1335 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame[main:6]: the entry seems too short\n"));
1336 f << "###";
1337 break;
1338 }
1339 for (int i=0; i<2; ++i) {
1340 val=int(input->readLong(2));
1341 if (val)
1342 f << "f" << i << "=" << val << ",";
1343 }
1344 if (!zone.m_frameDates.empty()) {
1345 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame[main:6]: oops, we have already found a date list\n"));
1346 f << "###";
1347 }
1348 f << "dates=[";
1349 for (int i=0; i<n; ++i) {
1350 f << "[";
1351 std::array<int,3> date; // year,month,day
1352 for (size_t j=0; j<3; ++j) date[j]=int(input->readLong(j==0 ? 2 : 1));
1353 f << date[0] << "/" << date[1] << "/" << date[2] << ",";
1354 val=int(input->readLong(2));
1355 if (val) f << "hours?=" << std::hex << val << std::dec << ",";
1356 f << "],";
1357 zone.m_frameDates.push_back(date);
1358 }
1359 f << "],";
1360 break;
1361 }
1362 default:
1363 MWAW_DEBUG_MSG(("StudentWritingCParser::readFrame[main:%d]: unknown type\n", zType));
1364 f << "##zType=" << zType << ",";
1365 break;
1366 }
1367 }
1368 }
1369 ascii().addPos(entry.begin());
1370 ascii().addNote(f.str().c_str());
1371 }
1372 return true;
1373 }
1374
1375 bool StudentWritingCParser::readParagraph(StudentWritingCParserInternal::Zone &zone)
1376 {
1377 auto input=getInput();
1378 if (!input || zone.m_type!=4) {
1379 MWAW_DEBUG_MSG(("StudentWritingCParser::readParagraph: called will incorrect zone type\n"));
1380 return false;
1381 }
1382 libmwaw::DebugStream f;
1383 std::set<int> cIds;
1384 bool first=true;
1385 std::map<int,MWAWParagraph> &idToPara=zone.m_idToParagraphMap;
1386 for (auto const &dataIt : zone.m_idToEntryMap) {
1387 auto const &id=dataIt.first;
1388 auto const &entry=dataIt.second;
1389 input->seek(entry.begin()+10, librevenge::RVNG_SEEK_SET);
1390 f.str("");
1391 f << "Entries(Paragraph)[Z" << zone.m_id << "]:";
1392 if (first) {
1393 f << "id=" << id << ",";
1394 // normally, begin with id=1 but find some case where the first id=6 and data in id=7
1395 f << "header,";
1396 if (entry.length()<10+4) {
1397 MWAW_DEBUG_MSG(("StudentWritingCParser::readParagraph: the first entry seems bad\n"));
1398 f << "###";
1399 }
1400 else {
1401 int N[2];
1402 for (int i=0; i<2; ++i) {
1403 N[i]=int(input->readLong(2));
1404 if (N[i]==0)
1405 continue;
1406 if (i==0)
1407 f << "N=" << N[i] << ",";
1408 else {
1409 MWAW_DEBUG_MSG(("StudentWritingCParser::readParagraph: find unknown value for N1\n"));
1410 f << "N1=" << N[i] << ",###";
1411 }
1412 }
1413 if (N[0]<0 || (entry.length()-14)/4<N[0]) {
1414 MWAW_DEBUG_MSG(("StudentWritingCParser::readParagraph: the value for N0 seems bad\n"));
1415 f << "###";
1416 N[0]=0;
1417 }
1418 f << "zones=[";
1419 for (int i=0; i<N[0]; ++i) {
1420 f << "[";
1421 f << std::hex << input->readULong(2) << std::dec << ','; // unknown big number
1422 int cId=int(input->readLong(2));
1423 cIds.insert(cId);
1424 f << cId << ",";
1425 f << "],";
1426 }
1427 f << "],";
1428 }
1429 first=false;
1430 }
1431 else if (cIds.find(id)==cIds.end() || entry.length()!=10+0x5a) {
1432 f << "id=" << id << ",";
1433 MWAW_DEBUG_MSG(("StudentWritingCParser::readParagraph: find unexpected zone\n"));
1434 f << "###";
1435 }
1436 else {
1437 f << "P" << id << ",";
1438 MWAWParagraph para;
1439 para.m_marginsUnit=librevenge::RVNG_POINT;
1440 for (int i=0; i<3; ++i) {
1441 int val=int(input->readLong(2));
1442 para.m_margins[i]=double(val)/20;
1443 }
1444 *para.m_margins[1]+=*para.m_margins[0];
1445 int val=int(input->readLong(1)); // 0
1446 if (val) f << "f0=" << val << ",";
1447 val=int(input->readULong(1)); // 0-3
1448 switch (val&3) {
1449 case 0:// left
1450 default:
1451 break;
1452 case 1:
1453 para.m_justify=MWAWParagraph::JustificationCenter;
1454 break;
1455 case 2:
1456 para.m_justify=MWAWParagraph::JustificationFull;
1457 break;
1458 case 3:
1459 para.m_justify=MWAWParagraph::JustificationRight;
1460 break;
1461 }
1462 if (val&0xfc) f << "fl1=" << std::hex << (val&0xfc) << std::dec << ",";
1463 val=int(input->readULong(1));
1464 if (val&3)
1465 para.setInterline(1+double(val&3)/2, librevenge::RVNG_PERCENT);
1466 if (val&0xfc) f << "fl2=" << std::hex << (val&0xfc) << std::dec << ",";
1467 int N=int(input->readLong(1));
1468 if (N<0 || N>20) {
1469 MWAW_DEBUG_MSG(("StudentWritingCParser::readParagraph: the number of tabs seems bad\n"));
1470 f << "###N=" << N << ",";
1471 N=0;
1472 }
1473 for (int i=0; i<N; ++i) {
1474 MWAWTabStop tab;
1475 val=int(input->readULong(2));
1476 switch (val&3) {
1477 case 0: // left
1478 default:
1479 break;
1480 case 1:
1481 tab.m_alignment=MWAWTabStop::CENTER;
1482 break;
1483 case 2:
1484 tab.m_alignment=MWAWTabStop::RIGHT;
1485 break;
1486 case 3:
1487 tab.m_alignment=MWAWTabStop::DECIMAL;
1488 break;
1489 }
1490 tab.m_position=double(input->readLong(2))/20/72;
1491 para.m_tabs->push_back(tab);
1492 if (val&0xfffc)
1493 f << "#tab" << i << "=" << std::hex << (val&0xfffc) << std::dec << ",";;
1494 }
1495 f << para << ",";
1496 idToPara[id]=para;
1497 }
1498 if (input->tell()!=entry.end())
1499 ascii().addDelimiter(input->tell(),'|');
1500 ascii().addPos(entry.begin());
1501 ascii().addNote(f.str().c_str());
1502 }
1503 return true;
1504 }
1505
1506 bool StudentWritingCParser::readPicture(StudentWritingCParserInternal::Zone &zone)
1507 {
1508 auto input=getInput();
1509 if (!input || zone.m_type!=6) {
1510 MWAW_DEBUG_MSG(("StudentWritingCParser::readPicture: called will incorrect zone type\n"));
1511 return false;
1512 }
1513 libmwaw::DebugStream f;
1514 std::set<int> pictIds;
1515 for (auto const &dataIt : zone.m_idToEntryMap) {
1516 auto const &id=dataIt.first;
1517 auto const &entry=dataIt.second;
1518 input->seek(entry.begin()+10, librevenge::RVNG_SEEK_SET);
1519 f.str("");
1520 f << "Entries(Picture)[Z" << zone.m_id << "]:id=" << id << ",";
1521 switch (id) {
1522 case 1: {
1523 if (entry.length()!=10+30) {
1524 MWAW_DEBUG_MSG(("StudentWritingCParser::readPicture: the first entry seems bad\n"));
1525 f << "###";
1526 break;
1527 }
1528 StudentWritingCParserInternal::PictureStruct pictEntry;
1529 float fDim[4];
1530 for (auto &d : fDim) d=float(input->readLong(2))/20; // checkme unit?
1531 pictEntry.m_box=MWAWBox2f(MWAWVec2f(fDim[0],fDim[1]), MWAWVec2f(fDim[2],fDim[3]));
1532 f << "box=" << pictEntry.m_box << ",";
1533 for (int i=0; i<4; ++i) { // 0
1534 int val=int(input->readLong(2));
1535 if (val)
1536 f << "f" << i << "=" << val << ",";
1537 }
1538 int dim[2];
1539 for (auto &d : dim) d=int(input->readLong(2));
1540 f << "res?=" << MWAWVec2i(dim[0],dim[1]) << ",";
1541 int val=int(input->readULong(2)); // 0|10|12
1542 if (val) f << "fl=" << std::hex << val << std::dec << ",";
1543 val=int(input->readULong(2));
1544 if (val!=2) f << "f4=" << val << ",";
1545 int cIds[3];
1546 for (int &cId : cIds) cId=int(input->readLong(2));
1547 pictEntry.m_ids[0]=cIds[0];
1548 pictEntry.m_ids[1]=cIds[2];
1549 if (cIds[0]==zone.m_id && cIds[1]==6)
1550 pictIds.insert(cIds[2]);
1551 else {
1552 MWAW_DEBUG_MSG(("StudentWritingCParser::readPicture: reading picture in other zone is not implemented\n"));
1553 f << "###";
1554 }
1555 f << "Z" << cIds[0] << ":" << cIds[2] << ",";
1556 zone.m_idToPictureMap[id]=pictEntry;
1557 break;
1558 }
1559 default:
1560 if (pictIds.find(id)==pictIds.end()) {
1561 MWAW_DEBUG_MSG(("StudentWritingCParser::readPicture: find unknown id=%d\n", id));
1562 f << "###";
1563 break;
1564 }
1565 if (entry.length()<10+20) {
1566 MWAW_DEBUG_MSG(("StudentWritingCParser::readPicture: the picture size seems to short\n"));
1567 f << "###";
1568 break;
1569 }
1570 std::shared_ptr<MWAWPict> pict(MWAWPictData::get(input, int(entry.length()-10)));
1571 MWAWEmbeddedObject object;
1572 if (pict && pict->getBinary(object) && !object.m_dataList.empty()) {
1573 zone.m_idToObjectMap[id]=object;
1574 #ifdef DEBUG_WITH_FILES
1575 static int volatile pictName = 0;
1576 libmwaw::DebugStream f2;
1577 f2 << "PICT-" << ++pictName << ".pct";
1578 libmwaw::Debug::dumpFile(object.m_dataList[0], f2.str().c_str());
1579 ascii().skipZone(entry.begin()+10, entry.end()-1);
1580 #endif
1581 }
1582 break;
1583 }
1584 if (input->tell()!=entry.end())
1585 ascii().addDelimiter(input->tell(),'|');
1586 ascii().addPos(entry.begin());
1587 ascii().addNote(f.str().c_str());
1588 }
1589 return true;
1590 }
1591
1592 bool StudentWritingCParser::readFontsList(MWAWEntry const &entry)
1593 {
1594 MWAWInputStreamPtr input = getInput();
1595 long endPos=entry.end();
1596 if (entry.length()<10+36 || !input->checkPosition(endPos)) {
1597 MWAW_DEBUG_MSG(("StudentWritingCParser::readFontsList: the entry seems too short\n"));
1598 return false;
1599 }
1600 input->seek(entry.begin()+10, librevenge::RVNG_SEEK_SET);
1601 libmwaw::DebugStream f;
1602 f << "Entries(Fonts):";
1603 bool isMain=true;
1604 if (entry.id()!=m_state->m_ids[1].first) {
1605 static bool first=true;
1606 if (first) {
1607 MWAW_DEBUG_MSG(("StudentWritingCParser::readFontsList: find multiple fonts list zones\n"));
1608 first=false;
1609 }
1610 f << "zone=Z" << entry.id() << ",";
1611 isMain=false;
1612 }
1613 int val=int(input->readLong(2));
1614 if (val!=0x14)
1615 f << "f0=" << val << ",";
1616 int N=int(input->readLong(2));
1617 f << "N=" << N << ",";
1618 if ((entry.length()-10-36)/34<N || N<0) {
1619 f << "###";
1620 MWAW_DEBUG_MSG(("StudentWritingCParser::readFontsList: can not read the number of entries\n"));
1621 ascii().addPos(entry.begin());
1622 ascii().addNote(f.str().c_str());
1623 return true;
1624 }
1625 for (int i=0; i<16; ++i) {
1626 val=int(input->readLong(2));
1627 if (val)
1628 f << "f" << i+1 << "=" << val << ",";
1629 }
1630 ascii().addPos(entry.begin());
1631 ascii().addNote(f.str().c_str());
1632
1633 for (int i=0; i<N; ++i) {
1634 long pos=input->tell();
1635 f.str("");
1636 f << "Fonts-FN" << i << ":";
1637 f << "f0=" << input->readLong(2) << ","; // unsure, number between -500 and 600, can be dupplicated in the same files
1638 std::string name;
1639 for (int j=0; j<32; ++j) {
1640 char c=char(input->readLong(1));
1641 if (!c)
1642 break;
1643 name+=c;
1644 }
1645 if (!name.empty() && isMain)
1646 m_state->m_idToFontNameMap[i]=name;
1647 f << name << ",";
1648 input->seek(pos+34, librevenge::RVNG_SEEK_SET);
1649 ascii().addPos(pos);
1650 ascii().addNote(f.str().c_str());
1651 }
1652
1653 if (input->tell()!=endPos) {
1654 MWAW_DEBUG_MSG(("StudentWritingCParser::readFontsList: find extra data\n"));
1655 ascii().addPos(input->tell());
1656 ascii().addNote("Fonts-Extra:###");
1657 }
1658 return true;
1659 }
1660
1661 ////////////////////////////////////////////////////////////
1662 // read the print info
1663 ////////////////////////////////////////////////////////////
1664 bool StudentWritingCParser::readPrintInfo()
1665 {
1666 MWAWInputStreamPtr input = getInput();
1667 long pos = input->tell();
1668 if (!input->checkPosition(pos+0x78)) {
1669 MWAW_DEBUG_MSG(("StudentWritingCParser::readPrintInfo: the entry seems too short\n"));
1670 return false;
1671 }
1672 libmwaw::DebugStream f;
1673 // print info
1674 libmwaw::PrinterInfo info;
1675 if (!info.read(input)) return false;
1676 f << "Entries(PrintInfo):"<< info;
1677
1678 MWAWVec2i paperSize = info.paper().size();
1679 MWAWVec2i pageSize = info.page().size();
1680 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
1681 paperSize.x() <= 0 || paperSize.y() <= 0) {
1682 f << "###";
1683 ascii().addPos(pos);
1684 ascii().addNote(f.str().c_str());
1685 return true;
1686 }
1687
1688 // define margin from print info
1689 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
1690 MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
1691
1692 // move margin left | top
1693 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
1694 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
1695 lTopMargin -= MWAWVec2i(decalX, decalY);
1696 rBotMargin += MWAWVec2i(decalX, decalY);
1697
1698 // decrease right | bottom
1699 int rightMarg = rBotMargin.x() -50;
1700 if (rightMarg < 0) rightMarg=0;
1701 int botMarg = rBotMargin.y() -50;
1702 if (botMarg < 0) botMarg=0;
1703
1704 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
1705 getPageSpan().setMarginBottom(botMarg/72.0);
1706 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
1707 getPageSpan().setMarginRight(rightMarg/72.0);
1708 getPageSpan().setFormLength(paperSize.y()/72.);
1709 getPageSpan().setFormWidth(paperSize.x()/72.);
1710
1711 if (input->tell()!=pos+0x78)
1712 ascii().addDelimiter(input->tell(),'|');
1713 ascii().addPos(pos);
1714 ascii().addNote(f.str().c_str());
1715
1716 return true;
1717 }
1718
1719 ////////////////////////////////////////////////////////////
1720 //
1721 // Low level
1722 //
1723 ////////////////////////////////////////////////////////////
1724
1725 ////////////////////////////////////////////////////////////
1726 // read the header
1727 ////////////////////////////////////////////////////////////
1728 bool StudentWritingCParser::checkHeader(MWAWHeader *header, bool /*strict*/)
1729 {
1730 *m_state = StudentWritingCParserInternal::State();
1731
1732 MWAWInputStreamPtr input = getInput();
1733 if (!input || !input->hasDataFork())
1734 return false;
1735
1736 if (!input->checkPosition(352+120)) {
1737 MWAW_DEBUG_MSG(("StudentWritingCParser::checkHeader: file is too short\n"));
1738 return false;
1739 }
1740 input->seek(0,librevenge::RVNG_SEEK_SET);
1741 if (input->readULong(4) != 0x1a544c43)
1742 return false;
1743 int val=int(input->readULong(1));
1744 #ifndef DEBUG
1745 if (val<2 || val>3)
1746 return false;
1747 #else
1748 if (val>5) {
1749 MWAW_DEBUG_MSG(("StudentWritingCParser::checkHeader: unexpected document type\n"));
1750 return false;
1751 }
1752 #endif
1753 if (input->readULong(2) != 0x4646)
1754 return false;
1755
1756 ascii().addPos(0);
1757 ascii().addNote("FileHeader:");
1758
1759 if (header)
1760 header->reset(MWAWDocument::MWAW_T_STUDENTWRITING, 1);
1761
1762 return true;
1763 }
1764
1765 ////////////////////////////////////////////////////////////
1766 // send data
1767 ////////////////////////////////////////////////////////////
1768 bool StudentWritingCParser::sendZone(int id)
1769 {
1770 if (m_state->m_sendZoneSet.find(id)!=m_state->m_sendZoneSet.end()) {
1771 MWAW_DEBUG_MSG(("StudentWritingCParser::sendZone: oops, find a loop for zone %d\n", id));
1772 return false;
1773 }
1774
1775 auto input=getInput();
1776 auto listener=getTextListener();
1777 if (!input || !listener) {
1778 MWAW_DEBUG_MSG(("StudentWritingCParser::sendZone: called with no listener\n"));
1779 return false;
1780 }
1781
1782 auto const &it=m_state->m_idToZoneMap.find(id);
1783 if (it==m_state->m_idToZoneMap.end() || !it->second) {
1784 MWAW_DEBUG_MSG(("StudentWritingCParser::sendZone: unknown zone=%d\n", id));
1785 return false;
1786 }
1787
1788 auto const &zone=*it->second;
1789 if (zone.m_type!=5) {
1790 MWAW_DEBUG_MSG(("StudentWritingCParser::sendZone: sending a zone with type=%d is not implemented\n", zone.m_type));
1791 return false;
1792 }
1793 bool isMainZone=id==m_state->m_ids[0].first;
1794 if (zone.m_idToDataMap.find(1)==zone.m_idToDataMap.end()) {
1795 MWAW_DEBUG_MSG(("StudentWritingCParser::sendZone: can not find the main sub zone\n"));
1796 return false;
1797 }
1798
1799 m_state->m_sendZoneSet.insert(id);
1800 auto const &mainData=zone.m_idToDataMap.find(1)->second;
1801 //
1802 // background picture
1803 //
1804 if (mainData.m_ids[8].first) {
1805 auto const &pictIt=m_state->m_idToZoneMap.find(mainData.m_ids[8].first);
1806 if (pictIt==m_state->m_idToZoneMap.end() || !pictIt->second || pictIt->second->m_type!=6) {
1807 MWAW_DEBUG_MSG(("StudentWritingCParser::sendZone[background]: can not find picture=%d\n", mainData.m_ids[8].first));
1808 }
1809 else if (!m_state->m_sendBoxesStack.empty()) {
1810 // FIXME: the text hides the picture...
1811 MWAWPosition pos(MWAWVec2f(0,0), m_state->m_sendBoxesStack.top().size(), librevenge::RVNG_POINT);
1812 pos.setRelativePosition(MWAWPosition::Frame);
1813 pos.m_wrapping = MWAWPosition::WBackground;
1814
1815 sendPicture(pos, mainData.m_ids[8].first);
1816 }
1817 else {
1818 MWAW_DEBUG_MSG(("StudentWritingCParser::sendZone: oops, sending background picture is not implemented\n"));
1819 }
1820 }
1821 //
1822 // FRAMES
1823 //
1824 auto const &frameIt=m_state->m_idToZoneMap.find(mainData.m_ids[1].first);
1825 if (frameIt==m_state->m_idToZoneMap.end() || !frameIt->second || frameIt->second->m_type!=3) {
1826 MWAW_DEBUG_MSG(("StudentWritingCParser::sendZone: can not find the frame zone=%d\n", mainData.m_ids[1].first));
1827 }
1828 else {
1829 auto const &frames=*frameIt->second;
1830 for (auto const &frame : frames.m_frames) {
1831 MWAWPosition pos(frame.m_boxes[0][0], frame.m_boxes[0].size(), librevenge::RVNG_POINT);
1832 pos.setRelativePosition(isMainZone ? MWAWPosition::Page : MWAWPosition::Frame);
1833 if (isMainZone && frame.m_page)
1834 pos.setPage(frame.m_page+(mainData.m_ids[7].first ? 1 : 0));
1835 pos.m_wrapping = MWAWPosition::WDynamic;
1836
1837 if (frame.m_id.second==5) {
1838 m_state->m_sendBoxesStack.push(frame.m_boxes[0]);
1839 auto subdoc=std::make_shared<StudentWritingCParserInternal::SubDocument>(*this, input,frame.m_id.first);
1840 listener->insertTextBox(pos, subdoc);
1841 m_state->m_sendBoxesStack.pop();
1842 }
1843 else if (frame.m_id.second==6)
1844 sendPicture(pos, frame.m_id.first);
1845 else {
1846 MWAW_DEBUG_MSG(("StudentWritingCParser::sendZone: find unexpected sub zone type=%d\n", frame.m_id.second));
1847 }
1848 }
1849 }
1850 if (mainData.m_ids[7].first) {
1851 // we need to retrieve the title page
1852 auto const &titleIt=m_state->m_idToZoneMap.find(mainData.m_ids[7].first);
1853 if (titleIt==m_state->m_idToZoneMap.end() || !titleIt->second || titleIt->second->m_type!=1) {
1854 MWAW_DEBUG_MSG(("StudentWritingCParser::sendZone[background]: can not find title page=%d\n", mainData.m_ids[7].first));
1855 }
1856 else {
1857 sendText(*titleIt->second, zone, id==m_state->m_ids[0].first);
1858 listener->insertBreak(MWAWListener::PageBreak);
1859 }
1860 }
1861
1862 auto const &pageIt=m_state->m_idToZoneMap.find(mainData.m_ids[6].first);
1863 if (pageIt==m_state->m_idToZoneMap.end() || !pageIt->second || pageIt->second->m_type!=1) {
1864 MWAW_DEBUG_MSG(("StudentWritingCParser::sendZone[background]: can not find text page=%d\n", mainData.m_ids[6].first));
1865 return false;
1866 }
1867 bool result=sendText(*pageIt->second, zone, id==m_state->m_ids[0].first);
1868
1869 if (id==m_state->m_ids[0].first && frameIt!=m_state->m_idToZoneMap.end() &&
1870 !frameIt->second->m_idToFrameBiblioMap.empty()) {
1871 listener->insertBreak(MWAWListener::PageBreak);
1872 // reset to default
1873 listener->setFont(MWAWFont());
1874 MWAWParagraph para;
1875 para.m_justify=MWAWParagraph::JustificationCenter;
1876 listener->setParagraph(para);
1877 listener->insertUnicodeString("Bibliography");
1878 listener->insertEOL();
1879 para.m_justify=MWAWParagraph::JustificationLeft;
1880 listener->setParagraph(para);
1881 listener->insertEOL();
1882 for (auto const &biblioIt : frameIt->second->m_idToFrameBiblioMap) {
1883 listener->insertUnicode(0x2022);
1884 listener->insertChar(' ');
1885 listener->insertUnicodeString(biblioIt.second);
1886 listener->insertChar('.');
1887 listener->insertEOL();
1888 }
1889 }
1890 m_state->m_sendZoneSet.erase(id);
1891 return result;
1892 }
1893
1894 bool StudentWritingCParser::sendPicture(MWAWPosition const &pos, int id)
1895 {
1896 auto listener=getTextListener();
1897 auto it=m_state->m_idToZoneMap.find(id);
1898 if (!listener || it==m_state->m_idToZoneMap.end() || !it->second || it->second->m_type!=6) {
1899 MWAW_DEBUG_MSG(("StudentWritingCParser::sendPicture: can not find picture %d\n", id));
1900 return false;
1901 }
1902 auto const *zone=it->second.get();
1903 auto const &pIt=zone->m_idToPictureMap.find(1);
1904 if (pIt==zone->m_idToPictureMap.end()) {
1905 MWAW_DEBUG_MSG(("StudentWritingCParser::sendPicture: can not find the picture header for id=%d\n", id));
1906 return false;
1907 }
1908
1909 int ids[]= {pIt->second.m_ids[0], pIt->second.m_ids[1]};
1910 if (ids[0]!=id) {
1911 it=m_state->m_idToZoneMap.find(ids[0]);
1912 if (it==m_state->m_idToZoneMap.end() || !it->second || it->second->m_type!=6) {
1913 MWAW_DEBUG_MSG(("StudentWritingCParser::sendPicture: can not find the picture final zone %d\n", ids[0]));
1914 return false;
1915 }
1916 zone=it->second.get();
1917 }
1918 auto const &objIt=zone->m_idToObjectMap.find(ids[1]);
1919 if (objIt==zone->m_idToObjectMap.end()) {
1920 MWAW_DEBUG_MSG(("StudentWritingCParser::sendPicture: can not find the embedded picture in zone %d[%d]\n", ids[0], ids[1]));
1921 return false;
1922 }
1923 listener->insertPicture(pos,objIt->second);
1924 return true;
1925 }
1926
1927 bool StudentWritingCParser::sendText(StudentWritingCParserInternal::Zone const &textZone,
1928 StudentWritingCParserInternal::Zone const &zone, bool isMain)
1929 {
1930 auto input=getInput();
1931 auto listener=getTextListener();
1932 if (!input || !listener || zone.m_type!=5 || textZone.m_type!=1 || zone.m_idToDataMap.find(1)==zone.m_idToDataMap.end()) {
1933 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: called with bad data\n"));
1934 return false;
1935 }
1936
1937 auto const &mainData=zone.m_idToDataMap.find(1)->second;
1938
1939 // retrieve the paragraph zone
1940 auto const &paraIt=m_state->m_idToZoneMap.find(mainData.m_ids[2].first);
1941 std::map<int, MWAWParagraph> const *paraIdMap=nullptr;
1942 if (paraIt==m_state->m_idToZoneMap.end() || !paraIt->second || paraIt->second->m_type!=4) {
1943 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: can not find the para zone=%d\n", mainData.m_ids[2].first));
1944 }
1945 else
1946 paraIdMap=&paraIt->second->m_idToParagraphMap;
1947 // retrieve the list of sub zone
1948 auto const &subIt=m_state->m_idToZoneMap.find(mainData.m_ids[0].first);
1949 std::map<int,StudentWritingCParserInternal::PageStruct const *> pageLimits;
1950 if (subIt==m_state->m_idToZoneMap.end() || !subIt->second || subIt->second->m_type!=2) {
1951 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: can not find the sub zone=%d\n", mainData.m_ids[0].first));
1952 }
1953 else {
1954 for (auto const &p : subIt->second->m_idToPageMap)
1955 pageLimits[p.second.m_firstChar] = &p.second;
1956 }
1957 // retrieve the note entries, dates
1958 auto const &frameIt=m_state->m_idToZoneMap.find(mainData.m_ids[1].first);
1959 std::vector<std::array<int,3> > dates;
1960 std::map<int, StudentWritingCParserInternal::ZoneEntry> const *idToFrameNoteMap=nullptr;
1961 if (frameIt==m_state->m_idToZoneMap.end() || !frameIt->second || frameIt->second->m_type!=3) {
1962 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: can not find the frame zone=%d\n", mainData.m_ids[1].first));
1963 }
1964 else {
1965 dates=frameIt->second->m_frameDates;
1966 idToFrameNoteMap=&frameIt->second->m_idToFrameNoteMap;
1967 }
1968
1969 auto const &mainIt=textZone.m_idToEntryMap.find(1);
1970 if (mainIt==textZone.m_idToEntryMap.end() || !mainIt->second.valid() || mainIt->second.length()<10+10) {
1971 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: can not find the main zone\n"));
1972 return false;
1973 }
1974 auto const &entry=mainIt->second;
1975 input->seek(entry.begin()+10+2, librevenge::RVNG_SEEK_SET);
1976
1977 int begPos[2];
1978 int N[2];
1979 for (int i=0; i<2; ++i) {
1980 begPos[i]=int(input->readULong(2));
1981 N[i]=int(input->readLong(2));
1982 if (begPos[i]<10 || N[i]<0 || (entry.length()-10-begPos[i])/10<N[i] || begPos[i]+10+10*N[i]>entry.length()) {
1983 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: a sub zone seems bad\n"));
1984 N[i]=0;
1985 }
1986 }
1987
1988 input->seek(entry.begin()+10+begPos[0], librevenge::RVNG_SEEK_SET);
1989 std::vector<std::array<int,5> > textData;
1990 std::array<int,5> values; // text: zone[id], num[max], num[char], 0, 0
1991 for (int n=0; n<N[0]; ++n) {
1992 for (auto &v : values) v=int(input->readLong(2));
1993 textData.push_back(values);
1994 }
1995
1996 input->seek(entry.begin()+10+begPos[1], librevenge::RVNG_SEEK_SET);
1997 std::vector<std::array<int,5> > styleData;
1998 // style: zone[id], num[max], num[style], 0, num[char]
1999 for (int n=0; n<N[1]; ++n) {
2000 for (auto &v : values) v=int(input->readLong(2));
2001 styleData.push_back(values);
2002 }
2003
2004 // first retrieve the styles data
2005 std::vector<std::array<int,3> > styles; // type, numChar, values
2006 for (auto const &st : styleData) {
2007 auto const &stIt=textZone.m_idToEntryMap.find(st[0]);
2008 if (stIt==textZone.m_idToEntryMap.end() || !stIt->second.valid() || st[2]<0 || stIt->second.length()<10+6*st[2]) {
2009 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: can not find style zone=%d\n", st[0]));
2010 break;
2011 }
2012 input->seek(stIt->second.begin()+10, librevenge::RVNG_SEEK_SET);
2013 std::array<int,3> data;
2014 for (int i=0; i<st[2]; ++i) {
2015 for (auto &d : data) d=int(input->readULong(2));
2016 styles.push_back(data);
2017 }
2018 }
2019
2020 // now retrieve the text
2021 int actChar=0, actStyleChar=0;
2022 std::array<int,3> actDate= {{0,0,0}};
2023 auto stIt=styles.begin();
2024 MWAWFont font;
2025 int numColumns=1;
2026 for (auto const &txt : textData) {
2027 auto const &txtIt=textZone.m_idToEntryMap.find(txt[0]);
2028 if (txtIt==textZone.m_idToEntryMap.end() || !txtIt->second.valid() || txt[2]<0 || txtIt->second.length()<10+txt[2]) {
2029 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: can not find text zone=%d\n", txt[0]));
2030 actChar=-1;
2031 continue;
2032 }
2033 input->seek(txtIt->second.begin()+10, librevenge::RVNG_SEEK_SET);
2034 for (int i=0; i<txt[2]; ++i) {
2035 auto const &sectIt=pageLimits.find(actChar);
2036 if (sectIt!=pageLimits.end() && sectIt->second) {
2037 auto const &page=*sectIt->second;
2038 if (page.m_numColumns!=numColumns && page.m_numColumns>=1) {
2039 if (listener->isSectionOpened())
2040 listener->closeSection();
2041 numColumns=page.m_numColumns;
2042 if (numColumns>1) {
2043 MWAWSection section;
2044 // CHANGEME: use margins, etc...
2045 section.setColumns(numColumns, getPageSpan().getPageWidth()/double(numColumns), librevenge::RVNG_INCH);
2046 listener->openSection(section);
2047 }
2048 }
2049 }
2050 bool isSpecialChar=false;
2051 while (actStyleChar<=actChar && stIt!=styles.end()) {
2052 auto data=*(stIt++);
2053 if (data[0]==1) {
2054 isSpecialChar=true; // with 0xb
2055 if (!idToFrameNoteMap || idToFrameNoteMap->find(data[2])==idToFrameNoteMap->end()) {
2056 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: can not retrieve the note=%d\n", data[2]));
2057 }
2058 else {
2059 MWAWSubDocumentPtr subdoc(new StudentWritingCParserInternal::SubDocument(*this, input, idToFrameNoteMap->find(data[2])->second.first));
2060 listener->insertNote(MWAWNote(MWAWNote::EndNote), subdoc);
2061 }
2062 }
2063 else if (data[0]==2) {
2064 isSpecialChar=true; // with 0x7
2065 if (data[2]>0 && data[2]<=int(dates.size()))
2066 actDate=dates[size_t(data[2]-1)];
2067 else {
2068 if (data[2]) {
2069 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: can not retrieve the actual date=%d\n", data[2]));
2070 }
2071 actDate= {{0,0,0}};
2072 }
2073 }
2074 else if (data[0]>=0x10 && data[0]<=0x18) {
2075 uint32_t flags=font.flags();
2076 switch (data[0]) {
2077 case 0x10:
2078 case 0x11:
2079 case 0x15:
2080 case 0x18: {
2081 uint32_t fl=data[0]==0x10 ? MWAWFont::boldBit :
2082 data[0]==0x11 ? MWAWFont::italicBit :
2083 data[0]==0x15 ? MWAWFont::outlineBit : MWAWFont::shadowBit;
2084 if (data[2]==1)
2085 flags|=fl;
2086 else if (data[2]==0)
2087 flags&=uint32_t(~fl);
2088 else {
2089 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: unexpected flag\n"));
2090 }
2091 break;
2092 }
2093 case 0x12:
2094 if (data[2]==1)
2095 font.setUnderlineStyle(MWAWFont::Line::Simple);
2096 else if (data[2]==0)
2097 font.setUnderlineStyle(MWAWFont::Line::None);
2098 else {
2099 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: unexpected underline flag\n"));
2100 }
2101 break;
2102 case 0x13: {
2103 auto const &nameIt=m_state->m_idToFontNameMap.find(data[2]);
2104 if (nameIt==m_state->m_idToFontNameMap.end()) {
2105 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: can not find font FN%d\n", data[2]));
2106 break;
2107 }
2108 if (m_state->m_idToFontNameUsed.find(data[2])==m_state->m_idToFontNameUsed.end()) {
2109 getFontConverter()->setCorrespondance(data[2], nameIt->second);
2110 m_state->m_idToFontNameUsed.insert(data[2]);
2111 }
2112 font.setId(data[2]);
2113 break;
2114 }
2115 case 0x14:
2116 if (data[2]<=0) {
2117 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: the font size=%d seems bad\n", data[2]));
2118 break;
2119 }
2120 font.setSize(float(data[2])/10);
2121 break;
2122 case 0x16: {
2123 MWAWColor color;
2124 if (m_state->getColor(data[2], color))
2125 font.setColor(color);
2126 else
2127 font.setColor(MWAWColor::black());
2128 break;
2129 }
2130 case 0x17:
2131 switch (data[2]) {
2132 case 0:
2133 font.set(MWAWFont::Script());
2134 break;
2135 case 1:
2136 font.set(MWAWFont::Script::super100());
2137 break;
2138 case 2:
2139 font.set(MWAWFont::Script::sub100());
2140 break;
2141 default:
2142 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: unknown script=%d\n", data[2]));
2143 break;
2144 }
2145 break;
2146 default:
2147 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: unexpected type=%d\n", data[0]));
2148 break;
2149 }
2150 font.setFlags(flags);
2151 listener->setFont(font);
2152 }
2153 else if (data[0]>=0x20 && data[0]<=0x22) {
2154 isSpecialChar=true; // 0xb
2155 if (data[0]==0x20)
2156 listener->insertField(MWAWField(MWAWField::PageNumber));
2157 else if (data[0]==0x21) {
2158 if (actDate[0]) { // fixme, use a real date field with a fixed date
2159 std::stringstream s;
2160 s << actDate[1] << "/" << actDate[2] << "/" << actDate[0]-1;
2161 listener->insertUnicodeString(librevenge::RVNGString(s.str().c_str()));
2162 }
2163 else {
2164 MWAWField date(MWAWField::Date);
2165 date.m_DTFormat = "%a, %b %d, %Y";
2166 listener->insertField(date);
2167 }
2168 }
2169 else
2170 listener->insertUnicode(0x2022);
2171 }
2172 else if (data[0]==0x100) {
2173 if (paraIdMap) {
2174 auto const &it=paraIdMap->find(data[2]);
2175 if (it!=paraIdMap->end())
2176 listener->setParagraph(it->second);
2177 }
2178 else {
2179 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: unknown paragraph id=%d\n", data[2]));
2180 }
2181 }
2182 else if (data[0]==0x300) { // column break
2183 if (data[2]>0 && data[2]<=numColumns)
2184 listener->insertBreak(MWAWListener::ColumnBreak);
2185 }
2186 else if (data[0]==0x500) {
2187 if (isMain)
2188 listener->insertBreak(MWAWListener::PageBreak);
2189 }
2190 else {
2191 }
2192 actStyleChar+=data[1];
2193 }
2194 unsigned char c=(unsigned char)(input->readULong(1));
2195 switch (c) {
2196 case 0x9:
2197 listener->insertTab();
2198 break;
2199 case 0xd:
2200 listener->insertEOL();
2201 break;
2202 default:
2203 if (c<0x1f) {
2204 if (!isSpecialChar) {
2205 MWAW_DEBUG_MSG(("StudentWritingCParser::sendText: find odd char c=%d\n", int(c)));
2206 }
2207 }
2208 else
2209 listener->insertCharacter(c);
2210 break;
2211 }
2212 if (actChar>=0) ++actChar;
2213 }
2214 }
2215 if (listener->isSectionOpened())
2216 listener->closeSection();
2217 return true;
2218 }
2219
2220
2221 ////////////////////////////////////////////////////////////
2222 // decoder
2223 ////////////////////////////////////////////////////////////
2224 namespace StudentWritingCParserInternal
2225 {
2226 /** a basic LWZ decoder
2227
2228 \note this code is freely inspired from https://github.com/MichaelDipperstein/lzw GLP 3
2229 */
2230 struct LWZDecoder {
2231 static int const e_firstCode=(1<<8);
2232 static int const e_maxCodeLen=14;
2233 static int const e_maxCode=(1<<e_maxCodeLen);
2234
2235 //! constructor
2236 LWZDecoder(unsigned char const *data, unsigned long len)
2237 : m_data(data)
2238 , m_len(len)
2239
2240 , m_pos(0)
2241 , m_bit(0)
2242 , m_dictionary()
2243 {
2244 initDictionary();
2245 }
2246
2247 //! decode data
2248 bool decode(std::vector<unsigned char> &output);
2249 protected:
2250 void initDictionary()
2251 {
2252 m_dictionary.resize(2); // 100 and 101
2253 m_dictionary.reserve(e_maxCode - e_firstCode); // max table 4000
2254 }
2255
2256 unsigned getBit() const
2257 {
2258 if (m_pos>=m_len)
2259 throw libmwaw::ParseException();
2260 unsigned val=(m_data[m_pos]>>(7-m_bit++))&1;
2261 if (m_bit==8) {
2262 ++m_pos;
2263 m_bit=0;
2264 }
2265 return val;
2266 }
2267 unsigned getCodeWord(unsigned codeLen) const
2268 {
2269 unsigned code=0;
2270 for (unsigned i=0; i<codeLen;) {
2271 if (m_bit==0 && (codeLen-i)>=8 && m_pos<m_len) {
2272 code = (code<<8) | unsigned(m_data[m_pos++]);
2273 i+=8;
2274 continue;
2275 }
2276 code = (code<<1) | getBit();
2277 ++i;
2278 }
2279 return code;
2280 }
2281
2282 struct LWZEntry {
2283 //! constructor
2284 LWZEntry(unsigned int prefixCode=0, unsigned char suffix=0)
2285 : m_suffix(suffix)
2286 , m_prefixCode(prefixCode)
2287 {
2288 }
2289 /** last char in encoded string */
2290 unsigned char m_suffix;
2291 /** code for remaining chars in string */
2292 unsigned int m_prefixCode;
2293 };
2294
2295 unsigned char decodeRec(unsigned int code, std::vector<unsigned char> &output)
2296 {
2297 unsigned char c;
2298 unsigned char firstChar;
2299
2300 if (code >= e_firstCode) {
2301 if (code-e_firstCode >= m_dictionary.size()) {
2302 MWAW_DEBUG_MSG(("StudentWritingCParserInternal::LWZDecoder::decodeRec: bad id=%x/%x\n", code, unsigned(m_dictionary.size())));
2303 throw libmwaw::ParseException();
2304 }
2305 /* code word is string + c */
2306 c = m_dictionary[code - e_firstCode].m_suffix;
2307 code = m_dictionary[code - e_firstCode].m_prefixCode;
2308
2309 /* evaluate new code word for remaining string */
2310 firstChar = decodeRec(code, output);
2311 }
2312 else /* code word is just c */
2313 firstChar = c = (unsigned char)code;
2314
2315 output.push_back(c);
2316 return firstChar;
2317 }
2318
2319 LWZDecoder(LWZDecoder const &)=delete;
2320 LWZDecoder &operator=(LWZDecoder const &)=delete;
2321 unsigned char const *m_data;
2322 unsigned long m_len;
2323 mutable unsigned long m_pos, m_bit;
2324
2325 std::vector<LWZEntry> m_dictionary;
2326 };
2327
2328 bool LWZDecoder::decode(std::vector<unsigned char> &output)
2329 try
2330 {
2331 unsigned int currentCodeLen = 9;
2332 unsigned lastCode=0;
2333 unsigned char c=(unsigned char) 0;
2334 unsigned endDictCode=0x1ff;
2335 bool first=true;
2336 while (true) {
2337 unsigned code=getCodeWord(currentCodeLen);
2338 if (code==0x100) {
2339 initDictionary();
2340 currentCodeLen = 9;
2341 endDictCode=0x1ff;
2342
2343 lastCode=0;
2344 c=(unsigned char) 0;
2345 first=true;
2346 continue;
2347 }
2348 if (code==0x101) { // end of code
2349 if (m_pos+2<m_len) {
2350 MWAW_DEBUG_MSG(("StudentWritingCParserInternal::LWZDecoder::decode: unexpected end at position %ld/%ld\n", m_pos, m_len));
2351 }
2352 break;
2353 }
2354 if (code < e_firstCode+m_dictionary.size())
2355 /* we have a known code. decode it */
2356 c = decodeRec(code, output);
2357 else {
2358 /***************************************************************
2359 * We got a code that's not in our dictionary. This must be due
2360 * to the string + char + string + char + string exception.
2361 * Build the decoded string using the last character + the
2362 * string from the last code.
2363 ***************************************************************/
2364 unsigned char tmp = c;
2365 c = decodeRec(lastCode, output);
2366 output.push_back(tmp);
2367 }
2368 /* if room, add new code to the dictionary */
2369 if (!first && m_dictionary.size()+e_firstCode < e_maxCode-1) {
2370 if (lastCode>=e_firstCode+m_dictionary.size()) {
2371 MWAW_DEBUG_MSG(("StudentWritingCParserInternal::LWZDecoder::decode: oops a loop with %x/%x\n", lastCode, unsigned(m_dictionary.size())));
2372 break;
2373 }
2374 m_dictionary.push_back(LWZEntry(lastCode, c));
2375 if (m_dictionary.size()+e_firstCode>endDictCode) {
2376 ++currentCodeLen;
2377 endDictCode=2*endDictCode+1;
2378 }
2379 }
2380
2381 /* save character and code for use in unknown code word case */
2382 lastCode = code;
2383 first=false;
2384 }
2385 return true;
2386 }
2387 catch (...)
2388 {
2389 return false;
2390 }
2391
2392 }
2393
2394 MWAWInputStreamPtr StudentWritingCParser::decode()
2395 {
2396 MWAWInputStreamPtr bad;
2397 auto input=getInput();
2398 if (m_state->m_isUncompressed)
2399 return input;
2400 long const begPos=0x1d8;
2401 if (!input->checkPosition(begPos)) {
2402 MWAW_DEBUG_MSG(("StudentWritingCParser::decode: the file is too short\n"));
2403 return bad;
2404 }
2405 std::set<long> listBeginPosition;
2406 listBeginPosition.insert(input->size());
2407 input->seek(begPos, librevenge::RVNG_SEEK_SET);
2408 while (!input->isEnd()) {
2409 long pos=input->tell();
2410 if (!input->checkPosition(pos+18) || input->readLong(4)!=0x1a46461a) {
2411 MWAW_DEBUG_MSG(("StudentWritingCParser::decode: oops code break at position %ld\n", pos));
2412 return bad;
2413 }
2414 long nextPos=long(input->readLong(4));
2415 if (nextPos==0) { // ok last position
2416 listBeginPosition.insert(pos);
2417 break;
2418 }
2419 if (nextPos<begPos+18 || !input->checkPosition(nextPos) || listBeginPosition.find(nextPos)!=listBeginPosition.end()) {
2420 MWAW_DEBUG_MSG(("StudentWritingCParser::decode: oops code break at position %ld\n", pos));
2421 return bad;
2422 }
2423 listBeginPosition.insert(pos);
2424 input->seek(nextPos, librevenge::RVNG_SEEK_SET);
2425 }
2426
2427 input->seek(0, librevenge::RVNG_SEEK_SET);
2428 unsigned long read;
2429 const unsigned char *data = input->read((unsigned long)(begPos), read);
2430 if (!data || read != (unsigned long)(begPos)) {
2431 MWAW_DEBUG_MSG(("StudentWritingCParser::decode: can not retrieve the begin data\n"));
2432 return bad;
2433 }
2434
2435 auto stream=std::make_shared<MWAWStringStream>(data, unsigned(begPos));
2436 for (auto posIt=listBeginPosition.begin(); posIt!=listBeginPosition.end();) {
2437 long first=*(posIt++);
2438 if (posIt==listBeginPosition.end()) break;
2439 long end=*posIt;
2440 if (first+18>end) {
2441 MWAW_DEBUG_MSG(("StudentWritingCParser::decode: oops the zone at position %ld seems too short\n", first));
2442 return bad;
2443 }
2444 input->seek(first+8, librevenge::RVNG_SEEK_SET);
2445 long dataSize=long(input->readLong(4));
2446 if (dataSize<0 || dataSize>=10000000) {
2447 MWAW_DEBUG_MSG(("StudentWritingCParser::decode: oops can not read the data size of the zone at position %ld\n", first));
2448 return bad;
2449 }
2450
2451 input->seek(-4, librevenge::RVNG_SEEK_CUR);
2452 data=input->read(10, read);
2453 if (!data || read!=10) {
2454 MWAW_DEBUG_MSG(("StudentWritingCParser::decode: can not retrieve zone's header at position %ld\n", first));
2455 return bad;
2456 }
2457 stream->append(data, 10);
2458
2459 if (dataSize==0)
2460 continue;
2461
2462 data=input->read(size_t(end-18-first), read);
2463 if (!data || read!=(unsigned long)(end-18-first)) {
2464 MWAW_DEBUG_MSG(("StudentWritingCParser::decode: can not retrieve zone %ld-%ld\n", first, end));
2465 continue;
2466 }
2467 StudentWritingCParserInternal::LWZDecoder decoder(data, read);
2468 std::vector<unsigned char> output;
2469 output.reserve(size_t(dataSize));
2470 decoder.decode(output);
2471 if (output.size()!=size_t(dataSize)) {
2472 MWAW_DEBUG_MSG(("StudentWritingCParser::decode: unexpected output size %lx-%lx\n", long(output.size()), long(dataSize)));
2473 continue;
2474 }
2475 stream->append(output.data(), unsigned(output.size()));
2476 }
2477
2478 MWAWInputStreamPtr res(new MWAWInputStream(stream, false));
2479 res->seek(0, librevenge::RVNG_SEEK_SET);
2480 return res;
2481 }
2482
2483 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: