TextConverter.cpp
Go to the documentation of this file.
00001 //
00002 // TextConverter.cpp
00003 //
00004 // $Id: //poco/1.3/Foundation/src/TextConverter.cpp#2 $
00005 //
00006 // Library: Foundation
00007 // Package: Text
00008 // Module:  TextConverter
00009 //
00010 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
00011 // and Contributors.
00012 //
00013 // Permission is hereby granted, free of charge, to any person or organization
00014 // obtaining a copy of the software and accompanying documentation covered by
00015 // this license (the "Software") to use, reproduce, display, distribute,
00016 // execute, and transmit the Software, and to prepare derivative works of the
00017 // Software, and to permit third-parties to whom the Software is furnished to
00018 // do so, all subject to the following:
00019 // 
00020 // The copyright notices in the Software and this entire statement, including
00021 // the above license grant, this restriction and the following disclaimer,
00022 // must be included in all copies of the Software, in whole or in part, and
00023 // all derivative works of the Software, unless such copies or derivative
00024 // works are solely in the form of machine-executable object code generated by
00025 // a source language processor.
00026 // 
00027 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00028 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00029 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
00030 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
00031 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
00032 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00033 // DEALINGS IN THE SOFTWARE.
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 } // namespace Poco


pluginlib
Author(s): Tully Foote and Eitan Marder-Eppstein
autogenerated on Sat Dec 28 2013 17:20:19