#!/usr/bin/perl -w
use strict;
####################################3
# spec2def.pl John Klehm
# Takes a spec file and generates the painful part of the def file
# 2007/07/02 version 0.5

if ($#ARGV < 0) {
	print "usage: spec2def.pl filename\nVersion 0.5";
	exit;
}


### spec function pattern
#$1 function name
#$2 parmeter types hopefully - everything in the parens really
our $SPECFUNCPAT = 'stdcall (\p{IsWord}+)\((.*)\)';


###OPEN FILE
my $FH;
my $fileText = "";
my $defaultDelim = $/;
undef $/;###get rid of input delimeter to speed up dumping of file into a string
open(FH, $ARGV[0]) or die "\n\nCould not open file: $ARGV[0]\n\n";
$fileText = <FH>; ###Dump file into string
close FH;
$/ = $defaultDelim;


###PARSE FILE
my $outputText = '';
my $paramBytes = 0;
my $numberOfParams = 0;
while ($fileText =~ /$SPECFUNCPAT/go) {
	###if there are parameters
	if ($2 ne '') {
		$numberOfParams = `echo $2 | wc -w` or die "Could not count words!";
		chomp($numberOfParams);
		$paramBytes = $numberOfParams * 4;
		$outputText .= "$1\@$paramBytes\n";

		$numberOfParams = 0;
		$paramBytes = 0;
	} else {
		$outputText .= "$1\n";
	}
}
print $outputText;
