DEADSOFTWARE

08bd498f174bbe7167562eff91fb8cd7fae2bd4d
[odcread.git] / reader.cc
1 #include <reader.h>
2 #include <alien.h>
4 #include <string>
6 namespace odc {
8 Reader::Reader(std::istream &rider): d_rider(rider), d_cancelled(false), d_readAlien(false), d_typeList(),
9 d_state(new ReaderState()) {}
11 SHORTCHAR Reader::readSChar() {
12 SHORTCHAR out;
13 d_rider.read(&out, 1);
14 return out;
15 }
17 INTEGER Reader::readInt() {
18 char *buf = new char[4];
19 d_rider.read(buf, 4);
20 if (isLittleEndian()) {
21 return *(INTEGER *)buf;
22 } else {
23 char *out = new char[4];
24 out[0] = buf[3]; out[1] = buf[2]; out[2] = buf[1]; out[3] = buf[0];
25 return *(INTEGER *)out;
26 }
27 }
29 void Reader::readSString(SHORTCHAR *out) {
30 while (*out = readSChar()) {
31 ++out;
32 }
33 }
35 Store* Reader::readStore() {
36 SHORTCHAR kind = readSChar();
37 if (kind == Store::NIL) {
38 std::cout << "NIL STORE" << std::endl;
39 return readNilStore();
40 } else if (kind == Store::LINK) {
41 std::cout << "LINK STORE" << std::endl;
42 return readLinkStore();
43 } else if (kind == Store::NEWLINK) {
44 std::cout << "NEWLINK STORE" << std::endl;
45 return readNewLinkStore();
46 } else if (kind == Store::STORE) {
47 std::cout << "STORE STORE" << std::endl;
48 return readStoreOrElemStore(false);
49 } else if (kind == Store::ELEM) {
50 std::cout << "ELEM STORE" << std::endl;
51 return readStoreOrElemStore(true);
52 } else {
53 throw 20;
54 }
55 }
56 // PROCEDURE (VAR rd: Reader) ReadStore* (OUT x: Store), NEW;
57 // VAR a: Alien; t: Kernel.Type;
58 // len, pos, pos1, id, comment, next, down, downPos, nextTypeId, nextElemId, nextStoreId: INTEGER;
59 // kind: SHORTCHAR; path: TypePath; type: TypeName;
60 // save: ReaderState;
61 Store *Reader::readNilStore() {
62 return 0;
63 }
64 // IF kind = nil THEN
65 // rd.ReadInt(comment); rd.ReadInt(next);
66 // rd.st.end := rd.Pos();
67 // IF (next > 0) OR ((next = 0) & ODD(comment)) THEN rd.st.next := rd.st.end + next ELSE rd.st.next := 0 END;
68 // x := NIL
69 Store *Reader::readLinkStore() {
70 return 0;
71 }
72 // ELSIF kind = link THEN
73 // rd.ReadInt(id); rd.ReadInt(comment); rd.ReadInt(next);
74 // rd.st.end := rd.Pos();
75 // IF (next > 0) OR ((next = 0) & ODD(comment)) THEN rd.st.next := rd.st.end + next ELSE rd.st.next := 0 END;
76 // x := ThisStore(rd.eDict, id)
77 Store *Reader::readNewLinkStore() {
78 return 0;
79 }
80 // ELSIF kind = newlink THEN
81 // rd.ReadInt(id); rd.ReadInt(comment); rd.ReadInt(next);
82 // rd.st.end := rd.Pos();
83 // IF (next > 0) OR ((next = 0) & ODD(comment)) THEN rd.st.next := rd.st.end + next ELSE rd.st.next := 0 END;
84 // x := ThisStore(rd.sDict, id)
86 Store *Reader::readStoreOrElemStore(bool isElem) {
87 INTEGER id = isElem ? d_elemList.size() : d_storeList.size();
88 TypePath path = readPath();
89 std::cout << path.toString() << std::endl;
90 const std::string &type = path[0];
91 INTEGER comment = readInt();
92 std::streampos pos1 = d_rider.tellg();
93 std::streamoff next = readInt();
94 std::streamoff down = readInt();
95 std::streamoff len = readInt();
96 std::streampos pos = d_rider.tellg();
97 if (next > 0) {
98 d_state->next = pos1 + next + (std::streamoff)4;
99 } else {
100 d_state->next = 0;
102 int downPos = 0;
103 if (down > 0) {
104 downPos = pos1 + down + (std::streamoff)8;
106 d_state->end = pos + len;
107 d_cause = 0;
108 // FIXME: insert whole bunch of checks here
109 // ASSERT(len >= 0, 101);
110 // IF next # 0 THEN
111 // ASSERT(rd.st.next > pos1, 102);
112 // IF down # 0 THEN
113 // ASSERT(downPos < rd.st.next, 103)
114 // END
115 // END;
116 // IF down # 0 THEN
117 // ASSERT(downPos > pos1, 104);
118 // ASSERT(downPos < rd.st.end, 105)
119 // END;
121 void *t = 0; // FIXME type lookup here
122 if (t != 0) {
123 // x := NewStore(t); x.isElem := kind = elem
124 } else {
125 // rd.cause := thisTypeRes; AlienTypeReport(rd.cause, type);
126 // x := NIL
129 Store *x = 0;
130 if (x != 0) { // IF READING SUCCEEDS, INSERT MORE CHECKS HERE
131 } else {
132 // rd.SetPos(pos);
133 // ASSERT(rd.cause # 0, 107);
134 Alien *alien = new Alien(id, path); //, d_cause); //,file
135 if (d_store == 0) {
136 d_store = alien;
137 } else {
138 // join(d_store, alien)
139 std::cout << "Man, should have written join(.,.)" << std::endl;
141 if (isElem) {
142 d_elemList.push_back(alien);
143 } else {
144 d_storeList.push_back(alien);
146 ReaderState *save = d_state;
147 // rd.nextTypeId := nextTypeId; rd.nextElemId := nextElemId; rd.nextStoreId := nextStoreId;
148 internalizeAlien(alien, downPos, save->end);
149 d_state = save;
150 // ASSERT(rd.Pos() = rd.st.end, 108);
151 // rd.cause := 0; rd.cancelled := FALSE; rd.readAlien := TRUE
152 return alien;
155 return x;
157 // t := ThisType(type);
158 // IF t # NIL THEN
159 // x := NewStore(t); x.isElem := kind = elem
160 // ELSE
161 // rd.cause := thisTypeRes; AlienTypeReport(rd.cause, type);
162 // x := NIL
163 // END;
164 // IF x # NIL THEN
165 // IF SamePath(t, path) THEN
166 // IF kind = elem THEN
167 // x.id := id; AddStore(rd.eDict, rd.eHead, x)
168 // ELSE
169 // x.id := id; AddStore(rd.sDict, rd.sHead, x)
170 // END;
171 // save := rd.st; rd.cause := 0; rd.cancelled := FALSE;
172 // x.Internalize(rd);
173 // rd.st := save;
174 // IF rd.cause # 0 THEN x := NIL
175 // ELSIF (rd.Pos() # rd.st.end) OR rd.rider.eof THEN
176 // rd.cause := inconsistentVersion; AlienReport(rd.cause);
177 // x := NIL
178 // END
179 // ELSE
180 // rd.cause := inconsistentType; AlienTypeReport(rd.cause, type);
181 // x := NIL
182 // END
183 // END;
184 //
185 // IF x # NIL THEN
186 // IF rd.noDomain THEN
187 // rd.store := x;
188 // rd.noDomain := FALSE
189 // ELSE
190 // Join(rd.store, x)
191 // END
192 // ELSE (* x is an alien *)
193 // rd.SetPos(pos);
194 // ASSERT(rd.cause # 0, 107);
195 // NEW(a); a.path := path; a.cause := rd.cause; a.file := rd.rider.Base();
196 // IF rd.noDomain THEN
197 // rd.store := a;
198 // rd.noDomain := FALSE
199 // ELSE
200 // Join(rd.store, a)
201 // END;
202 // IF kind = elem THEN
203 // a.id := id; AddStore(rd.eDict, rd.eHead, a)
204 // ELSE
205 // a.id := id; AddStore(rd.sDict, rd.sHead, a)
206 // END;
207 // save := rd.st;
208 // rd.nextTypeId := nextTypeId; rd.nextElemId := nextElemId; rd.nextStoreId := nextStoreId;
209 // InternalizeAlien(rd, a.comps, downPos, pos, len);
210 // rd.st := save;
211 // x := a;
212 // ASSERT(rd.Pos() = rd.st.end, 108);
213 // rd.cause := 0; rd.cancelled := FALSE; rd.readAlien := TRUE
214 // END
217 void Reader::internalizeAlien(Alien *alien, std::streampos down, std::streampos end) {
218 std::streampos next = down != 0 ? down : end;
219 while (d_rider.tellg() < end) {
220 if (d_rider.tellg() < next) { // for some reason, this means its a piece (unstructured)
221 std::cout << "Alien Piece" << std::endl;
222 size_t len = next - d_rider.tellg();
223 char *buf = new char[len];
224 d_rider.read(buf, len);
225 AlienComponent *comp = new AlienPiece(buf, len);
226 alien->getComponents().push_back(comp);
227 } else { // that means we've got a store
228 std::cout << "Alien Store" << std::endl;
229 d_rider.seekg(next);
230 AlienComponent *comp = new AlienPart(readStore());
231 alien->getComponents().push_back(comp);
232 next = d_state->next > 0 ? d_state->next : end;
237 TypePath Reader::readPath() {
238 TypePath path;
239 SHORTCHAR kind = readSChar();
240 SHORTCHAR *buf = new SHORTCHAR[64]; // TypeName has a maximum of length 64 (including terminator).
241 int i;
242 for (i = 0; kind == Store::NEWEXT; ++i) {
243 readSString(buf);
244 path.push_back(std::string(buf));
245 addPathComponent(i == 0, path[i]);
246 // IF path[i] # elemTName THEN INC(i) END;
247 kind = readSChar();
250 if (kind == Store::NEWBASE) {
251 readSString(buf);
252 path.push_back(std::string(buf));
253 addPathComponent(i == 0, path[i]);
254 ++i;
255 } else if (kind == Store::OLDTYPE) {
256 int id = readInt();
257 if (i > 0) {
258 d_typeList[d_typeList.size() - 1]->baseId = id;
260 while (id != -1) {
261 path.push_back(d_typeList[id]->name);
262 id = d_typeList[id]->baseId;
263 // IF path[i] # elemTName THEN INC(i) END
264 ++i;
266 } else {
267 throw 100;
269 return path;
272 void Reader::addPathComponent(bool first, const std::string &typeName) {
273 int next = d_typeList.size();
274 int curr = next - 1;
275 if (!first) {
276 d_typeList[curr]->baseId = next;
278 d_typeList.push_back(new TypeEntry(typeName));
281 } // namespace odc