viewing paste rA_itemtype_converter.pl | Perl

Posted on the
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
use Scalar::Util qw(looks_like_number);
 
my $sFileins;
my @aFilein = ("../db/re/item_db.txt","../db/pre-re/item_db.txt","../db/item_db2.txt");
my $sFileouts;
my @aFileout = ("../db/re/item_db.txt","../db/pre-re/item_db.txt","../db/item_db2.txt");
my $sHelp = 0;
 
 
Main();
 
sub GetArgs {
    GetOptions(
    'i=s' => \$sFileins, #Output file name.
    'o=s' => \$sFileouts, #Input file name.
    'help!' => \$sHelp,
    ) or $sHelp=1; #Display help if invalid options are supplied.
 
    if( $sHelp ) {
        print "Incorrect option specified. Available options:\n"
            ."\t --o=filename => Output file name. (file must be separate by coma if multiple) \n"
            ."\t --i=filenames => Input files name. (file must be separate by coma if multiple) \n";
        exit;
    }
    if($sFileins){
        chomp($sFileins);
        @aFilein = split(",",$sFileins);
    }
    if($sFileouts){
        chomp($sFileouts);
        @aFileout = split(",",$sFileouts);
    }
    unless(scalar(@aFileout)==scalar(@aFilein)){
        print "ERROR: number of filein doesn't match number of fileout, you must speficy each fileout for each filein:\n"
            ."afilein = [ @aFilein ] \n"
            ."afileout = [ @aFileout ] \n";
        exit;
    }
}
 
sub Main {
    my $sI=0;
    my($filename, $dir, $suffix) = fileparse($0);
    chdir $dir; #put ourself like was called in tool folder
    GetArgs();
    print "Welcome to rA itemtype converter\n";
    print "Were going to convert those files : @aFilein into @aFileout \n";
    foreach my $sFile (@aFilein){
        my $sReplace=0; #should we replace file when finished
        my $sFileouttmp=$aFileout[$sI];
        if($sFile eq $sFileouttmp){
            $sReplace=1;
            $sFileouttmp = $sFileouttmp.".out";
            print "Asking to replace file tmp fileout= $sFileouttmp \n";
        }
        unless(open FHIN,"$sFile"){
            print "Error, can't read or locate $sFile.\n";
            next;
        }
        unless(open FHOUT,">$sFileouttmp"){
           print "ERROR: Can't write or locate $aFileout[$sI].\n";
           next;
        }
        $sI++;
        while (<FHIN>){
            if( $_ =~ /^\s*$/) {  #ignore empty line
                print FHOUT $_; 
                next;  
            } 
            my @champ = split(",",$_);
            my $sDoconvertion=0; #should this comment be converted
            if( $_ =~ /^\/\// ) { # // line
                if(scalar(@champ)>3){
                    $champ[0] =~ s!\/\/!!g;
                    $sDoconvertion=looks_like_number($champ[0]);
                    $champ[0] = "//".$champ[0]; #recomment it
                }
                if($sDoconvertion==0) {
                    print FHOUT $_;
                    next; 
                }
            }
            if(scalar(@champ>3)){
                if($champ[3] == 4) { $champ[3]=5; }
                elsif($champ[3] == 5) { $champ[3]=4; }
                my $newline = join(",",@champ);
                print FHOUT $newline;
            }
            else { print FHOUT $_; }
        }
        close FHOUT;
        close FHIN;
        if($sReplace){
            unlink $sFile;
            rename $sFileouttmp, $sFile;
        }
    }
}
 
Viewed 1645 times, submitted by lighta.