summaryrefslogtreecommitdiff
path: root/bin/DependencyGenerator/Preprocessor.pm
blob: f28ab6f2397e4b586f3a25b0ed876e756bbe6f8a (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
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
package Preprocessor;

# ************************************************************
# Description   : Preprocesses the supplied file.
# Author        : Chad Elliott
# Create Date   : 2/10/2002
# ************************************************************

# ************************************************************
# Pragmas
# ************************************************************

use strict;
use FileHandle;

# ************************************************************
# Subroutine Section
# ************************************************************

sub new {
  my($class)   = shift;
  my($macros)  = shift;
  my($options) = shift;
  my($ipaths)  = shift;
  my($self)    = bless {'macros'  => $macros,
                        'options' => $options,
                        'ipaths'  => $ipaths,
                        'files'   => {},
                       }, $class;
  return $self;
}


sub locateFile {
  my($self) = shift;
  my($file) = shift;

  foreach my $dir ('.', @{$self->{'ipaths'}}) {
    if (-r "$dir/$file") {
      return "$dir/$file";
    }
  }

  return undef;
}


sub getFiles {
  my($self) = shift;
  my($file) = shift;

  foreach my $inc (@{$self->{'files'}->{$file}}) {
    if (!defined $self->{'ifiles'}->{$inc}) {
      $self->{'ifiles'}->{$inc} = 1;
      $self->getFiles($inc);
    }
  }
}


sub process {
  my($self)   = shift;
  my($file)   = shift;
  my($noincs) = shift;
  my($fh)     = new FileHandle();
  my(@incs)   = ();

  if (open($fh, $file)) {
    my($ifcount) = 0;
    my(@zero)    = ();

    $self->{'files'}->{$file} = [];
    while(<$fh>) {
      $_ =~ s/\/\/.*//;

      if (/#\s*if\s+0/) {
        push(@zero, $ifcount);
        ++$ifcount;
      }
      elsif (/#\s*if/) {
        ++$ifcount;
      }
      elsif (/#\s*endif/) {
        --$ifcount;
        if (defined $zero[0] && $ifcount == $zero[$#zero]) {
          pop(@zero);
        }
      }
      elsif (!defined $zero[0] &&
             /#\s*include\s+(\/\*\*\/\s*)?[<"]([^">]+)[">]/) {
        my($inc) = $self->locateFile($2);
        if (defined $inc) {
          $inc =~ s/\\/\//g;
          push(@{$self->{'files'}->{$file}}, $inc);
          if (!defined $self->{'files'}->{$inc}) {
            ## Process this file, but do not return the include files
            $self->process($inc, 1);
          }
        }
      }
    }
    close($fh);

    if (!$noincs) {
      $self->{'ifiles'} = {};
      $self->getFiles($file);
      @incs = keys %{$self->{'ifiles'}};
    }
  }

  return \@incs;
}


1;