mdisk: Remove color from "Already mounted" message
[sunny256-utils.git] / zerosplit
blobcaa17d3f0ea660c0930ec37ddb206944e32c3c37
1 #!/usr/bin/env perl
3 #=======================================================================
4 # zerosplit
5 # File ID: 76d291aa-f744-11dd-9dc1-000475e441b9
6 # [Description]
8 # Character set: UTF-8
9 # ©opyleft 2008– Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later, see end of
11 # file for legal stuff.
12 #=======================================================================
14 use strict;
15 use warnings;
16 use Getopt::Long;
18 $| = 1;
20 our $Debug = 0;
22 our %Opt = (
24 'char' => 0,
25 'count' => 0,
26 'debug' => 0,
27 'help' => 0,
28 'verbose' => 0,
29 'version' => 0,
33 our $progname = $0;
34 $progname =~ s/^.*\/(.*?)$/$1/;
35 our $VERSION = "0.00";
37 Getopt::Long::Configure("bundling");
38 GetOptions(
40 "char|c=o" => \$Opt{'char'},
41 "count|n=i" => \$Opt{'count'},
42 "debug" => \$Opt{'debug'},
43 "help|h" => \$Opt{'help'},
44 "verbose|v+" => \$Opt{'verbose'},
45 "version" => \$Opt{'version'},
47 ) || die("$progname: Option error. Use -h for help.\n");
49 $Opt{'debug'} && ($Debug = 1);
50 $Opt{'help'} && usage(0);
51 if ($Opt{'version'}) {
52 print_version();
53 exit(0);
56 if ($Opt{'char'} < 0 || $Opt{'char'} > 255) {
57 die("$progname: $Opt{'char'}: Invalid character value\n");
60 my $Count = $Opt{'count'};
61 $Count ||
62 die("$progname: Has to specify --count option " .
63 "with a value greater than 0\n");
64 my $curr_count = $Count + 1;
66 $/ = chr($Opt{'char'});
68 my $File;
69 my $file_count = 0;
71 while (<>) {
72 my $Curr = $_;
73 if ($curr_count >= $Count) {
74 $File = sprintf("%08x", $file_count);
75 if (open(FP, ">$File")) {
76 $file_count++;
77 $curr_count = 0;
78 msg(1, "Creating $File");
79 } else {
80 die("$progname: $File: Cannot create file: $!\n");
83 print(FP "$Curr");
84 $curr_count++;
87 sub print_version {
88 # Print program version {{{
89 print("$progname v$VERSION\n");
90 # }}}
91 } # print_version()
93 sub usage {
94 # Send the help message to stdout {{{
95 my $Retval = shift;
97 if ($Opt{'verbose'}) {
98 print("\n");
99 print_version();
101 print(<<END);
103 Split a stream separated by a special character into files.
105 Usage: $progname [options] [file [files [...]]]
107 Options:
109 -c x, --char x
110 Use character with value x as separator. Default: 0.
111 -n x, --count x
112 Store x records in every file.
113 -h, --help
114 Show this help.
115 -v, --verbose
116 Increase level of verbosity. Can be repeated.
117 --version
118 Print version information.
119 --debug
120 Print debugging messages.
123 exit($Retval);
124 # }}}
125 } # usage()
127 sub msg {
128 # Print a status message to stderr based on verbosity level {{{
129 my ($verbose_level, $Txt) = @_;
131 if ($Opt{'verbose'} >= $verbose_level) {
132 print(STDERR "$progname: $Txt\n");
134 # }}}
135 } # msg()
137 sub D {
138 # Print a debugging message {{{
139 $Debug || return;
140 my @call_info = caller;
141 chomp(my $Txt = shift);
142 my $File = $call_info[1];
143 $File =~ s#\\#/#g;
144 $File =~ s#^.*/(.*?)$#$1#;
145 print(STDERR "$File:$call_info[2] $$ $Txt\n");
146 return("");
147 # }}}
148 } # D()
150 __END__
152 # Plain Old Documentation (POD) {{{
154 =pod
156 =head1 NAME
160 =head1 SYNOPSIS
162 [options] [file [files [...]]]
164 =head1 DESCRIPTION
168 =head1 OPTIONS
170 =over 4
172 =item B<-h>, B<--help>
174 Print a brief help summary.
176 =item B<-v>, B<--verbose>
178 Increase level of verbosity. Can be repeated.
180 =item B<--version>
182 Print version information.
184 =item B<--debug>
186 Print debugging messages.
188 =back
190 =head1 BUGS
194 =head1 AUTHOR
196 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
198 =head1 COPYRIGHT
200 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
201 This is free software; see the file F<COPYING> for legalese stuff.
203 =head1 LICENCE
205 This program is free software: you can redistribute it and/or modify it
206 under the terms of the GNU General Public License as published by the
207 Free Software Foundation, either version 2 of the License, or (at your
208 option) any later version.
210 This program is distributed in the hope that it will be useful, but
211 WITHOUT ANY WARRANTY; without even the implied warranty of
212 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
213 See the GNU General Public License for more details.
215 You should have received a copy of the GNU General Public License along
216 with this program.
217 If not, see L<http://www.gnu.org/licenses/>.
219 =head1 SEE ALSO
221 =cut
223 # }}}
225 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :