summaryrefslogtreecommitdiffstats
path: root/Utilities/Scripts/pragma-once.pl
blob: 41162ecee395cf9c376605a105913750c7b0c0a5 (plain)
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
#!/usr/bin/perl

use strict;
use warnings;

use constant {
  START => 0,
  HAVE_IFNDEF => 1,
  HAVE_DEFINE => 2,
  HAVE_ENDIF => 3,
  HAVE_PRAGMA_ONCE => 4,
};
my $state = START;
my $blank_count = 0;
my $endif = '';
while (<>) {
  if ($state == START) {
    if (/^#ifndef [a-zA-Z_][a-zA-Z0-9_]*$/) {
      $state = HAVE_IFNDEF;
      print "#pragma once\n";
    } else {
      if (/^#pragma once$/) {
        $state = HAVE_PRAGMA_ONCE;
      }
      print;
    }
  } elsif ($state == HAVE_IFNDEF) {
    if (/^#define [a-zA-Z_][a-zA-Z0-9_]*$/) {
      $blank_count = 0;
      $state = HAVE_DEFINE;
    } else {
      print;
    }
  } elsif ($state == HAVE_DEFINE) {
    if (/^#endif/) {
      $endif = $_;
      $state = HAVE_ENDIF;
    } elsif (/^$/) {
      $blank_count++;
    } else {
      for (my $i = 0; $i < $blank_count; $i++) {
        print "\n";
      }
      $blank_count = 0;
      print;
    }
  } elsif ($state == HAVE_ENDIF) {
    for (my $i = 0; $i < $blank_count; $i++) {
      print "\n";
    }
    $blank_count = 0;
    print $endif;
    $state = HAVE_DEFINE;
    if (/^#endif/) {
      $endif = $_;
      $state = HAVE_ENDIF;
    } elsif (/^$/) {
      $blank_count++;
    } else {
      print;
    }
  } elsif ($state == HAVE_PRAGMA_ONCE) {
    print;
  }
  if (eof) {
    if ($state != HAVE_ENDIF && $state != HAVE_PRAGMA_ONCE) {
      print STDERR "Malformed header file: $ARGV\n";
      exit 1;
    }
    $state = START;
    $blank_count = 0;
  }
}