###########################################################################
#
# multiread.pm --
#
# Copyright (C) 1999 DigiLib Systems Limited, NZ
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################

# the multiread object will read in a number of encodings,
# the results are always returned in the utf-8 format

# encodings currently supported are
#
# utf8             - either utf8 or unicode (automatically detected)
# unicode          - just unicode (doesn't currently do endian detection)
#
# plus all encodings in the "encodings" package

package multiread;

use unicode;

sub new {
    my ($class) = @_;

    my $self = {'handle'    => "",
		'first'     => 1,
		'encoding'  => "utf8",
		'bigendian' => 1};

    return bless $self, $class;
}

# set_handle expects the file to be already open but
# not read yet
sub set_handle {
    my $self = shift (@_);
    ($self->{'handle'}) = @_;
    $self->{'first'} = 1;
    $self->{'encoding'} = "utf8";
    $self->{'bigendian'} = 1;
}

# set_encoding should be called after set_handle
sub set_encoding {
    my $self = shift (@_);
    ($self->{'encoding'}) = @_;
}

sub get_encoding {
    my $self = shift (@_);
    return $self->{'encoding'};
}

# undef will be returned if the eof has been reached
# the result will always be returned in utf-8
# if automatic detection between utf8 and unicode is desired
# then the encoding should be initially set to utf8
sub read_unicode_char {
    my $self = shift (@_);

    # make sure we have a file handle
    return undef if ($self->{'handle'} eq "");
    my $handle = $self->{'handle'};
    binmode ($handle);

    if ($self->{'encoding'} eq "utf8") {
	# utf-8 text, how many characters we get depends 
	# on what we find
	my $c1 = "";
	my $c2 = "";
	my $c3 = "";

	while (!eof ($handle)) {
	    $c1 = ord (getc ($handle));
	    if ($self->{'first'}) {
		$self->{'first'} = 0;

		if ($c1 == 0xfe || $c1 == 0xff) {
		    $c2 = ord (getc ($handle)) if (!eof ($handle));
		    
		    # if unicode fall through to the unicode reading code
		    if ($c1 == 0xff && $c2 == 0xfe) {
			$self->{'encoding'} = "unicode";
			$self->{'bigendian'} = 0;
			last;

		    } elsif ($c1 == 0xfe && $c2 == 0xff) {
			$self->{'encoding'} = "unicode";
			$self->{'bigendian'} = 1;
			last;
		    }

		    # an error, but we might be able to recover
		    # from it
		    $c1 = $c2;
		}
	    }

	    if ($c1 <= 0x7f) {
		# one byte character
		return chr ($c1);

	    } elsif ($c1 >= 0xc0 && $c1 <= 0xdf) {
		# two byte character
		$c2 = getc ($handle) if (!eof ($handle));
		return chr ($c1) . $c2;

	    } elsif ($c1 >= 0xe0 && $c1 <= 0xef) {
		# three byte character
		$c2 = getc ($handle) if (!eof ($handle));
		$c3 = getc ($handle) if (!eof ($handle));
		return chr ($c1) . $c2 . $c3;
	    }

	    # if we get here there was an error in the file, we should
	    # be able to recover from it however, maybe the file is in
	    # another encoding
	}

	return undef if (eof ($handle));
    }

    if ($self->{'encoding'} eq "unicode") {
	# unicode text, get the next two characters
	return undef if (eof ($handle));
	my $c1 = ord (getc ($handle));
	return undef if (eof ($handle));
	my $c2 = ord (getc ($handle));

	return &unicode::unicode2utf8 ([(($self->{'bigendian'}) ? ($c1*256+$c2) : ($c2*256+$c1))]);
    }

    return undef;
}


# undef will be returned if the eof has been reached
# the result will always be returned in utf-8
sub read_line {
    my $self = shift (@_);

    # make sure we have a file handle
    return undef if ($self->{'handle'} eq "");

    my $handle = $self->{'handle'};

    if ($self->{'first'} && $self->{'encoding'} eq "utf8") {
	# special case for the first line of utf8 text to detect whether
	# the file is in utf8 or unicode
	my $out = "";
	my $thisc = "";
	while (defined ($thisc = $self->read_unicode_char())) {
	    $out .= $thisc;
	    last if ($thisc eq "\n");
	}
	
	return $out if (length ($out) > 0);
	return undef;
    }

    if ($self->{'encoding'} eq "utf8") {
	# utf-8 line
	return <$handle>;
    }

    if ($self->{'encoding'} eq "unicode") {
	# unicode line
	my $c = "";
	my ($c1, $c2) = ("", "");
	my $out = "";
	while (read ($handle, $c, 2) == 2) {
	    $c1 = ord (substr ($c, 0, 1));
	    $c2 = ord (substr ($c, 1, 1));
	    $c = &unicode::unicode2utf8([(($self->{'bigendian'}) ? ($c1*256+$c2) : ($c2*256+$c1))]);
	    $out .= $c;
	    last if ($c eq "\n");
	}

	return $out if (length ($out) > 0);
	return undef;
    }

    if ($self->{'encoding'} eq "iso_8859_1") {
	# we'll use ascii2utf8() for this as it's faster than going
	# through convert2unicode()
	my $line = "";
	if (defined ($line = <$handle>)) {
	    return &unicode::ascii2utf8 (\$line);
	}
    }

    # everything else uses unicode::convert2unicode
    my $line = "";
    if (defined ($line = <$handle>)) {
	return &unicode::unicode2utf8 (&unicode::convert2unicode ($self->{'encoding'}, \$line));
    }

    return undef;
}


# will convert entire contents of file to utf8 and append result to $outputref
# this may be a slightly faster way to get the contents of a file than by 
# recursively calling read_line()
sub read_file {
    my $self = shift (@_);
    my ($outputref) = @_;

    # make sure we have a file handle
    return if ($self->{'handle'} eq "");

    my $handle = $self->{'handle'};

    if ($self->{'first'} && $self->{'encoding'} eq "utf8") {
	# special case for the first line of utf8 text to detect whether
	# the file is in utf8 or unicode
	
	# possible to have no text here...
	my $read_text = $self->read_line ();
	$$outputref .= $read_text if (defined($read_text));
    }

    if ($self->{'encoding'} eq "utf8") {
	undef $/;
	$$outputref .=  <$handle>;
	$/ = "\n";
	return;
    }

    if ($self->{'encoding'} eq "unicode") {
	my $line = "";
	while (defined ($line = $self->read_line())) {
	    $$outputref .= $line;
	}
	return;
    }

    if ($self->{'encoding'} eq "iso_8859_1") {
	# we'll use ascii2utf8() for this as it's faster than going
	# through convert2unicode()
	undef $/;
	my $text = <$handle>;
	$/ = "\n";
	$$outputref .= &unicode::ascii2utf8 (\$text);
	return;
    }

    # everything else uses unicode::convert2unicode
    undef $/;
    my $text = <$handle>;
    $/ = "\n";
    $$outputref .= &unicode::unicode2utf8 (&unicode::convert2unicode ($self->{'encoding'}, \$text));
}

1;
