#! /usr/local/bin/perl5
#
# Convert ANSI-C style function prototypes into the wrappergen input format, 
# which is
#  <num of functions>
#  <function return type>
#  <function name>
#  <number of args>
#  <type>
#  <argname>
#  <blank>
#  <type>
#  <argname>
#  ...
#
# For simplicity, this reads from standard input and writes to standard output
# The last thing written is the number of functions.
$num_functions = 0;
while (<>) {
      if (/([^\(]*)\s* ([A-Za-z0-9_]*)\((.*)\)\s*;/) {
          $ftype = $1;
	  $fname = $2;
	  $args  = $3;
	  $num_functions++;
	  print "$ftype\n";
	  print "$fname\n";
	  @arglist = split(/,/,$args);
	  $nargs = $#arglist + 1;
	  print "$nargs\n";
	  for (@arglist) {
	      $arg = $_;
	      # separate arg type from name
#	      @pieces = split(/\s/, $arg );
#	      $argname = $pieces[$#pieces];
#	      $#pieces = $#pieces - 1;
#	      $argtype = join(' ',@pieces );
	      /(.*[\s\*])([A-Za-z_0-9]*)/;
	      $argtype = $1;
	      $argname = $2;
	      $argtype =~ s/^\s*//;
	      print "$argtype\n";
	      print "$argname\n\n";
	  }
      }
      else {
          print "Unrecognized line $_";
      }				
  }

print "$num_functions\n";
