2 ########################################################################
4 ########################################################################
6 Source file position: `<groff-source>/contrib/glilypond/oop_fh.pl'
7 Installed position: `<prefix>/lib/groff/glilypond/oop_fh.pl'
9 Copyright (C) 2013-2013 Free Software Foundation, Inc.
10 Written by Bernd Warken <groff-bernd.warken-72@web.de>
12 This file is part of `glilypond', which is part of `GNU groff'.
14 glilypond - integrate `lilypond' into `groff' files
16 `GNU groff' is free software: you can redistribute it and/or modify it
17 under the terms of the `GNU General Public License' as published by the
18 `Free Software Foundation', either version 3 of the License, or (at your
19 option) any later version.
21 `GNU groff' is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the `GNU
24 General Public License' for more details.
26 You should have received a copy of the 'GNU General Public License`
27 along with `groff', see the files `COPYING' and `LICENSE' in the top
28 directory of the `groff' source package. If not, see
29 <http://www.gnu.org/licenses/>.
41 ########################################################################
42 # OOP for writing file handles that are open by default, like STD*
43 ########################################################################
45 # -------------------------- _FH_WRITE_OPENED --------------------------
47 { # FH_OPENED: base class for all opened file handles, like $TD*
49 package _FH_WRITE_OPENED;
53 my ( $pkg, $std ) = @_;
68 print { $self->{'fh'} } $_;
75 # ------------------------------ FH_STDOUT ----------------------------
77 { # FH_STDOUT: print to noral output STDOUT
81 @FH_STDOUT::ISA = qw( _FH_WRITE_OPENED );
84 &_FH_WRITE_OPENED::new( '_FH_WRITE_OPENED', *STDOUT );
90 # ------------------------------ FH_STDERR -----------------------------
92 { # FH_STDERR: print to STDERR
96 @FH_STDERR::ISA = qw( _FH_WRITE_OPENED );
99 &_FH_WRITE_OPENED::new( 'FH_OPENED', *STDERR );
105 ########################################################################
106 # OOP for file handles that write into a file or string
107 ########################################################################
109 # ------------------------------- FH_FILE ------------------------------
111 { # FH_FILE: base class for writing into a file or string
117 my ( $pkg, $file ) = @_;
121 'opened' => main::FALSE,
132 my $file = $self->{'file'};
133 if ( $file && -e $file ) {
134 die "file $file is not writable" unless ( -w $file );
135 die "$file is a directory" if ( -d $file );
137 open $self->{'fh'}, ">", $self->{'file'}
138 or die "could not open file `$file' for writing: $!";
139 $self->{'opened'} = main::TRUE;
144 close $self->{'fh'} if ( $self->{'opened'} );
145 $self->{'opened'} = main::FALSE;
150 $self->open() unless ( $self->{'opened'} );
152 print { $self->{'fh'} } $_;
159 # ------------------------------ FH_STRING -----------------------------
161 { # FH_STRING: write into a string
163 package FH_STRING; # write to \string
165 @FH_STRING::ISA = qw( FH_FILE );
168 my $pkg = shift; # string is a reference to scalar
173 'opened' => main::FALSE,
179 open $self->{'fh'}, ">", \ $self->{'string'}
180 or die "could not open string for writing: $!";
181 $self->{'opened'} = main::TRUE;
184 sub get { # get string, move to array ref, close, and return array ref
186 return '' unless ( $self->{'opened'} );
187 my $a = &string2array( $self->{'string'} );
195 # -------------------------------- FH_NULL -----------------------------
197 { # FH_NULL: write to null device
201 @FH_NULL::ISA = qw( FH_FILE FH_STRING );
205 my $devnull = File::Spec->devnull();
206 $devnull = '' unless ( -e $devnull && -w $devnull );
211 &FH_FILE::new( $pkg, $devnull );
213 &FH_STRING::new( $pkg );
220 ########################################################################
221 # OOP for reading file handles
222 ########################################################################
224 # ---------------------------- FH_READ_FILE ----------------------------
226 { # FH_READ_FILE: read a file
228 package FH_READ_FILE;
232 my ( $pkg, $file ) = @_;
233 die "File `$file' cannot be read." unless ( -f $file && -r $file );
237 'opened' => main::FALSE,
248 my $file = $self->{'file'};
249 if ( $file && -e $file ) {
250 die "file $file is not writable" unless ( -r $file );
251 die "$file is a directory" if ( -d $file );
253 open $self->{'fh'}, "<", $self->{'file'}
254 or die "could not read file `$file': $!";
255 $self->{'opened'} = main::TRUE;
260 close $self->{'fh'} if ( $self->{'opened'} );
261 $self->{'opened'} = main::FALSE;
265 # Read 1 line of the file into a chomped string.
266 # Do not close the read handle at the end.
268 $self->open() unless ( $self->{'opened'} );
271 if ( defined($res = CORE::readline($self->{'fh'}) ) ) {
281 # Read the complete file into an array reference.
282 # Close the read handle at the end.
283 # Return array reference.
285 $self->open() unless ( $self->{'opened'} );
289 while ( defined ( $line = CORE::readline $self->{'fh'} ) ) {
294 $self->{'opened'} = main::FALSE;
300 # end of OOP definitions
305 ########################################################################