2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.5 2003/06/06 04:00:35 joko |
6 |
|
## + binary mode file write |
7 |
|
## + don't add a trailing newline always |
8 |
|
## |
9 |
|
## Revision 1.4 2003/05/13 09:23:03 joko |
10 |
|
## pre-flight checks for existance of base directory of to-be-executed script |
11 |
|
## |
12 |
## Revision 1.3 2003/03/31 05:47:01 janosch |
## Revision 1.3 2003/03/31 05:47:01 janosch |
13 |
## Mis mif gex |
## Mis mif gex |
14 |
## |
## |
49 |
|
|
50 |
|
|
51 |
use Data::Dumper; |
use Data::Dumper; |
52 |
|
use File::Basename; |
53 |
|
|
54 |
sub s2f { |
sub s2f { |
55 |
my $filename = shift; |
my $filename = shift; |
56 |
my $string = shift; |
my $string = shift; |
57 |
|
my $args = shift; |
58 |
|
|
59 |
|
# pre-flight checks: Does directory exist? |
60 |
|
my $dirname = dirname($filename); |
61 |
|
if (not -e $dirname) { |
62 |
|
print STDERR __PACKAGE__ . ':' . __LINE__ . ": ERROR: Directory '$dirname' does not exist! (Write attempt)" . "\n"; |
63 |
|
return; |
64 |
|
} |
65 |
|
|
66 |
|
# Perform: File write |
67 |
open(FH, '>' . $filename); |
open(FH, '>' . $filename); |
68 |
|
if ($args->{mode} && $args->{mode} eq 'binary') { |
69 |
|
binmode(FH); |
70 |
|
} |
71 |
print FH $string; |
print FH $string; |
72 |
print FH "\n"; |
# Always inject ending newline? No, since it unneccessarily |
73 |
|
# modifies files with absolutely *no* changes in content. |
74 |
|
print FH "\n" if $string !~ /\n$/; |
75 |
close(FH); |
close(FH); |
76 |
} |
} |
77 |
|
|
78 |
sub f2s { |
sub f2s { |
79 |
my $filename = shift; |
my $filename = shift; |
80 |
|
|
81 |
|
# pre-flight checks: Does file exist? |
82 |
if (! -e $filename) { |
if (! -e $filename) { |
83 |
print STDERR __PACKAGE__ . ':' . __LINE__ . ": File $filename does not exist!" . "\n"; |
print STDERR __PACKAGE__ . ':' . __LINE__ . ": ERROR: File '$filename' does not exist! (Read attempt)" . "\n"; |
84 |
return; |
return; |
85 |
} |
} |
86 |
|
|
87 |
# read file at once (be careful with big files!!!) |
# read file at once (be careful with big files!!!) |
88 |
open(FH, '<' . $filename); |
open(FH, '<' . $filename); |
89 |
my @buf_arr = <FH>; |
my @buf_arr = <FH>; |