std.c: Improve some comments
[sunny256-utils.git] / zeropad
blobac48780e0e5c547dc4e80266f94bafd4a7a55aa3
1 #!/usr/bin/env perl
3 #=======================================================================
4 # zeropad
5 # File ID: 7028de4a-f744-11dd-a3ac-000475e441b9
7 # Pads all integers read from stdin with zeros so they all have the same
8 # length.
10 # Character set: UTF-8
11 # ©opyleft 2008– Øyvind A. Holm <sunny@sunbase.org>
12 # License: GNU General Public License version 2 or later, see end of
13 # file for legal stuff.
14 #=======================================================================
16 use strict;
17 use warnings;
18 use Getopt::Long;
20 local $| = 1;
22 our %Opt = (
24 'help' => 0,
25 'hex' => 0,
26 'quiet' => 0,
27 'size' => 0,
28 'verbose' => 0,
29 'version' => 0,
33 our $progname = $0;
34 $progname =~ s/^.*\/(.*?)$/$1/;
35 our $VERSION = '0.1.1';
37 Getopt::Long::Configure('bundling');
38 GetOptions(
40 'help|h' => \$Opt{'help'},
41 'hex|x' => \$Opt{'hex'},
42 'quiet|q+' => \$Opt{'quiet'},
43 'size|s=i' => \$Opt{'size'},
44 'verbose|v+' => \$Opt{'verbose'},
45 'version' => \$Opt{'version'},
47 ) || die("$progname: Option error. Use -h for help.\n");
49 $Opt{'verbose'} -= $Opt{'quiet'};
50 $Opt{'help'} && usage(0);
51 if ($Opt{'version'}) {
52 print_version();
53 exit(0);
56 my $max_length = 0;
58 exit(main());
60 sub main {
61 # {{{
62 my $Retval = 0;
64 my @Array = ();
66 my $DIGITS = $Opt{'hex'} ? '0-9A-Fa-f' : '0-9';
68 while (<>) {
69 my $Line = $_;
70 push(@Array, $Line);
71 $Line =~ s/([$DIGITS]+)/check_max($1)/ge;
74 for my $Curr (@Array) {
75 $Curr =~ s/([$DIGITS]+)/pad_number($1)/ge;
76 print($Curr);
79 return $Retval;
80 # }}}
81 } # main()
83 sub check_max {
84 # {{{
85 my $Str = shift;
86 if (length($Str) > $max_length) {
87 $max_length = $Opt{'size'} ? $Opt{'size'} : length($Str);
89 return("");
90 # }}}
91 } # check_max()
93 sub pad_number {
94 # {{{
95 my $Num = shift;
96 return $Num if ($max_length <= length($Num));
97 return('0' x ($max_length - length($Num)) . $Num);
98 # }}}
99 } # pad_number()
101 sub print_version {
102 # Print program version {{{
103 print("$progname $VERSION\n");
104 return;
105 # }}}
106 } # print_version()
108 sub usage {
109 # Send the help message to stdout {{{
110 my $Retval = shift;
112 if ($Opt{'verbose'}) {
113 print("\n");
114 print_version();
116 print(<<"END");
118 Usage: $progname [options] [file [files [...]]]
120 Pads all integers read from stdin with zeros so they all have the same
121 length. I.e.: 1 5 12 156 1024 = 0001 0005 0012 0156 1024.
123 Options:
125 -h, --help
126 Show this help.
127 -q, --quiet
128 Be more quiet. Can be repeated to increase silence.
129 -s X, --size X
130 Instead of padding to the size of the largest number, pad to a width
131 of X digits.
132 -v, --verbose
133 Increase level of verbosity. Can be repeated.
134 -x, --hex
135 Include hexadecimal digits a-f and A-F.
136 --version
137 Print version information.
140 exit($Retval);
141 # }}}
142 } # usage()
144 sub msg {
145 # Print a status message to stderr based on verbosity level {{{
146 my ($verbose_level, $Txt) = @_;
148 if ($Opt{'verbose'} >= $verbose_level) {
149 print(STDERR "$progname: $Txt\n");
151 return;
152 # }}}
153 } # msg()
155 __END__
157 # This program is free software; you can redistribute it and/or modify
158 # it under the terms of the GNU General Public License as published by
159 # the Free Software Foundation; either version 2 of the License, or (at
160 # your option) any later version.
162 # This program is distributed in the hope that it will be useful, but
163 # WITHOUT ANY WARRANTY; without even the implied warranty of
164 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
165 # See the GNU General Public License for more details.
167 # You should have received a copy of the GNU General Public License
168 # along with this program.
169 # If not, see L<http://www.gnu.org/licenses/>.
171 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :