#!/Perl

use Data::Dumper;
use XML::Simple;
my $outfil = 'tempxout.htm';
my ($OF);

my $file = 'c:\\FG0910-1\\source\\projects\\VC8\\FlightGear.vcproj';

open $OF, ">$outfil" or die "Can not create $outfil!\n";

my $xs1 = XML::Simple->new();
my $doc = $xs1->XMLin($file);
prt( "Hello, World... loaded $file ...\n");
show_type($doc, 'doc', 1);
$a = "pearl";
$b = [ $a ];
$c = { 'b' => $b };
$d = [ $c ];
$e = { 'd' => $d };
$f = { 'e' => $e };
prt( Data::Dumper->Dump([$f], [qw(f)]) );
show_type($f, 'f', 1);
#my $chr = <STDIN>;

my $tree1 = 'Files';
my $tree2 = 'Filter';
my $tree3 = 'File';

#prt( Dumper($doc) );
prt( "Dump of WHOLE document tree ...\n" );
prt( Data::Dumper->Dump([$doc], [qw(doc)]) );
prt( "Dump of Configurations tree ...\n" );
prt( Data::Dumper->Dump([$doc->{'Configurations'}], [qw(doc-config)]) );
prt( "Dump of Files tree ...\n" );
prt( Data::Dumper->Dump([$doc->{'Files'}], [qw(doc-files)]) );
prt( "Dump of $tree1 $tree2 tree ...\n" );
prt( Data::Dumper->Dump([$doc->{$tree1}->{$tree2}], [qw(doc-files-filter)]) );
my $mh = $doc->{$tree1}->{$tree2};
prt( "Dump of mh ...\n" );
prt( Data::Dumper->Dump([$mh], [qw(mh)]) );
#my %cmh = $doc->{$tree1}->{$tree2};
#my %cmh = \$doc->{$tree1}->{$tree2};
#prt( "Dump of mh File ...\n" );
#prt( Data::Dumper->Dump([$mh->{$tree3}], [qw(mh-File)]) );
#while ( my ($key, $value) = each(%cmh) ) {
#        prt( "$key => $value\n" );
#}
#while ( my ($key, $value) = each(%$mh) ) {
#        prt( "$key => $value\n" );
#}
#for my $key ( keys %cmh ) {
#        my $value = $hash{$key};
#        print "$key => $value\n";
#}
my $do_out2 = 1;
my $do_out3 = 0;
prt( "Try enumerate of mh ...\n" );
show_type($mh, 'mh', 1);
#while( my ($k, $v) = each %{$mh} ) {
#        print "key: $k, value: $v.\n";
#}
if ($do_out2) {
	#for my $k1 (sort keys %$mh) {
	for my $k1 (keys %$mh) {
	#foreach my $k1 ($mh) {
#		prt( "k1: $k1\n" );
		show_type($k1, 'k1', 1);
		#foreach my $k2 ($k1) {
		#	prt( "k2: $k2\n" );
		#}
		if ($do_out3) {
			#if (defined %{$mh->{$k1}} ) {
			if (defined $mh->{$k1} ) {
				for my $k2 ( sort keys %{$mh->{$k1}} ) {
					prt( "\tk2: $k2\n" );
					if (defined %{$mh->{$k1}->{$k2}} ) {
						for my $k3 ( sort keys %{$mh->{$k1}->{$k2}} ) {
							prt( "\\tk3: $k3\n" );
						}
					}
				}
			}
		}
	}
}

my $mh2 = $doc->{$tree1}->{$tree2}->{$tree3};
#my $mh2 = $mh->{'File'};
prt( "Dump of mh2 ...\n" );
prt( Data::Dumper->Dump([$mh2], [qw(mh2)]) );
prt( "Dump of $tree1 $tree2 $tree3 tree ...\n" );
prt( Data::Dumper->Dump([$doc->{$tree1}->{$tree2}->{$tree3}], [qw(doc-files-filter-file)]) );

#my @arr = $doc->{$tree1}->{$tree2}->{$tree3};
#my @arr = $doc->{$tree1}->{$tree2};
#my $cnt = scalar @arr;
#prt( "Got $cnt file items using $tree1, $tree2, ...\n");
#foreach my $item (@arr) {
#	prt( "$item\n" );
#}
close $OF;

sub prt {
	###tolog (@_);
	print @_;
	print $OF @_;
}

sub show_type {
	my ($k, $name, $out) = @_;
	my $type = ref($k);
	my $msg = "$name ";
	if ($type) {
		if ($type eq 'HASH' ) {
			my $hcnt = scalar keys %$k;
			$msg .= "(HASH) hcnt=$hcnt ";
			for my $k1 (keys %$k) {
				$msg .= "key=$k1 ";
				#if (defined %{$k{$k1}} ) {
				#	my $v = %{$k{$k1}};
				if (defined $$k{$k1} ) {
					my $v = $$k{$k1};
					my $t2 = ref($v);
					if ($t2) {
						$msg .= "\n{".show_type($v, "$k1 => ", 0).'}';
					} else {
						$msg .= "val=$v ";
					}
				} else {
					$msg .= 'v=undef ';
				}
			}
		} elsif ($type eq 'ARRAY') {
			my $cnt = scalar @$k;
			$msg .= "(ARRAY) acnt=$cnt ";
			foreach my $t (@$k) {
				my $t2 = ref($t);
				if ($t2) {
					$msg .= "\n{".show_type($t, 'sta => ', 0).'}';
				} else {
					$msg .= "$t ";
				}
			}
		} elsif ($type eq 'CODE') {
			$msg .= "is CODE ... ";
		} elsif ($type eq 'GLOB') {
			$msg .= "is GLOB ... ";
		} else {
			$msg .= "Type is $type ... ";
			$msg .= "[$k] not HASH, ARRAY, CODE. GLOB ... ";
		}
	} else {
		$msg .= "(SCALAR) no Type [$k] ...";
	}
	prt( "$msg\n" ) if ($out);
	return $msg;
}

#eof

