File: FileUtil.pm

package info (click to toggle)
renrot 1.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 392 kB
  • sloc: perl: 1,722; makefile: 2
file content (161 lines) | stat: -rw-r--r-- 4,143 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package Image::RenRot::FileUtil;

#
# vim: ts=2 sw=2 et :
#

use strict;
use warnings;
require 5.006;
require Exporter;
use File::Path;

use Image::RenRot::Logging;

use vars qw(@ISA @EXPORT);

@ISA = qw(Exporter);
@EXPORT = qw(makedir splitext);

########################################################################################
#
# getFileDatLns() gets data from a given file in array of lines. it returns hash
#                 of format: $hash{'field1'}[0] => 1
#                            ...
#                            $hash{'field1'}[N] => fieldN
#                 where N is the number of fields, starting from 1
#
sub getFileDatLns {
  my $self = shift;
  my $file = shift; # name of the file to be processed

  if (not defined $file or $file eq "") {
    warnmsg ("Can't read file with empty name!\n");
    return;
  }

  if (open(XXXFILE, "<$file")) {
    binmode XXXFILE;
    my %xxxData; # splited line hash
    my @chunks = ();  # arr, chunks to be placed to
    my ($i, $j);

    while (<XXXFILE>){
      chomp;
      @chunks = split(/\s+/);
      $xxxData{$chunks[0]}[0] = 1;
      for ($i = 1; $i < scalar(@chunks); $i++) {
        $xxxData{$chunks[0]}[$i] = $chunks[$i];
        dbgmsg (4, "xxxData{$chunks[0]}[$i] = $chunks[$i]\n");
      }
      undef @chunks;
    }
    unless (close(XXXFILE)) { errmsg ("$file wasn't closed!\n"); }
    return \%xxxData;
  }

  warnmsg ("Can't read file: $file!\n");
  return;
}

########################################################################################
#
# getFileDataLines() gets data from a given file in array of lines
#
sub getFileDataLines {
  my $self = shift;
  my $file = shift;

  if (not defined $file or $file eq "") {
    warnmsg ("Can't read file with empty name!\n");
    return;
  }

  if (open(XXXFILE, "<$file")) {
    binmode XXXFILE;
    my @xxxData = <XXXFILE>;
    unless (close(XXXFILE)) { errmsg ("$file wasn't closed!\n"); }
    return @xxxData;
  }

  warnmsg ("Can't read file: $file!\n");
  return;
}

########################################################################################
#
# getFileData() gets data from a given file in one-line string
#
sub getFileData {
  my $self = shift;
  my $file = shift;

  my @result = $self->getFileDataLines($file);
  return join ("", @result) if (scalar(@result) > 0);
  return undef;
}

########################################################################################
# Usage      : makedir($dir);
# Purpose    : makes one level directory
# Returns    : none
# Parameters : $dir str - directory to make
# Throws     : no exceptions
# Comments   : none
# See Also   : n/a
sub makedir {
  my $new_dir = shift;
  if (not -d $new_dir) {
    eval { mkpath($new_dir, 0, 0700) };
    if ($@) {
      errmsg ("Couldn't create $new_dir: $@");
    }
  }
}

########################################################################################
# Usage      : piper();
# Purpose    : opens two pipes for process object via the command passed as argument
# Returns    : $pipe_obj processed via $pipe_cmd
# Parameters : $pipe_obj bin - the object to be processed via pipe
#            : $pipe_cmd str - the command for the processing
# Throws     : no exceptions
# Comments   : none
# See Also   : n/a
sub piper {
  use FileHandle;
  use IPC::Open2;

  my $self = shift;

  my $pipe_obj = shift;	# the object to be processed via pipe
  my $pipe_cmd = shift;	# the pipe command

  local (*READ_FROM_FH, *WRITE_TO_FH);	# file handlers
  unless (open2(\*READ_FROM_FH, \*WRITE_TO_FH, $pipe_cmd)) {
    errmsg ("Unable to create the pipe.\n");
    return;
  }

  binmode WRITE_TO_FH;
  print WRITE_TO_FH $pipe_obj;

  unless (close(WRITE_TO_FH)) { warnmsg ("WRITE handle wasn't closed!\n"); };

  binmode READ_FROM_FH;
  my @piped_arr = <READ_FROM_FH>;

  unless (close(READ_FROM_FH)) { warnmsg ("READ handle wasn't closed!\n"); };

  return join("", @piped_arr);
}

sub splitext {
  my $filename = shift;

  return ($1, $2) if ($filename =~ m/(.*)\.([^\/\.]+)$/);
  return ($filename, "");
}

########################################################################################
1;  # end