OSDN Git Service

d3889c4d94464d95057c6be3872999b016983ac7
[simplecms/utakata.git] / utf8.h
1 #ifndef _UTF8_H_
2 #define _UTF8_H_
3
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 #include <exception>
8
9 #include "smart_ptr.h"
10 #include "InputStream.h"
11
12 namespace utakata {
13
14     namespace utf8 {
15
16         // inputstreamの準備が出来ていない場合に送出される例外
17         class CStreamException : public std::exception
18         {
19         public:
20             CStreamException(const std::string& str) : str_(str) {}
21             virtual ~CStreamException() throw() {}
22
23             const char* what() throw() {
24                 return str_.c_str();
25             }
26         private:
27
28             std::string str_;
29         };
30
31         class CUTF8InputStream : public IInputStream
32         {
33             /**
34                入力ストリームから、UTF-8のデータを指定した文字だけ読みだして
35                返す。
36             */
37         public:
38             
39             // 入力に利用するストリームは最初に渡される。
40             // 最初に渡さない場合には、後から開けるようにしておかなけりゃならない。
41             CUTF8InputStream();
42             CUTF8InputStream(const smart_ptr<std::istream>& strm);
43             virtual ~CUTF8InputStream(){}
44
45             bool open(const smart_ptr<std::istream>& strm);
46
47             std::vector<unsigned char> read();
48             std::vector<unsigned char> read(int num);
49
50             std::vector<unsigned char> peek();
51
52
53         private:
54
55             smart_ptr<std::istream> strm_;
56         };
57
58
59
60         // UTF-8のコードを表すバイト列をUTF-8のコードに変換する。
61         long generateUTF8Code(const std::vector<unsigned char>& code);
62         long generateUTF8Code(const std::string& ch);
63         
64         struct CheckUTF8Byte
65         {
66             // UTF8の先頭バイト以外であるかどうかをチェックする。
67             const unsigned char checker;
68             bool good;
69             CheckUTF8Byte() : checker(0x2), good(true) {}
70
71             template<class T>
72             void operator()(const T& t) {
73                 // 先頭ビットが10ではない場合、チェックに失敗する。
74                 T tmp = t >> 6;
75                 if ( ((tmp & 0x3)) != checker) {
76                     good = false;
77                 }
78             }
79         };
80
81         struct PutBack
82         {
83             // 渡されたデータをistreamにputbackする。
84             smart_ptr<std::istream> strm_;
85             PutBack(const smart_ptr<std::istream>& strm) : strm_(strm) {}
86
87             template<class T>
88             void operator()(T t)
89                 {
90                     strm_->putback(t);
91                 }
92         };
93         
94         // 与えられたバイト列の先頭から、UTF8一文字に該当しているかどうかを返す。
95         // バイト列がUTF8に該当する場合、そのバイト列のサイズを返す。
96         bool is_utf8_one(const std::vector<unsigned char>& bytes, size_t& size);
97
98         // 与えられたバイト列全てが、UTF8に該当しているかどうかを返す。
99         bool is_utf8_all(const std::vector<unsigned char>& bytes);
100
101         // UTF-8の先頭バイトとして正しいフォーマットであるかどうか。
102         // 正しいフォーマットである場合、渡したバイトを含めた、一文字である
103         // バイト数を返す。
104         bool is_utf8_first_byte(unsigned char c, size_t& size);
105     };
106 }
107
108 #endif /* _UTF8_H_ */