DEADSOFTWARE

Add type handling (disabled until StdTextModel.internalize is implemented)
[odcread.git] / reader.cc
index 54ca8c1a2ff99ffee3554ca75353775616ea6964..3ccfb7144bae80159e5172977dd3e9f475a55de9 100644 (file)
--- a/reader.cc
+++ b/reader.cc
@@ -1,8 +1,12 @@
 #include <reader.h>
+#include <alien.h>
+
+#include <string>
 
 namespace odc {
 
-Reader::Reader(std::istream &rider): d_rider(rider), d_cancelled(false), d_readAlien(false) {}
+Reader::Reader(std::istream &rider): d_rider(rider), d_cancelled(false), d_readAlien(false), d_typeList(),
+       d_state(new ReaderState()) {}
 
 SHORTCHAR Reader::readSChar() {
        SHORTCHAR out;
@@ -10,6 +14,12 @@ SHORTCHAR Reader::readSChar() {
        return out;
 }
 
+BYTE Reader::readByte() {
+       BYTE out;
+       d_rider.read((char*)&out, 1);
+       return out;
+}
+
 INTEGER Reader::readInt() {
        char *buf = new char[4];
        d_rider.read(buf, 4);
@@ -22,17 +32,36 @@ INTEGER Reader::readInt() {
        }
 }
 
+void Reader::readSString(SHORTCHAR *out) {
+       while (*out = readSChar()) {
+               ++out;
+       }
+}
+
+INTEGER Reader::readVersion(INTEGER min, INTEGER max) {
+       INTEGER version = readByte();
+       //if (version < min || version > max) {
+               //                      rd.TurnIntoAlien(alienVersion)
+       //}
+       return version;
+}
+
 Store* Reader::readStore() {
        SHORTCHAR kind = readSChar();
        if (kind == Store::NIL) {
+               std::cout << "NIL STORE" << std::endl;
                return readNilStore();
        } else if (kind == Store::LINK) {
+               std::cout << "LINK STORE" << std::endl;
                return readLinkStore();
        } else if (kind == Store::NEWLINK) {
+               std::cout << "NEWLINK STORE" << std::endl;
                return readNewLinkStore();
        } else if (kind == Store::STORE) {
+               std::cout << "STORE STORE" << std::endl;
                return readStoreOrElemStore(false);
        } else if (kind == Store::ELEM) {
+               std::cout << "ELEM STORE" << std::endl;
                return readStoreOrElemStore(true);
        } else {
                throw 20;
@@ -69,24 +98,28 @@ Store *Reader::readNewLinkStore() {
 //                     x := ThisStore(rd.sDict, id)
 
 Store *Reader::readStoreOrElemStore(bool isElem) {
-       INTEGER id = isElem ? d_nextElemId++ : d_nextStoreId++;
-       CHAR** path = newTypePath();
-       readPath(path);
-       CHAR* type = path[0];
-       std::cout << type << std::endl;
+       INTEGER id = isElem ? d_elemList.size() : d_storeList.size();
+       TypePath path = readPath();
+       std::cout << path.toString() << std::endl;
+       const std::string &type = path[0];
        INTEGER comment = readInt();
-       return 0;
-}
-//                     ReadPath(rd, path); type := path[0];
-//                     nextTypeId := rd.nextTypeId; nextElemId := rd.nextElemId; nextStoreId := rd.nextStoreId;
-//                     rd.ReadInt(comment);
-//                     pos1 := rd.Pos();
-//                     rd.ReadInt(next); rd.ReadInt(down); rd.ReadInt(len);
-//                     pos := rd.Pos();
-//                     IF next > 0 THEN rd.st.next := pos1 + next + 4 ELSE rd.st.next := 0 END;
-//                     IF down > 0 THEN downPos := pos1 + down + 8 ELSE downPos := 0 END;
-//                     rd.st.end := pos + len;
-//                     rd.cause := 0;
+       std::streampos pos1 = d_rider.tellg();
+       std::streamoff next = readInt();
+       std::streamoff down = readInt();
+       std::streamoff len = readInt();
+       std::streampos pos = d_rider.tellg();
+       if (next > 0) {
+               d_state->next = pos1 + next + (std::streamoff)4;
+       } else {
+               d_state->next = 0;
+       }
+       int downPos = 0;
+       if (down > 0) {
+               downPos = pos1 + down + (std::streamoff)8;
+       }
+       d_state->end = pos + len;
+       d_cause = 0;
+       // FIXME: insert whole bunch of checks here
 //                     ASSERT(len >= 0, 101);
 //                     IF next # 0 THEN
 //                             ASSERT(rd.st.next > pos1, 102);
@@ -98,6 +131,45 @@ Store *Reader::readStoreOrElemStore(bool isElem) {
 //                             ASSERT(downPos > pos1, 104);
 //                             ASSERT(downPos < rd.st.end, 105)
 //                     END;
+
+       const TypeProxyBase *t = TypeRegister::getInstance().get(type); // FIXME type lookup here
+       Store *x = 0;
+       if (t != 0) {
+               x = t->newInstance(id);
+               x->internalize(*this);
+//                             x := NewStore(t); x.isElem := kind = elem
+       } else {
+//                             rd.cause := thisTypeRes; AlienTypeReport(rd.cause, type);
+//                             x := NIL
+       }
+
+       if (x != 0) { // IF READING SUCCEEDS, INSERT MORE CHECKS HERE
+       } else {
+//                             rd.SetPos(pos);
+//                             ASSERT(rd.cause # 0, 107);
+               Alien *alien = new Alien(id, path); //, d_cause); //,file
+               if (d_store == 0) {
+                       d_store = alien;
+               } else {
+                       // join(d_store, alien)
+                       std::cout << "Man, should have written join(.,.)" << std::endl;
+               }
+               if (isElem) {
+                       d_elemList.push_back(alien);
+               } else {
+                       d_storeList.push_back(alien);
+               }
+               ReaderState *save = d_state;
+//                             rd.nextTypeId := nextTypeId; rd.nextElemId := nextElemId; rd.nextStoreId := nextStoreId;
+               internalizeAlien(alien, downPos, save->end);
+               d_state = save;
+//                             ASSERT(rd.Pos() = rd.st.end, 108);
+//                             rd.cause := 0; rd.cancelled :=  FALSE; rd.readAlien := TRUE
+               return alien;
+       }
+
+       return x;
+}
 //                     t := ThisType(type);
 //                     IF t # NIL THEN
 //                             x := NewStore(t); x.isElem := kind = elem
@@ -157,60 +229,79 @@ Store *Reader::readStoreOrElemStore(bool isElem) {
 //                             rd.cause := 0; rd.cancelled :=  FALSE; rd.readAlien := TRUE
 //                     END
 
-void Reader::readPath(CHAR **path) {
+
+void Reader::internalizeAlien(Alien *alien, std::streampos down, std::streampos end) {
+       std::streampos next = down != 0 ? down : end;
+       while (d_rider.tellg() < end) {
+               if (d_rider.tellg() < next) { // for some reason, this means its a piece (unstructured)
+                       std::cout << "Alien Piece" << std::endl;
+                       size_t len = next - d_rider.tellg();
+                       char *buf = new char[len];
+                       d_rider.read(buf, len);
+                       AlienComponent *comp = new AlienPiece(buf, len);
+                       alien->getComponents().push_back(comp);
+               } else { // that means we've got a store
+                       std::cout << "Alien Store" << std::endl;
+                       d_rider.seekg(next);
+                       AlienComponent *comp = new AlienPart(readStore());
+                       alien->getComponents().push_back(comp);
+                       next = d_state->next > 0 ? d_state->next : end;
+               }
+       }
+}
+
+std::string &Reader::fixTypeName(std::string &name) {
+       size_t pos = name.size() - 4;
+       if (pos > 0 && name.substr(pos, 4).compare("Desc") == 0) {
+               return name.replace(pos, 4, "^");
+       }
+       return name;
+}
+
+TypePath Reader::readPath() {
+       TypePath path;
        SHORTCHAR kind = readSChar();
-       for (int i = 0; kind == Store::NEWEXT; ++i) {
-               std::cout << "NEWEXT" << std::endl;
-               //readXString(path[i]);
-//                     AddPathComp(rd); INC(rd.nextTypeId);
+       SHORTCHAR *buf = new SHORTCHAR[64]; // TypeName has a maximum of length 64 (including terminator).
+       int i;
+       for (i = 0; kind == Store::NEWEXT; ++i) {
+               readSString(buf);
+               std::string name(buf);
+               path.push_back(fixTypeName(name));
+               addPathComponent(i == 0, path[i]);
 //                     IF path[i] # elemTName THEN INC(i) END;
-//                     rd.ReadSChar(kind)
+               kind = readSChar();
        }
 
        if (kind == Store::NEWBASE) {
-               std::cout << "NEWBASE" << std::endl;
+               readSString(buf);
+               std::string name(buf);
+               path.push_back(fixTypeName(name));
+               addPathComponent(i == 0, path[i]);
+               ++i;
        } else if (kind == Store::OLDTYPE) {
-               std::cout << "OLDTYPE" << std::endl;
+               int id = readInt();
+               if (i > 0) {
+                       d_typeList[d_typeList.size() - 1]->baseId = id;
+               }
+               while (id != -1) {
+                       path.push_back(d_typeList[id]->name);
+                       id = d_typeList[id]->baseId;
+//                             IF path[i] # elemTName THEN INC(i) END
+                       ++i;
+               }
        } else {
                throw 100;
        }
+       return path;
+}
 
-       // FIXME
-       path[0][0] = 'H';
-       path[0][1] = 'i';
-       path[0][2] = 0;
+void Reader::addPathComponent(bool first, const std::string &typeName) {
+       int next = d_typeList.size();
+       int curr = next - 1;
+       if (!first) {
+               d_typeList[curr]->baseId = next;
+       }
+       d_typeList.push_back(new TypeEntry(typeName));
 }
-//     PROCEDURE ReadPath (VAR rd: Reader; VAR path: TypePath);
-//             VAR h: TypeDict; id, extId: INTEGER; i: INTEGER; kind: SHORTCHAR;
-//
-//             PROCEDURE AddPathComp (VAR rd: Reader);
-//             BEGIN
-//                     IF h # NIL THEN AddBaseId(h, extId, rd.nextTypeId) END;
-//                     AddType(rd.tDict, rd.tHead, rd.nextTypeId, path[i]);
-//                     h := rd.tHead; extId := rd.nextTypeId
-//             END AddPathComp;
-//
-//     BEGIN
-//             h := NIL; i := 0; rd.ReadSChar(kind);
-//             WHILE kind = newExt DO
-//                     rd.ReadXString(path[i]);
-//                     AddPathComp(rd); INC(rd.nextTypeId);
-//                     IF path[i] # elemTName THEN INC(i) END;
-//                     rd.ReadSChar(kind)
-//             END;
-//             IF kind = newBase THEN
-//                     rd.ReadXString(path[i]);
-//                     AddPathComp(rd); INC(rd.nextTypeId); INC(i)
-//             ELSE
-//                     ASSERT(kind = oldType, 100);
-//                     rd.ReadInt(id);
-//                     IF h # NIL THEN AddBaseId(h, extId, id) END;
-//                     REPEAT
-//                             GetThisType(rd.tDict, id, path[i]); id := ThisBaseId(rd.tDict, id);
-//                             IF path[i] # elemTName THEN INC(i) END
-//                     UNTIL id = -1
-//             END;
-//             path[i] := ""
-//     END ReadPath;
 
 } // namespace odc