| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # file::ini Brik | ||
| 5 | # | ||
| 6 | package Metabrik::File::Ini; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | input => [ qw(file) ], | ||
| 20 | output => [ qw(file) ], | ||
| 21 | encoding => [ qw(utf8|ascii) ], | ||
| 22 | overwrite => [ qw(0|1) ], | ||
| 23 | }, | ||
| 24 | attributes_default => { | ||
| 25 | encoding => 'utf8', | ||
| 26 | overwrite => 1, | ||
| 27 | }, | ||
| 28 | commands => { | ||
| 29 | read => [ qw(input|OPTIONAL) ], | ||
| 30 | write => [ qw(ini_hash output|OPTIONAL) ], | ||
| 31 | }, | ||
| 32 | require_modules => { | ||
| 33 | 'Metabrik::String::Ini' => [ ], | ||
| 34 | 'Metabrik::File::Text' => [ ], | ||
| 35 | }, | ||
| 36 | }; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub read { | ||
| 40 | my $self = shift; | ||
| 41 | my ($input) = @_; | ||
| 42 | |||
| 43 | $input ||= $self->input; | ||
| 44 | $self->brik_help_run_undef_arg('read', $input) or return; | ||
| 45 | |||
| 46 | my $ft = Metabrik::File::Text->new_from_brik_init($self) or return; | ||
| 47 | $ft->encoding($self->encoding); | ||
| 48 | |||
| 49 | my $string = $ft->read($input) or return; | ||
| 50 | |||
| 51 | my $si = Metabrik::String::Ini->new_from_brik_init($self) or return; | ||
| 52 | |||
| 53 | my $ini_hash = $si->decode($string) or return; | ||
| 54 | |||
| 55 | return $ini_hash; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub write { | ||
| 59 | my $self = shift; | ||
| 60 | my ($ini_hash, $output) = @_; | ||
| 61 | |||
| 62 | $output ||= $self->output; | ||
| 63 | $self->brik_help_run_undef_arg('write', $output) or return; | ||
| 64 | $self->brik_help_run_undef_arg('write', $ini_hash) or return; | ||
| 65 | $self->brik_help_run_invalid_arg('write', $ini_hash, 'HASH') or return; | ||
| 66 | |||
| 67 | my $si = Metabrik::String::Ini->new_from_brik_init($self) or return; | ||
| 68 | |||
| 69 | my $string = $si->encode($ini_hash) or return; | ||
| 70 | |||
| 71 | my $ft = Metabrik::File::Text->new_from_brik_init($self) or return; | ||
| 72 | $ft->encoding($self->encoding); | ||
| 73 | |||
| 74 | $ft->write($string, $output) or return; | ||
| 75 | |||
| 76 | return $output; | ||
| 77 | } | ||
| 78 | |||
| 79 | 1; | ||
| 80 | |||
| 81 | __END__ | ||