00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "Poco/TextConverter.h"
00038 #include "Poco/TextIterator.h"
00039 #include "Poco/TextEncoding.h"
00040
00041
00042 namespace {
00043 int nullTransform(int ch)
00044 {
00045 return ch;
00046 }
00047 }
00048
00049
00050 namespace Poco {
00051
00052
00053 TextConverter::TextConverter(const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar):
00054 _inEncoding(inEncoding),
00055 _outEncoding(outEncoding),
00056 _defaultChar(defaultChar)
00057 {
00058 }
00059
00060
00061 TextConverter::~TextConverter()
00062 {
00063 }
00064
00065
00066 int TextConverter::convert(const std::string& source, std::string& destination, Transform trans)
00067 {
00068 int errors = 0;
00069 TextIterator it(source, _inEncoding);
00070 TextIterator end(source);
00071 unsigned char buffer[TextEncoding::MAX_SEQUENCE_LENGTH];
00072
00073 while (it != end)
00074 {
00075 int c = *it;
00076 if (c == -1) { ++errors; c = _defaultChar; }
00077 c = trans(c);
00078 int n = _outEncoding.convert(c, buffer, sizeof(buffer));
00079 if (n == 0) n = _outEncoding.convert(_defaultChar, buffer, sizeof(buffer));
00080 poco_assert (n <= (int)sizeof(buffer));
00081 destination.append((const char*) buffer, n);
00082 ++it;
00083 }
00084 return errors;
00085 }
00086
00087
00088 int TextConverter::convert(const void* source, int length, std::string& destination, Transform trans)
00089 {
00090 poco_check_ptr (source);
00091
00092 int errors = 0;
00093 const unsigned char* it = (const unsigned char*) source;
00094 const unsigned char* end = (const unsigned char*) source + length;
00095 unsigned char buffer[TextEncoding::MAX_SEQUENCE_LENGTH];
00096
00097 while (it < end)
00098 {
00099 unsigned char c = *it;
00100 int n = _inEncoding.characterMap()[c];
00101 int uc;
00102 if (n == -1)
00103 {
00104 ++errors;
00105 uc = _defaultChar;
00106 ++it;
00107 }
00108 else if (n >= 0)
00109 {
00110 uc = n;
00111 ++it;
00112 }
00113 else
00114 {
00115 if (it - n <= end)
00116 {
00117 uc = _inEncoding.convert(it);
00118 if (uc == -1) uc = _defaultChar;
00119 }
00120 else
00121 {
00122 ++errors;
00123 uc = _defaultChar;
00124 }
00125 it -= n;
00126 }
00127 uc = trans(uc);
00128 n = _outEncoding.convert(uc, buffer, sizeof(buffer));
00129 if (n == 0) n = _outEncoding.convert(_defaultChar, buffer, sizeof(buffer));
00130 poco_assert (n <= (int)sizeof(buffer));
00131 destination.append((const char*) buffer, n);
00132 }
00133 return errors;
00134 }
00135
00136
00137 int TextConverter::convert(const std::string& source, std::string& destination)
00138 {
00139 return convert(source, destination, nullTransform);
00140 }
00141
00142
00143 int TextConverter::convert(const void* source, int length, std::string& destination)
00144 {
00145 return convert(source, length, destination, nullTransform);
00146 }
00147
00148
00149 }