DEADSOFTWARE

Try to use character conversion
[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 // Character encoding conversions
13 #include <locale.h>
14 #include <iconv.h>
15 #include <errno.h>
16 #include <string.h>
18 namespace odc {
19 class Context {
20 public:
21 virtual void addPiece(std::string &piece) = 0;
22 virtual std::string getPlainText() const = 0;
23 };
24 class PartContext : public Context {
25 private:
26 std::string d_text;
27 public:
28 virtual void addPiece(std::string &piece) {
29 d_text += piece;
30 }
31 virtual std::string getPlainText() const {
32 return d_text;
33 }
34 };
35 class FoldContext : public Context {
36 private:
37 bool d_collapsed;
38 bool d_haveFirst; // flag that first part has been set
39 std::string d_firstPart;
40 std::string d_remainder;
41 public:
42 FoldContext(bool collapsed) : d_collapsed(collapsed), d_haveFirst(false) {}
43 virtual void addPiece(std::string &piece) {
44 if (!d_haveFirst) {
45 d_haveFirst = true;
46 d_firstPart = piece;
47 } else {
48 d_remainder += piece;
49 }
50 }
51 virtual std::string getPlainText() const {
52 if (d_collapsed) {
53 return std::string("##=>") + d_remainder + "\n" + d_firstPart +"##<=";
54 } else {
55 return std::string("##=>") + d_firstPart + "\n" + d_remainder +"##<=";
56 }
57 }
58 };
60 class MyVisitor : public Visitor {
61 private:
62 std::stack<Context*> d_context;
64 void terminateContext() {
65 Context *c = d_context.top();
66 d_context.pop();
67 if (d_context.empty()) {
68 std::cout << c->getPlainText() << std::endl;
69 } else {
70 std::string text = c->getPlainText();
71 d_context.top()->addPiece(text);
72 }
73 delete c;
74 }
76 public:
77 virtual void partStart() {
78 d_context.push(new PartContext());
79 }
80 virtual void partEnd() {
81 terminateContext();
82 }
83 virtual void foldLeft(bool collapsed) {
84 d_context.push(new FoldContext(collapsed));
85 }
86 virtual void foldRight() {
87 terminateContext();
88 }
89 char *getCharSet() {
90 return "UTF-8"; // FIXME setlocale(LC_CTYPE, 0) + processing
91 }
92 virtual void textShortPiece(const ShortPiece *piece) {
93 iconv_t conv = iconv_open("UTF-8", "ISO-8859-1");
94 if (conv == (iconv_t)-1) {
95 std::string str("iconv initialization error: ");
96 str += strerror(errno);
97 throw str.c_str();
98 }
99 size_t bytesIn = piece->size() + 1;
100 SHORTCHAR *in = piece->getBuffer();
101 size_t bytesOut = bytesIn; // FIXME probably not safe.
102 char *out = new char[bytesIn];
103 char *outPtr = out;
104 size_t rval = iconv(conv, &in, &bytesIn, &outPtr, &bytesOut);
105 if (rval == (size_t)-1) {
106 std::string str("iconv error: ");
107 str += strerror(errno);
108 throw str.c_str();
110 iconv_close(conv);
111 std::string str(out);
112 for (std::string::iterator it = str.begin(); it < str.end(); ++it) {
113 if (*it == '\r') *it = '\n';
115 d_context.top()->addPiece(str);
117 virtual void textLongPiece(const LongPiece *piece) {
118 char *out = (char*)piece->getBuffer();
119 std::string str(out);
120 d_context.top()->addPiece(str);
121 //d_convLong = iconv_open(setlocale(LC_CTYPE, 0), "UCS-2");
122 /*
123 iconv_t conv = iconv_open("UTF-8", "UTF-8");
124 if (conv == (iconv_t)-1) {
125 std::string str("iconv initialization error: ");
126 str += strerror(errno);
127 throw str.c_str();
129 size_t bytesIn = piece->size() + 1;
130 char *in = (char*)piece->getBuffer();
131 size_t bytesOut = bytesIn; // FIXME probably not safe.
132 char *out = new char[bytesIn];
133 char *outPtr = out;
134 size_t rval = iconv(conv, &in, &bytesIn, &outPtr, &bytesOut);
135 if (rval == (size_t)-1) {
136 std::string str("iconv error: ");
137 str += strerror(errno);
138 throw str.c_str();
140 iconv_close(conv);
141 std::string str(out);
142 for (std::string::iterator it = str.begin(); it < str.end(); ++it) {
143 if (*it == '\r') *it = '\n';
145 d_context.top()->addPiece(str);*/
147 };
149 Store* importDocument(std::istream &is) {
150 const INTEGER docTag = 0x6F4F4443;
151 const INTEGER docVersion = 0;
152 Reader r(is);
153 INTEGER tag = r.readInt();
154 if (tag == docTag) {
155 INTEGER version = r.readInt();
156 if (version != docVersion) {
157 throw 100;
159 Store *s = r.readStore();
160 return s;
162 return 0;
166 int main(int argc, char *argv[]) {
167 if (argc < 2) {
168 return 1;
171 // Set the locale according to the terminal's environment
172 setlocale(LC_ALL, "");
174 std::ifstream in(argv[1], std::ios::in | std::ios::binary);
176 odc::Store* s;
177 try {
178 s = odc::importDocument(in);
179 } catch (int trap) {
180 std::cerr << "Exception in parsing file: BlackBox trap no. " << trap << std::endl;
181 return 2;
182 } catch (const char * exception) {
183 std::cerr << "Exception in parsing file: " << exception << std::endl;
184 return 2;
186 // std::cout << s->toPlainText() << std::endl;
187 // std::cout << std::endl << std::endl;
189 try {
190 odc::MyVisitor visitor;
191 s->accept(visitor);
192 } catch (const char * exception) {
193 std::cerr << "Exception in processing document: " << exception << std::endl;
194 return 3;
196 // std::cout << s->toString() << std::endl;
197 // std::cout << in.tellg() << " " << in.eof() << std::endl;
199 // odc::TypePath path;
200 // odc::ContainerModel(0).getTypePath(&path);
201 // std::cout << path.toString() << std::endl;
202 return 0;