YSTest: TextFile.cpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

TextFile.cpp
浏览该文件的文档.
1 /*
2 © 2009-2016 FrankHB.
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 "YSLib/Service/YModules.h"
29 #include YFM_YSLib_Service_TextFile
30 #include YFM_CHRLib_Convert
31 #include <ystdex/utility.hpp> // for ystdex::as_const;
32
33 namespace YSLib
34 {
35
36 namespace Text
37 {
38
39 Encoding
40 VerifyEncoding(std::FILE* fp, char* s, size_t buflen, size_t txt_len,
41 Encoding enc)
42 {
43 const auto n(min(txt_len, buflen));
44
45 std::char_traits<char>::assign(Nonnull(s) + n, buflen - n, char());
46 if(std::fread(s, 1, n, Nonnull(fp)) == n)
47 {
48 std::rewind(fp);
49 if(VerifyUC(&ystdex::as_const(s[0]), ptrdiff_t(n), enc))
50 return enc;
51 }
52 return CharSet::Null;
53 }
54 Encoding
55 VerifyEncoding(std::istream& stream, char* s, size_t buflen, size_t txt_len,
56 Encoding enc)
57 {
58 const auto n(min(txt_len, buflen));
59
60 std::char_traits<char>::assign(Nonnull(s) + n, buflen - n, char());
61 stream.read(s, std::streamsize(n));
62 if(stream)
63 {
64 stream.seekg(0);
65 if(VerifyUC(&ystdex::as_const(s[0]), ptrdiff_t(n), enc))
66 return enc;
67 }
68 return CharSet::Null;
69 }
70
71 pair<Encoding, size_t>
72 DetectBOM(const char* buf)
73 {
74 YAssertNonnull(buf);
75 #define YSL_Impl_DetectBOM(_n) \
76 if(CheckBOM(buf, BOM_##_n)) \
77 return {CharSet::_n, size(BOM_##_n) - 1};
78 YSL_Impl_DetectBOM(UTF_32LE)
79 YSL_Impl_DetectBOM(UTF_32BE)
80 YSL_Impl_DetectBOM(UTF_8)
81 YSL_Impl_DetectBOM(UTF_16LE)
82 YSL_Impl_DetectBOM(UTF_16BE)
83 #undef YSL_Impl_DetectBOM
84 return {CharSet::Null, 0};
85 }
86 pair<Encoding, size_t>
87 DetectBOM(string_view sv)
88 {
89 YAssertNonnull(sv.data());
90 #define YSL_Impl_DetectBOM(_n) \
91 if(sv.size() >= size(BOM_##_n) - 1 && CheckBOM(sv.data(), BOM_##_n)) \
92 return {CharSet::_n, size(BOM_##_n) - 1};
93 YSL_Impl_DetectBOM(UTF_32LE)
94 YSL_Impl_DetectBOM(UTF_32BE)
95 YSL_Impl_DetectBOM(UTF_8)
96 YSL_Impl_DetectBOM(UTF_16LE)
97 YSL_Impl_DetectBOM(UTF_16BE)
98 #undef YSL_Impl_DetectBOM
99 return {CharSet::Null, 0};
100 }
101 pair<Encoding, size_t>
102 DetectBOM(std::istream& is, std::size_t fsize, Encoding enc)
103 {
104 is.seekg(0);
105 if(fsize > 1U)
106 {
107 array<char, 4> buf;
108
109 is.read(buf.data(), 4);
110 if(bool(is) || is.eof())
111 {
112 const auto res(DetectBOM(string_view(buf.data(),
113 CheckNonnegativeScalar<size_t>(is.gcount()))));
114
115 is.seekg(std::istream::off_type(res.second));
116 if(res.second != 0)
117 return res;
118 }
119 else
120 return {CharSet::Null, 0};
121 }
122
123 char s[64U];
124
125 return {VerifyEncoding(is, s, size(s), size_t(fsize), enc), 0};
126 }
127
128 size_t
129 WriteBOM(std::ostream& os, Encoding enc)
130 {
131 switch(enc)
132 {
133 #define YSL_Impl_WriteBOM(_n) \
134 case CharSet::_n: \
135 ystdex::write_literal(os, BOM_##_n); \
136 return size(BOM_##_n) - 1;
137 YSL_Impl_WriteBOM(UTF_16LE)
138 YSL_Impl_WriteBOM(UTF_16BE)
139 YSL_Impl_WriteBOM(UTF_8)
140 YSL_Impl_WriteBOM(UTF_32LE)
141 YSL_Impl_WriteBOM(UTF_32BE)
142 #undef YSL_Impl_WriteBOM
143 default:
144 return 0;
145 }
146 }
147
148 } // namespace Text;
149
150 } // namespace YSLib;
151
CHRLib::enc
enc
Definition: CharacterProcessing.h:81
ystdex::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:415
platform::Nonnull
_type && Nonnull(_type &&p) ynothrow
断言并返回非空指针。
Definition: Debug.h:411
YSLib::Text::buf
const char * buf
Definition: TextFile.h:77
YSLib::Text::VerifyEncoding
Encoding VerifyEncoding(std::FILE *fp, char *s, size_t buflen, size_t txt_len, Encoding enc)
Definition: TextFile.cpp:40
YSL_Impl_DetectBOM
#define YSL_Impl_DetectBOM(_n)
utility.hpp
实用设施。
CHRLib::VerifyUC
bool VerifyUC(_tIn &&first, ystdex::remove_reference_t< _tIn > last, Encoding enc)
验证指定字符序列是否可转换为 UCS-2 字符串。
Definition: Convert.hpp:84
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
CHRLib::CharSet::UTF_8
yconstexpr const Encoding UTF_8(csUTF8)
CHRLib::CharSet::Null
空字符集。
Definition: Encoding.h:63
platform::size_t
YF_API size_t
Definition: FileIO.h:1262
CHRLib::CharSet::Encoding
Encoding
字符流编码标识。
Definition: Encoding.h:58
CHRLib::CharSet::UTF_32LE
yconstexpr const Encoding UTF_32LE(csUTF32LE)
ystdex::assign
void assign(_tCon &con, _tParams &&...args)
插入参数指定的元素到容器。
Definition: container.hpp:412
YModules.h
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:114
YSLib::Text::WriteBOM
YF_API size_t WriteBOM(std::ostream &, Text::Encoding)
写入指定编码的 BOM 。
Definition: TextFile.cpp:129
YSLib::Text::DetectBOM
YF_API size_t DetectBOM(const char *)
Definition: TextFile.cpp:72
ystdex::size
auto size(const _tRange &c) -> decltype(c.size())
类容器访问。
Definition: range.hpp:233
CHRLib::CharSet::UTF_32BE
yconstexpr const Encoding UTF_32BE(csUTF32BE)
CHRLib::CharSet::UTF_16BE
yconstexpr const Encoding UTF_16BE(csUTF16BE)
fp
tidy_ptr< std::istream > fp
Definition: TextManager.cpp:86
YSL_Impl_WriteBOM
#define YSL_Impl_WriteBOM(_n)
CHRLib::CharSet::UTF_16LE
yconstexpr const Encoding UTF_16LE(csUTF16LE)
Text
ystdex::min
const _type & min(const _type &a, const _type &b, _fComp comp=less< _type >())
取较小的元素。
Definition: algorithm.hpp:521
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
ystdex::as_const
add_const_t< _type > & as_const(_type &t)
转换 const 引用。
Definition: utility.hpp:50
platform::n
size_t n
Definition: YCLib/FileSystem.cpp:321