Ignore:
Timestamp:
08/18/12 17:26:01 (12 years ago)
Author:
nanardon
Message:

Support an encoding parameter for Unix files base

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Unix.pm

    r1071 r1123  
    88use LATMOS::Accounts::Log; 
    99use Fcntl qw(:flock); 
     10use Encode; 
    1011 
    1112our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; 
     
    7677sub _load_unix_file { 
    7778    my ($self, $file, $callback) = @_; 
     79    my $enc = find_encoding($self->config('encoding') || 'iso-8859-14') 
     80        or do { 
     81        $self->log(LA_ERROR, "Cannot find encoding %s", $self->config('encoding') || 'iso-8859-15'); 
     82        return; 
     83    }; 
    7884    open(my $handle, '<', $file) or do { 
    7985        $self->log(LA_ERR, "Cannot open unix file `%s' for reading (%s)", $file, $!); 
     
    8490    while (my $line = <$handle>) { 
    8591        chomp($line); 
     92        $line = encode('utf-8', $line); 
    8693        my @ch = split(':', $line); 
    8794        $callback->(@ch); 
     
    195202sub _save_unix_file { 
    196203    my ($self, $file, @data) = @_; 
     204    my $enc = find_encoding($self->config('encoding') || 'iso-8859-14') 
     205        or do { 
     206        $self->log(LA_ERROR, "Cannot find encoding %s", $self->config('encoding') || 'iso-8859-15'); 
     207        return; 
     208    }; 
    197209    open(my $handle, '>>', $file) or do { 
    198210        la_log(LA_ERR, "Cannot open unix file `%s' for writing (%s)", $file, $!); 
     
    202214    truncate($handle, 0); 
    203215    foreach my $line (@data) { 
    204         print $handle join(':', map { my $f = $_; $f =~ s/:/;/g; $f } map { defined($_) ? $_ : '' } @$line) . "\n"; 
     216        my $string = join(':', map { my $f = $_; $f =~ s/:/;/g; $f } map { defined($_) ? $_ : '' } @$line) . "\n"; 
     217        $string = decode('utf8', $string); 
     218        print $handle $string; 
    205219    } 
    206220    close($handle); 
Note: See TracChangeset for help on using the changeset viewer.