From 63b438eb6e7c0e06bf37614c5de3c03f25301af5 Mon Sep 17 00:00:00 2001 From: Gert van Valkenhoef Date: Wed, 10 Aug 2011 21:52:31 +0100 Subject: [PATCH] Successful read of StdTextModel (including actual text) --- reader.cc | 46 ++++++++++++++++++++++++++++++++++++++++------ reader.h | 4 +++- textmodel.cc | 38 ++++++++++++++++++++++++++++++++++++++ textmodel.h | 25 +++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 7 deletions(-) diff --git a/reader.cc b/reader.cc index b027bd5..1f60ce8 100644 --- a/reader.cc +++ b/reader.cc @@ -15,6 +15,38 @@ SHORTCHAR Reader::readSChar() { return out; } +void Reader::readSChar(SHORTCHAR *buf, size_t len) { + d_rider.read(buf, len); +} + +CHAR Reader::readLChar() { + CHAR buf; + char *bufPtr = (char *)&buf; + d_rider.read(bufPtr, 2); + if (isLittleEndian()) { + return buf; + } else { + CHAR out; + char *outPtr = (char *)&out; + outPtr[0] = bufPtr[1]; outPtr[1] = bufPtr[0]; + return out; + } +} + +void Reader::readLChar(CHAR *buf, size_t len) { + char *bufPtr = (char *)buf; + int len2 = len * 2; + d_rider.read(bufPtr, len2); + if (isBigEndian()) { + char tmp; + for (int i = 0; i < len2; i += 2) { + tmp = bufPtr[i]; + bufPtr[i] = bufPtr[i + 1]; + bufPtr[i + 1] = tmp; + } + } +} + BYTE Reader::readByte() { BYTE out; d_rider.read((char*)&out, 1); @@ -22,14 +54,16 @@ BYTE Reader::readByte() { } INTEGER Reader::readInt() { - char *buf = new char[4]; - d_rider.read(buf, 4); + INTEGER buf; + char *bufPtr = (char*)&buf; + d_rider.read(bufPtr, 4); if (isLittleEndian()) { - return *(INTEGER *)buf; + return buf; } else { - char *out = new char[4]; - out[0] = buf[3]; out[1] = buf[2]; out[2] = buf[1]; out[3] = buf[0]; - return *(INTEGER *)out; + INTEGER out; + char *outPtr = (char *)&out; + outPtr[0] = bufPtr[3]; outPtr[1] = bufPtr[2]; outPtr[2] = bufPtr[1]; outPtr[3] = bufPtr[0]; + return out; } } diff --git a/reader.h b/reader.h index b4f475c..21178f3 100644 --- a/reader.h +++ b/reader.h @@ -131,17 +131,19 @@ private: * Reads a short character (00X..0FFX). */ SHORTCHAR readSChar(); + void readSChar(SHORTCHAR *buf, size_t len); /* PROCEDURE (VAR rd: Reader) ReadXChar (OUT x: CHAR) * NEW * Same as ReadSChar, but has a CHAR-type parameter. * This procedure is provided to simplify migration from Release 1.2 to 1.3. */ - CHAR readXChar(); /** * PROCEDURE (VAR rd: Reader) ReadChar (OUT x: CHAR) * NEW * Reads a character (0000X..0FFFFX). */ + CHAR readLChar(); + void readLChar(CHAR *buf, size_t len); /** * PROCEDURE (VAR rd: Reader) ReadByte (OUT x: BYTE) * NEW diff --git a/textmodel.cc b/textmodel.cc index bc76a32..92ff862 100644 --- a/textmodel.cc +++ b/textmodel.cc @@ -2,6 +2,7 @@ #include #include +#include namespace odc { @@ -59,9 +60,11 @@ void StdTextModel::internalize(Reader &reader) { if (reader.isCancelled()) return; std::vector dict; + std::vector pieces; INTEGER len = reader.readInt(); BYTE ano = reader.readByte(); std::cout << "len " << len << " ano " << (int)ano << std::endl; + int skip = 0; while (ano != -1) { if (ano == dict.size()) { Store *attr = reader.readStore(); // readAttributes(); Stores.Join(t, attr) @@ -70,11 +73,14 @@ void StdTextModel::internalize(Reader &reader) { INTEGER pieceLen = reader.readInt(); if (pieceLen > 0) { // shortchar piece std::cout << "Found SChar piece" << std::endl; + 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; + assert(pieceLen % 2 == 0); + pieces.push_back(new LongPiece(pieceLen / 2)); // len := -len; ASSERT(~ODD(len), 100); // NEW(lp); lp.len := len DIV 2; lp.attr := attr; // lp.file := rd.rider.Base(); lp.org := org; un := lp; @@ -86,9 +92,14 @@ void StdTextModel::internalize(Reader &reader) { // rd.ReadInt(v.w); rd.ReadInt(v.h); Views.ReadView(rd, v.view); // v.view.InitContext(NewContext(v, t)); // un := v; INC(org) -- increment org by one?? WTH? + pieces.push_back(new ViewPiece()); + ++skip; } ano = reader.readByte(); } + for (int i = 0; i < pieces.size(); ++i) { + pieces[i]->read(reader); + } // rd.SetPos(org); } // PROCEDURE (t: StdModel) Internalize (VAR rd: Stores.Reader); @@ -135,4 +146,31 @@ void StdTextModel::internalize(Reader &reader) { // END Internalize; // +TextPiece::TextPiece(size_t len): d_len(len) {} + +LongPiece::LongPiece(size_t len): TextPiece(len) {} + +void LongPiece::read(Reader &reader) { + CHAR *buf = new CHAR[d_len]; + reader.readLChar(buf, d_len); + delete buf; +} + +ShortPiece::ShortPiece(size_t len): TextPiece(len) {} + +void ShortPiece::read(Reader &reader) { + SHORTCHAR *buf = new SHORTCHAR[d_len + 1]; + reader.readSChar(buf, d_len); + buf[d_len] = 0; + std::cout << "READING SHORT PIECE" << std::endl; + std::cout << std::string(buf) << std::endl; + delete buf; +} + +ViewPiece::ViewPiece(): TextPiece(0) {} + +void ViewPiece::read(Reader &reader) { + reader.readByte(); +} + } // namespace odc diff --git a/textmodel.h b/textmodel.h index 13a3d56..93fa5b7 100644 --- a/textmodel.h +++ b/textmodel.h @@ -20,6 +20,31 @@ namespace odc { virtual void internalize(Reader &reader); }; + class TextPiece { + public: + const size_t d_len; + TextPiece(size_t len); + virtual void read(Reader &reader) = 0; + }; + + class LongPiece : public TextPiece { + public: + LongPiece(size_t len); + virtual void read(Reader &reader); + }; + + class ShortPiece : public TextPiece { + public: + ShortPiece(size_t len); + virtual void read(Reader &reader); + }; + + class ViewPiece : public TextPiece { + public: + ViewPiece(); + virtual void read(Reader &reader); + }; + class StdTextModel : public TextModel { private: static const std::string TYPENAME; -- 2.29.2