DEADSOFTWARE

Successful read of StdTextModel (including actual text)
[odcread.git] / reader.cc
1 #include <reader.h>
2 #include <alien.h>
4 #include <string>
5 #include <assert.h>
7 namespace odc {
9 Reader::Reader(std::istream &rider): d_rider(rider), d_cancelled(false), d_readAlien(false), d_typeList(),
10 d_state(new ReaderState()) {}
12 SHORTCHAR Reader::readSChar() {
13 SHORTCHAR out;
14 d_rider.read(&out, 1);
15 return out;
16 }
18 void Reader::readSChar(SHORTCHAR *buf, size_t len) {
19 d_rider.read(buf, len);
20 }
22 CHAR Reader::readLChar() {
23 CHAR buf;
24 char *bufPtr = (char *)&buf;
25 d_rider.read(bufPtr, 2);
26 if (isLittleEndian()) {
27 return buf;
28 } else {
29 CHAR out;
30 char *outPtr = (char *)&out;
31 outPtr[0] = bufPtr[1]; outPtr[1] = bufPtr[0];
32 return out;
33 }
34 }
36 void Reader::readLChar(CHAR *buf, size_t len) {
37 char *bufPtr = (char *)buf;
38 int len2 = len * 2;
39 d_rider.read(bufPtr, len2);
40 if (isBigEndian()) {
41 char tmp;
42 for (int i = 0; i < len2; i += 2) {
43 tmp = bufPtr[i];
44 bufPtr[i] = bufPtr[i + 1];
45 bufPtr[i + 1] = tmp;
46 }
47 }
48 }
50 BYTE Reader::readByte() {
51 BYTE out;
52 d_rider.read((char*)&out, 1);
53 return out;
54 }
56 INTEGER Reader::readInt() {
57 INTEGER buf;
58 char *bufPtr = (char*)&buf;
59 d_rider.read(bufPtr, 4);
60 if (isLittleEndian()) {
61 return buf;
62 } else {
63 INTEGER out;
64 char *outPtr = (char *)&out;
65 outPtr[0] = bufPtr[3]; outPtr[1] = bufPtr[2]; outPtr[2] = bufPtr[1]; outPtr[3] = bufPtr[0];
66 return out;
67 }
68 }
70 void Reader::readSString(SHORTCHAR *out) {
71 while (*out = readSChar()) {
72 ++out;
73 }
74 }
76 void Reader::turnIntoAlien(int cause) {
77 assert(cause > 0);
78 d_cancelled = true;
79 d_cause = cause;
80 d_readAlien = true;
81 }
83 bool Reader::isCancelled() {
84 return d_cancelled;
85 }
87 INTEGER Reader::readVersion(INTEGER min, INTEGER max) {
88 INTEGER version = readByte();
89 if (version < min || version > max) {
90 turnIntoAlien(ALIENVERSION);
91 }
92 return version;
93 }
95 Store* Reader::readStore() {
96 SHORTCHAR kind = readSChar();
97 if (kind == Store::NIL) {
98 std::cout << "NIL STORE" << std::endl;
99 return readNilStore();
100 } else if (kind == Store::LINK) {
101 std::cout << "LINK STORE" << std::endl;
102 return readLinkStore();
103 } else if (kind == Store::NEWLINK) {
104 std::cout << "NEWLINK STORE" << std::endl;
105 return readNewLinkStore();
106 } else if (kind == Store::STORE) {
107 std::cout << "STORE STORE" << std::endl;
108 return readStoreOrElemStore(false);
109 } else if (kind == Store::ELEM) {
110 std::cout << "ELEM STORE" << std::endl;
111 return readStoreOrElemStore(true);
112 } else {
113 std::cout << std::hex << (unsigned int)kind << std::endl;
114 throw 20;
117 // PROCEDURE (VAR rd: Reader) ReadStore* (OUT x: Store), NEW;
118 // VAR a: Alien; t: Kernel.Type;
119 // len, pos, pos1, id, comment, next, down, downPos, nextTypeId, nextElemId, nextStoreId: INTEGER;
120 // kind: SHORTCHAR; path: TypePath; type: TypeName;
121 // save: ReaderState;
122 Store *Reader::readNilStore() {
123 return 0;
125 // IF kind = nil THEN
126 // rd.ReadInt(comment); rd.ReadInt(next);
127 // rd.st.end := rd.Pos();
128 // IF (next > 0) OR ((next = 0) & ODD(comment)) THEN rd.st.next := rd.st.end + next ELSE rd.st.next := 0 END;
129 // x := NIL
130 Store *Reader::readLinkStore() {
131 return 0;
133 // ELSIF kind = link THEN
134 // rd.ReadInt(id); rd.ReadInt(comment); rd.ReadInt(next);
135 // rd.st.end := rd.Pos();
136 // IF (next > 0) OR ((next = 0) & ODD(comment)) THEN rd.st.next := rd.st.end + next ELSE rd.st.next := 0 END;
137 // x := ThisStore(rd.eDict, id)
138 Store *Reader::readNewLinkStore() {
139 return 0;
141 // ELSIF kind = newlink THEN
142 // rd.ReadInt(id); rd.ReadInt(comment); rd.ReadInt(next);
143 // rd.st.end := rd.Pos();
144 // IF (next > 0) OR ((next = 0) & ODD(comment)) THEN rd.st.next := rd.st.end + next ELSE rd.st.next := 0 END;
145 // x := ThisStore(rd.sDict, id)
147 Store *Reader::readStoreOrElemStore(bool isElem) {
148 INTEGER id = isElem ? d_elemList.size() : d_storeList.size();
149 TypePath path = readPath();
150 std::cout << path.toString() << std::endl;
151 const std::string &type = path[0];
152 INTEGER comment = readInt();
153 std::streampos pos1 = d_rider.tellg();
154 std::streamoff next = readInt();
155 std::streamoff down = readInt();
156 std::streamoff len = readInt();
157 std::streampos pos = d_rider.tellg();
158 if (next > 0) {
159 d_state->next = pos1 + next + (std::streamoff)4;
160 } else {
161 d_state->next = 0;
163 int downPos = 0;
164 if (down > 0) {
165 downPos = pos1 + down + (std::streamoff)8;
167 d_state->end = pos + len;
168 d_cause = 0;
169 assert(len >= 0);
170 if (next != 0) {
171 assert(d_state->next > pos1);
172 if (down != 0) {
173 assert(downPos < d_state->next);
176 if (down > 0) {
177 assert(downPos > pos1);
178 assert(downPos < d_state->end);
181 const TypeProxyBase *t = TypeRegister::getInstance().get(type); // FIXME type lookup here
182 Store *x = 0;
183 if (t != 0) {
184 x = t->newInstance(id);
185 } else {
186 d_cause = TYPENOTFOUND;
189 if (x != 0) { // IF READING SUCCEEDS, INSERT MORE CHECKS HERE
190 if (true) { // samePath(x, path)
191 ReaderState *save = d_state;
192 d_state = new ReaderState();
193 x->internalize(*this);
194 delete d_state;
195 d_state = save;
197 if (d_cause != 0) {
198 x = 0;
200 assert(d_rider.tellg() == d_state->end);
201 assert(!d_rider.eof());
202 } else {
203 // rd.cause := inconsistentType; AlienTypeReport(rd.cause, type);
204 x = 0;
208 if (x != 0) {
209 if (d_store == 0) {
210 d_store = x;
211 } else {
212 // join(d_store, x)
213 std::cout << "Man, should have written join(.,.)" << std::endl;
215 if (isElem) {
216 d_elemList.push_back(x);
217 } else {
218 d_storeList.push_back(x);
220 } else {
221 d_rider.seekg(pos); // x->internalize() could have left us anywhere
222 assert(d_cause != 0);
223 Alien *alien = new Alien(id, path); //, d_cause); //,file
224 if (d_store == 0) {
225 d_store = alien;
226 } else {
227 // join(d_store, alien)
228 std::cout << "Man, should have written join(.,.)" << std::endl;
230 if (isElem) {
231 d_elemList.push_back(alien);
232 } else {
233 d_storeList.push_back(alien);
235 ReaderState *save = d_state;
236 d_state = new ReaderState();
237 internalizeAlien(alien, downPos, save->end);
238 delete d_state;
239 d_state = save;
240 assert(d_rider.tellg() == d_state->end);
242 // we've just read the alien, so reset the state
243 d_cause = 0;
244 d_cancelled = false;
245 d_readAlien = true;
246 return alien;
249 return x;
253 void Reader::internalizeAlien(Alien *alien, std::streampos down, std::streampos end) {
254 std::streampos next = down != 0 ? down : end;
255 while (d_rider.tellg() < end) {
256 if (d_rider.tellg() < next) { // for some reason, this means its a piece (unstructured)
257 std::cout << "Alien Piece" << std::endl;
258 size_t len = next - d_rider.tellg();
259 char *buf = new char[len];
260 d_rider.read(buf, len);
261 AlienComponent *comp = new AlienPiece(buf, len);
262 alien->getComponents().push_back(comp);
263 } else { // that means we've got a store
264 std::cout << "Alien Store" << std::endl;
265 d_rider.seekg(next);
266 AlienComponent *comp = new AlienPart(readStore());
267 alien->getComponents().push_back(comp);
268 next = d_state->next > 0 ? d_state->next : end;
273 std::string &Reader::fixTypeName(std::string &name) {
274 size_t pos = name.size() - 4;
275 if (pos > 0 && name.substr(pos, 4).compare("Desc") == 0) {
276 return name.replace(pos, 4, "^");
278 return name;
281 TypePath Reader::readPath() {
282 TypePath path;
283 SHORTCHAR kind = readSChar();
284 SHORTCHAR *buf = new SHORTCHAR[64]; // TypeName has a maximum of length 64 (including terminator).
285 int i;
286 for (i = 0; kind == Store::NEWEXT; ++i) {
287 readSString(buf);
288 std::string name(buf);
289 path.push_back(fixTypeName(name));
290 addPathComponent(i == 0, path[i]);
291 // IF path[i] # elemTName THEN INC(i) END;
292 kind = readSChar();
295 if (kind == Store::NEWBASE) {
296 readSString(buf);
297 std::string name(buf);
298 path.push_back(fixTypeName(name));
299 addPathComponent(i == 0, path[i]);
300 ++i;
301 } else if (kind == Store::OLDTYPE) {
302 int id = readInt();
303 if (i > 0) {
304 d_typeList[d_typeList.size() - 1]->baseId = id;
306 while (id != -1) {
307 path.push_back(d_typeList[id]->name);
308 id = d_typeList[id]->baseId;
309 // IF path[i] # elemTName THEN INC(i) END
310 ++i;
312 } else {
313 throw 100;
315 return path;
318 void Reader::addPathComponent(bool first, const std::string &typeName) {
319 int next = d_typeList.size();
320 int curr = next - 1;
321 if (!first) {
322 d_typeList[curr]->baseId = next;
324 d_typeList.push_back(new TypeEntry(typeName));
327 } // namespace odc