DEADSOFTWARE

Read NIL stores correctly
[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 INTEGER comment = readInt();
124 std::streamoff next = readInt();
125 d_state->end = d_rider.tellg();
126 if (next > 0 || (next == 0 && comment % 2 == 1)) {
127 d_state->next = d_state->end + next;
128 } else {
129 d_state->next = 0;
131 return 0;
133 // IF kind = nil THEN
134 // 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 := NIL
138 Store *Reader::readLinkStore() {
139 return 0;
141 // ELSIF kind = link 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.eDict, id)
146 Store *Reader::readNewLinkStore() {
147 return 0;
149 // ELSIF kind = newlink THEN
150 // rd.ReadInt(id); rd.ReadInt(comment); rd.ReadInt(next);
151 // rd.st.end := rd.Pos();
152 // IF (next > 0) OR ((next = 0) & ODD(comment)) THEN rd.st.next := rd.st.end + next ELSE rd.st.next := 0 END;
153 // x := ThisStore(rd.sDict, id)
155 Store *Reader::readStoreOrElemStore(bool isElem) {
156 INTEGER id = isElem ? d_elemList.size() : d_storeList.size();
157 TypePath path = readPath();
158 std::cout << path.toString() << std::endl;
159 const std::string &type = path[0];
160 INTEGER comment = readInt();
161 std::streampos pos1 = d_rider.tellg();
162 std::streamoff next = readInt();
163 std::streamoff down = readInt();
164 std::streamoff len = readInt();
165 std::streampos pos = d_rider.tellg();
166 if (next > 0) {
167 d_state->next = pos1 + next + (std::streamoff)4;
168 } else {
169 d_state->next = 0;
171 int downPos = 0;
172 if (down > 0) {
173 downPos = pos1 + down + (std::streamoff)8;
175 d_state->end = pos + len;
176 d_cause = 0;
177 assert(len >= 0);
178 if (next != 0) {
179 assert(d_state->next > pos1);
180 if (down != 0) {
181 assert(downPos < d_state->next);
184 if (down > 0) {
185 assert(downPos > pos1);
186 assert(downPos < d_state->end);
189 const TypeProxyBase *t = TypeRegister::getInstance().get(type); // FIXME type lookup here
190 Store *x = 0;
191 if (t != 0) {
192 x = t->newInstance(id);
193 } else {
194 d_cause = TYPENOTFOUND;
197 if (x != 0) { // IF READING SUCCEEDS, INSERT MORE CHECKS HERE
198 if (true) { // samePath(x, path)
199 ReaderState *save = d_state;
200 d_state = new ReaderState();
201 x->internalize(*this);
202 delete d_state;
203 d_state = save;
205 if (d_cause != 0) {
206 x = 0;
208 assert(d_rider.tellg() == d_state->end);
209 assert(!d_rider.eof());
210 } else {
211 // rd.cause := inconsistentType; AlienTypeReport(rd.cause, type);
212 x = 0;
216 if (x != 0) {
217 if (d_store == 0) {
218 d_store = x;
219 } else {
220 // join(d_store, x)
221 std::cout << "Man, should have written join(.,.)" << std::endl;
223 if (isElem) {
224 d_elemList.push_back(x);
225 } else {
226 d_storeList.push_back(x);
228 } else {
229 d_rider.seekg(pos); // x->internalize() could have left us anywhere
230 assert(d_cause != 0);
231 Alien *alien = new Alien(id, path); //, d_cause); //,file
232 if (d_store == 0) {
233 d_store = alien;
234 } else {
235 // join(d_store, alien)
236 std::cout << "Man, should have written join(.,.)" << std::endl;
238 if (isElem) {
239 d_elemList.push_back(alien);
240 } else {
241 d_storeList.push_back(alien);
243 ReaderState *save = d_state;
244 d_state = new ReaderState();
245 internalizeAlien(alien, downPos, save->end);
246 delete d_state;
247 d_state = save;
248 assert(d_rider.tellg() == d_state->end);
250 // we've just read the alien, so reset the state
251 d_cause = 0;
252 d_cancelled = false;
253 d_readAlien = true;
254 return alien;
257 return x;
261 void Reader::internalizeAlien(Alien *alien, std::streampos down, std::streampos end) {
262 std::streampos next = down != 0 ? down : end;
263 while (d_rider.tellg() < end) {
264 if (d_rider.tellg() < next) { // for some reason, this means its a piece (unstructured)
265 std::cout << "Alien Piece" << std::endl;
266 size_t len = next - d_rider.tellg();
267 char *buf = new char[len];
268 d_rider.read(buf, len);
269 AlienComponent *comp = new AlienPiece(buf, len);
270 alien->getComponents().push_back(comp);
271 } else { // that means we've got a store
272 std::cout << "Alien Store" << std::endl;
273 d_rider.seekg(next);
274 AlienComponent *comp = new AlienPart(readStore());
275 alien->getComponents().push_back(comp);
276 next = d_state->next > 0 ? d_state->next : end;
281 std::string &Reader::fixTypeName(std::string &name) {
282 size_t pos = name.size() - 4;
283 if (pos > 0 && name.substr(pos, 4).compare("Desc") == 0) {
284 return name.replace(pos, 4, "^");
286 return name;
289 TypePath Reader::readPath() {
290 TypePath path;
291 SHORTCHAR kind = readSChar();
292 SHORTCHAR *buf = new SHORTCHAR[64]; // TypeName has a maximum of length 64 (including terminator).
293 int i;
294 for (i = 0; kind == Store::NEWEXT; ++i) {
295 readSString(buf);
296 std::string name(buf);
297 path.push_back(fixTypeName(name));
298 addPathComponent(i == 0, path[i]);
299 // IF path[i] # elemTName THEN INC(i) END;
300 kind = readSChar();
303 if (kind == Store::NEWBASE) {
304 readSString(buf);
305 std::string name(buf);
306 path.push_back(fixTypeName(name));
307 addPathComponent(i == 0, path[i]);
308 ++i;
309 } else if (kind == Store::OLDTYPE) {
310 int id = readInt();
311 if (i > 0) {
312 d_typeList[d_typeList.size() - 1]->baseId = id;
314 while (id != -1) {
315 path.push_back(d_typeList[id]->name);
316 id = d_typeList[id]->baseId;
317 // IF path[i] # elemTName THEN INC(i) END
318 ++i;
320 } else {
321 throw 100;
323 return path;
326 void Reader::addPathComponent(bool first, const std::string &typeName) {
327 int next = d_typeList.size();
328 int curr = next - 1;
329 if (!first) {
330 d_typeList[curr]->baseId = next;
332 d_typeList.push_back(new TypeEntry(typeName));
335 } // namespace odc