OSDN Git Service

sublexer.cpp -- sublexer.hをインターフェースのみとし、実装をsublexer_impl.cpp/hに
[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
38             const unsigned char EOF_;
39             
40         public:
41             
42             // 入力に利用するストリームは最初に渡される。
43             // 最初に渡さない場合には、後から開けるようにしておかなけりゃならない。
44             CUTF8InputStream();
45             CUTF8InputStream(const smart_ptr<std::istream>& strm);
46             virtual ~CUTF8InputStream(){}
47
48             bool open(const smart_ptr<std::istream>& strm);
49
50             std::vector<unsigned char> read();
51             std::vector<unsigned char> read(int num);
52
53             std::vector<unsigned char> peek();
54
55             // ファイルの終端に到達しているかどうかを返す。
56             // trueを返す場合、readの結果は常にEOF文字を返す。
57             bool isEOF() const;
58             
59         private:
60
61             smart_ptr<std::istream> strm_;
62         };
63
64
65
66         // UTF-8のコードを表すバイト列をUTF-8のコードに変換する。
67         long generateUTF8Code(const std::vector<unsigned char>& code);
68         long generateUTF8Code(const std::string& ch);
69         
70         struct CheckUTF8Byte
71         {
72             // UTF8の先頭バイト以外であるかどうかをチェックする。
73             const unsigned char checker;
74             bool good;
75             CheckUTF8Byte() : checker(0x2), good(true) {}
76
77             template<class T>
78             void operator()(const T& t) {
79                 // 先頭ビットが10ではない場合、チェックに失敗する。
80                 T tmp = t >> 6;
81                 if ( ((tmp & 0x3)) != checker) {
82                     good = false;
83                 }
84             }
85         };
86
87         struct PutBack
88         {
89             // 渡されたデータをistreamにputbackする。
90             smart_ptr<std::istream> strm_;
91             PutBack(const smart_ptr<std::istream>& strm) : strm_(strm) {}
92
93             template<class T>
94             void operator()(T t)
95                 {
96                     strm_->putback(t);
97                 }
98         };
99         
100         // 与えられたバイト列の先頭から、UTF8一文字に該当しているかどうかを返す。
101         // バイト列がUTF8に該当する場合、そのバイト列のサイズを返す。
102         bool is_utf8_one(const std::vector<unsigned char>& bytes, size_t& size);
103
104         // 与えられたバイト列全てが、UTF8に該当しているかどうかを返す。
105         bool is_utf8_all(const std::vector<unsigned char>& bytes);
106
107         // UTF-8の先頭バイトとして正しいフォーマットであるかどうか。
108         // 正しいフォーマットである場合、渡したバイトを含めた、一文字である
109         // バイト数を返す。
110         bool is_utf8_first_byte(unsigned char c, size_t& size);
111     };
112 }
113
114 #endif /* _UTF8_H_ */