X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=odcread.cc;h=891ea990102ba11cd63da7e952d66b4c2a3c379f;hb=4016b9a7f02803e28582abb02cd410dcb5021fe6;hp=e790fdd36bcdc3b211d3c4d277cf9462ee6db3dd;hpb=c71568094a35bbda781ac2070e50a59a3b5ac9f7;p=odcread.git diff --git a/odcread.cc b/odcread.cc index e790fdd..891ea99 100644 --- a/odcread.cc +++ b/odcread.cc @@ -9,6 +9,13 @@ #include #include +// Character encoding conversions +#include // determine the current charset +#include // locale support +#include // charset conversions +#include // error codes +#include // string descriptions of error codes + namespace odc { class Context { public: @@ -80,14 +87,64 @@ namespace odc { virtual void foldRight() { terminateContext(); } + char *getCharSet() { + return nl_langinfo(CODESET); + } virtual void textShortPiece(const ShortPiece *piece) { - std::string text = piece->getText(); - d_context.top()->addPiece(text); + iconv_t conv = iconv_open(getCharSet(), "ISO-8859-1"); + if (conv == (iconv_t)-1) { + std::string str("iconv initialization error: "); + str += strerror(errno); + throw str.c_str(); + } + size_t bytesIn = piece->size() + 1; + SHORTCHAR *in = piece->getBuffer(); + size_t bytesOut = bytesIn; // FIXME probably not safe. + char *out = new char[bytesIn]; + char *outPtr = out; + size_t rval = iconv(conv, &in, &bytesIn, &outPtr, &bytesOut); + if (rval == (size_t)-1) { + std::string str("iconv error: "); + str += strerror(errno); + throw str.c_str(); + } + iconv_close(conv); + std::string str(out); + for (std::string::iterator it = str.begin(); it < str.end(); ++it) { + if (*it == '\r') *it = '\n'; + } + d_context.top()->addPiece(str); } virtual void textLongPiece(const LongPiece *piece) { - throw "Long Piece not handled"; - //std::string text = piece->getText(); - //d_context.top()->addPiece(text); + /* + char *out = (char*)piece->getBuffer(); + std::string str(out); + d_context.top()->addPiece(str); + */ + //d_convLong = iconv_open(setlocale(LC_CTYPE, 0), "UCS-2"); + iconv_t conv = iconv_open(getCharSet(), "UCS-2"); + if (conv == (iconv_t)-1) { + std::string str("iconv initialization error: "); + str += strerror(errno); + throw str.c_str(); + } + size_t bytesIn = piece->size() + 2; + char *in = (char*)piece->getBuffer(); + size_t bytesOut = bytesIn; // FIXME probably not safe. + char *out = new char[bytesIn]; + char *outPtr = out; + size_t rval = iconv(conv, &in, &bytesIn, &outPtr, &bytesOut); + if (rval == (size_t)-1) { + std::string str("iconv error: "); + str += strerror(errno); + throw str.c_str(); + } + iconv_close(conv); + std::string str(out); + for (std::string::iterator it = str.begin(); it < str.end(); ++it) { + if (*it == '\r') *it = '\n'; + } + d_context.top()->addPiece(str); } }; @@ -112,13 +169,32 @@ int main(int argc, char *argv[]) { if (argc < 2) { return 1; } + + // Set the locale according to the terminal's environment + setlocale(LC_ALL, ""); + std::ifstream in(argv[1], std::ios::in | std::ios::binary); - odc::Store* s = odc::importDocument(in); + + odc::Store* s; + try { + s = odc::importDocument(in); + } catch (int trap) { + std::cerr << "Exception in parsing file: BlackBox trap no. " << trap << std::endl; + return 2; + } catch (const char * exception) { + std::cerr << "Exception in parsing file: " << exception << std::endl; + return 2; + } // std::cout << s->toPlainText() << std::endl; // std::cout << std::endl << std::endl; - odc::MyVisitor visitor; - s->accept(visitor); + try { + odc::MyVisitor visitor; + s->accept(visitor); + } catch (const char * exception) { + std::cerr << "Exception in processing document: " << exception << std::endl; + return 3; + } // std::cout << s->toString() << std::endl; // std::cout << in.tellg() << " " << in.eof() << std::endl;