| 1 | package Panotools::Script::Line; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Storable qw/ dclone /; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Panotools::Script::Line - Panorama Tools script data | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | Base class for a line in a panotools script | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | A line starts with a single letter identifier then a series of | ||
| 19 | namevalue items separated by whitespace | ||
| 20 | |||
| 21 | =head1 USAGE | ||
| 22 | |||
| 23 | my $line = new Panotools::Script::Line::Foo; | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | sub new | ||
| 28 | { | ||
| 29 | my $class = shift; | ||
| 30 | $class = ref $class || $class; | ||
| 31 | my $self = bless {}, $class; | ||
| 32 | $self->_defaults; | ||
| 33 | return $self; | ||
| 34 | } | ||
| 35 | |||
| 36 | =pod | ||
| 37 | |||
| 38 | my $identifier = $line->Identifier; | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | sub Identifier | ||
| 43 | { | ||
| 44 | return '#'; | ||
| 45 | } | ||
| 46 | |||
| 47 | =pod | ||
| 48 | |||
| 49 | $line->Parse ('f a1.0 b2.0 bar3.0'); | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub Parse | ||
| 54 | { | ||
| 55 | my $self = shift; | ||
| 56 | my $string = shift || return 0; | ||
| 57 | my $valid = $self->_valid; | ||
| 58 | my @res = $string =~ / ([a-zA-Z][^ "]+)|([a-zA-Z][a-z]*"[^"]+")/g; | ||
| 59 | for my $token (grep { defined $_ } @res) | ||
| 60 | { | ||
| 61 | my ($key, $value) = $token =~ /$valid/; | ||
| 62 | next unless defined $key; | ||
| 63 | $self->{$key} = $value; | ||
| 64 | } | ||
| 65 | $self->_sanitise; | ||
| 66 | return 1; | ||
| 67 | } | ||
| 68 | |||
| 69 | =pod | ||
| 70 | |||
| 71 | my $string = $line->Assemble; | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub Assemble | ||
| 76 | { | ||
| 77 | my $self = shift; | ||
| 78 | $self->_sanitise; | ||
| 79 | my @tokens; | ||
| 80 | for my $entry (sort keys %{$self}) | ||
| 81 | { | ||
| 82 | push @tokens, $entry . $self->{$entry}; | ||
| 83 | } | ||
| 84 | return (join ' ', ($self->Identifier, @tokens)) ."\n" if (@tokens); | ||
| 85 | return ''; | ||
| 86 | } | ||
| 87 | |||
| 88 | =pod | ||
| 89 | |||
| 90 | $line->Set (a => 'something', b => 2); | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub Set | ||
| 95 | { | ||
| 96 | my $self = shift; | ||
| 97 | my %hash = @_; | ||
| 98 | for my $entry (sort keys %hash) | ||
| 99 | { | ||
| 100 | $self->{$entry} = $hash{$entry}; | ||
| 101 | } | ||
| 102 | $self->_sanitise; | ||
| 103 | } | ||
| 104 | |||
| 105 | =pod | ||
| 106 | |||
| 107 | Clone a line object | ||
| 108 | |||
| 109 | $clone = $l->Clone; | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | sub Clone | ||
| 114 | { | ||
| 115 | my $self = shift; | ||
| 116 | dclone ($self); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _defaults {} | ||
| 120 | |||
| 121 | sub _valid { return '^(.)(.*)' } | ||
| 122 | |||
| 123 | sub _sanitise | ||
| 124 | { | ||
| 125 | my $self = shift; | ||
| 126 | my $valid = $self->_valid; | ||
| 127 | for my $key (keys %{$self}) | ||
| 128 | { | ||
| 129 | delete $self->{$key} unless (grep /$valid/, $key); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; | ||
| 134 |