X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=odcread.cc;h=a509ab6bc7a72468e72243815aaf794a47057240;hb=067f77e22bace6d17204e3f1f677dd6a5ca6f563;hp=858028e6babf5e252ede8ade0c0f71d2946ceb4e;hpb=41d9c65ec79728a8771cd2288dfc3458569c6801;p=odcread.git diff --git a/odcread.cc b/odcread.cc index 858028e..a509ab6 100644 --- a/odcread.cc +++ b/odcread.cc @@ -3,11 +3,18 @@ #include #include -#include -#include -#include -#include -#include +#include "oberon.h" +#include "reader/reader.h" +#include "store/store.h" +#include "textmodel/textmodel.h" +#include "visitor/visitor.h" + +// 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 { @@ -80,14 +87,61 @@ 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); + std::string str = convert((char *)piece->getBuffer(), piece->size() + 1, (char *)"ISO-8859-1", 1); + 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); + std::string str = convert((char *)piece->getBuffer(), piece->size() + 2, (char *)"UCS-2", 2); + d_context.top()->addPiece(str); + } + + /** + * Convert an input character buffer in the given encoding to the + * locale's encoding. + */ + std::string convert(char *in, size_t bytesIn, char *encodingIn, size_t inBytesPerChar) { + // Convert from the input encoding to the locale's encoding + iconv_t conv = iconv_open(getCharSet(), encodingIn); + + // Handle errors by throwing a readable message + if (conv == (iconv_t)-1) { + std::string str("iconv initialization error: "); + str += strerror(errno); + throw str.c_str(); + } + + // Assume at most 4 bytes per character are needed + size_t bytesOut = 4 * bytesIn / inBytesPerChar; + + // Allocate the output buffer + char *out = new char[bytesOut]; + char *outPtr = out; + + // Perform conversion + 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(); + } + + // Free the iconv state + iconv_close(conv); + + // Copy result into a std::string + std::string str(out); + delete out; + + // Convert newlines + for (std::string::iterator it = str.begin(); it < str.end(); ++it) { + if (*it == '\r') *it = '\n'; + } + + return str; } }; @@ -112,6 +166,10 @@ 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;