Browse Source

* Subtitle::SSA::Record : add error() method

master
Alex 'AdUser' Z 9 years ago
parent
commit
346ac0d9e2
  1. 27
      lib/Subtitle/SSA/Record.pm

27
lib/Subtitle/SSA/Record.pm

@ -10,6 +10,15 @@ use overload
'""' => \&to_string, '""' => \&to_string,
; ;
sub error {
my ($self, $text) = @_;
if (defined $text) {
$self->{_error} = $text;
return;
}
return $self->{_error};
}
sub set { sub set {
my ($self, $field, $value) = @_; my ($self, $field, $value) = @_;
return unless $field; return unless $field;
@ -22,7 +31,8 @@ sub set {
sub parse_format_line { sub parse_format_line {
my ($self, $line) = @_; my ($self, $line) = @_;
return unless $line and $line =~ m{^Format:}oi; return $self->error("not looks like Format line")
unless $line and $line =~ m{^Format:}oi;
chomp $line; chomp $line;
$line =~ s{^Format:\s*}{}oi; $line =~ s{^Format:\s*}{}oi;
@ -30,7 +40,8 @@ sub parse_format_line {
my @fields; my @fields;
foreach my $field (split(/,\s*/o, $line)) { foreach my $field (split(/,\s*/o, $line)) {
$field = lc($field); $field = lc($field);
return unless exists $self->{_fields}->{$field}; return $self->error("unknown field: $field")
unless exists $self->{_fields}->{$field};
push @fields, $field; push @fields, $field;
} }
@ -49,15 +60,18 @@ sub parse {
$format //= $self->{_format}; $format //= $self->{_format};
my $PREFIX = $self->{_prefix}; my $PREFIX = $self->{_prefix};
return unless $line and $line =~ m{^$PREFIX:}oi; return $self->error("not looks like $PREFIX line")
return unless $format and ref($format) eq 'ARRAY'; unless $line and $line =~ m{^$PREFIX:}oi;
return $self->error("passed custom fields order not ARRAY ref")
unless $format and ref($format) eq 'ARRAY';
chomp $line; chomp $line;
$line =~ s{^$PREFIX:\s*}{}oi; $line =~ s{^$PREFIX:\s*}{}oi;
my $fieldcnt = scalar @{ $format }; my $fieldcnt = scalar @{ $format };
my @values = split /,\s*/o, $line, $fieldcnt; my @values = split /,\s*/o, $line, $fieldcnt;
# check that values count match fields count # check that values count match fields count
return if scalar @values != $fieldcnt; return $self->error("number of fields less than expected count")
if scalar @values < $fieldcnt;
foreach my $field (@{ $format }) { foreach my $field (@{ $format }) {
my $d = $self->{_fields}->{$field}; my $d = $self->{_fields}->{$field};
@ -66,7 +80,8 @@ sub parse {
$value = hex($1); $value = hex($1);
} elsif ($d->{type} eq 't') { } elsif ($d->{type} eq 't') {
$value = parse_timing($value); $value = parse_timing($value);
return if $value < 0; # parsing failure return $self->error("can't parse timing: $value")
if $value < 0; # parsing failure
} }
$self->set($field => $value); $self->set($field => $value);
} }

Loading…
Cancel
Save