src/Iconv.cpp
branchv_0
changeset 18 9d566568d37c
equal deleted inserted replaced
17:46151cd23815 18:9d566568d37c
       
     1 /**
       
     2  * Relational pipes (library)
       
     3  * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the:
       
     7  *  - GNU Lesser General Public License as published by the Free Software Foundation;
       
     8  *    version 3 of the License or (at your option)
       
     9  *  - GNU General Public License as published by the Free Software Foundation;
       
    10  *    version 2 of the License.
       
    11  *
       
    12  * This program is distributed in the hope that it will be useful,
       
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    15  * GNU General Public License for more details.
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License
       
    18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    19  */
       
    20 
       
    21 #include "../include/relpipe/common/text/Iconv.h"
       
    22 
       
    23 namespace relpipe {
       
    24 namespace common {
       
    25 namespace text {
       
    26 
       
    27 class Iconv::IconvInternal {
       
    28 public:
       
    29 	static const size_t BUFFER_SIZE_MINIMUM;
       
    30 	static const size_t BUFFER_SIZE_DEFAULT;
       
    31 	const size_t bufferSize;
       
    32 	iconv_t cd;
       
    33 
       
    34 	IconvInternal(std::string to, std::string from, const size_t bufferSize) : bufferSize(std::max(bufferSize, BUFFER_SIZE_MINIMUM)) {
       
    35 		cd = iconv_open(to.c_str(), from.c_str());
       
    36 		if (errno) throw std::string("iconv_open() error: ") + strerror(errno); // TODO: custom exception
       
    37 	}
       
    38 
       
    39 	virtual ~IconvInternal() {
       
    40 		iconv_close(cd);
       
    41 	}
       
    42 
       
    43 };
       
    44 
       
    45 const size_t Iconv::IconvInternal::BUFFER_SIZE_MINIMUM = 2;  // TODO: enough?
       
    46 const size_t Iconv::IconvInternal::BUFFER_SIZE_DEFAULT = 20;
       
    47 
       
    48 Iconv::Iconv(std::string to, std::string from) : Iconv(to, from, IconvInternal::BUFFER_SIZE_DEFAULT) {
       
    49 }
       
    50 
       
    51 Iconv::Iconv(std::string to, std::string from, size_t bufferSize) {
       
    52 	internal = new Iconv::IconvInternal(to, from, bufferSize);
       
    53 
       
    54 }
       
    55 
       
    56 Iconv::~Iconv() {
       
    57 	delete internal;
       
    58 }
       
    59 
       
    60 std::string Iconv::convert(std::string originalText) {
       
    61 	std::stringstream result;
       
    62 	size_t inBytesLeft = originalText.size();
       
    63 	size_t outBytesLeft = internal->bufferSize;
       
    64 	char outBuffer[internal->bufferSize];
       
    65 	char* outBuf = (char*) outBuffer;
       
    66 	char* inBuf = (char*) originalText.c_str();
       
    67 
       
    68 	do {
       
    69 		size_t nconv = iconv(internal->cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft);
       
    70 		if (nconv < 0) throw std::string("iconv() error: ") + strerror(errno); // TODO: custom exception
       
    71 		// TODO: throw exception if locale is not initialized and platform default "" is used (avoid infinite loop)
       
    72 		result.write(outBuffer, internal->bufferSize - outBytesLeft);
       
    73 		outBytesLeft = internal->bufferSize;
       
    74 		outBuf = (char*) outBuffer;
       
    75 	} while (inBytesLeft > 0);
       
    76 
       
    77 	return result.str();
       
    78 }
       
    79 
       
    80 }
       
    81 }
       
    82 }