1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # multiread.pm --
|
---|
4 | #
|
---|
5 | # Copyright (C) 1999 DigiLib Systems Limited, NZ
|
---|
6 | #
|
---|
7 | # This program is free software; you can redistribute it and/or modify
|
---|
8 | # it under the terms of the GNU General Public License as published by
|
---|
9 | # the Free Software Foundation; either version 2 of the License, or
|
---|
10 | # (at your option) any later version.
|
---|
11 | #
|
---|
12 | # This program is distributed in the hope that it will be useful,
|
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | # GNU General Public License for more details.
|
---|
16 | #
|
---|
17 | # You should have received a copy of the GNU General Public License
|
---|
18 | # along with this program; if not, write to the Free Software
|
---|
19 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
20 | #
|
---|
21 | ###########################################################################
|
---|
22 |
|
---|
23 | # the multiread object will read in a number of encodings,
|
---|
24 | # the results are always returned in the utf-8 format
|
---|
25 |
|
---|
26 | # encodings currently supported are
|
---|
27 | #
|
---|
28 | # utf8 - either utf8 or unicode (automatically detected)
|
---|
29 | # unicode - just unicode (doesn't currently do endian detection)
|
---|
30 | # gb - GB
|
---|
31 | # iso_8859_[1-9] - 8 bit extended ascii encodings
|
---|
32 | # windows_125[0-8] - Windows codepages 1250 to 1258
|
---|
33 | # windows 874 - Windows codepage 874
|
---|
34 | # iscii_de - ISCII Devanagari
|
---|
35 | # shift_jis - Shift-JIS
|
---|
36 | # euc_jp - EUC encoded Japanese
|
---|
37 | # uhc - Unified Hangul Code (Korean)
|
---|
38 |
|
---|
39 | package multiread;
|
---|
40 |
|
---|
41 | use unicode;
|
---|
42 | use cjk;
|
---|
43 |
|
---|
44 | sub new {
|
---|
45 | my ($class) = @_;
|
---|
46 |
|
---|
47 | my $self = {'handle' => "",
|
---|
48 | 'first' => 1,
|
---|
49 | 'encoding' => "utf8",
|
---|
50 | 'bigendian' => 1};
|
---|
51 |
|
---|
52 | return bless $self, $class;
|
---|
53 | }
|
---|
54 |
|
---|
55 | # set_handle expects the file to be already open but
|
---|
56 | # not read yet
|
---|
57 | sub set_handle {
|
---|
58 | my $self = shift (@_);
|
---|
59 | ($self->{'handle'}) = @_;
|
---|
60 | $self->{'first'} = 1;
|
---|
61 | $self->{'encoding'} = "utf8";
|
---|
62 | $self->{'bigendian'} = 1;
|
---|
63 | }
|
---|
64 |
|
---|
65 | # set_encoding should be called after set_handle
|
---|
66 | sub set_encoding {
|
---|
67 | my $self = shift (@_);
|
---|
68 | ($self->{'encoding'}) = @_;
|
---|
69 | }
|
---|
70 |
|
---|
71 | sub get_encoding {
|
---|
72 | my $self = shift (@_);
|
---|
73 | return $self->{'encoding'};
|
---|
74 | }
|
---|
75 |
|
---|
76 | # undef will be returned if the eof has been reached
|
---|
77 | # the result will always be returned in utf-8
|
---|
78 | # if automatic detection between utf8 and unicode is desired
|
---|
79 | # then the encoding should be initially set to utf8
|
---|
80 | sub read_unicode_char {
|
---|
81 | my $self = shift (@_);
|
---|
82 |
|
---|
83 | # make sure we have a file handle
|
---|
84 | return undef if ($self->{'handle'} eq "");
|
---|
85 | my $handle = $self->{'handle'};
|
---|
86 | binmode ($handle);
|
---|
87 |
|
---|
88 | if ($self->{'encoding'} eq "utf8") {
|
---|
89 | # utf-8 text, how many characters we get depends
|
---|
90 | # on what we find
|
---|
91 | my $c1 = "";
|
---|
92 | my $c2 = "";
|
---|
93 | my $c3 = "";
|
---|
94 |
|
---|
95 | while (!eof ($handle)) {
|
---|
96 | $c1 = ord (getc ($handle));
|
---|
97 | if ($self->{'first'}) {
|
---|
98 | $self->{'first'} = 0;
|
---|
99 |
|
---|
100 | if ($c1 == 0xfe || $c1 == 0xff) {
|
---|
101 | $c2 = ord (getc ($handle)) if (!eof ($handle));
|
---|
102 |
|
---|
103 | # if unicode fall through to the unicode reading code
|
---|
104 | if ($c1 == 0xff && $c2 == 0xfe) {
|
---|
105 | $self->{'encoding'} = "unicode";
|
---|
106 | $self->{'bigendian'} = 0;
|
---|
107 | last;
|
---|
108 |
|
---|
109 | } elsif ($c1 == 0xfe && $c2 == 0xff) {
|
---|
110 | $self->{'encoding'} = "unicode";
|
---|
111 | $self->{'bigendian'} = 1;
|
---|
112 | last;
|
---|
113 | }
|
---|
114 |
|
---|
115 | # an error, but we might be able to recover
|
---|
116 | # from it
|
---|
117 | $c1 = $c2;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | if ($c1 <= 0x7f) {
|
---|
122 | # one byte character
|
---|
123 | return chr ($c1);
|
---|
124 |
|
---|
125 | } elsif ($c1 >= 0xc0 && $c1 <= 0xdf) {
|
---|
126 | # two byte character
|
---|
127 | $c2 = getc ($handle) if (!eof ($handle));
|
---|
128 | return chr ($c1) . $c2;
|
---|
129 |
|
---|
130 | } elsif ($c1 >= 0xe0 && $c1 <= 0xef) {
|
---|
131 | # three byte character
|
---|
132 | $c2 = getc ($handle) if (!eof ($handle));
|
---|
133 | $c3 = getc ($handle) if (!eof ($handle));
|
---|
134 | return chr ($c1) . $c2 . $c3;
|
---|
135 | }
|
---|
136 |
|
---|
137 | # if we get here there was an error in the file, we should
|
---|
138 | # be able to recover from it however, maybe the file is in
|
---|
139 | # another encoding
|
---|
140 | }
|
---|
141 |
|
---|
142 | return undef if (eof ($handle));
|
---|
143 | }
|
---|
144 |
|
---|
145 | if ($self->{'encoding'} eq "unicode") {
|
---|
146 | # unicode text, get the next two characters
|
---|
147 | return undef if (eof ($handle));
|
---|
148 | my $c1 = ord (getc ($handle));
|
---|
149 | return undef if (eof ($handle));
|
---|
150 | my $c2 = ord (getc ($handle));
|
---|
151 |
|
---|
152 | return &unicode::unicode2utf8 ([(($self->{'bigendian'}) ? ($c1*256+$c2) : ($c2*256+$c1))]);
|
---|
153 | }
|
---|
154 |
|
---|
155 | return undef;
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | # undef will be returned if the eof has been reached
|
---|
160 | # the result will always be returned in utf-8
|
---|
161 | sub read_line {
|
---|
162 | my $self = shift (@_);
|
---|
163 |
|
---|
164 | # make sure we have a file handle
|
---|
165 | return undef if ($self->{'handle'} eq "");
|
---|
166 |
|
---|
167 | my $handle = $self->{'handle'};
|
---|
168 |
|
---|
169 | if ($self->{'first'} && $self->{'encoding'} eq "utf8") {
|
---|
170 | # special case for the first line of utf8 text to detect whether
|
---|
171 | # the file is in utf8 or unicode
|
---|
172 | my $out = "";
|
---|
173 | my $thisc = "";
|
---|
174 | while (defined ($thisc = $self->read_unicode_char())) {
|
---|
175 | $out .= $thisc;
|
---|
176 | last if ($thisc eq "\n");
|
---|
177 | }
|
---|
178 |
|
---|
179 | return $out if (length ($out) > 0);
|
---|
180 | return undef;
|
---|
181 | }
|
---|
182 |
|
---|
183 | if ($self->{'encoding'} eq "utf8") {
|
---|
184 | # utf-8 line
|
---|
185 | return <$handle>;
|
---|
186 | }
|
---|
187 |
|
---|
188 | if ($self->{'encoding'} eq "unicode") {
|
---|
189 | # unicode line
|
---|
190 | my $c = "";
|
---|
191 | my ($c1, $c2) = ("", "");
|
---|
192 | my $out = "";
|
---|
193 | while (read ($handle, $c, 2) == 2) {
|
---|
194 | $c1 = ord (substr ($c, 0, 1));
|
---|
195 | $c2 = ord (substr ($c, 1, 1));
|
---|
196 | $c = &unicode::unicode2utf8([(($self->{'bigendian'}) ? ($c1*256+$c2) : ($c2*256+$c1))]);
|
---|
197 | $out .= $c;
|
---|
198 | last if ($c eq "\n");
|
---|
199 | }
|
---|
200 |
|
---|
201 | return $out if (length ($out) > 0);
|
---|
202 | return undef;
|
---|
203 | }
|
---|
204 |
|
---|
205 | if ($self->{'encoding'} eq "gb") {
|
---|
206 | # GB or GBK
|
---|
207 | my $line = "";
|
---|
208 | if (defined ($line = <$handle>)) {
|
---|
209 | return &unicode::unicode2utf8 (&cjk::gb2unicode ($line));
|
---|
210 | }
|
---|
211 | return undef;
|
---|
212 | }
|
---|
213 |
|
---|
214 | if ($self->{'encoding'} eq "iso_8859_1") {
|
---|
215 | # special case for iso_8859_1 as &ascii2utf8($line) is faster than
|
---|
216 | # &unicode2utf8(iso2unicode('1', $line))
|
---|
217 | my $line = "";
|
---|
218 | if (defined ($line = <$handle>)) {
|
---|
219 | return &unicode::ascii2utf8 ($line);
|
---|
220 | }
|
---|
221 | return undef;
|
---|
222 | }
|
---|
223 |
|
---|
224 | if ($self->{'encoding'} =~ /^iso_8859_(\d+)$/) {
|
---|
225 | my $line = "";
|
---|
226 | if (defined ($line = <$handle>)) {
|
---|
227 | return &unicode::unicode2utf8(&unicode::iso2unicode ($1, $line));
|
---|
228 | }
|
---|
229 | return undef;
|
---|
230 | }
|
---|
231 |
|
---|
232 | if ($self->{'encoding'} =~ /windows_(\d{3,4})$/) {
|
---|
233 | my $line = "";
|
---|
234 | if (defined ($line = <$handle>)) {
|
---|
235 | return &unicode::unicode2utf8(&unicode::windows2unicode ($1, $line));
|
---|
236 | }
|
---|
237 | return undef;
|
---|
238 | }
|
---|
239 |
|
---|
240 | if ($self->{'encoding'} =~ /^koi8_[ru]$/) {
|
---|
241 | my $line = "";
|
---|
242 | if (defined ($line = <$handle>)) {
|
---|
243 | return &unicode::unicode2utf8(&unicode::cyrillic2unicode ($self->{'encoding'}, $line));
|
---|
244 | }
|
---|
245 | return undef;
|
---|
246 | }
|
---|
247 |
|
---|
248 | if ($self->{'encoding'} eq "iscii_de") {
|
---|
249 | my $line = "";
|
---|
250 | if (defined ($line = <$handle>)) {
|
---|
251 | return &unicode::unicode2utf8(&unicode::iscii2unicode ("Devanagari", $line));
|
---|
252 | }
|
---|
253 | return undef;
|
---|
254 | }
|
---|
255 |
|
---|
256 | # unknown encoding
|
---|
257 | return undef;
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | # will convert entire contents of file to utf8 and append result to $outputref
|
---|
262 | # this may be a slightly faster way to get the contents of a file than by
|
---|
263 | # recursively calling read_line()
|
---|
264 | sub read_file {
|
---|
265 | my $self = shift (@_);
|
---|
266 | my ($outputref) = @_;
|
---|
267 |
|
---|
268 | # make sure we have a file handle
|
---|
269 | return if ($self->{'handle'} eq "");
|
---|
270 |
|
---|
271 | my $handle = $self->{'handle'};
|
---|
272 |
|
---|
273 | if ($self->{'first'} && $self->{'encoding'} eq "utf8") {
|
---|
274 | # special case for the first line of utf8 text to detect whether
|
---|
275 | # the file is in utf8 or unicode
|
---|
276 | $$text .= $self->read_line ();
|
---|
277 | }
|
---|
278 |
|
---|
279 | if ($self->{'encoding'} eq "utf8") {
|
---|
280 | undef $/;
|
---|
281 | $$outputref .= <$handle>;
|
---|
282 | $/ = "\n";
|
---|
283 | return;
|
---|
284 | }
|
---|
285 |
|
---|
286 | if ($self->{'encoding'} eq "unicode") {
|
---|
287 | my $line = "";
|
---|
288 | while (defined ($line = $self->read_line())) {
|
---|
289 | $$outputref .= $line;
|
---|
290 | }
|
---|
291 | return;
|
---|
292 | }
|
---|
293 |
|
---|
294 | if ($self->{'encoding'} eq "gb") {
|
---|
295 | undef $/;
|
---|
296 | my $text = <$handle>;
|
---|
297 | $/ = "\n";
|
---|
298 | $$outputref .= &unicode::unicode2utf8 (&cjk::gb2unicode ($text));
|
---|
299 | return;
|
---|
300 | }
|
---|
301 |
|
---|
302 | if ($self->{'encoding'} eq "iso_8859_1") {
|
---|
303 | # special case for iso_8859_1 as &ascii2utf8($text) is faster than
|
---|
304 | # &unicode2utf8(iso2unicode('1', $text))
|
---|
305 | undef $/;
|
---|
306 | my $text = <$handle>;
|
---|
307 | $/ = "\n";
|
---|
308 | $$outputref .= &unicode::ascii2utf8 ($text);
|
---|
309 | return;
|
---|
310 | }
|
---|
311 |
|
---|
312 | if ($self->{'encoding'} eq "shift_jis") {
|
---|
313 | undef $/;
|
---|
314 | my $text = <$handle>;
|
---|
315 | $/ = "\n";
|
---|
316 | $$outputref .= &unicode::unicode2utf8(&cjk::sjis2unicode ($text));
|
---|
317 | return;
|
---|
318 | }
|
---|
319 |
|
---|
320 | if ($self->{'encoding'} eq "euc_jp") {
|
---|
321 | undef $/;
|
---|
322 | my $text = <$handle>;
|
---|
323 | $/ = "\n";
|
---|
324 | $$outputref .= &unicode::unicode2utf8(&cjk::eucjp2unicode ($text));
|
---|
325 | return;
|
---|
326 | }
|
---|
327 |
|
---|
328 | if ($self->{'encoding'} eq "euc_kr") {
|
---|
329 | undef $/;
|
---|
330 | my $text = <$handle>;
|
---|
331 | $/ = "\n";
|
---|
332 | $$outputref .= &unicode::unicode2utf8(&cjk::euckr2unicode ($text));
|
---|
333 | return;
|
---|
334 | }
|
---|
335 |
|
---|
336 | if ($self->{'encoding'} eq "uhc") {
|
---|
337 | undef $/;
|
---|
338 | my $text = <$handle>;
|
---|
339 | $/ = "\n";
|
---|
340 | $$outputref .= &unicode::unicode2utf8(&cjk::uhc2unicode ($text));
|
---|
341 | return;
|
---|
342 | }
|
---|
343 |
|
---|
344 | # if we get to here we assume it's a simple 8 bit encoding
|
---|
345 | undef $/;
|
---|
346 | my $text = <$handle>;
|
---|
347 | $/ = "\n";
|
---|
348 | $$outputref .= &unicode::unicode2utf8(&unicode::simple2unicode ($self->{'encoding'}, $text));
|
---|
349 | }
|
---|
350 |
|
---|
351 | 1;
|
---|