Annotation of nono/lib/object_id.pl, revision 1.1

1.1     ! root        1: #
        !             2: # nono
        !             3: # Copyright (C) 2021 nono project
        !             4: # Licensed under nono-license.txt
        !             5: #
        !             6: 
        !             7: # object_id.txt から object_id.h, object_id.cpp を作る。
        !             8: #
        !             9: # usage: perl object_id.pl object_id.txt
        !            10: 
        !            11: {
        !            12:        # ファイル名
        !            13:        $infile = $ARGV[0];
        !            14:        $hdrfile = $infile;
        !            15:        $srcfile = $infile;
        !            16:        $hdrfile =~ s/txt$/h/;
        !            17:        $srcfile =~ s/txt$/cpp/;
        !            18: 
        !            19:        open(IN, $infile) || die "Cannot open infile: ${infile}\n";
        !            20:        while (<IN>) {
        !            21:                # Remove comments.
        !            22:                chomp;
        !            23:                s/#.*$//;
        !            24:                next if /^$/;
        !            25: 
        !            26:                @names = split(/\s+/);
        !            27: 
        !            28:                if (@names == 1) {
        !            29:                        $names[1] = $names[0];
        !            30:                }
        !            31: 
        !            32:                # %n があれば 0..(n-1) に展開する
        !            33:                if ($names[0] =~ /%(\d+)/) {
        !            34:                        $atnum = $1 + 0;
        !            35:                        for ($i = 0; $i < $atnum; $i++) {
        !            36:                                @rep_names = ();
        !            37:                                $r = $names[0];
        !            38:                                $r =~ s/%${atnum}/${i}/g;
        !            39:                                push(@rep_names, $r);
        !            40:                                $r = $names[1];
        !            41:                                $r =~ s/%/${i}/g;
        !            42:                                push(@rep_names, $r);
        !            43: 
        !            44:                                parse_line(@rep_names);
        !            45:                        }
        !            46:                } else {
        !            47:                        parse_line(@names);
        !            48:                }
        !            49:        }
        !            50: 
        !            51:        # 文字列として出力
        !            52:        $hdrnew = "";
        !            53:        $srcnew = "";
        !            54: 
        !            55:        $hdrnew .= "// Generated from ${0}\n";
        !            56:        $hdrnew .= "\n";
        !            57:        $hdrnew .= "#pragma once\n";
        !            58:        $hdrnew .= "\n";
        !            59:        $hdrnew .= "enum {\n";
        !            60:        $hdrnew .= $defs;
        !            61:        $hdrnew .= "};\n";
        !            62: 
        !            63:        $srcnew .= "// Generated from ${0}\n";
        !            64:        $srcnew .= "\n";
        !            65:        $srcnew .= "#include \"object.h\"\n";
        !            66:        $srcnew .= "\n";
        !            67:        $srcnew .= "/*static*/ const std::vector<const char *>\n";
        !            68:        $srcnew .= "Object::idname {\n";
        !            69:        $srcnew .= $msgs;
        !            70:        $srcnew .= "};\n";
        !            71:        $srcnew .= "\n";
        !            72:        $srcnew .= "/*static*/ const std::vector<const char *>\n";
        !            73:        $srcnew .= "Object::idtext {\n";
        !            74:        $srcnew .= $text;
        !            75:        $srcnew .= "};\n";
        !            76: 
        !            77:        # 更新があれば書き出す
        !            78:        do_update($hdrfile, $hdrnew);
        !            79:        do_update($srcfile, $srcnew);
        !            80: 
        !            81:        exit 0;
        !            82: }
        !            83: 
        !            84: sub parse_line()
        !            85: {
        !            86:        local @names = @_;
        !            87: 
        !            88:        $defs .= "\tOBJ_${names[0]},\n";
        !            89:        $msgs .= "\t\"${names[1]}\",\n";
        !            90:        $text .= "\t\"OBJ_${names[0]}\",\n";
        !            91: }
        !            92: 
        !            93: sub do_update()
        !            94: {
        !            95:        local $filename = $_[0];
        !            96:        local $filebody = $_[1];
        !            97:        local $old;
        !            98:        local @contents;
        !            99: 
        !           100:        # まず出力先ファイルを文字列として読み込む
        !           101:        $old = "";
        !           102:        if (open(IN, $filename)) {
        !           103:                @contents = <IN>;
        !           104:                $old = join("", @contents);
        !           105:                close(IN);
        !           106:        }
        !           107: 
        !           108:        # 全文を比較して変更があれば出力
        !           109:        if ($filebody ne $old) {
        !           110:                open(OUT, ">${filename}") || die "Cannot open ${filename}\n";
        !           111:                print OUT $filebody;
        !           112:                close(OUT);
        !           113:                print "Updated ${filename}\n";
        !           114:        }
        !           115: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.