f53e30a8e896035d8a769d2de4cf10b27c9e8d33
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() {
14 d_rider
.read(&out
, 1);
18 void Reader::readSChar(SHORTCHAR
*buf
, size_t len
) {
19 d_rider
.read(buf
, len
);
22 CHAR
Reader::readLChar() {
24 char *bufPtr
= (char *)&buf
;
25 d_rider
.read(bufPtr
, 2);
26 if (isLittleEndian()) {
30 char *outPtr
= (char *)&out
;
31 outPtr
[0] = bufPtr
[1]; outPtr
[1] = bufPtr
[0];
36 void Reader::readLChar(CHAR
*buf
, size_t len
) {
37 char *bufPtr
= (char *)buf
;
39 d_rider
.read(bufPtr
, len2
);
42 for (int i
= 0; i
< len2
; i
+= 2) {
44 bufPtr
[i
] = bufPtr
[i
+ 1];
50 BYTE
Reader::readByte() {
52 d_rider
.read((char*)&out
, 1);
56 INTEGER
Reader::readInt() {
58 char *bufPtr
= (char*)&buf
;
59 d_rider
.read(bufPtr
, 4);
60 if (isLittleEndian()) {
64 char *outPtr
= (char *)&out
;
65 outPtr
[0] = bufPtr
[3]; outPtr
[1] = bufPtr
[2]; outPtr
[2] = bufPtr
[1]; outPtr
[3] = bufPtr
[0];
70 void Reader::readSString(SHORTCHAR
*out
) {
71 while (*out
= readSChar()) {
76 void Reader::turnIntoAlien(int cause
) {
83 bool Reader::isCancelled() {
87 INTEGER
Reader::readVersion(INTEGER min
, INTEGER max
) {
88 INTEGER version
= readByte();
89 if (version
< min
|| version
> max
) {
90 turnIntoAlien(ALIENVERSION
);
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);
113 std::cout
<< std::hex
<< (unsigned int)kind
<< std::endl
;
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
;
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;
138 Store
*Reader::readLinkStore() {
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() {
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();
167 d_state
->next
= pos1
+ next
+ (std::streamoff
)4;
173 downPos
= pos1
+ down
+ (std::streamoff
)8;
175 d_state
->end
= pos
+ len
;
179 assert(d_state
->next
> pos1
);
181 assert(downPos
< d_state
->next
);
185 assert(downPos
> pos1
);
186 assert(downPos
< d_state
->end
);
189 const TypeProxyBase
*t
= TypeRegister::getInstance().get(type
); // FIXME type lookup here
192 x
= t
->newInstance(id
);
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);
208 assert(d_rider
.tellg() == d_state
->end
);
209 assert(!d_rider
.eof());
211 // rd.cause := inconsistentType; AlienTypeReport(rd.cause, type);
221 std::cout
<< "Man, should have written join(.,.)" << std::endl
;
224 d_elemList
.push_back(x
);
226 d_storeList
.push_back(x
);
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
235 // join(d_store, alien)
236 std::cout
<< "Man, should have written join(.,.)" << std::endl
;
239 d_elemList
.push_back(alien
);
241 d_storeList
.push_back(alien
);
243 ReaderState
*save
= d_state
;
244 d_state
= new ReaderState();
245 internalizeAlien(alien
, downPos
, save
->end
);
248 assert(d_rider
.tellg() == d_state
->end
);
250 // we've just read the alien, so reset the state
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
;
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, "^");
289 TypePath
Reader::readPath() {
291 SHORTCHAR kind
= readSChar();
292 SHORTCHAR
*buf
= new SHORTCHAR
[64]; // TypeName has a maximum of length 64 (including terminator).
294 for (i
= 0; kind
== Store::NEWEXT
; ++i
) {
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;
303 if (kind
== Store::NEWBASE
) {
305 std::string
name(buf
);
306 path
.push_back(fixTypeName(name
));
307 addPathComponent(i
== 0, path
[i
]);
309 } else if (kind
== Store::OLDTYPE
) {
312 d_typeList
[d_typeList
.size() - 1]->baseId
= id
;
315 path
.push_back(d_typeList
[id
]->name
);
316 id
= d_typeList
[id
]->baseId
;
317 // IF path[i] # elemTName THEN INC(i) END
326 void Reader::addPathComponent(bool first
, const std::string
&typeName
) {
327 int next
= d_typeList
.size();
330 d_typeList
[curr
]->baseId
= next
;
332 d_typeList
.push_back(new TypeEntry(typeName
));