• 工具脚本(网络编码)


    生成结构体网络编码的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";
    
            }
    }


    作者:zhenjing.chen
    出处:http://www.cnblogs.com/zhenjing/
    未注明转载的文章,版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    js面向对象编程-高级内容
    (转)js中的hasOwnProperty和isPrototypeOf方法
    Bootstrap_表单
    Bootstrap_表格
    Bootstrap_排版
    Bootstrap_网格系统
    Bootstrap_CSS概览
    redis的搜索组件 redis-search4j
    有哪些值得学习的spring boot开源项目?
    国内最火的10款Java开源项目,都是国人开发,CMS居多
  • 原文地址:https://www.cnblogs.com/zhenjing/p/tools.html
Copyright © 2020-2023  润新知