root/applications/bdmtoolbox/doc/from_bdm/class_hierarchy.pl @ 1048

Revision 1043, 1.7 kB (checked in by smidl, 14 years ago)

Filter of BDM movet to bdmtoolbox

  • Property svn:executable set to *
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Data::Dumper;
6use Getopt::Long;
7
8my $GIVEN_CLASS = 'bdm::DS';
9GetOptions(
10    'class=s'   => \$GIVEN_CLASS,
11) or die('Wrong options format');
12
13my $prev_class = {
14    name        => '',
15    daughters   => [],
16};
17$prev_class->{parent} = $prev_class;
18
19my $class;
20
21my $prev_indent = 0;
22my $indent;
23
24my $classes_list = {};
25my $classes_hier = $prev_class;
26
27while (my $line = <>) {
28    # parse the current line
29    chomp $line;
30    $line =~ m{ 
31        ^       # start of the line
32        ([ ]+)  # indentation
33        (.+)    # class name itself
34        $       # end of the line
35    }xms;
36    $indent        = length $1; # number of indent spaces
37    my $class_name = $2;        # the rest of the line
38
39    # find the parent class: this simple while covers all situations
40    while ($indent <= $prev_indent) {
41        $prev_class = $prev_class->{parent};
42        $prev_indent--;
43    }
44
45    $class = {
46        name      => $class_name,
47        daughters => [],
48        parent    => $prev_class,
49    };
50
51    push @{ $prev_class->{daughters} }, $class; # place into the hierarchy
52    $classes_list->{$class_name}      = $class; # store in hash by name
53   
54    # store current values as previous for the next iteration
55    $prev_indent = $indent;
56    $prev_class  = $class;
57}
58
59#print "==Hierarchy";
60#print_class($classes_hier, 0);
61#
62#print "\n==List\n";
63#map { print "$_\n" } sort keys %{ $classes_list };
64#
65#print "\n==Given\n";
66print_class($classes_list->{$GIVEN_CLASS}, 0);
67
68sub print_class {
69    my $class  = shift;
70    my $indent = shift;
71
72    print " " x $indent;
73    print $class->{name}, "\n";
74   
75    map { print_class($_, $indent+1) }  @{ $class->{daughters} };
76}
Note: See TracBrowser for help on using the browser.