src/TextCodec.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 <memory>
       
    22 
       
    23 #include "../include/relpipe/common/text/TextCodec.h"
       
    24 
       
    25 namespace relpipe {
       
    26 namespace common {
       
    27 namespace text {
       
    28 
       
    29 class TextCodec::TextCodecInternal {
       
    30 private:
       
    31 public:
       
    32 	static const std::string UTF8;
       
    33 	static const std::string DEFAULT;
       
    34 	Iconv* bytesToText;
       
    35 	Iconv* textToBytes;
       
    36 	//std::unique_ptr<Iconv> bytesToText;
       
    37 	//std::unique_ptr<Iconv> textToBytes;
       
    38 	bool sameEncoding = false;
       
    39 
       
    40 	TextCodecInternal(const std::string& encoding) {
       
    41 		// TODO: set sameEncoding = true if $LANG or current locale is same as encoding
       
    42 		bytesToText = new Iconv(encoding, DEFAULT);
       
    43 		textToBytes = new Iconv(DEFAULT, encoding);
       
    44 		//bytesToText.reset(new Iconv(encoding, DEFAULT));
       
    45 		//textToBytes.reset(new Iconv(DEFAULT, encoding));
       
    46 	}
       
    47 
       
    48 	virtual ~TextCodecInternal() {
       
    49 		delete bytesToText;
       
    50 		delete textToBytes;
       
    51 	}
       
    52 };
       
    53 
       
    54 const std::string TextCodec::TextCodecInternal::UTF8 = "UTF-8";
       
    55 const std::string TextCodec::TextCodecInternal::DEFAULT = "";
       
    56 
       
    57 TextCodec::TextCodec(const std::string& encoding) {
       
    58 	internal = new TextCodec::TextCodecInternal(encoding);
       
    59 }
       
    60 
       
    61 TextCodec::TextCodec() : TextCodec(TextCodec::TextCodecInternal::UTF8) {
       
    62 }
       
    63 
       
    64 TextCodec::~TextCodec() {
       
    65 	delete internal;
       
    66 }
       
    67 
       
    68 std::string TextCodec::fromBytes(std::string bytes) {
       
    69 	if (internal->sameEncoding) return bytes;
       
    70 	else return internal->bytesToText->convert(bytes);
       
    71 }
       
    72 
       
    73 std::string TextCodec::toBytes(std::string text) {
       
    74 	if (internal->sameEncoding) return text;
       
    75 	else return internal->textToBytes->convert(text);
       
    76 }
       
    77 
       
    78 }
       
    79 }
       
    80 }