File Coverage

blib/lib/Apache/Description.pm
Criterion Covered Total %
statement 19 75 25.3
branch 1 44 2.2
condition 0 9 0.0
subroutine 6 16 37.5
pod 9 11 81.8
total 35 155 22.5


line stmt bran cond sub pod time code
1             package Apache::Description;
2              
3 1     1   27518 use 5.008;
  1         4  
  1         43  
4 1     1   6 use strict;
  1         1  
  1         38  
5 1     1   4 use warnings;
  1         8  
  1         40  
6 1     1   880 use IO::File;
  1         11531  
  1         138  
7 1     1   8 use Carp;
  1         2  
  1         1060  
8              
9             our $VERSION = '0.5';
10              
11             my ($filename, $fh, @prev);
12             my $regexp = <<'END_RE';
13             ^AddDescription
14             \s+
15             ("?) # quote ?
16             (([^"\\]|\\")+) # description
17             \s*
18             \1 # quote ?
19             \s+
20              
21             ("?) # quote ?
22             (([^"\\]|\\")+) # filename
23             \s*
24             \4 # quote ?
25             $ # end of regexp
26             END_RE
27              
28             sub new {
29 1     1 1 12 my $self = shift;
30              
31             ## you can give a filename in argument
32 1         2 $filename = shift;
33 1 50       5 $self->parse if defined $filename;
34              
35 1         3 return bless {}, $self;
36             }
37              
38             ## this subroutine checks the filename
39             sub parse {
40 0     0 1   my $self = shift;
41              
42             ## have we already open a file ?
43 0 0         if ( defined $fh ) {
44 0           carp "$filename is already in use\n";
45              
46             } else {
47 0 0         $filename = $filename ? $filename : shift;
48              
49 0 0 0       if ( (not defined $filename) or (not -e $filename) ) {
50 0           croak "invalid filename : $filename";
51             }
52              
53 0           $self->open();
54             }
55             }
56              
57             ## just open the file .htaccess
58             sub open {
59 0     0 0   $fh = IO::File->new($filename, "r+");
60              
61 0 0         if ( not defined $fh) {
62 0           croak "impossible to open $filename in read-write : $!";
63             }
64             }
65              
66             ## add a description
67             sub add($$){
68 0     0 1   my ($self, $file, $desc) = @_;
69              
70 0           print $fh qq/AddDescription "$desc" "$file"\n/;
71             }
72              
73              
74             ## remove an entry
75             ## this operation is "expensive" : two files are created, and I
76             ## need to parse the whole file.
77             ## if there are more than one directive for the file wanted, they are
78             ## both deleted.
79             sub remove($) {
80 0     0 1   my ($self, $wanted) = @_;
81 0           my $fd;
82              
83 0           $fh->setpos(0);
84 0           $fd = IO::File->new(">/tmp/htaccess.$$");
85            
86 0 0 0       croak "no file descriptor available : $!" unless (defined $fh or not defined $fd);
87              
88 0           while ( <$fh> ) {
89 0 0         chomp if defined;
90              
91 0 0         if ( m/$regexp/xio ) {
92              
93 0 0         if ($5 ne $wanted)
94 0           { print $fd "$_\n" }
95              
96             } else {
97 0           print $fd "$_\n";
98             }
99             }
100              
101 0 0 0       croak "no file descriptor available : $!" if (not defined $fh or not defined $fd);
102            
103 0 0         rename "/tmp/htaccess.$$", $filename
104             or croak "rename(htaccess.$$,$filename) : $!";
105             }
106              
107             ## this function can return an array, or a scalar
108             ## according to the context of the next description.
109             ##
110             ## @ array = ($filename, $description)
111             ##
112             ## $ scal = qq/$filename:$description/
113             ##
114             sub next {
115 0     0 1   my @data;
116              
117 0 0         croak "no file descriptor available" unless defined $fh;
118              
119 0           while ( <$fh> ) {
120 0 0         chomp if defined;
121              
122 0 0         next unless m/$regexp/xio;
123 0           @data = ($5, $2);
124              
125             ## storing the last directive
126 0           @prev = @data;
127 0           last;
128             }
129              
130 0 0         return wantarray ? @data : join ':',@data;
131             }
132              
133             ## return the previous directive.
134             ## it's the same format than next()
135             sub prev {
136 0 0   0 1   return wantarray ? @prev : join ':',@prev;
137             }
138              
139             ## returns all descriptions in a hash reference
140             ##
141             sub getall {
142 0     0 1   my $self = shift;
143 0           my (%hash, $desc);
144              
145 0 0         croak "no file descriptor available" unless defined $fh;
146              
147 0           while ( my ($f, $d) = $self->next() ) {
148 0 0         last if not defined $f;
149              
150 0           $hash{"$f"} = $d;
151             }
152              
153 0           return \%hash;
154             }
155              
156             sub get($) {
157 0     0 1   my $self = shift;
158 0           my $wanted = shift;
159 0           my $ret = undef;
160              
161 0 0         croak "no file descriptor available" unless defined $fh;
162              
163 0           while ( my ($f, $d) = $self->next() ) {
164 0 0         last if not defined $f;
165              
166 0 0         if ( $f eq $wanted) {
167 0           $ret = $d;
168 0           last;
169             }
170             }
171              
172 0           return $ret;
173             }
174              
175             sub rename {
176 0     0 0   print qq/Not implemented yet\n/;
177             }
178              
179             sub ispresent($) {
180 0     0 1   my $self = shift;
181 0           my $file = shift;
182              
183 0 0         return $self->get($file) ? 1 : 0;
184             }
185              
186              
187             1;
188              
189             __END__