installed_progs.t: Python checks stdout too, 150 ok
[sunny256-utils.git] / sortxml
blob8be6b774d9634cb3b43f995f5aa5651b219dc78f
1 #!/usr/bin/env perl
3 #=======================================================================
4 # sortxml
5 # File ID: 76c6f804-1463-11de-aecb-000475e441b9
7 # Sorts the X elements in an XML file.
9 # Character set: UTF-8
10 # ©opyleft 2009– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
15 use strict;
16 use warnings;
17 use Getopt::Long;
19 local $| = 1;
21 our %Opt = (
23 'help' => 0,
24 'quiet' => 0,
25 'reverse' => 0,
26 'sort' => '',
27 'unique' => 0,
28 'verbose' => 0,
29 'version' => 0,
33 our $progname = $0;
34 $progname =~ s/^.*\/(.*?)$/$1/;
35 our $VERSION = '0.1.0';
37 Getopt::Long::Configure('bundling');
38 GetOptions(
40 'help|h' => \$Opt{'help'},
41 'quiet|q+' => \$Opt{'quiet'},
42 'reverse|r' => \$Opt{'reverse'},
43 'sort|s=s' => \$Opt{'sort'},
44 'unique|u' => \$Opt{'unique'},
45 'verbose|v+' => \$Opt{'verbose'},
46 'version' => \$Opt{'version'},
48 ) || die("$progname: Option error. Use -h for help.\n");
50 $Opt{'verbose'} -= $Opt{'quiet'};
51 $Opt{'help'} && usage(0);
52 if ($Opt{'version'}) {
53 print_version();
54 exit(0);
57 exit(main());
59 sub main {
60 # {{{
61 my $Retval = 0;
63 my $sortele = $Opt{'sort'} || die("$progname: -s/--sort not defined\n");
64 my $buf = '';
65 my @Data = ();
66 my %dupfree = ();
68 while (my $Curr = <>) {
69 if ($Curr =~ /<$sortele\b/) {
70 $buf = $Curr;
71 if ($Curr =~ /<\/$sortele>/) {
72 $Opt{'unique'}
73 ? ($dupfree{$buf} = 1)
74 : push(@Data, $buf);
75 $buf = "";
77 while ($Curr = <>) {
78 $buf .= $Curr;
79 if ($Curr =~ /<\/$sortele>/) {
80 $Opt{'unique'}
81 ? ($dupfree{$buf} = 1)
82 : push(@Data, $buf);
83 $buf = "";
86 if ($Opt{'unique'}) {
87 print($Opt{'reverse'}
88 ? reverse sort(keys %dupfree)
89 : sort(keys %dupfree));
90 } else {
91 print($Opt{'reverse'}
92 ? reverse sort(@Data)
93 : sort(@Data));
95 print($buf);
96 last;
97 } else {
98 print($Curr);
102 return $Retval;
103 # }}}
104 } # main()
106 sub print_version {
107 # Print program version {{{
108 print("$progname $VERSION\n");
109 return;
110 # }}}
111 } # print_version()
113 sub usage {
114 # Send the help message to stdout {{{
115 my $Retval = shift;
117 if ($Opt{'verbose'}) {
118 print("\n");
119 print_version();
121 print(<<"END");
123 Usage: $progname [options] -s X [file [files [...]]]
125 Sorts the X elements in an XML file.
127 Options:
129 -h, --help
130 Show this help.
131 -q, --quiet
132 Be more quiet. Can be repeated to increase silence.
133 -r, --reverse
134 Sort in reverse order.
135 -s X, --sort X
136 Sort file using element X.
137 -u, --unique
138 Remove duplicated elements X.
139 -v, --verbose
140 Increase level of verbosity. Can be repeated.
141 --version
142 Print version information.
145 exit($Retval);
146 # }}}
147 } # usage()
149 sub msg {
150 # Print a status message to stderr based on verbosity level {{{
151 my ($verbose_level, $Txt) = @_;
153 if ($Opt{'verbose'} >= $verbose_level) {
154 print(STDERR "$progname: $Txt\n");
156 return;
157 # }}}
158 } # msg()
160 __END__
162 # This program is free software; you can redistribute it and/or modify
163 # it under the terms of the GNU General Public License as published by
164 # the Free Software Foundation; either version 2 of the License, or (at
165 # your option) any later version.
167 # This program is distributed in the hope that it will be useful, but
168 # WITHOUT ANY WARRANTY; without even the implied warranty of
169 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
170 # See the GNU General Public License for more details.
172 # You should have received a copy of the GNU General Public License
173 # along with this program.
174 # If not, see L<http://www.gnu.org/licenses/>.
176 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :