-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmdUnicode.cpp
208 lines (171 loc) · 5.28 KB
/
cmdUnicode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright [2014] <lgb (LiuGuangBao)>
//=====================================================================================
//
// Filename: cmdUnicode.cpp
//
// Description: Unicode 实现工具
//
// Version: 1.0
// Created: 2014年12月31日 14时40分42秒
// Revision: none
// Compiler: gcc
//
// Author: lgb (LiuGuangBao), easyeagel@gmx.com
// Organization: ezbty.org
//
//=====================================================================================
//
#include<functional>
#include<core/encode.hpp>
#include<core/utf8/utf8.h>
#include<boost/filesystem/fstream.hpp>
#include"option.hpp"
#include"optionFileSet.hpp"
namespace ezsh
{
namespace bf=boost::filesystem;
class CmdUnicode:public CmdBaseT<CmdUnicode, FileSetCmdBase<>>
{
typedef CmdBaseT<CmdUnicode, FileSetCmdBase> BaseThis;
public:
CmdUnicode()
:BaseThis("unicode - unicode file or dir")
{
static const char* opts[]={"bomAdd", "bomRemove", "valid"};
opt_.add_options()
(opts[0], "add BOM in file header")
(opts[1], "remove BOM in file header")
(opts[2], "check files utf8 encode")
;
modeOnly_.add(std::begin(opts), std::end(opts));
modeOnly_.doit(*this);
}
static const char* nameGet()
{
return "unicode";
}
void doit()
{
const auto& vm=mapGet();
auto& files=fileGet();
files.init(vm);
void (CmdUnicode::* fileOne)(const FileUnit& )=nullptr;
if(modeOnly_.oneGet()=="bomAdd")
fileOne=&CmdUnicode::bomAdd;
else if (modeOnly_.oneGet()=="bomRemove")
fileOne=&CmdUnicode::bomRemove;
else if (modeOnly_.oneGet()=="valid")
fileOne=&CmdUnicode::valid;
assert(fileOne);
files.loop([this, fileOne](FileUnit&& fu)
{
(this->*fileOne)(fu);
return errorBreak()==false;
}
);
}
private:
void valid(const FileUnit& file)
{
bf::ifstream strm;
const auto result=bomUtf8Check(file, strm);
if(strm.is_open()==false)
return;
stdOut()
<< core::WCharConverter::to(file.total.string())
<< (result.second ? ": valid" : ": invalid") << std::endl;
}
void bomAdd(const FileUnit& file)
{
bf::ifstream strm;
const auto result=bomUtf8Check(file, strm);
if(strm.is_open()==false)
return;
if(result.second==false)
{
stdErr() << "utf8-invalid: " << file.total << std::endl;
return;
}
if(result.first==true)
{
stdErr() << "utf8-BOM: " << file.total << std::endl;
return;
}
strm.seekg(0);
auto newFile=file.total;
newFile += pathCreate(".new");
bf::ofstream out(newFile);
out.write(reinterpret_cast<const char*>(utf8::bom), sizeof(utf8::bom));
std::copy(std::istreambuf_iterator<char>(strm), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(out));
strm.close();
out.close();
ErrorCode ec;
bf::rename(newFile, file.total, ec);
if(ec.bad())
stdErr() << "rename-failed: " << ec.message() << std::endl;
}
void bomRemove(const FileUnit& file)
{
bf::ifstream strm;
const auto result=bomUtf8Check(file, strm);
if(strm.is_open()==false)
return;
if(result.second==false)
{
stdErr() << "utf8-invalid: " << file.total << std::endl;
return;
}
if(result.first==false)
{
stdErr() << "utf8-no-BOM: " << file.total << std::endl;
return;
}
strm.seekg(3);
auto newFile=file.total;
newFile += pathCreate(".new");
bf::ofstream out(newFile);
std::copy(std::istreambuf_iterator<char>(strm), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(out));
out.close();
strm.close();
ErrorCode ec;
bf::rename(newFile, file.total, ec);
if(ec.bad())
stdErr() << "rename-failed: " << ec.message() << std::endl;
}
private:
std::pair<bool, bool> bomUtf8Check(const FileUnit& file, bf::ifstream& strm)
{
if(!file.isExist())
{
stdErr() << file.total << ": notExist" << std::endl;
return std::make_pair(false, false);
}
ErrorCode ec;
if(file.isDir())
{
stdErr() << file.total << ": isDir" << std::endl;
return std::make_pair(false, false);
}
strm.open(file.total);
if(!strm)
{
stdErr() << file.total << ": openFailed" << std::endl;
return std::make_pair(false, false);
}
std::istreambuf_iterator<char> const end;
std::istreambuf_iterator<char> const strmItr(strm);
const bool isBOM=utf8::starts_with_bom(strmItr, end);
if(!isBOM)
strm.seekg(0);
auto const itr=std::istreambuf_iterator<char>(strm);
const auto isValid=utf8::is_valid(itr, end);
return std::make_pair(isBOM, isValid);
}
private:
OptionOneAndOnly modeOnly_;
};
namespace
{
static CmdRegisterT<CmdUnicode> gsRegister;
}
}