I really like this approach, thanks for suggesting it. I've had to go in and modify some Python scripts to match changes in the source code, and it's usually quite a pain to find out where things are being set/called/parsed/whatever. Anything I can do to make it easier will definitely be appreciated by non-Perl programmers.
Of course since it's Monday (that's my excuse and I'm sticking to it) I still feel like I'm writing very messy code. Any suggestions on the added sub would be appreciated.
Also note that the header files are auto-generated by a tool we're using, so the format of the struct definitions is always the same. And my subroutine takes this into account -- it fails horribly with comments in the code, but works just fine for the "live" code.
use strict; use warnings; use Data::Dumper; my %default_values = ( 'float' => 0, 'int' => 3, # Unique value for visibility durin +g testing 'bool' => 'false' ); my %structs; parse_struct_definitions(); print Dumper(%structs); sub parse_struct_definitions { # Reads the typedef struct lines in the __DATA__ section to popula +te the # %structs hash. Created for simple updates to the defined struct +ures # (simply copy and paste from the header files into the DATA secti +on # below) local $/ = 'typedef struct {'; while (my $line = <DATA>) { chomp $line; next if $line !~ /\w/; # For me to parse out the data more easily: $line =~ s/\n/ /g; $line =~ s/\s+/ /g; # Break the line into members and the struct name my ($member_string, $name) = $line =~ /([^\}]+)\s*\}\s*(.+)/; my @members = split ';', $member_string; $name =~ tr/; //d; foreach my $member (@members) { next if $member !~ /\w/; my ($type, $member_name) = split " ", $member; push @{$structs{$name}}, [ $type, $member_name, $default_v +alues{$type} ]; } } } # end of parse_struct_definitions __DATA__ typedef struct { float one; float two; int three; bool potato; } struct_name; typedef struct { float one; /* 0.0 */ float two; /* 0.0 */ int three; /* 0 */ bool potato; /* false */ } struct_name_with_comments;
In reply to Re^2: C parsing questions
by Nkuvu
in thread C parsing questions
by Nkuvu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |