1
package Image::RenRot::FileUtil;
13
use Image::RenRot::Logging;
15
use vars qw(@ISA @EXPORT);
18
@EXPORT = qw(makedir splitext);
20
########################################################################################
22
# getFileDatLns() gets data from a given file in array of lines. it returns hash
23
# of format: $hash{'field1'}[0] => 1
25
# $hash{'field1'}[N] => fieldN
26
# where N is the number of fields, starting from 1
30
my $file = shift; # name of the file to be processed
32
if (not defined $file or $file eq "") {
33
warnmsg ("Can't read file with empty name!\n");
37
if (open(XXXFILE, "<$file")) {
39
my %xxxData; # splited line hash
40
my @chunks = (); # arr, chunks to be placed to
45
@chunks = split(/\s+/);
46
$xxxData{$chunks[0]}[0] = 1;
47
for ($i = 1; $i < scalar(@chunks); $i++) {
48
$xxxData{$chunks[0]}[$i] = $chunks[$i];
49
dbgmsg (4, "xxxData{$chunks[0]}[$i] = $chunks[$i]\n");
53
unless (close(XXXFILE)) { errmsg ("$file wasn't closed!\n"); }
57
warnmsg ("Can't read file: $file!\n");
61
########################################################################################
63
# getFileDataLines() gets data from a given file in array of lines
65
sub getFileDataLines {
69
if (not defined $file or $file eq "") {
70
warnmsg ("Can't read file with empty name!\n");
74
if (open(XXXFILE, "<$file")) {
76
my @xxxData = <XXXFILE>;
77
unless (close(XXXFILE)) { errmsg ("$file wasn't closed!\n"); }
81
warnmsg ("Can't read file: $file!\n");
85
########################################################################################
87
# getFileData() gets data from a given file in one-line string
93
my @result = $self->getFileDataLines($file);
94
return join ("", @result) if (scalar(@result) > 0);
98
########################################################################################
99
# Usage : makedir($dir);
100
# Purpose : makes one level directory
102
# Parameters : $dir str - directory to make
103
# Throws : no exceptions
108
if (not -d $new_dir) {
109
eval { mkpath($new_dir, 0, 0700) };
111
errmsg ("Couldn't create $new_dir: $@");
116
########################################################################################
118
# Purpose : opens two pipes for process object via the command passed as argument
119
# Returns : $pipe_obj processed via $pipe_cmd
120
# Parameters : $pipe_obj bin - the object to be processed via pipe
121
# : $pipe_cmd str - the command for the processing
122
# Throws : no exceptions
131
my $pipe_obj = shift; # the object to be processed via pipe
132
my $pipe_cmd = shift; # the pipe command
134
local (*READ_FROM_FH, *WRITE_TO_FH); # file handlers
135
unless (open2(\*READ_FROM_FH, \*WRITE_TO_FH, $pipe_cmd)) {
136
errmsg ("Unable to create the pipe.\n");
141
print WRITE_TO_FH $pipe_obj;
143
unless (close(WRITE_TO_FH)) { warnmsg ("WRITE handle wasn't closed!\n"); };
145
binmode READ_FROM_FH;
146
my @piped_arr = <READ_FROM_FH>;
148
unless (close(READ_FROM_FH)) { warnmsg ("READ handle wasn't closed!\n"); };
150
return join("", @piped_arr);
154
my $filename = shift;
156
return ($1, $2) if ($filename =~ m/(.*)\.([^\/\.]+)$/);
157
return ($filename, "");
160
########################################################################################