File: update_decoder_flag_consts.pl

package info (click to toggle)
libsereal-decoder-perl 4.018%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,392 kB
  • sloc: ansic: 11,310; perl: 6,197; sh: 25; makefile: 9
file content (67 lines) | stat: -rw-r--r-- 2,065 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use strict;
use warnings;
use Data::Dumper;

my %sets;
my %flag_consts;
my %flag_names;
my ( @flags, @static, @volatile );
my $file= "srl_decoder.h";
{
    open my $fh, "<", $file
        or die "Failed to open '$file' for read: $!";

    while (<$fh>) {
        if ( m/#define (SRL_F_(\w+))\s+(.*)/ || /(\w+(VOLATILE_FLAGS))\s+(.*)/ ) {

            #print;
            my $full_name= $1;
            my $name= $2;
            my $value= $3;
            $name =~ s/_?DECODER_?//g;
            $flag_names{$full_name}= $name;
            if ( $value =~ s/UL\z// ) {
                $flag_consts{$full_name}= 0 + eval $value;
            }
            else {
                $value =~ s/(SRL_F_\w+)/\$flag_consts{$1}/g;
                $sets{ "SRL_F_DECODER_" . $name }= 0 + eval $value;
            }
        }
    }
    foreach my $key ( sort { $flag_consts{$a} <=> $flag_consts{$b} } keys %flag_consts ) {
        my $is_volatile= $flag_consts{$key} & $sets{SRL_F_DECODER_VOLATILE_FLAGS};
        push @static,   $is_volatile ? undef             : $flag_names{$key};
        push @volatile, $is_volatile ? $flag_names{$key} : undef;
        push @flags,    $flag_names{$key};
    }
}

my %consts= ( %sets, %flag_consts );
$consts{_FLAG_NAME}= \@flags;
$consts{_FLAG_NAME_VOLATILE}= \@volatile;
$consts{_FLAG_NAME_STATIC}= \@static;

my $infile= "lib/Sereal/Decoder.pm";
my $outfile= "$infile.new";
open my $fh, "<", $infile
    or die "Failed to read '$infile': $!";
open my $ofh, ">", $outfile
    or die "Failed to write to '$outfile': $!";
while (<$fh>) {
    if (/#begin generated/) {
        print $ofh $_;
        my $s= Data::Dumper->new( [ \%consts ] )->Sortkeys(1)->Terse(1)->Dump();
        chop($s);
        $s .= "; #end generated\n";
        print $ofh $s;
    }
    unless ( /#begin generated/ ... /#end generated/ ) {
        print $ofh $_;
    }
}
close $ofh;
close $fh;
rename $infile,  "$infile.bak" or die "Failed to rename '$infile' to '$infile.bak': $!";
rename $outfile, $infile       or die "Failed to rename '$outfile' to '$infile': $!";