Mercurial > hg > lukkari
changeset 74:b51ad733b624
Improvements in the parser, now also produces "XML" output.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 24 Oct 2012 07:28:06 +0300 |
parents | 241839ebb09f |
children | 3d9e42477367 |
files | parsedata.pl |
diffstat | 1 files changed, 37 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/parsedata.pl Wed Oct 24 06:43:12 2012 +0300 +++ b/parsedata.pl Wed Oct 24 07:28:06 2012 +0300 @@ -363,40 +363,65 @@ } -sub getDataStruct($); +sub getDataStruct($$); -sub getDataStruct($) +sub getDataStruct($$) { my @out = (); - my $tmp = $_[0]; + my ($tmp, $first) = @_; if (ref($tmp) eq "ARRAY") { my @str = (); foreach my $item (@{$tmp}) { - push(@str, getDataStruct($item)); + push(@str, getDataStruct($item, 0)); } - push(@out, "array(".join(", ", @str).")"); + if (scalar(@str) > 0) + { + push(@out, "array(".join(", ", @str).")") if ($opt_mode eq "php"); + push(@out, "<group>".join("", @str)."</group>") if ($opt_mode eq "xml"); + } } elsif (ref($tmp) eq "HASH") { my @str = (); foreach my $key (keys %{$tmp}) { - push(@out, "\"".$key."\" => ".getDataStruct($$tmp{$key})); + push(@out, "\"".$key."\" => ".getDataStruct($$tmp{$key}, 1)) if ($opt_mode eq "php"); + push(@out, "<".$key.">".getDataStruct($$tmp{$key}, 1)."</".$key.">") if ($opt_mode eq "xml"); } - push(@out, "array(".join(", ", @str).")"); + if (scalar(@str) > 0) + { + push(@out, "array(".join(", ", @str).")") if ($opt_mode eq "php"); + push(@out, join("", @str)) if ($opt_mode eq "xml"); + } } elsif ($tmp =~ /^\d+$/) { - push(@out, $tmp); + if ($opt_mode eq "php") + { + push(@out, $tmp); + } + else + { + push(@out, $first ? $tmp : "<item>".$tmp."</item>"); + } } else { - push(@out, "\"".$tmp."\""); + if ($opt_mode eq "php") + { + push(@out, "\"".$tmp."\""); + } + else + { + push(@out, $first ? $tmp : "<item>".$tmp."</item>"); + } } - return join(", ", @out); + + return join(", ", @out) if ($opt_mode eq "php"); + return join("", @out); } # Skip zero position this way (can't use foreach here) @@ -475,7 +500,7 @@ print "\$classHourDefs = array(\n"; foreach my $cid (sort { $a <=> $b } keys %{$hourDefs}) { - print " $cid => array(".getDataStruct($$hourDefs{$cid})."),\n"; + print " $cid => array(".getDataStruct($$hourDefs{$cid}, 0)."),\n"; } print ");\n\n"; @@ -511,19 +536,7 @@ print " <classes>\n"; foreach my $cid (sort { $a <=> $b } keys %{$hourDefs}) { - print " <class id=\"$cid\" "; - foreach my $key (keys %{$$hourDefs{$cid}}) { - my $a = $$hourDefs{$cid}{$key}; - if (ref($a) eq "ARRAY") { - print "<$key>".join("", map { "\"".escape($_)."\""; } @$a)."</$key>"; - } - elsif ($a =~ /^\d+$/) { - print "$key=\"".$a."\" "; - } else { - print "$key=\"".escape($a)."\" "; - } - } - print " </class>\n"; + print " <class id=\"$cid\">".getDataStruct($$hourDefs{$cid}, 0)."</class>\n"; } print " </classes>\n\n";