X-Git-Url: https://deadsoftware.ru/gitweb?p=odcread.git;a=blobdiff_plain;f=textmodel.cc;h=dd982dc490873b612a2377269d35fa3b30e0edac;hp=f9dbae242b007f39c8438e7dfae1708cae626a8c;hb=41d9c65ec79728a8771cd2288dfc3458569c6801;hpb=d7f2452e20b04d1559b7bdd3aa49b6fbf7d0abaf diff --git a/textmodel.cc b/textmodel.cc index f9dbae2..dd982dc 100644 --- a/textmodel.cc +++ b/textmodel.cc @@ -4,26 +4,15 @@ #include #include -//#include -//#include - namespace odc { const std::string TextModel::TYPENAME("TextModels.Model^"); -const TypeProxy TextModel::PROXY; +const TypeProxy TextModel::PROXY; TextModel::TextModel(INTEGER id) : ContainerModel(id) {} -const std::string &TextModel::getType() { - return TYPENAME; -} - -const std::string *TextModel::getSuper() { - return &ContainerModel::getType(); -} - const std::string &TextModel::getTypeName() const { - return getType(); + return TYPENAME; } void TextModel::internalize(Reader &reader) { @@ -33,20 +22,12 @@ void TextModel::internalize(Reader &reader) { } const std::string StdTextModel::TYPENAME("TextModels.StdModel^"); -const TypeProxy StdTextModel::PROXY; +const TypeProxy StdTextModel::PROXY; StdTextModel::StdTextModel(INTEGER id) : TextModel(id), d_pieces() {} -const std::string &StdTextModel::getType() { - return TYPENAME; -} - -const std::string *StdTextModel::getSuper() { - return &TextModel::getType(); -} - const std::string &StdTextModel::getTypeName() const { - return getType(); + return TYPENAME; } /* @@ -62,92 +43,43 @@ void StdTextModel::internalize(Reader &reader) { reader.readVersion(0, 1); if (reader.isCancelled()) return; - std::vector dict; - INTEGER len = reader.readInt(); + std::vector dict; // attribute dictionary + + // reads the meta-data + INTEGER len = reader.readInt(); // lenght of meta-data section BYTE ano = reader.readByte(); - //std::cout << "len " << len << " ano " << (int)ano << std::endl; - int skip = 0; while (ano != -1) { + // This part reads the piece's attributes. These are ignored for now, + // but may be needed in the future. if (ano == dict.size()) { - Store *attr = reader.readStore(); // readAttributes(); Stores.Join(t, attr) - dict.push_back(attr); + dict.push_back(reader.readStore()); } + Store *attr = dict[ano]; + INTEGER pieceLen = reader.readInt(); if (pieceLen > 0) { // shortchar piece - //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; assert(pieceLen % 2 == 0); - d_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; -// INC(org, len) -- increment org by len ? + d_pieces.push_back(new LongPiece(-pieceLen / 2)); } else { // embedded view - //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)); -// un := v; INC(org) -- increment org by one?? WTH? d_pieces.push_back(new ViewPiece(view)); - ++skip; } ano = reader.readByte(); } + + // reads the pieces described in the meta-data for (int i = 0; i < d_pieces.size(); ++i) { d_pieces[i]->read(reader); } -// rd.SetPos(org); -} -// PROCEDURE (t: StdModel) Internalize (VAR rd: Stores.Reader); -// VAR u, un: Run; sp: Piece; lp: LPiece; v: ViewRef; -// org, len: INTEGER; ano: BYTE; thisVersion: INTEGER; -// attr: Attributes; dict: AttrDict; -// BEGIN -// ASSERT(t.Domain() = NIL, 20); ASSERT(t.len = 0, 21); -// t.Internalize^(rd); IF rd.cancelled THEN RETURN END; -// rd.ReadVersion(minVersion, maxStdModelVersion, thisVersion); -// IF rd.cancelled THEN RETURN END; -// StdInit(t); -// dict.len := 0; u := t.trailer; -// rd.ReadInt(len); org := rd.Pos() + len; -// rd.ReadByte(ano); -// WHILE ano # -1 DO -// IF ano = dict.len THEN -// ReadAttr(rd, attr); Stores.Join(t, attr); -// IF dict.len < dictSize THEN dict.attr[dict.len] := attr; INC(dict.len) END -// ELSE -// attr := dict.attr[ano] -// END; -// rd.ReadInt(len); -// IF len > 0 THEN (* piece *) -// NEW(sp); sp.len := len; sp.attr := attr; -// sp.file := rd.rider.Base(); sp.org := org; un := sp; -// INC(org, len) -// ELSIF len < 0 THEN (* longchar piece *) -// 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; -// INC(org, len) -// ELSE (* len = 0 => embedded view *) -// 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)); -// un := v; INC(org) -// END; -// INC(t.len, un.len); u.next := un; un.prev := u; u := un; -// rd.ReadByte(ano) -// END; -// rd.SetPos(org); -// u.next := t.trailer; t.trailer.prev := u -// END Internalize; -// + + // free memory from stores in the dictionary + for (int i = 0; i < dict.size(); ++i) { + delete dict[i]; + } +} std::string StdTextModel::toString() { std::string sofar = "StdTextModel { "; @@ -167,23 +99,24 @@ void StdTextModel::accept(Visitor &visitor) const { TextPiece::TextPiece(size_t len): d_len(len) {} -LongPiece::LongPiece(size_t len): TextPiece(len) {} +LongPiece::LongPiece(size_t len): TextPiece(len * 2) {} LongPiece::~LongPiece() { delete d_buf; } void LongPiece::read(Reader &reader) { - d_buf = new CHAR[d_len]; - reader.readLChar(d_buf, d_len); + d_buf = new CHAR[d_len / 2 + 1]; + reader.readLChar(d_buf, d_len / 2); + d_buf[d_len / 2] = 0; } std::string LongPiece::toString() const { - return std::string("LongPiece(FIXME)");// + std::wstring((wchar_t*)d_buf) + std::string(")"); + return std::string("LongPiece(FIXME)"); } -std::string LongPiece::getText() const { - return std::string("FIXME");// + std::wstring((wchar_t*)d_buf) + std::string(")"); +std::wstring LongPiece::getText() const { + return std::wstring((wchar_t*)d_buf); } void LongPiece::accept(Visitor &visitor) const { @@ -197,16 +130,9 @@ ShortPiece::~ShortPiece() { } void ShortPiece::read(Reader &reader) { -// static char piece[] = "pieceA"; 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; } std::string ShortPiece::toString() const { @@ -225,7 +151,11 @@ void ShortPiece::accept(Visitor &visitor) const { visitor.textShortPiece(this); } -ViewPiece::ViewPiece(Store *view): TextPiece(0), d_view(view) {} +ViewPiece::ViewPiece(Store *view): TextPiece(1), d_view(view) {} + +ViewPiece::~ViewPiece() { + delete d_view; +} void ViewPiece::read(Reader &reader) { reader.readByte();