package common_19_2_19_3;

for $testFileName (@main::cList) {
	if (open (TEST, '<' . $testFileName)) {
		my $test ;
		{
			local($/) =undef ;
			$test = <TEST>;
		}
		close(TEST) ;
		&lookForInclude($testFileName,$test);
	} else {
		warn "\"$testFileName\": cannot read : $!\n" ;
	}
}

my (%macroDefArr);

sub lookForInclude {
	my($testFileName,$test) = @_;
	my($number) ;
	my($line) ;
	$number = 0 ;
	foreach $line (split /\n/, $test) { 
		$number++ ;
		if ($line =~ /^\s*#\s*include/) {
		    check_include_form($line,$number);
		} elsif ($line =~ m{^\s*#\s*define\s+[a-zA-Z_]\w*\s+}) {
		    check_define_form($line,$number);
		}
	}
	
	undef %macroDefArr;
}

sub check_define_form {
	my($name,$number)=@_ ;
	if ($name =~ m{^\s*#\s*define\s+([a-zA-Z_]\w*)\s+\"([^\>]+)\"}) {
	    $macroDefArr{$1} = $2;
	} elsif ($name =~ m{^\s*#\s*define\s+([a-zA-Z_]\w*)\s+\<([^\>]+)\>}) {
	    $macroDefArr{$1} = $2;
	}
}

# Check characters for Rule 88.
sub check_form {
	my($fileName,$number)=@_ ;
	# Note : we do not allow "" as a valid file name (rule 89)
	if (!($fileName !~ m{\'|\"|\\|\/\*})) {
		push (@{$Violation_19_2{$testFileName}}, $number);
	} elsif (!($fileName !~ m{\'|\\|\/\*})) {
		push (@{$Violation_19_2{$testFileName}}, $number);
	}
}

sub check_include_form {
	my($name,$number)=@_ ;
	# Note : we do not allow "" as a valid file name (rule 89)
	if ($name =~ m{^\s*\#\s*include\s*\<([^\>]+)\>}) {
		check_form($1, $number);		
	} elsif ($name =~ m{^\s*\#\s*include\s*\"([^\"]+)\"}) {
		check_form($1, $number);
	} else {
	    if ($name =~ m{^\s*#\s*include\s+([a-zA-Z_]\w*)} && exists $macroDefArr{$1}) {
	        check_form($macroDefArr{$1}, $number);
	    }
	    else {
		# include is not recognizable. Rule 89 triggers.
		    push (@{$Violation_19_3{$testFileName}}, $number);
		}
	}
}


