# PREPFILE.PM
#
# Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
#


# module for preprocessing makmake-style project files


package Prepfile;

require Exporter;
@ISA=qw(Exporter);

@EXPORT=qw(
	Prepfile_SetVerbose Prepfile_ProcessL
);


use Checkgcc;
use Pathutl;

my %Mode;

BEGIN {
	$Mode{'Verbose'}=0;
}

sub Prepfile_SetVerbose () {
	$Mode{'Verbose'}=1;
}

sub Prepfile_ProcessL ($$@) {
# use the C++ preprocessor to process a file.  The function fills the data structure specified
# by the first argument, an array reference, according to the contents of the second argument -
# a filehandle.  Any other arguments are assumed to be MACROS and are defined for preprocessing.

	my ($ArrayRef,$FILE,@Macros)=@_;
	die "\nERROR: Project File \"$FILE\" not found\n" unless -e $FILE;

	my @CppCall;
	# don't use -P because upsets linenum info when #defs used
	push @CppCall, 'CPP.EXE';

	my $Macro;
	# add CL macros to the CPP call for preprocessing of the file
	foreach $Macro (@Macros) {
		$Macro=uc $Macro;
		push @CppCall, "-D $Macro=$Macro";
	}
	# all macros made upper case and suppress user-expansion of macros for nefarious purposes

	push @CppCall, join '',"\"",&Path_RltToWork($FILE),"\"";
	if ($Mode{'Verbose'}) {
		print "@CppCall\n";
	}
	my $CPPPIPE;
	open CPPPIPE,"@CppCall |" or die "\nERROR: Can't invoke CPP.EXE\n";
	undef @CppCall;


	# read the processed output
	#--------------------------

	my $LineNum=0;
	while (<CPPPIPE>) {
# strip out the file information lines that cpp inserts
		next if /^#/o;
# update the line number - this is put in as the first thing in the list for each line of the file
		$LineNum++;
		next if /^\s*$/o;
		my @Tmp=();
		push @Tmp, $LineNum;
		# get rid of the space that cpp puts in most of the time after macro expansion (CPP help doesn't say exactly when!)
		# don't upper case everything until you've done this.
		foreach $Macro (@Macros) {
			s/$Macro /$Macro/g;
		}
		$_=uc $_;
		while (/("([^"\t\n\r\f]+)"|([^ "\t\n\r\f]+))/go) {
			push @Tmp, $2 ? $2 : $3;
		}
		push @{$ArrayRef}, [ @Tmp ];
	}

	close CPPPIPE or die "\nERROR: CPP.EXE Failure\n";
}

1;
