DEADSOFTWARE

Properly detect locale's charset
[odcread.git] / odcread.cc
index caa87e7713f0dc937cd24c717c54dbbc2add4160..891ea990102ba11cd63da7e952d66b4c2a3c379f 100644 (file)
 #include <visitor.h>
 
 // Character encoding conversions
-#include <locale.h>
-#include <iconv.h>
-#include <errno.h>
-#include <string.h>
+#include <langinfo.h> // determine the current charset
+#include <locale.h> // locale support
+#include <iconv.h> // charset conversions
+#include <errno.h> // error codes
+#include <string.h> // string descriptions of error codes
 
 namespace odc {
        class Context {
@@ -87,10 +88,10 @@ namespace odc {
                        terminateContext();
                }
                char *getCharSet() {
-                       return "UTF-8"; // FIXME setlocale(LC_CTYPE, 0) + processing
+                       return nl_langinfo(CODESET);
                }
                virtual void textShortPiece(const ShortPiece *piece) {
-                       iconv_t conv = iconv_open("UTF-8", "ISO-8859-1");
+                       iconv_t conv = iconv_open(getCharSet(), "ISO-8859-1");
                        if (conv == (iconv_t)-1) {
                                std::string str("iconv initialization error: ");
                                str += strerror(errno);
@@ -115,18 +116,19 @@ namespace odc {
                        d_context.top()->addPiece(str);
                }
                virtual void textLongPiece(const LongPiece *piece) {
+               /*      
                        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("UTF-8", "UTF-8");
+                       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() + 1;
+                       size_t bytesIn = piece->size() + 2;
                        char *in = (char*)piece->getBuffer();
                        size_t bytesOut = bytesIn; // FIXME probably not safe.
                        char *out = new char[bytesIn];
@@ -142,7 +144,7 @@ namespace odc {
                        for (std::string::iterator it = str.begin(); it < str.end(); ++it) {
                                if (*it == '\r') *it = '\n';
                        }
-                       d_context.top()->addPiece(str);*/
+                       d_context.top()->addPiece(str);
                }
        };