原文地址:http://cjmxp007.blog.163.com/blog/static/35473837201061541933667/
1 #ifndef TRANSFORM
2 #define TRANSFORM
3 #include <map>
4 using namespace std;
5 // 是否启用map转换,建议启用
6 // 使用map转换的话,同一个常量字符串不会做两次转换
7 #define TEST_TYPE_MAP
8
9 typedef map<const char*, const char*> strmap;
10
11 class CUtf8String
12 {
13 public:
14 inline CUtf8String(const char* gb2312)
15 {
16 m_bIsConst = true;
17 #ifdef TEST_TYPE_MAP
18 if (m[gb2312])
19 {
20 m_utf8 = m[gb2312];
21 return ;
22 }
23 #endif
24 int buffLen = 0;
25 WCHAR wbuff[5120];
26 MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wbuff, 5120);
27 buffLen = WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, NULL, 0, 0, 0);
28 m_utf8 = new char[buffLen+1];
29 WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, (LPSTR)m_utf8, buffLen, 0, 0);
30 #ifdef TEST_TYPE_MAP
31 m[gb2312] = m_utf8;
32 #endif
33 }
34
35 inline CUtf8String(char* gb2312)
36 {
37 m_bIsConst = false;
38 int buffLen = 0;
39 WCHAR wbuff[5120];
40 MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wbuff, 5120);
41 buffLen = WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, NULL, 0, 0, 0);
42 m_utf8 = new char[buffLen+1];
43 WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, (LPSTR)m_utf8, buffLen, 0, 0);
44 }
45
46 inline ~CUtf8String()
47 {
48 #ifndef TEST_TYPE_MAP
49 if (m_utf8)
50 {
51 delete m_utf8;
52 m_utf8 = 0;
53 }
54 #else
55 if (!m_bIsConst)
56 {
57 if (m_utf8)
58 {
59 delete m_utf8;
60 m_utf8 = 0;
61 }
62 }
63 #endif
64 }
65
66 inline operator char*()
67 {
68 return (char*)m_utf8;
69 }
70 private:
71 const char* m_utf8;
72 bool m_bIsConst;
73 #ifdef TEST_TYPE_MAP
74 static strmap m;
75 #endif
76 };
77
78 class CGb2312String
79 {
80 public:
81 inline CGb2312String(const char* utf8)
82 {
83 #ifdef TEST_TYPE_MAP
84 if (m[utf8])
85 {
86 m_gb2312 = 0;
87 m_gb2312 = m[utf8];
88 }
89 #endif
90 int buffLen = 0;
91 WCHAR wbuff[5120];
92 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wbuff, 5120);
93 buffLen = WideCharToMultiByte(CP_ACP, 0, wbuff, -1, NULL, 0, 0, 0);
94 m_gb2312 = new char[buffLen+1];
95 WideCharToMultiByte(CP_ACP, 0, wbuff, -1, (LPSTR)m_gb2312, buffLen, 0, 0);
96 #ifdef TEST_TYPE_MAP
97 m[utf8] = m_gb2312;
98 #endif
99 }
100
101 inline CGb2312String(char* utf8)
102 {
103 #ifdef TEST_TYPE_MAP
104 if (m[utf8])
105 {
106 m_gb2312 = 0;
107 m_gb2312 = m[utf8];
108 }
109 #endif
110 int buffLen = 0;
111 WCHAR wbuff[5120];
112 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wbuff, 5120);
113 buffLen = WideCharToMultiByte(CP_ACP, 0, wbuff, -1, NULL, 0, 0, 0);
114 m_gb2312 = new char[buffLen+1];
115 WideCharToMultiByte(CP_ACP, 0, wbuff, -1, (LPSTR)m_gb2312, buffLen, 0, 0);
116 #ifdef TEST_TYPE_MAP
117 m[utf8] = m_gb2312;
118 #endif
119 }
120
121 inline ~CGb2312String()
122 {
123 #ifndef TEST_TYPE_MAP
124 if (m_gb2312)
125 {
126 delete m_gb2312;
127 m_gb2312 = 0;
128 }
129 #endif
130 }
131
132 inline operator char*()
133 {
134 return (char*)m_gb2312;
135 }
136 private:
137 const char* m_gb2312;
138 #ifdef TEST_TYPE_MAP
139 static strmap m;
140 #endif
141 };
142
143 #ifdef TEST_TYPE_MAP
144 strmap CUtf8String::m;
145 strmap CGb2312String::m;
146 #endif
147 #endif
148
149
150 //===================无聊的分割线==========================
151
152 #define U (CUtf8String)
153
154 #define W (CGb2312String)
155
156 // 使用方法
157 int main(int argc, char* argv[])
158 {
159 // 打印出乱码即为UTF8的编码,方便吧。C++还是确实很强悍的
160 printf("%s", U"你好中国!");
161 }
2 #define TRANSFORM
3 #include <map>
4 using namespace std;
5 // 是否启用map转换,建议启用
6 // 使用map转换的话,同一个常量字符串不会做两次转换
7 #define TEST_TYPE_MAP
8
9 typedef map<const char*, const char*> strmap;
10
11 class CUtf8String
12 {
13 public:
14 inline CUtf8String(const char* gb2312)
15 {
16 m_bIsConst = true;
17 #ifdef TEST_TYPE_MAP
18 if (m[gb2312])
19 {
20 m_utf8 = m[gb2312];
21 return ;
22 }
23 #endif
24 int buffLen = 0;
25 WCHAR wbuff[5120];
26 MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wbuff, 5120);
27 buffLen = WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, NULL, 0, 0, 0);
28 m_utf8 = new char[buffLen+1];
29 WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, (LPSTR)m_utf8, buffLen, 0, 0);
30 #ifdef TEST_TYPE_MAP
31 m[gb2312] = m_utf8;
32 #endif
33 }
34
35 inline CUtf8String(char* gb2312)
36 {
37 m_bIsConst = false;
38 int buffLen = 0;
39 WCHAR wbuff[5120];
40 MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wbuff, 5120);
41 buffLen = WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, NULL, 0, 0, 0);
42 m_utf8 = new char[buffLen+1];
43 WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, (LPSTR)m_utf8, buffLen, 0, 0);
44 }
45
46 inline ~CUtf8String()
47 {
48 #ifndef TEST_TYPE_MAP
49 if (m_utf8)
50 {
51 delete m_utf8;
52 m_utf8 = 0;
53 }
54 #else
55 if (!m_bIsConst)
56 {
57 if (m_utf8)
58 {
59 delete m_utf8;
60 m_utf8 = 0;
61 }
62 }
63 #endif
64 }
65
66 inline operator char*()
67 {
68 return (char*)m_utf8;
69 }
70 private:
71 const char* m_utf8;
72 bool m_bIsConst;
73 #ifdef TEST_TYPE_MAP
74 static strmap m;
75 #endif
76 };
77
78 class CGb2312String
79 {
80 public:
81 inline CGb2312String(const char* utf8)
82 {
83 #ifdef TEST_TYPE_MAP
84 if (m[utf8])
85 {
86 m_gb2312 = 0;
87 m_gb2312 = m[utf8];
88 }
89 #endif
90 int buffLen = 0;
91 WCHAR wbuff[5120];
92 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wbuff, 5120);
93 buffLen = WideCharToMultiByte(CP_ACP, 0, wbuff, -1, NULL, 0, 0, 0);
94 m_gb2312 = new char[buffLen+1];
95 WideCharToMultiByte(CP_ACP, 0, wbuff, -1, (LPSTR)m_gb2312, buffLen, 0, 0);
96 #ifdef TEST_TYPE_MAP
97 m[utf8] = m_gb2312;
98 #endif
99 }
100
101 inline CGb2312String(char* utf8)
102 {
103 #ifdef TEST_TYPE_MAP
104 if (m[utf8])
105 {
106 m_gb2312 = 0;
107 m_gb2312 = m[utf8];
108 }
109 #endif
110 int buffLen = 0;
111 WCHAR wbuff[5120];
112 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wbuff, 5120);
113 buffLen = WideCharToMultiByte(CP_ACP, 0, wbuff, -1, NULL, 0, 0, 0);
114 m_gb2312 = new char[buffLen+1];
115 WideCharToMultiByte(CP_ACP, 0, wbuff, -1, (LPSTR)m_gb2312, buffLen, 0, 0);
116 #ifdef TEST_TYPE_MAP
117 m[utf8] = m_gb2312;
118 #endif
119 }
120
121 inline ~CGb2312String()
122 {
123 #ifndef TEST_TYPE_MAP
124 if (m_gb2312)
125 {
126 delete m_gb2312;
127 m_gb2312 = 0;
128 }
129 #endif
130 }
131
132 inline operator char*()
133 {
134 return (char*)m_gb2312;
135 }
136 private:
137 const char* m_gb2312;
138 #ifdef TEST_TYPE_MAP
139 static strmap m;
140 #endif
141 };
142
143 #ifdef TEST_TYPE_MAP
144 strmap CUtf8String::m;
145 strmap CGb2312String::m;
146 #endif
147 #endif
148
149
150 //===================无聊的分割线==========================
151
152 #define U (CUtf8String)
153
154 #define W (CGb2312String)
155
156 // 使用方法
157 int main(int argc, char* argv[])
158 {
159 // 打印出乱码即为UTF8的编码,方便吧。C++还是确实很强悍的
160 printf("%s", U"你好中国!");
161 }