Usermode/libunicode - Add C++ wrappers
[tpg/acess2.git] / Usermode / Libraries / libunicode.so_src / include_exp / unicode.h
1 /*
2  * Acess2 "libunicode" UTF Parser
3  * - By John Hodge (thePowersGang)
4  *
5  * unicode.h
6  * - Main header
7  */
8 #ifndef _LIBUNICODE__UNICODE_H_
9 #define _LIBUNICODE__UNICODE_H_
10
11 #include <stdint.h>
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 /**
18  * \breif Read a single codepoint from  a UTF-8 stream
19  * \return Number of bytes read
20  */
21 extern int      ReadUTF8(const char *Input, uint32_t *Val);
22 /**
23  * \brief Read backwards in the stream
24  */
25 extern int      ReadUTF8Rev(const char *Base, int Offset, uint32_t *Val);
26 /**
27  * \breif Write a single codepoint to a UTF-8 stream
28  */
29 extern int      WriteUTF8(char *buf, uint32_t Val);
30
31
32 static inline int       Unicode_IsPrinting(uint32_t Codepoint) { return 1; }
33
34 #ifdef __cplusplus
35 }       // extern "C"
36 #endif
37
38 #ifdef __cplusplus
39 #include <iterator>
40
41 namespace libunicode {
42
43 class utf8iterator:
44         public ::std::iterator< ::std::forward_iterator_tag, uint32_t >
45 {
46         const char*     m_curpos;
47         uint32_t        m_curval;
48 public:
49         utf8iterator():
50                 m_curpos(0)
51         {
52         }
53         utf8iterator(const char *pos):
54                 m_curpos(pos)
55         {
56                 (*this)++;
57         }
58         utf8iterator(const utf8iterator& other) {
59                 m_curpos = other.m_curpos;
60                 m_curval = other.m_curval;
61         }
62         utf8iterator& operator=(const utf8iterator& other) {
63                 m_curpos = other.m_curpos;
64                 m_curval = other.m_curval;
65                 return *this;
66         }
67         
68         bool operator== (const utf8iterator& other) {
69                 return other.m_curpos == m_curpos;
70         }
71         bool operator!= (const utf8iterator& other) {
72                 return other.m_curpos != m_curpos;
73         }
74         utf8iterator& operator++() {
75                 m_curpos += ::ReadUTF8(m_curpos, &m_curval);
76                 return *this;
77         }
78         utf8iterator operator++(int) {
79                 utf8iterator    rv(*this);
80                 m_curpos += ::ReadUTF8(m_curpos, &m_curval);
81                 return rv;
82         }
83         uint32_t operator*() const {
84                 return m_curval;
85         }
86         uint32_t operator->() const {
87                 return m_curval;
88         }
89 };
90
91 class utf8string
92 {
93         const char* m_data;
94         size_t  m_len;
95         
96         size_t _strlen(const char*s) {
97                 size_t  l = 0;
98                 while(*s)       l ++;
99                 return l;
100         }
101 public:
102         utf8string(const char* c_str):
103                 m_data(c_str),
104                 m_len(_strlen(c_str))
105         {
106         }
107         utf8string(const char* c_str, size_t len):
108                 m_data(c_str),
109                 m_len(len)
110         {
111         }
112         utf8string(const ::std::string& str):
113                 m_data(str.c_str()),
114                 m_len(str.size())
115         {
116         }
117         
118         utf8iterator begin() const {
119                 return utf8iterator(m_data);
120         }
121         utf8iterator end() const {
122                 return utf8iterator(m_data + m_len);
123         }
124 };
125
126
127 };
128
129 #endif
130
131 #endif
132

UCC git Repository :: git.ucc.asn.au