src/TextCodec.cpp
author František Kučera <franta-hg@frantovo.cz>
Wed, 20 Nov 2019 14:56:41 +0100
branchv_0
changeset 18 9d566568d37c
permissions -rw-r--r--
Iconv and TextCodec classes for converting text encodings
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes (library)
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the:
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 *  - GNU Lesser General Public License as published by the Free Software Foundation;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *    version 3 of the License or (at your option)
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *  - GNU General Public License as published by the Free Software Foundation;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 *    version 2 of the License.
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 *
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * This program is distributed in the hope that it will be useful,
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * GNU General Public License for more details.
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 *
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
 */
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <memory>
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include "../include/relpipe/common/text/TextCodec.h"
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
namespace relpipe {
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
namespace common {
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace text {
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
class TextCodec::TextCodecInternal {
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
private:
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
public:
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	static const std::string UTF8;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	static const std::string DEFAULT;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	Iconv* bytesToText;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	Iconv* textToBytes;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	//std::unique_ptr<Iconv> bytesToText;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	//std::unique_ptr<Iconv> textToBytes;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	bool sameEncoding = false;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	TextCodecInternal(const std::string& encoding) {
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		// TODO: set sameEncoding = true if $LANG or current locale is same as encoding
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		bytesToText = new Iconv(encoding, DEFAULT);
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		textToBytes = new Iconv(DEFAULT, encoding);
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		//bytesToText.reset(new Iconv(encoding, DEFAULT));
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		//textToBytes.reset(new Iconv(DEFAULT, encoding));
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	}
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	virtual ~TextCodecInternal() {
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		delete bytesToText;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		delete textToBytes;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	}
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
};
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
const std::string TextCodec::TextCodecInternal::UTF8 = "UTF-8";
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
const std::string TextCodec::TextCodecInternal::DEFAULT = "";
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
TextCodec::TextCodec(const std::string& encoding) {
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	internal = new TextCodec::TextCodecInternal(encoding);
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
}
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
TextCodec::TextCodec() : TextCodec(TextCodec::TextCodecInternal::UTF8) {
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
}
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
TextCodec::~TextCodec() {
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	delete internal;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
}
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
std::string TextCodec::fromBytes(std::string bytes) {
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	if (internal->sameEncoding) return bytes;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	else return internal->bytesToText->convert(bytes);
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
}
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
std::string TextCodec::toBytes(std::string text) {
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	if (internal->sameEncoding) return text;
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	else return internal->textToBytes->convert(text);
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
}
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
}
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
}
9d566568d37c Iconv and TextCodec classes for converting text encodings
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
}