Debian Codegraph @

buildfunc.pm

raw
1 #package;
2
3 use strict;
4 use warnings;
5
6 # This file contains functions to build .pd from the HDF prototypes
7
8 # Define a low-level perl interface to HDF from these definitions.
9 sub create_low_level
10 {
11 # This file must be modified to only include
12 # netCDF 3 function definitions.
13 # Also, all C function declarations must be on one line.
14 my $defn = shift;
15 my $sub = "create_low_level()";
16
17 my @lines = split (/\n/, $defn);
18
19 foreach my $line (@lines)
20 {
21
22 next if ( $line =~ /^\#/ ); # Skip commented out lines
23 next if ( $line =~ /^\s*$/ ); # Skip blank lines
24
25 unless ($line =~ /^(\w+\**)\s+(\w+)\((.+)\)(\+*\d*)\;/)
26 {
27 die "$sub: Can't parse this line!\n";
28 }
29 my ($return_type, $func_name, $params, $add) = ($1, $2, $3, $4);
30
31 my @vars;
32 my @types;
33 my $output = {};
34 foreach my $param ( split (/,/, $params) )
35 {
36 my ($varname) = ($param =~ /(\w+)$/);
37 $param =~ s/$varname//; # parm now contains the full C type
38 $output->{$varname} = 1
39 if (($param =~ /\*/) && ($param !~ /const/));
40 $param =~ s/const //; # get rid of 'const' in C type
41 $param =~ s/^\s+//;
42 $param =~ s/\s+$//; # pare off the variable type from 'parm'
43
44 push (@vars, $varname);
45 push (@types, $param);
46 }
47
48 # Create the XS header:
49 my $xsout = '';
50 $xsout .= "$return_type\n";
51 $xsout .= "_$func_name (" . join (", ", @vars) . ")\n";
52
53 # Add in the variable declarations:
54 foreach my $i ( 0 .. $#vars )
55 {
56 $xsout .= "\t$types[$i]\t$vars[$i]\n";
57 }
58
59 # Add the CODE section:
60 $xsout .= "CODE:\n";
61 $xsout .= "\tRETVAL = ";
62 $xsout .= "$add + "
63 if defined($add);
64 $xsout .= "$func_name (";
65
66 # Add more variable stuff:
67 foreach my $i ( 0 .. $#vars )
68 {
69 my $type = $types[$i];
70 if ($type =~ /PDL/)
71 {
72 $type =~ s/PDL//; # Get rid of PDL type when writing xs CODE section
73 $xsout .= "($type)$vars[$i]"."->data,";
74 }
75 else
76 {
77 $xsout .= "$vars[$i],";
78 }
79 }
80 chop ($xsout); # remove last comma
81 $xsout .= ");\n";
82
83 # Add the OUTPUT section:
84 $xsout .= "OUTPUT:\n";
85 $xsout .= "\tRETVAL\n";
86 foreach my $var ( sort keys %$output )
87 {
88 $xsout .= "\t$var\n";
89 }
90 $xsout .= "\n\n";
91
92 # Add it to the PDL::PP file:
93 pp_addxs ('', $xsout);
94 }
95 } # End of create_low_level()...
96
97 1;