YSTest: chrproc.cpp 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
chrproc.cpp
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2009 - 2013.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "CHRLib/chrproc.h"
29 #include "CHRLib/MapEx.h"
30 #include <cctype>
31 #include <cstdlib>
32 #include <cwchar>
33 #include <ystdex/cstdio.h>
34 #include <ystdex/cstring.h>
35 #include "CHRLib/Convert.hpp"
36
37 CHRLIB_BEGIN
38
39 using std::size_t;
40 using std::tolower;
41 using std::malloc;
42 using std::strlen;
43 using std::memcpy;
44 using ystdex::ntctslen;
45 using ystdex::input_monomorphic_iterator;
46
47 ConversionResult
48 MBCToUC(ucs2_t& uc, const char*& c, Encoding enc, ConversionState&& st)
49 {
50 if(const auto pfun = FetchMapperPtr<ConversionResult(ucs2_t&,
51 input_monomorphic_iterator&&, ConversionState&&)>(enc))
52 return ConvertCharacter(pfun, uc, c, std::move(st));
53 return ConversionResult::Unhandled;
54 }
55 ConversionResult
56 MBCToUC(ucs2_t& uc, std::FILE* fp, Encoding enc, ConversionState&& st)
57 {
58 yconstraint(fp);
59
60 if(const auto pfun = FetchMapperPtr<ConversionResult(ucs2_t&,
61 input_monomorphic_iterator&&, ConversionState&&)>(enc))
62 {
63 ystdex::ifile_iterator i(*fp);
64 const auto r(ConvertCharacter(pfun, uc, i, std::move(st)));
65
66 std::ungetc(*i, fp);
67 return r;
68 }
69 return ConversionResult::Unhandled;
70 }
71 ConversionResult
72 MBCToUC(const char*& c, Encoding enc, ConversionState&& st)
73 {
74 if(const auto pfun = FetchMapperPtr<ConversionResult(
75 input_monomorphic_iterator&&, ConversionState&&)>(enc))
76 return ConvertCharacter(pfun, c, std::move(st));
77 return ConversionResult::Unhandled;
78 }
79 ConversionResult
80 MBCToUC(std::FILE* fp, Encoding enc, ConversionState&& st)
81 {
82 yconstraint(fp);
83
84 if(const auto pfun = FetchMapperPtr<ConversionResult(
85 input_monomorphic_iterator&&, ConversionState&&)>(enc))
86 {
87 ystdex::ifile_iterator i(*fp);
88 const auto r(ConvertCharacter(pfun, i, std::move(st)));
89
90 std::ungetc(*i, fp);
91 return r;
92 }
93 return ConversionResult::Unhandled;
94 }
95
96 size_t
97 UCToMBC(char* d, const ucs2_t& s, Encoding enc)
98 {
99 yconstraint(d);
100
101 size_t l(0);
102
103 if(const auto pfun = FetchMapperPtr<byte(char*, const ucs2_t&)>(enc))
104 l = pfun(d, s);
105 return l;
106 }
107
108
109 size_t
110 MBCSToUCS2(ucs2_t* d, const char* s, Encoding enc)
111 {
112 yconstraint(d),
113 yconstraint(s);
114
115 const auto p(d);
116
117 if(const auto pfun = FetchMapperPtr<ConversionResult(ucs2_t&,
118 input_monomorphic_iterator&&, ConversionState&&)>(enc))
119 while(!ystdex::is_null(*s))
120 {
121 ConversionState st;
122
123 ConvertCharacter(pfun, *d++, s, std::move(st));
124 }
125 *d = 0;
126 return d - p;
127 }
128
129 size_t
130 UCS2ToMBCS(char* d, const ucs2_t* s, Encoding enc)
131 {
132 yconstraint(d),
133 yconstraint(s);
134
135 const auto p(d);
136
137 if(const auto pfun = FetchMapperPtr<byte(char*, const ucs2_t&)>(enc))
138 while(!ystdex::is_null(*s))
139 d += pfun(d, *s++);
140 *d = 0;
141 return d - p;
142 }
143
144 size_t
145 UCS4ToUCS2(ucs2_t* d, const ucs4_t* s)
146 {
147 yconstraint(d),
148 yconstraint(s);
149
150 const auto p(d);
151
152 while(!ystdex::is_null(*s))
153 *d++ = *s++;
154 *d = 0;
155 return d - p;
156 }
157
158
159 char*
160 strdup(const ucs2_t* s, Encoding enc)
161 {
162 yconstraint(s);
163
164 // FIXME: size for max MBC sequence length > 4;
165 const auto str(static_cast<char*>(malloc((ystdex::ntctslen(s) << 2) + 1)));
166
167 UCS2ToMBCS(str, s, enc);
168 return str;
169 }
170
171 ucs2_t*
172 ucsdup(const char* s, Encoding enc)
173 {
174 yconstraint(s);
175
176 const auto p(static_cast<ucs2_t*>(malloc((strlen(s) + 1) << 1)));
177
178 if(YB_LIKELY(p))
179 MBCSToUCS2(p, s, enc);
180 return p;
181 }
182 ucs2_t*
183 ucsdup(const ucs2_t* str)
184 {
185 yconstraint(str);
186
187 const size_t n(ntctslen(str) * sizeof(ucs2_t));
188 const auto p(static_cast<ucs2_t*>(malloc(n + sizeof(ucs2_t))));
189
190 if(YB_LIKELY(p))
191 memcpy(p, str, n);
192 return p;
193 }
194 ucs2_t*
195 ucsdup(const ucs4_t* s)
196 {
197 yconstraint(s);
198
199 const auto p(static_cast<ucs2_t*>(malloc((ntctslen(s) + 1)
200 * sizeof(ucs2_t))));
201
202 if(YB_LIKELY(p))
203 UCS4ToUCS2(p, s);
204 return p;
205 }
206
207 CHRLIB_END
208