| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | my $xs_file = 'XS/Structs.xs'; | |||||||||||||||||||||||||||
| 5 | 1 | my $module = 'Astro::Nova'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | open my $oh, '>', $xs_file or die "Can't open $xs_file for output: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 8 | 1 | print $oh " ### WARNING! AUTO-GENERATED! ALL CHANGES WILL BE LOST!\n\n"; | |||||||||||||||||||||||||||
| 9 | 1 | parse($oh); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 12 | 1 | my $oh = shift; | |||||||||||||||||||||||||||
| 13 | 1 | my %structs; | |||||||||||||||||||||||||||
| 14 | my $struct; | ||||||||||||||||||||||||||||
| 15 | 1 | ▶ | while (<DATA>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 100 | chomp; | |||||||||||||||||||||||||||
| 17 | 100 | ▶ | if (/^\s*$/ and $struct) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
/^\s*$/ and $struct
MC/DC100%2/2
/^\s*$/ and $struct
/^\s*$/
$struct
Truth table100%3/3
A and B
A= /^\s*$/B= $struct
| |||||||||||||||||||||||||||||
| 18 | 17 | $structs{$struct->{name}} = $struct; | |||||||||||||||||||||||||||
| 19 | 17 | $struct = undef; | |||||||||||||||||||||||||||
| 20 | 17 | next; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | 83 | ▶ | next if /^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 82 | ▶ | if (not $struct) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 18 | ▶ | /^\s*(\S+)\s+(\S.*)$/ or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 18 | $struct = {members => {}}; | |||||||||||||||||||||||||||
| 27 | 18 | $struct->{name} = $2; | |||||||||||||||||||||||||||
| 28 | 18 | $struct->{package} = $1; | |||||||||||||||||||||||||||
| 29 | 18 | $struct->{name} =~ s/\s+$//; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | else { | ||||||||||||||||||||||||||||
| 32 | 64 | ▶ | /^\s*([^\t]+)\t+(.*)$/ or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 64 | my ($mtype, $mname) = ($1, $2); | |||||||||||||||||||||||||||
| 34 | 64 | $mtype =~ s/^\s+//; $mtype =~ s/\s+$//; | |||||||||||||||||||||||||||
| 35 | 64 | $mname =~ s/^\s+//; $mname =~ s/\s+$//; | |||||||||||||||||||||||||||
| 36 | 64 | $struct->{members}{$mname} = $mtype; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 1 | ▶ | if ($struct) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 1 | $structs{$struct->{name}} = $struct; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 1 | write_structs($oh, \%structs); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | sub write_structs { | ||||||||||||||||||||||||||
|
Subroutine
write_structs: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | 1 | my ($oh, $structs) = @_; | |||||||||||||||||||||||||||
| 47 | 1 | foreach my $struct (values %$structs) { | |||||||||||||||||||||||||||
| 48 | 18 | print $oh "\nMODULE=$module PACKAGE=$struct->{package}\n\n"; | |||||||||||||||||||||||||||
| 49 | 18 | print $oh constructor($struct), "\n"; | |||||||||||||||||||||||||||
| 50 | 18 | print $oh destructor($struct), "\n"; | |||||||||||||||||||||||||||
| 51 | 18 | print $oh accessors($struct, $structs), "\n"; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 18 | ▶ | sub accessors { | ||||||||||||||||||||||||||
|
Subroutine
accessors: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 56 | 18 | my ($struct, $structs_hash) = @_; | |||||||||||||||||||||||||||
| 57 | 18 | my $name = $struct->{name}; | |||||||||||||||||||||||||||
| 58 | 18 | my $members = $struct->{members}; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 18 | my @code; | |||||||||||||||||||||||||||
| 61 | 18 | foreach my $field (keys %$members) { | |||||||||||||||||||||||||||
| 62 | 64 | my $type = $members->{$field}; | |||||||||||||||||||||||||||
| 63 | 64 | ▶ | if ($type !~ /^\s*struct/ or $type =~ /^\s*struct.*\*\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $type =~ /^\s*struct/ or $type =~ /^\s*struct.*\*\s*$/
MC/DC50%1/2
not $type =~ /^\s*struct/ or $type =~ /^\s*struct.*\*\s*$/
not $type =~ /^\s*struct/
$type =~ /^\s*struct.*\*\s*$/
Truth table66%2/3
not(A) or B
A= not $type =~ /^\s*struct/B= $type =~ /^\s*struct.*\*\s*$/
| |||||||||||||||||||||||||||||
| 64 | 58 | push @code, <<HERE; | |||||||||||||||||||||||||||
| 65 | $type | ||||||||||||||||||||||||||||
| 66 | get_$field( self ) | ||||||||||||||||||||||||||||
| 67 | $name* self | ||||||||||||||||||||||||||||
| 68 | CODE: | ||||||||||||||||||||||||||||
| 69 | RETVAL = self->$field; | ||||||||||||||||||||||||||||
| 70 | OUTPUT: | ||||||||||||||||||||||||||||
| 71 | RETVAL | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | void | ||||||||||||||||||||||||||||
| 74 | set_$field( self, val ) | ||||||||||||||||||||||||||||
| 75 | $name* self | ||||||||||||||||||||||||||||
| 76 | $type val | ||||||||||||||||||||||||||||
| 77 | PPCODE: | ||||||||||||||||||||||||||||
| 78 | self->$field = val; | ||||||||||||||||||||||||||||
| 79 | HERE | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||||||||||
| 82 | 6 | my $field_class = $structs_hash->{$type}{package}; | |||||||||||||||||||||||||||
| 83 | 6 | ▶ | die "Can't find class for type '$type'" if not defined $field_class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 6 | push @code, <<HERE; | |||||||||||||||||||||||||||
| 85 | $type* | ||||||||||||||||||||||||||||
| 86 | get_$field( self ) | ||||||||||||||||||||||||||||
| 87 | $name* self | ||||||||||||||||||||||||||||
| 88 | INIT: | ||||||||||||||||||||||||||||
| 89 | const char* CLASS = "$field_class"; /* hack to work around perlobject.map */ | ||||||||||||||||||||||||||||
| 90 | CODE: | ||||||||||||||||||||||||||||
| 91 | RETVAL = ($type*)safemalloc( sizeof( $type ) ); | ||||||||||||||||||||||||||||
| 92 | if( RETVAL == NULL ){ | ||||||||||||||||||||||||||||
| 93 | warn("unable to malloc $type"); | ||||||||||||||||||||||||||||
| 94 | XSRETURN_UNDEF; | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | Copy(&(self->$field), (RETVAL), 1, $type); | ||||||||||||||||||||||||||||
| 97 | OUTPUT: | ||||||||||||||||||||||||||||
| 98 | RETVAL | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | void | ||||||||||||||||||||||||||||
| 101 | set_$field( self, val ) | ||||||||||||||||||||||||||||
| 102 | $name* self | ||||||||||||||||||||||||||||
| 103 | $type* val | ||||||||||||||||||||||||||||
| 104 | PPCODE: | ||||||||||||||||||||||||||||
| 105 | Copy(val, &(self->$field), 1, $type); | ||||||||||||||||||||||||||||
| 106 | HERE | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 18 | return join("\n\n", @code)."\n"; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 18 | ▶ | sub destructor { | ||||||||||||||||||||||||||
|
Subroutine
destructor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | 18 | my ($struct) = @_; | |||||||||||||||||||||||||||
| 115 | 18 | my $name = $struct->{name}; | |||||||||||||||||||||||||||
| 116 | 18 | return <<HERE; | |||||||||||||||||||||||||||
| 117 | void | ||||||||||||||||||||||||||||
| 118 | DESTROY(self) | ||||||||||||||||||||||||||||
| 119 | $name* self | ||||||||||||||||||||||||||||
| 120 | CODE: | ||||||||||||||||||||||||||||
| 121 | safefree( (char*)self ); | ||||||||||||||||||||||||||||
| 122 | HERE | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 18 | ▶ | sub constructor { | ||||||||||||||||||||||||||
|
Subroutine
constructor: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 126 | 18 | my ($struct) = @_; | |||||||||||||||||||||||||||
| 127 | 18 | my $name = $struct->{name}; | |||||||||||||||||||||||||||
| 128 | 18 | my $package = $struct->{package}; | |||||||||||||||||||||||||||
| 129 | 18 | my $initsection = ''; | |||||||||||||||||||||||||||
| 130 | 18 | my $assignsection = ''; | |||||||||||||||||||||||||||
| 131 | my @init = ( | ||||||||||||||||||||||||||||
| 132 | {re => qr/^\s*(?:unsigned\s+)?(?:int|long\s*long|long|short|char)\s*$/, | ||||||||||||||||||||||||||||
| 133 | init => '0', | ||||||||||||||||||||||||||||
| 134 | assign => 'SvIV(*saveSV)', | ||||||||||||||||||||||||||||
| 135 | }, | ||||||||||||||||||||||||||||
| 136 | {re => qr/^\s*(?:double|float)\s*$/, | ||||||||||||||||||||||||||||
| 137 | init => '0.', | ||||||||||||||||||||||||||||
| 138 | assign => 'SvNV(*saveSV)', | ||||||||||||||||||||||||||||
| 139 | }, | ||||||||||||||||||||||||||||
| 140 | {re => qr/^\s*struct\b/, | ||||||||||||||||||||||||||||
| 141 | 6 | ▶ | init => sub {my ($f, $t) = @_; "Zero(&(RETVAL->$f), 1, $t);" }, # hack | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 142 | 6 | ▶ | assign => sub { # hack | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | 6 | my ($f, $t) = @_; | |||||||||||||||||||||||||||
| 144 | 6 | "if ( sv_isobject(*saveSV) && (SvTYPE(SvRV(*saveSV)) == SVt_PVMG) ) { | |||||||||||||||||||||||||||
| 145 | $t* original = ($t*)SvIV((SV*)SvRV( *saveSV )); | ||||||||||||||||||||||||||||
| 146 | Copy(original, &(RETVAL->$f), 1, $t); | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | else { | ||||||||||||||||||||||||||||
| 149 | warn(\"Invalid argument passed to constructor\"); | ||||||||||||||||||||||||||||
| 150 | XSRETURN_UNDEF; | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | " | ||||||||||||||||||||||||||||
| 153 | }, | ||||||||||||||||||||||||||||
| 154 | }, | ||||||||||||||||||||||||||||
| 155 | 18 | ); | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 18 | foreach my $field (keys %{$struct->{members}}) { | |||||||||||||||||||||||||||
| 158 | 64 | my $type = $struct->{members}{$field}; | |||||||||||||||||||||||||||
| 159 | 64 | foreach my $init (@init) { | |||||||||||||||||||||||||||
| 160 | 118 | my $re = $init->{re}; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 118 | ▶ | if ($type =~ $re) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 64 | ▶ | if (ref($init->{init})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 6 | $initsection .= "\n " . $init->{init}->($field, $type); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | else { | ||||||||||||||||||||||||||||
| 167 | 58 | $initsection .= "\n RETVAL->$field = " . $init->{init} . ";"; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 64 | ▶ | if (ref($init->{assign})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | $assignsection .= "\n saveSV = hv_fetchs(tmphash, \"$field\", 0);" . | ||||||||||||||||||||||||||||
| 172 | 6 | "\n if (saveSV != NULL && SvOK(*saveSV)) {\n " . $init->{assign}->($field, $type) . | |||||||||||||||||||||||||||
| 173 | "\n }\n"; | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | else { | ||||||||||||||||||||||||||||
| 176 | 58 | $assignsection .= "\n saveSV = hv_fetchs(tmphash, \"$field\", 0);" . | |||||||||||||||||||||||||||
| 177 | "\n if (saveSV != NULL && SvOK(*saveSV)) {\n RETVAL->$field = $init->{assign};" . | ||||||||||||||||||||||||||||
| 178 | "\n }\n"; | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 64 | last; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 18 | return <<HERE; | |||||||||||||||||||||||||||
| 186 | $name* | ||||||||||||||||||||||||||||
| 187 | new(class, ...) | ||||||||||||||||||||||||||||
| 188 | SV* class | ||||||||||||||||||||||||||||
| 189 | PREINIT: | ||||||||||||||||||||||||||||
| 190 | char* CLASS; | ||||||||||||||||||||||||||||
| 191 | HV* tmphash; | ||||||||||||||||||||||||||||
| 192 | int iStack; | ||||||||||||||||||||||||||||
| 193 | SV** saveSV; | ||||||||||||||||||||||||||||
| 194 | CODE: | ||||||||||||||||||||||||||||
| 195 | if (sv_isobject(class)) { | ||||||||||||||||||||||||||||
| 196 | CLASS = (char*)sv_reftype(SvRV(class), 1); | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | else { | ||||||||||||||||||||||||||||
| 199 | if (!SvPOK(class)) | ||||||||||||||||||||||||||||
| 200 | croak("Need an object or class name as first argument to the constructor of $package"); | ||||||||||||||||||||||||||||
| 201 | CLASS = (char*)SvPV_nolen(class); | ||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | RETVAL = ($name*)safemalloc( sizeof($name) ); | ||||||||||||||||||||||||||||
| 204 | if (RETVAL == NULL) { | ||||||||||||||||||||||||||||
| 205 | warn("unable to malloc $name"); | ||||||||||||||||||||||||||||
| 206 | XSRETURN_UNDEF; | ||||||||||||||||||||||||||||
| 207 | }$initsection | ||||||||||||||||||||||||||||
| 208 | if (items > 1) { | ||||||||||||||||||||||||||||
| 209 | if (!(items % 2)) { | ||||||||||||||||||||||||||||
| 210 | safefree(RETVAL); | ||||||||||||||||||||||||||||
| 211 | croak("Uneven number of arguments to constructor of $package"); | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | tmphash = (HV*)sv_2mortal((SV*)newHV()); | ||||||||||||||||||||||||||||
| 214 | for (iStack = 1; iStack < items; iStack += 2) { | ||||||||||||||||||||||||||||
| 215 | HE *he; | ||||||||||||||||||||||||||||
| 216 | he = hv_store_ent(tmphash, ST(iStack), newSVsv(ST(iStack+1)), 0); | ||||||||||||||||||||||||||||
| 217 | if (!he) | ||||||||||||||||||||||||||||
| 218 | croak("Failed to write value to hash."); | ||||||||||||||||||||||||||||
| 219 | }$assignsection | ||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | OUTPUT: | ||||||||||||||||||||||||||||
| 222 | RETVAL | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | HERE | ||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | __DATA__ | ||||||||||||||||||||||||||||