DEADSOFTWARE

Add README
[odcread.git] / odcread.cc
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 #include <stack>
6 #include <oberon.h>
7 #include <reader.h>
8 #include <store.h>
9 #include <textmodel.h>
10 #include <visitor.h>
12 namespace odc {
13 class Context {
14 public:
15 virtual void addPiece(std::string &piece) = 0;
16 virtual std::string getPlainText() const = 0;
17 };
18 class PartContext : public Context {
19 private:
20 std::string d_text;
21 public:
22 virtual void addPiece(std::string &piece) {
23 d_text += piece;
24 }
25 virtual std::string getPlainText() const {
26 return d_text;
27 }
28 };
29 class FoldContext : public Context {
30 private:
31 bool d_collapsed;
32 bool d_haveFirst; // flag that first part has been set
33 std::string d_firstPart;
34 std::string d_remainder;
35 public:
36 FoldContext(bool collapsed) : d_collapsed(collapsed), d_haveFirst(false) {}
37 virtual void addPiece(std::string &piece) {
38 if (!d_haveFirst) {
39 d_haveFirst = true;
40 d_firstPart = piece;
41 } else {
42 d_remainder += piece;
43 }
44 }
45 virtual std::string getPlainText() const {
46 if (d_collapsed) {
47 return std::string("##=>") + d_remainder + "\n" + d_firstPart +"##<=";
48 } else {
49 return std::string("##=>") + d_firstPart + "\n" + d_remainder +"##<=";
50 }
51 }
52 };
54 class MyVisitor : public Visitor {
55 private:
56 std::stack<Context*> d_context;
58 void terminateContext() {
59 Context *c = d_context.top();
60 d_context.pop();
61 if (d_context.empty()) {
62 std::cout << c->getPlainText() << std::endl;
63 } else {
64 std::string text = c->getPlainText();
65 d_context.top()->addPiece(text);
66 }
67 delete c;
68 }
70 public:
71 virtual void partStart() {
72 d_context.push(new PartContext());
73 }
74 virtual void partEnd() {
75 terminateContext();
76 }
77 virtual void foldLeft(bool collapsed) {
78 d_context.push(new FoldContext(collapsed));
79 }
80 virtual void foldRight() {
81 terminateContext();
82 }
83 virtual void textShortPiece(const ShortPiece *piece) {
84 std::string text = piece->getText();
85 d_context.top()->addPiece(text);
86 }
87 virtual void textLongPiece(const LongPiece *piece) {
88 throw "Long Piece not handled";
89 //std::string text = piece->getText();
90 //d_context.top()->addPiece(text);
91 }
92 };
94 Store* importDocument(std::istream &is) {
95 const INTEGER docTag = 0x6F4F4443;
96 const INTEGER docVersion = 0;
97 Reader r(is);
98 INTEGER tag = r.readInt();
99 if (tag == docTag) {
100 INTEGER version = r.readInt();
101 if (version != docVersion) {
102 throw 100;
104 Store *s = r.readStore();
105 return s;
107 return 0;
111 int main(int argc, char *argv[]) {
112 if (argc < 2) {
113 return 1;
115 std::ifstream in(argv[1], std::ios::in | std::ios::binary);
116 odc::Store* s = odc::importDocument(in);
117 // std::cout << s->toPlainText() << std::endl;
118 // std::cout << std::endl << std::endl;
120 odc::MyVisitor visitor;
121 s->accept(visitor);
122 // std::cout << s->toString() << std::endl;
123 // std::cout << in.tellg() << " " << in.eof() << std::endl;
125 // odc::TypePath path;
126 // odc::ContainerModel(0).getTypePath(&path);
127 // std::cout << path.toString() << std::endl;
128 return 0;