File: xml.pl

package info (click to toggle)
qstat 2.17-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,220 kB
  • sloc: ansic: 24,706; makefile: 86; perl: 78; sh: 62
file content (65 lines) | stat: -rwxr-xr-x 1,298 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl -w
# simple qstat xml output parser, prints values specified as command
# line arguments
#
# Author: Ludwig Nussel
#
# Usage Examples:
#
# Print server name:
#   "qstat/server/name"
#
# Print second server rule (numbered from zero):
#   "qstat/server/rules/rule/1"
#
# Print the rule with name "gamename":
#   "qstat/server/rules/rule/[name=gamename]"
#
# Print name of sixth player:
#   "qstat/server/players/player/5/name"
#
# Print clan of player with name "suCk3r":
#   "qstat/server/players/player/[name=suCk3r]/clan"

use strict;
use XML::Bare;
use Data::Dumper;

sub getvalue {
	my $x = shift;
	my @a = split(/\//, shift);
	for my $n (@a) {
		if ($n =~ /^[[:digit:]]+$/) {
			return undef unless exists $x->[$n];
			$x = $x->[$n];
		} elsif ($n =~ /\[(.*)=(.*)\]/) {
			my ($k, $v) = ($1, $2);
			my $r;
			for my $i (@$x) {
				next unless exists $i->{$k};
				if($i->{$k}->{value} eq $v) {
					$r = $i;
					last;
				}
			}
			return undef unless $r;
			$x = $r;
		} else {
			return undef unless exists $x->{$n};
			$x = $x->{$n};
		}
	}
	return $x->{value};
}

sub printvalue {
	my $val = getvalue(@_);
	$val = "(undefined)" unless defined $val;
	print $val, "\n"
}

my $xml = XML::Bare->new(text => join('', <STDIN>))->parse();

for (@ARGV) {
	printvalue($xml, $_);
}