C-Include
view release on metacpan or search on metacpan
parse( $self );
Storable::store($self, $param{cache}) if $param{cache};
}
bless $self, $class;
}
###########################
# TEXT PARSER
sub parse() {
my $self = shift;
my %define;
# Define defaults
$self->{''}{VERSION} = $VERSION;
$define{__PERL__} = '';
%{$self->{typedef}} = %TYPES;
$self->{typedef}{$_[0]} = $self->{typedef}{$_[1]} while @_ = each %ALIASES;
# Kill comments
}
# Unreashed code
else{ die "WARNING: Unreashed code '$line'\n" }
}
@$self{ keys %define } = values %define;
}
sub sizeof($){
my ($self, $op) = @_;
return $self->{typedef}{$op}[1] if exists $self->{typedef}{$op};
return $self->{struct}{$op}{''}{length} if exists $self->{struct}{$op};
return 0;
}
sub make_struct($){
my ($self, $struct) = @_;
die "WARNING: Can't make struct with unknown type: '$struct'!\n"
unless exists $self->{struct}{$struct};
return new C::Include::Struct( $self->{struct}{$struct} );
}
sub defined($){ return exists shift->{$_[0]}; }
sub INC{ return exists($INC{scalar($_[0] || caller)}) ? $INC{scalar($_[0] || caller)} : undef; }
sub import{ shift; $INC{scalar(caller)} = new C::Include( @_ ) if @_; }
################################
## package C::Include::Struct ##
################################
package C::Include::Struct;
use 5.005;
use strict;
use Storable;
sub new($){
my ($class, $self) = @_;
$self = Storable::dclone( $self );
bless $self, $class;
}
sub pack(;$){
my ($self, $FILE, $data) = (shift, shift);
if( $$self{''}{bitsets} ){
for my $item( @{$$self{''}{bitsets}} ){
my $i = $#{$$item{fields}};
my $data;
for my $size ( reverse @{$$item{mask}} ){
$data <<= $size<1 ? 1-$size : $size;
$data += ${$$item{fields}[$i--]} unless $size < 1;
}
$$item{buffer} = $data;
}
}
$data = pack $$self{''}{mask}, map{ $$_ }@{$$self{''}{buffers}};
print $FILE $data if $FILE and defined fileno $FILE;
return $data;
}
sub unpack($;$$){
my $self = shift;
my $data = shift;
if( defined fileno $data ){
my( $pos, $len ) = ( shift || 0, shift || $$self{''}{length} );
my $readed = read $data, $data, $len, $pos;
return 0 if $readed < $len;
}
my $i = 0;
$data >>= $size<1 ? 1-$size : $size;
${$$item{fields}[$i++]} = $prev - ($data << $size) unless $size < 1;
$prev = $data;
}
}
}
return 1;
}
sub link($$){
my ($self, $old, $new) = @_;
$old = \$self->{$old} unless ref $old;
$_ == $old and return $_ = $new for @{ $self->{''}{buffers} };
die "Can't relink!";
}
sub size(){ return shift->{''}{length} }
1;
__END__
=head1 NAME
C::Include - Package to easy operate with binary data via describing they like C/C++ structs.
=head1 CLASSES AND PACKAGES
Include/hiew.pm view on Meta::CPAN
package hiew;
require Exporter;
@hiew::ISA = 'Exporter';
@EXPORT = qw/hiew/;
sub hiew(\$){
my $str = shift;
my @lines = unpack 'a16'x(length($$str)/16+(length($$str)%16?1:0)), $$str;
for( 0..$#lines ){
printf "%08X: %-17s³ %-17s %-16s\n", $_*16,
(map{
sprintf '%-2s ' x 8, map{
sprintf'%02X',$_
}unpack 'C*', $_
}unpack 'a8a8', $lines[$_]),
( run in 0.454 second using v1.01-cache-2.11-cpan-65fba6d93b7 )