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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.