Repository navigation
Expand file tree
/
Copy path2utf8
More file actions
executable file
·209 lines (151 loc) · 3.76 KB
/
Copy path2utf8
File metadata and controls
executable file
·209 lines (151 loc) · 3.76 KB
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
#!/bin/bash
#===============================================================================
#
# FILE: 2utf8.sh
#
# USAGE: ./2utf8.sh
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: adaptee (), adaptee@gmail.com
# COMPANY: Open Source Corporation
# VERSION: 1.0
# CREATED: 09/25/10 11:33:25 CST
# REVISION: ---
#===============================================================================
if which tput 2>&1 >/dev/null ;then
BRIGHTRED=$(tput bold;tput setaf 1)
BRIGHTGREEN=$(tput bold;tput setaf 2)
NOCOLOR=$(tput sgr0)
else
BRIGHTRED='\e[01;31m'
BRIGHTGREEN='\e[01;32m'
NOCOLOR='\e[00m'
fi
green-echo ()
{
echo -e "${BRIGHTGREEN}$@${NOCOLOR}"
}
red-echo ()
{
echo -e "${BRIGHTRED}$@${NOCOLOR}"
}
function guess_encoding_by_enca()
{
local target="$1"
local encoding="unknown"
# ask chardet to run in batchmode, only providing the encdding name
# encoding name are forced upper case
local guess=$(enca -i "${target}" | tr '[a-z]' '[A-Z]' )
#echo -e "[debug] >>${guess}<<"
case "${guess}" in
"ASCII"|"UTF-8" )
encoding="done"
;;
"UTF-16BE" )
encoding="ucs-2be"
;;
"UTF-16LE" )
encoding="ucs-2le"
;;
"UTF-32BE" )
encoding="ucs-4be"
;;
"UTF-32LE" )
encoding="ucs-4le"
;;
"GB2312"|"GBK"|"GB18030" )
encoding="gb18030"
;;
"BIG5" )
encoding="big5"
;;
"SHIFT_JIS" )
encoding="sjis"
;;
"EUC_JP" )
encoding="ecu-jp"
;;
* )
encoding="unknown"
;;
esac
echo "${encoding}"
}
################################################################################
function guess_encoding_by_chardet()
{
local target="$1"
local encoding="unknown"
# ask chardet to run in batchmode, only providing the encdding name
# encoding name are forced upper case
local guess=$(chardet -b "${target}" | tr '[a-z]' '[A-Z]' )
#echo -e "[debug] >>${guess}<<"
case "${guess}" in
"ASCII"|"UTF-8" )
encoding="done"
;;
"UTF-16BE" )
encoding="ucs-2be"
;;
"UTF-16LE" )
encoding="ucs-2le"
;;
"UTF-32BE" )
encoding="ucs-4be"
;;
"UTF-32LE" )
encoding="ucs-4le"
;;
"GB2312"|"GBK"|"GB18030" )
encoding="gb18030"
;;
"BIG5" )
encoding="big5"
;;
"SHIFT_JIS" )
encoding="sjis"
;;
"EUC_JP" )
encoding="ecu-jp"
;;
* )
encoding="unknown"
;;
esac
echo "${encoding}"
}
function guess_encoding()
{
local target="$1"
local encoding="unknown"
if which chardet 2>&1 >/dev/null ; then
encoding=$(guess_encoding_by_chardet "${target}")
elif which enca 2>&1 >/dev/null ; then
ehcoding=$(guess_encoding_by_enca "${target}")
fi
echo "${encoding}"
}
for item in "$@" ; do
encoding=$(guess_encoding "${item}")
case "${encoding}" in
"done" )
green-echo "${item} is already using UTF-8"
;;
"unknown" )
red-echo "${item} is using unknown encoding"
;;
* )
old="${item}.$$.old"
temp="${item}.$$.temp"
cp "${item}" "${old}"
iconv -f "${encoding}" -t utf-8 -c "${item}" > "${temp}"
mv "${temp}" "${item}"
echo "${item} is converted from ${encoding} to utf-8"
;;
esac
done