代码改变世界

工具脚本(网络编码)

2012-05-18 19:56  zhenjing  阅读(650)  评论(0编辑  收藏  举报

生成结构体网络编码的perl脚本(使用google protocal更为通用):

#! /usr/bin/perl -w
## Copyright (C) 2011 by zhenjing

# usage : proto_hfile proto

my $classmane = "--@@@@--"; 

for(@ARGV)
{
    my $in = $_;
    if(not defined $in)  
    {
        die "Usage : $0 filename ";
    }

    my $out = $in;
    $out =~ s/(\.\w+)?$/.h/;   # change suffix name
 
    rename $in, $in.".proto";        # rename old file, use old name for new file
    $in .= ".proto";             

    unless (open IN,"<$in") 
    {
        die "Can't open file($in): $!";
    }
    unless(open OUT, ">$out")
    {
        die "Can't open file($out): $!";
    }

    print OUT "#include <stdint.h>\n";

    while(<IN>)
    {
        if( /struct\s+(\w+)/ || /class\s+(\w+)/ ) 
        {
            $classmane = $1;

            print OUT $_;
            next;
        }

        if( /}\s*;/ )      
        {
            &AddFuncDef($classmane);
            print OUT $_;
            next;
        }
        print OUT $_;
    }
}

sub AddFuncDef
{
    print OUT "\n";
    print OUT "\t$_[0]();\n";
    print OUT "\tvoid Hton();\n";
    print OUT "\tvoid Ntoh();\n";
    
    print OUT "\n\tvoid Print() const;\n";
    print OUT "\n";
}

 

#! /usr/bin/perl -w
## Copyright (C) 2011 by zhenjing

# usage : proto_cpp proto

# @ not support "long int"


my $classmane = "--@@@@--"; 
my $prifix = "::";

my @varShort = ();
my @varInt = ();
my @varLong = ();

for(@ARGV)
{
    my $in = $_;
    if(not defined $in)  
    {
        die "Usage : $0 filename ";
    }

    my $out = $in;
    $out =~ s/(\.\w+)?$/.cpp/;   # change suffix name

    # rename $in, $in.".proto";        # rename old file, use old name for new file
    # $in .= ".proto";           
    
    unless (-e $in) 
    {
        $in .= ".proto";   # use together with proto_hfile
    }

    unless (open IN,"<$in") 
    {
        die "Can't open file($in): $!";
    }
    unless(open OUT, ">$out")
    {
        die "Can't open file($out): $!";
    }

    print OUT "#include \"$in\"\n\n";
    print OUT "#include <stdio.h>\n";
    print OUT "#include <string.h>\n";
    print OUT "#include <arpa/inet.h>\n\n";

    while(<IN>)
    {
        if( /struct\s+(\w+)/ || /class\s+(\w+)/ ) 
        {
            $classmane = $1;

            next;
        }

        if( /}\s*;/ )      
        {
            &AddConstructFun;
            &AddNtohFunc;
            &AddHtonFunc;
            &AddPrintFun;
            &CleanVar;
            next;
        }
        
        if( /short\s+(\w+)/ || /int16_t\s+(\w+)/ || /uint16_t\s+(\w+)/ ) 
        {
            push @varShort, $1;
            next;
        } 
        if( /int\s+(\w+)/ || /int32_t\s+(\w+)/ || /uint32_t\s+(\w+)/ ) 
        {
            push @varInt, $1;
            next;
        }
        if( /long\s+long\s+(\w+)/ || /int64_t\s+(\w+)/ || /uint64_t\s+(\w+)/ ) 
        {
            push @varLong, $1;
            next;
        } 
        if( /long\s+(\w+)/ ) 
        {
            push @varLong, $1;
            next;
        }
    }
}

sub AddConstructFun
{
    print OUT "$classmane${prifix}$classmane()\n";
    print OUT "{\n";
 
    print OUT "\tmemset(this, 0, sizeof($classmane));\n";
    print OUT "}\n\n";
}

sub AddHtonFunc
{
    print OUT "void $classmane${prifix}Hton()\n";
    print OUT "{\n";
 
    foreach (@varShort) {
        print OUT "\t$_ = htons($_);\n";
    }  
    foreach (@varInt) {
        print OUT "\t$_ = htonl($_);\n";
    } 
    foreach (@varLong) {
        print OUT "\t$_ = htonll($_);\n";
    } 

    print OUT "}\n\n";
}

sub AddNtohFunc
{
    print OUT "void $classmane${prifix}Ntoh()\n";
    print OUT "{\n";
 
    foreach (@varShort) {
        print OUT "\t$_ = ntohs($_);\n";
    }  
    foreach (@varInt) {
        print OUT "\t$_ = ntohl($_);\n";
    } 
    foreach (@varLong) {
        print OUT "\t$_ = ntohll($_);\n";
    } 

    print OUT "}\n\n";
}

sub AddPrintFun
{
    print OUT "void $classmane${prifix}Print() const\n";
    print OUT "{\n\n";
 
    print OUT "}\n\n";
}


sub CleanVar
{
    @varShort = ();
    @varInt = ();
    @varLong = ();
}

 

#! /bin/bash
## Copyright (C) 2011 by zhenjing

# usage : net_proto proto

if [[ $# -lt 1 ]]
then
    echo "$0 proto_files ..."
fi

/data/home/nolan/bin/proto_hfile $@;
/data/home/nolan/bin/proto_cpp $@;

 

正则表达式测试脚本:

#! /usr/bin/perl -w
## Copyright (C) 2007 by zhenjing

# $PATTERN = "fred.*wilma|wilma.*fred";
# $PATTERN = "\\b\\w*a\\b";                 #注意在字符串中,\为转义字符,\\才表示反斜线
# $PATTERN = "(fred|bar){3}"; 
# $PATTERN = "class\\s+(\\w+)\$"; 
# $PATTERN = "\\s*(\\w+\\s+)"; 
# $PATTERN = "\\w+\\s+(\\w|:)+\\s*\\(";
$PATTERN = "class\\s+\\w+\\s*:\\s*public";    #class *** : public ***

while(<>)
{
        chomp;
        if(/$PATTERN/)
        {
                print "Matched: |$`<$&>$'|\n";
        }
        else
        {
                print "NO matched: |$_|\n";

        }
}