--- nfo/perl/libs/shortcuts/files.pm 2003/03/31 05:47:01 1.3 +++ nfo/perl/libs/shortcuts/files.pm 2003/06/06 04:00:35 1.5 @@ -1,7 +1,14 @@ ## --------------------------------------------------------------------------- -## $Id: files.pm,v 1.3 2003/03/31 05:47:01 janosch Exp $ +## $Id: files.pm,v 1.5 2003/06/06 04:00:35 joko Exp $ ## --------------------------------------------------------------------------- ## $Log: files.pm,v $ +## Revision 1.5 2003/06/06 04:00:35 joko +## + binary mode file write +## + don't add a trailing newline always +## +## Revision 1.4 2003/05/13 09:23:03 joko +## pre-flight checks for existance of base directory of to-be-executed script +## ## Revision 1.3 2003/03/31 05:47:01 janosch ## Mis mif gex ## @@ -42,22 +49,41 @@ use Data::Dumper; +use File::Basename; sub s2f { my $filename = shift; my $string = shift; + my $args = shift; + + # pre-flight checks: Does directory exist? + my $dirname = dirname($filename); + if (not -e $dirname) { + print STDERR __PACKAGE__ . ':' . __LINE__ . ": ERROR: Directory '$dirname' does not exist! (Write attempt)" . "\n"; + return; + } + + # Perform: File write open(FH, '>' . $filename); + if ($args->{mode} && $args->{mode} eq 'binary') { + binmode(FH); + } print FH $string; - print FH "\n"; + # Always inject ending newline? No, since it unneccessarily + # modifies files with absolutely *no* changes in content. + print FH "\n" if $string !~ /\n$/; close(FH); } sub f2s { my $filename = shift; + + # pre-flight checks: Does file exist? if (! -e $filename) { - print STDERR __PACKAGE__ . ':' . __LINE__ . ": File $filename does not exist!" . "\n"; + print STDERR __PACKAGE__ . ':' . __LINE__ . ": ERROR: File '$filename' does not exist! (Read attempt)" . "\n"; return; } + # read file at once (be careful with big files!!!) open(FH, '<' . $filename); my @buf_arr = ;