summaryrefslogtreecommitdiffstats
path: root/vespalib/fix-hg.pl
blob: fe5eb1101de40bdb197f27c346420c49269ea61f (plain) (blame)
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
#!/usr/bin/perl
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

foreach $arg ( @ARGV ) {
	$hgd = $arg;          # maybe: . "_";
	$hgd =~ s{\W}{_}g;
	$hgd =~ tr{a-z}{A-Z};
	$hgd =~ s{^_*}{H_};

# print "arg $arg header guard $hgd\n";

 	open(FOO, $arg) or die "Cannot open '$arg'\n";
	$backup = $arg . ".orig";
	rename ($arg, $backup);

        open(ARGVOUT, ">$arg") or die "cannot write to '$arg'\n";
        select(ARGVOUT);

	my $eic = 0;
	my $cnt = 1;

        while (<FOO>) {
		++$eic if m{#endif} ;
	}
	seek FOO, 0, 0;
        while (<FOO>) {
 		if ($cnt == 1 and m{^#ifndef}) {
			s{\s.*}{ $hgd};
			++$cnt;
		}
 		if ($cnt == 2 and m{^#define}) {
			s{\s.*}{ $hgd};
			++$cnt;
		}
		if ( m{#endif} ) {
			--$eic;
			if ($eic == 0) {
				s{.*#endif.*}{#endif // header guard};
			}
		}
		print;
	}
	close(FOO);
        select(STDOUT);
	close(ARGVOUT);

	unlink($backup);
}

exit 0;