DEADSOFTWARE

Completely convert .odc to .txt using Visitor
[odcread.git] / textmodel.cc
index 2028c2e5621c2c733f0254728740272d0b896f36..f9dbae242b007f39c8438e7dfae1708cae626a8c 100644 (file)
@@ -65,7 +65,7 @@ void StdTextModel::internalize(Reader &reader) {
        std::vector<Store *> dict;
        INTEGER len = reader.readInt();
        BYTE ano = reader.readByte();
-       std::cout << "len " << len << " ano " << (int)ano << std::endl;
+       //std::cout << "len " << len << " ano " << (int)ano << std::endl;
        int skip = 0;
        while (ano != -1) {
                if (ano == dict.size()) {
@@ -74,13 +74,13 @@ void StdTextModel::internalize(Reader &reader) {
                }
                INTEGER pieceLen = reader.readInt();
                if (pieceLen > 0) { // shortchar piece
-                       std::cout << "Found SChar piece" << std::endl;
+                       //std::cout << "Found SChar piece" << std::endl;
                        d_pieces.push_back(new ShortPiece(pieceLen));
 //                             NEW(sp); sp.len := len; sp.attr := attr;
 //                             sp.file := rd.rider.Base(); sp.org := org; un := sp;
 //                             INC(org, len) -- increment org by len ?
                } else if (pieceLen < 0) { // longchar piece
-                       std::cout << "Found LChar piece" << std::endl;
+                       //std::cout << "Found LChar piece" << std::endl;
                        assert(pieceLen % 2 == 0);
                        d_pieces.push_back(new LongPiece(pieceLen / 2));
 //                             len := -len; ASSERT(~ODD(len), 100);
@@ -88,9 +88,9 @@ void StdTextModel::internalize(Reader &reader) {
 //                             lp.file := rd.rider.Base(); lp.org := org; un := lp;
 //                             INC(org, len) -- increment org by len ?
                } else { // embedded view
-                       std::cout << "Found View piece" << std::endl;
-                       reader.readInt(); reader.readInt();
-                       Store *view = reader.readStore(); // fixme: save somewhere
+                       //std::cout << "Found View piece" << std::endl;
+                       reader.readInt(); reader.readInt(); // view width + height: ignore
+                       Store *view = reader.readStore();
 //                             NEW(v); v.len := 1; v.attr := attr;
 //                             rd.ReadInt(v.w); rd.ReadInt(v.h); Views.ReadView(rd, v.view);
 //                             v.view.InitContext(NewContext(v, t));
@@ -157,36 +157,72 @@ std::string StdTextModel::toString() {
        return sofar + "}";
 }
 
+void StdTextModel::accept(Visitor &visitor) const {
+       visitor.partStart();
+       for (int i = 0; i < d_pieces.size(); ++i) {
+               d_pieces[i]->accept(visitor);
+       }
+       visitor.partEnd();
+}
+
 TextPiece::TextPiece(size_t len): d_len(len) {}
 
 LongPiece::LongPiece(size_t len): TextPiece(len) {}
 
+LongPiece::~LongPiece() {
+       delete d_buf;
+}
+
 void LongPiece::read(Reader &reader) {
-       CHAR *buf = new CHAR[d_len];
-       reader.readLChar(buf, d_len);
-       delete buf;
+       d_buf = new CHAR[d_len];
+       reader.readLChar(d_buf, d_len);
+}
+
+std::string LongPiece::toString() const {
+       return std::string("LongPiece(FIXME)");// + std::wstring((wchar_t*)d_buf) + std::string(")");
+}
+
+std::string LongPiece::getText() const {
+       return std::string("FIXME");// + std::wstring((wchar_t*)d_buf) + std::string(")");
 }
 
-std::string LongPiece::toString() {
-       return std::string("LongPiece");
+void LongPiece::accept(Visitor &visitor) const {
+       visitor.textLongPiece(this);
 }
 
 ShortPiece::ShortPiece(size_t len): TextPiece(len) {}
 
+ShortPiece::~ShortPiece() {
+       delete d_buf;
+}
+
 void ShortPiece::read(Reader &reader) {
 //     static char piece[] = "pieceA";
-       SHORTCHAR *buf = new SHORTCHAR[d_len + 1];
-       reader.readSChar(buf, d_len);
-       buf[d_len] = 0;
+       d_buf = new SHORTCHAR[d_len + 1];
+       reader.readSChar(d_buf, d_len);
+       d_buf[d_len] = 0;
+//     std::cout.write(buf, d_len);
 //     std::ofstream ofs(piece, std::ios::out);
 //     ofs.write(buf, d_len);
 //     ofs.close();
 //     ++piece[5];
-       delete buf;
+//     delete buf;
 }
 
-std::string ShortPiece::toString() {
-       return std::string("ShortPiece");
+std::string ShortPiece::toString() const {
+       return std::string("ShortPiece(") + std::string(d_buf) + std::string(")");
+}
+
+std::string ShortPiece::getText() const {
+       std::string str(d_buf);
+       for (std::string::iterator it = str.begin(); it < str.end(); ++it) {
+               if (*it == '\r') *it = '\n';
+       }
+       return str;
+}
+
+void ShortPiece::accept(Visitor &visitor) const {
+       visitor.textShortPiece(this);
 }
 
 ViewPiece::ViewPiece(Store *view): TextPiece(0), d_view(view) {}
@@ -195,8 +231,12 @@ void ViewPiece::read(Reader &reader) {
        reader.readByte();
 }
 
-std::string ViewPiece::toString() {
+std::string ViewPiece::toString() const {
        return std::string("ViewPiece { ") + d_view->toString() + " }";
 }
 
+void ViewPiece::accept(Visitor &visitor) const {
+       return d_view->accept(visitor);
+}
+
 } // namespace odc