[go: up one dir, main page]

File: replace

package info (click to toggle)
coin3 3.1.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 48,344 kB
  • ctags: 70,042
  • sloc: cpp: 314,328; ansic: 15,927; sh: 13,635; makefile: 8,780; perl: 2,149; lex: 1,302; lisp: 1,247; yacc: 184; xml: 175; sed: 68
file content (135 lines) | stat: -rwxr-xr-x 3,694 bytes parent folder | download | duplicates (10)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#! /usr/bin/perl
############################################################################
#
#  replace -- script for replacing standard text blocks if found in large
#  file sets
#
#  sample:
#  find . -name "*.h" | xargs replace --orig orig.txt --new new.txt
#
# **************************************************************************

############################################################################

if ( @ARGV == 0 ) {
    &PrintUsage();
    exit 0;
}

%OPTIONS = ( # option perl code
    '--help',            '&PrintUsage(); exit 0;',
    '--usage',           '&PrintUsage(); exit 0;',
    '-h',                '&PrintUsage(); exit 0;',
    '-?',                '&BrintUsage(); exit 0;',
    '--version',         '&PrintVersion(); exit 0;',
    '-v',                '&PrintVersion(); exit 0;',
    '--orig',            '$next_is_orig = 1;',
    '-o',                '$next_is_orig = 1;',
    '--new',             '$next_is_new = 1;',
    '-n',                '$next_is_new = 1;',
    '--debug',           '$debug = 1;',
    '-d',                '$debug = 1;',
);

############################################################################
# separate files from options

$debug = 0;
$make_backups = 1;
$note_failures = 0;

@files = ();
@orig = ();
@new = ();

$next_is_orig = 0;
$next_is_new = 0;

foreach $arg ( @ARGV ) {
    if ( $next_is_orig == 1 ) {
        open(ORIG, $arg) || die "$0: error opening '$arg'";
        @orig = <ORIG>;
        chomp(@orig);
        close(ORIG);
        $next_is_orig = 0;
    } elsif ( $next_is_new == 1 ) {
        open(NEW, $arg) || die "$0: error opening '$arg'";
        @new = <NEW>;
        chomp(@new);
        close(NEW);
        $next_is_new = 0;
    } elsif ( defined $OPTIONS{$arg} ) {
        eval $OPTIONS{$arg};
    } else {
        push( @files, $arg );
    }
}

if ( scalar(@orig) == 0 ) {
    print "scalar(orig) == 0\n";
    &PrintUsage();
    exit 1;
}

if ( scalar(@files) == 0 ) {
    print "scalar(files) == 0\n";
    # stdin/stdout is not supported yet
    &PrintUsage();
    exit 1;
}

foreach $file ( @files ) {
    next if ! -f $file;
    if ( ! open(CURRENT, $file) ) {
        print STDERR "$0: unable to open file '$file'\n";
        next;
    }
    $match = 0;
    @current = <CURRENT>;
    chomp(@current);
    for ( $line = 0; $line < scalar(@current) - scalar(@orig) + 1; $line++ ) {
        if ( $current[$line] eq $orig[0] ) {
            $local_match = 1;
            for ( $off = 1; $off < scalar(@orig) && $local_match == 1; $off++ ) {
                $local_match = 0 if ( $current[$line + $off] ne $orig[$off] );
            }
            if ( $local_match == 1 ) {
                splice(@current, $line, scalar(@orig), @new);
                $line += scalar(@new);
                $match = 1;
            }
        }
    }
    if ( $match == 0 ) {
        printf "\r%-60s%-18s\n", $file . ":", "no change...";
    } else {
        printf "\r%-60s%-18s\n", $file . ":", "updating...";
        system( "mv $file $file.bak" );
        open( FILE, "> $file" ) || die $file, ": ", $!, "\n";
        foreach $line ( @current ) {
            print FILE $line, "\n";
        }
        close( FILE );
    }
}

# prints program usage
sub PrintUsage { ###########################################################
    print <<"END_OF_USAGE";
Usage:
    $0 [options] [files...]
Options:
    -o, --orig <file>    : the original text
    -n, --new <file>     : the new replacement text
END_OF_USAGE
}

# print CVS version
sub PrintVersion { #########################################################
    print '$Id$', "\n";
}

############################################################################