| 1 | #ifndef OTOCO_SAMPLE_ENCODE_STRING_H |
|---|
| 2 | #define OTOCO_SAMPLE_ENCODE_STRING_H |
|---|
| 3 | |
|---|
| 4 | #include <vector> |
|---|
| 5 | #include <unordered_map> |
|---|
| 6 | #include <string> |
|---|
| 7 | #include <exception> |
|---|
| 8 | |
|---|
| 9 | class EncodeStringException : public std::exception |
|---|
| 10 | { |
|---|
| 11 | const std::string message; |
|---|
| 12 | EncodeStringException() = delete; |
|---|
| 13 | EncodeStringException &operator =(const EncodeStringException &) = delete; |
|---|
| 14 | public: |
|---|
| 15 | explicit EncodeStringException(const std::string &mess) : std::exception(), message(mess) {} |
|---|
| 16 | EncodeStringException(const EncodeStringException &src) : std::exception(), message(src.message) {} |
|---|
| 17 | virtual ~EncodeStringException() throw() {} |
|---|
| 18 | virtual char const *what() const throw() { return message.c_str(); } |
|---|
| 19 | }; |
|---|
| 20 | |
|---|
| 21 | enum charset_t { |
|---|
| 22 | chset_Utf8, chset_C99, chset_Java, |
|---|
| 23 | chset_Ucs2, chset_Ucs2Be, chset_Ucs2Le, |
|---|
| 24 | chset_Ucs4, chset_Ucs4Be, chset_Ucs4Le, |
|---|
| 25 | chset_Utf16, chset_Utf16Be, chset_Utf16Le, |
|---|
| 26 | chset_Utf32, chset_Utf32Be, chset_Utf32Le, |
|---|
| 27 | chset_Utf7, |
|---|
| 28 | chset_EucJp, chset_EucJis0213, |
|---|
| 29 | chset_Iso2022Jp, chset_Iso2022Jp2, chset_Iso2022Jp1, chset_Iso2022Jp3, |
|---|
| 30 | chset_ShiftJis, chset_Cp932, chset_ShiftJisX0213, |
|---|
| 31 | }; |
|---|
| 32 | |
|---|
| 33 | namespace std { |
|---|
| 34 | template <> |
|---|
| 35 | inline size_t |
|---|
| 36 | hash<charset_t>::operator()(charset_t val) const |
|---|
| 37 | { |
|---|
| 38 | return static_cast<size_t>(val); |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | class EncodeStringImpl |
|---|
| 43 | { |
|---|
| 44 | typedef std::unordered_map<charset_t, char const *> cnmap_t; |
|---|
| 45 | static cnmap_t const CharsetNames; |
|---|
| 46 | static size_t const TerminateNullCharNum = 4; ///< 文字列終端のヌル文字に使用するバイト数。 |
|---|
| 47 | protected: |
|---|
| 48 | std::vector<char> result; |
|---|
| 49 | size_t result_length; |
|---|
| 50 | EncodeStringImpl(); |
|---|
| 51 | virtual ~EncodeStringImpl() {} |
|---|
| 52 | void encode(void const *src, size_t src_length, size_t chr_size, charset_t from_charset, charset_t to_charset); |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | template <typename ic_t, charset_t internal_charset> |
|---|
| 56 | class EncodeString : private EncodeStringImpl |
|---|
| 57 | { |
|---|
| 58 | std::basic_string<ic_t> source; |
|---|
| 59 | EncodeString() = delete; |
|---|
| 60 | EncodeString(const EncodeString &) = delete; |
|---|
| 61 | EncodeString &operator =(const EncodeString &) = delete; |
|---|
| 62 | public: |
|---|
| 63 | EncodeString(const std::basic_string<ic_t> &src, charset_t charset) : EncodeStringImpl(), source{src} |
|---|
| 64 | { encode(source.c_str(), source.size(), sizeof(ic_t), internal_charset, charset); } |
|---|
| 65 | EncodeString(const std::basic_string<ic_t> &&src, charset_t charset) : source{src} |
|---|
| 66 | { encode(source.c_str(), source.size(), sizeof(ic_t), internal_charset, charset); } |
|---|
| 67 | ~EncodeString() {} |
|---|
| 68 | const char *getCharArray() const { return &result[0]; } |
|---|
| 69 | const size_t &getLength() const { return result_length; } |
|---|
| 70 | }; |
|---|
| 71 | |
|---|
| 72 | #endif //OTOCO_SAMPLE_ENCODE_STRING_H |
|---|