# HG changeset patch # User Matti Hamalainen # Date 1351052886 -10800 # Node ID b51ad733b624e12e941221f45761175732378531 # Parent 241839ebb09f3197839727c4d9c931186d9ba0c0 Improvements in the parser, now also produces "XML" output. diff -r 241839ebb09f -r b51ad733b624 parsedata.pl --- 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, "".join("", @str)."") 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)."") 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 : "".$tmp.""); + } } else { - push(@out, "\"".$tmp."\""); + if ($opt_mode eq "php") + { + push(@out, "\"".$tmp."\""); + } + else + { + push(@out, $first ? $tmp : "".$tmp.""); + } } - 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 " \n"; foreach my $cid (sort { $a <=> $b } keys %{$hourDefs}) { - print " ".join("", map { "\"".escape($_)."\""; } @$a).""; - } - elsif ($a =~ /^\d+$/) { - print "$key=\"".$a."\" "; - } else { - print "$key=\"".escape($a)."\" "; - } - } - print " \n"; + print " ".getDataStruct($$hourDefs{$cid}, 0)."\n"; } print " \n\n";