parse-wormedit: declare hardcoded bytes in unpackf template
[wormy.git] / Parse / Binary / Nested.pm
index db5e2080eaec8187b65ae785b42b3a8175ca5a8c..872ccaac1e772d0b97384c1b93b2307cd8f0f715 100644 (file)
@@ -5,7 +5,7 @@ use warnings;
 
 use Carp;
 
-our $VERSION = '1.00';
+our $VERSION = '1.01';
 
 sub new {
        my ($class, $format) = @_;
@@ -26,6 +26,7 @@ sub template {
                                : $count."X[$count]$count/($value)";
                }
                else {
+                       $value =~ s/=\d*//g;  # hardcoded values
                        $value =~ s/^C(a)(\d+)/$1 . ($2 + 1)/e;  # length prefix
                }
                $value;
@@ -50,6 +51,13 @@ sub convert {
                elsif ($template =~ /^Ca/) {
                        $data->[0] = unpack 'C/a', $data->[0];
                }
+               elsif ($template =~ /^(?:[xX]\d*)*$/) {
+                       next;  # no values
+               }
+               elsif ($template =~ /=(\d+)?/) {
+                       $res{$field} = $1;
+                       next;
+               }
                $res{$field} = shift @$data;
        }
        return \%res;
@@ -84,6 +92,53 @@ Parse::Binary::Nested - Structured unpack
 
 =head1 DESCRIPTION
 
+Converts a string into a hash of values, just like C<unpack>
+except that it allows you to name and nest the resulting elements.
+
+Format declarations are simalar to C<pack> templates,
+with the following additions:
+
+=over
+
+=item *
+
+An array ref groups additional declarations,
+with the first value specifying a repetition.  If this count is variable,
+the resulting value will be an array ref of hashes.
+
+       repeat => ['C', name => 'a*', value => 'S']
+
+With a count of 1, it will return only a hash ref,
+thereby simply grouping declarations:
+
+       test_foo => 'C'
+       test => [1, foo => 'C']
+
+=item *
+
+A template value of C<Ca$length> is recognised as a length-preceded string
+with a constant (maximal) size, and will return only the string adjusted
+to its length.
+This behaviour is very similar to C<(C/a@x$length)>, except that it never reads
+more than the given number of bytes.
+
+=item *
+
+Hardcoded values can be inserted using C<=$number> values.
+This can for example be useful to retain forwards-compatibility:
+
+       rows => ['C',
+               type => '=1', # nothing read
+               data => 'S',
+       ]
+       
+       rows => ['C',
+               type => 'C',
+               data => 'S',
+       ]
+
+=back
+
 =head1 AUTHOR
 
 Mischa POSLAWSKY <perl@shiar.org>