Debian Codegraph @

src/lib/NisusWrtStruct.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 "MWAWDebug.hxx"
35 #include "MWAWInputStream.hxx"
36
37 #include "NisusWrtParser.hxx"
38
39 #include "NisusWrtStruct.hxx"
40
41 namespace NisusWrtStruct
42 {
43 std::ostream &operator<< (std::ostream &o, Position const &pos)
44 {
45 o << pos.m_paragraph << "x";
46 if (pos.m_word) o << pos.m_word << "x";
47 else o << "_x";
48 if (pos.m_char) o << pos.m_char;
49 else o << "_";
50
51 return o;
52 }
53
54 std::ostream &operator<< (std::ostream &o, FootnoteInfo const &fnote)
55 {
56 if (fnote.m_flags&0x4) o << "sepPos=right,";
57 if (fnote.m_flags&0x8) o << "endNotes,"; // footnote|endnote
58 if (fnote.m_flags&0x10) o << "renumber[Pages],";
59 if (fnote.m_flags&0x20) o << "maySep,"; // can put note on diff page than ref
60 if (fnote.m_flags&0x40) o << "dontBrk,"; // dont brk a footnote
61 if (fnote.m_flags&0x80) o << "notePos=bottom,";
62 if (fnote.m_flags&0xFF03)
63 o << "fl=" << std::hex << (fnote.m_flags&0xFF03) << std::dec << ",";
64 if (fnote.m_distToDocument != 5)
65 o << "dist=" << fnote.m_distToDocument << "[bef],";
66 if (fnote.m_distSeparator != 36)
67 o << "dist=" << fnote.m_distSeparator << "[between],";
68 if (fnote.m_separatorLength != 108)
69 o << "w[sep]=" << fnote.m_separatorLength << "pt";
70 if (fnote.m_unknown) o << "unkn=" << fnote.m_unknown << ",";
71 return o;
72 }
73
74 ////////////////////////////////////////////////////////////
75 // read a recursive zone
76 ////////////////////////////////////////////////////////////
77 bool RecursifData::read(NisusWrtParser &parser, MWAWEntry const &entry)
78 {
79 if (!m_info || m_info->m_zoneType >= 3) {
80 MWAW_DEBUG_MSG(("NisusWrtStruct::RecursifData::Read: find unexpected zoneType\n"));
81 return false;
82 }
83 if (m_level < 0 || m_level >= 3) {
84 MWAW_DEBUG_MSG(("NisusWrtStruct::RecursifData::Read: find unexpected level: %d\n", m_level));
85 return false;
86 }
87 if (entry.length() < 12) {
88 MWAW_DEBUG_MSG(("NisusWrtStruct::RecursifData::Read: the entry is bad\n"));
89 return false;
90 }
91
92 auto zoneId = int(m_info->m_zoneType);
93 entry.setParsed(true);
94 MWAWInputStreamPtr input = parser.rsrcInput();
95 libmwaw::DebugFile &asciiFile = parser.rsrcAscii();
96 long pos = entry.begin();
97 input->seek(pos, librevenge::RVNG_SEEK_SET);
98
99 libmwaw::DebugStream f;
100 if (m_level == 0) {
101 f << "Entries(" << entry.name() << "):";
102 asciiFile.addPos(pos);
103 asciiFile.addNote(f.str().c_str());
104 }
105 int num = 0;
106 while (input->tell() != entry.end()) {
107 pos = input->tell();
108 bool ok = true;
109 if (pos+12 > entry.end()) {
110 MWAW_DEBUG_MSG(("NisusWrtStruct::RecursifData::Read: can not read entry %d\n", num-1));
111 ok = false;
112 }
113 auto level = static_cast<int>(input->readLong(2));
114 if (level != m_level && level != m_level+1) {
115 MWAW_DEBUG_MSG(("NisusWrtStruct::RecursifData::Read: find unexpected level for entry %d\n", num-1));
116 ok = false;
117 }
118 f.str("");
119 f << entry.name() << level << "-" << num++;
120 if (zoneId) f << "[" << zoneId << "]";
121 f << ":";
122 if (!ok) {
123 f << "###";
124 asciiFile.addPos(pos);
125 asciiFile.addNote(f.str().c_str());
126 return false;
127 }
128 auto val = static_cast<int>(input->readLong(2));
129 f << "unkn=" << val << ","; // always x10 for level<=2 and c for level==3 ?
130 long sz = input->readLong(4);
131 long totalSz = sz;
132 long minSize = 16;
133 if (level == 3) {
134 totalSz += 13;
135 if (totalSz%2) totalSz++;
136 minSize = 14;
137 }
138 long endPos = pos+totalSz;
139 if (totalSz < minSize || endPos > entry.end()) {
140 MWAW_DEBUG_MSG(("NisusWrtStruct::RecursifData::Read: can not read entry %d\n", num-1));
141 f << "###";
142 asciiFile.addPos(pos);
143 asciiFile.addNote(f.str().c_str());
144 return false;
145 }
146
147 auto type = static_cast<int>(input->readLong(4));
148 if ((level == 1 && type == 0x7FFFFEDF) ||
149 (level == 2 && type == 0x7FFFFFFF))
150 ;
151 else if ((type>>16)==0x7FFF)
152 f << "type=" << type-0x7FFFFFFF-1 << ",";
153 else
154 f << "type=" << type << ",";
155
156 if (level != 3) {
157 val = static_cast<int>(input->readULong(4));
158 if ((level==1 && val==0x10) || (level==2 && val == 1))
159 ;
160 else
161 f << "wh=" << val << ",";
162 }
163 asciiFile.addPos(pos);
164 asciiFile.addNote(f.str().c_str());
165
166 Node child;
167 child.m_type = type;
168 child.m_entry = entry;
169 child.m_entry.setBegin(input->tell());
170 child.m_entry.setEnd(endPos);
171
172 if (level == 3) {
173 child.m_entry.setLength(sz);
174 m_childList.push_back(child);
175 input->seek(endPos, librevenge::RVNG_SEEK_SET);
176 continue;
177 }
178
179 if (child.m_entry.length()==0) {
180 if (level != 1) {
181 MWAW_DEBUG_MSG(("NisusWrtStruct::RecursifData::Read: Oops find 0 length child\n"));
182 asciiFile.addPos(pos);
183 asciiFile.addNote("###");
184 }
185 continue;
186 }
187
188 std::shared_ptr<RecursifData> childData(new RecursifData(*this));
189 childData->m_level = level;
190 child.m_data = childData;
191
192 if (!childData->read(parser, child.m_entry)) {
193 MWAW_DEBUG_MSG(("NisusWrtStruct::RecursifData::Read: can not read child entry\n"));
194 asciiFile.addPos(pos);
195 asciiFile.addNote("###");
196 }
197 else
198 m_childList.push_back(child);
199
200 input->seek(endPos, librevenge::RVNG_SEEK_SET);
201 }
202 return true;
203 }
204
205 }
206 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: