| 1 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | package MSDOS::Descript; | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Copyright 1997-2008 Christopher J. Madsen | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Author: Christopher J. Madsen <perl@cjmweb.net> | ||||||||||||||||||||||||||||
| 7 | # Created: 09 Nov 1997 | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 10 | # it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the | ||||||||||||||||||||||||||||
| 15 | # GNU General Public License or the Artistic License for more details. | ||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||
| 17 | # ABSTRACT: Manage 4DOS style DESCRIPT.ION files | ||||||||||||||||||||||||||||
| 18 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | require 5.006; | |||||||||||||||||||||||||||
| 21 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Tie::CPHash (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use vars qw($VERSION $hide_descriptions); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | #===================================================================== | ||||||||||||||||||||||||||||
| 28 | # Package Startup: | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | BEGIN | ||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||
| 32 | 2 | ▶ | $VERSION = '1.04'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # RECOMMEND PREREQ: MSDOS::Attrib | ||||||||||||||||||||||||||||
| 35 | # Try to load MSDOS::Attrib, but keep going without it: | ||||||||||||||||||||||||||||
| 36 | 2 | $hide_descriptions = do { local $@; eval { require MSDOS::Attrib; 1 } }; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | MSDOS::Attrib->import('set_attribs') if $hide_descriptions; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } # end BEGIN | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | #===================================================================== | ||||||||||||||||||||||||||||
| 42 | # Methods: | ||||||||||||||||||||||||||||
| 43 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 44 | # Constructor | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub new | ||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 2 | ▶ | my $self = {}; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 2 | ▶ | $self->{file} = $_[1] || '.'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_[1] || '.'
MC/DC0%0/1
$_[1] || '.'
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | $self->{file} =~ s![/\\]?$!/DESCRIPT.ION! if -d $self->{file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 2 | tie %{$self->{desc}},'Tie::CPHash'; | |||||||||||||||||||||||||||
| 52 | 2 | bless $self, $_[0]; | |||||||||||||||||||||||||||
| 53 | 2 | $self->read; | |||||||||||||||||||||||||||
| 54 | 2 | return $self; | |||||||||||||||||||||||||||
| 55 | } # end new | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 58 | # Destructor: | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub DESTROY | ||||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||
| 62 | 2 | ▶ | $_[0]->update if $_[0]->{autoupdate}; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | } # end DESTROY | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 66 | # Enable or disable automatic updates: | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub autoupdate | ||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $_[0]->{autoupdate} = (($#_ > 0) ? $_[1] : 1); | ||||||||||||||||||||||||||
|
Subroutine
autoupdate: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } # end autoupdate | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 74 | # Return true if the descriptions have changed: | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub changed | ||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 6 | ▶ | $_[0]->{changed}; | ||||||||||||||||||||||||||
|
Subroutine
changed: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | } # end changed | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 82 | # Read or update the description for a file: | ||||||||||||||||||||||||||||
| 83 | # | ||||||||||||||||||||||||||||
| 84 | # If DESC is the null string or undef, then delete FILE's description. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub description | ||||||||||||||||||||||||||||
| 87 | { | ||||||||||||||||||||||||||||
| 88 | 28 | ▶ | my ($self, $file, $desc) = @_; | ||||||||||||||||||||||||||
|
Subroutine
description: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 28 | $file =~ s/\.+$//; # Trailing dots don't count in MS-DOS | |||||||||||||||||||||||||||
| 91 | 28 | ▶ | if ($#_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 3 | my $old = $self->{desc}{$file}; | |||||||||||||||||||||||||||
| 93 | 3 | ▶ | if (not defined($desc) or $desc eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $desc or $desc eq ""
MC/DC100%2/2
not defined $desc or $desc eq ""
not defined $desc
$desc eq ""
Truth table100%3/3
not(A) or B
A= not defined $descB= $desc eq ""
| |||||||||||||||||||||||||||||
| 94 | 2 | ▶ | $self->{changed} = 1 if defined delete $self->{desc}{$file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||
| 96 | 1 | $self->{desc}{$file} = $desc; | |||||||||||||||||||||||||||
| 97 | 1 | ▶ | $self->{changed} = 1 if not defined $old or $old ne $desc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $old or $old ne $desc
MC/DC0%0/2
not defined $old or $old ne $desc
not defined $old
$old ne $desc
Truth table33%1/3
not(A) or B
A= not defined $oldB= $old ne $desc
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 3 | return $old; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 25 | $self->{desc}{$file}; | |||||||||||||||||||||||||||
| 102 | } # end description | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 105 | # Transfer the description when a file is renamed: | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub rename | ||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||
| 109 | 1 | ▶ | my ($self, $old, $new) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rename: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 1 | $old =~ s/\.+$//; # Trailing dots don't count in MS-DOS | |||||||||||||||||||||||||||
| 111 | 1 | $new =~ s/\.+$//; | |||||||||||||||||||||||||||
| 112 | 1 | my $desc = delete $self->{desc}{$old}; | |||||||||||||||||||||||||||
| 113 | 1 | ▶ | if (defined $desc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 1 | $self->{desc}{$new} = $desc; | |||||||||||||||||||||||||||
| 115 | 1 | $self->{changed} = 1; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } # end rename | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 120 | # Read the 4DOS description file: | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub read | ||||||||||||||||||||||||||||
| 123 | { | ||||||||||||||||||||||||||||
| 124 | 2 | ▶ | my ($self,$in) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 2 | ▶ | $in = $self->{file} unless $in; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | %{$self->{desc}} = (); | |||||||||||||||||||||||||||
| 128 | 2 | $self->read_add($in); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 2 | ▶ | delete $self->{changed} if $in eq $self->{file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | } # end read | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 134 | # Add descriptions from a file to the current database: | ||||||||||||||||||||||||||||
| 135 | # | ||||||||||||||||||||||||||||
| 136 | # Input: | ||||||||||||||||||||||||||||
| 137 | # IN: The name of the file to read | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | sub read_add | ||||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||||
| 141 | 2 | ▶ | my ($self,$in) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read_add: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 2 | ▶ | if (-r $in) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 2 | ▶ | open(DESCRIPT, $in) or croak "Unable to open $in"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 2 | ▶ | while (<DESCRIPT>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 10 | ▶ | m/^\"([^\"]+)\" (.+)$/ or m/^([^ ]+) (.+)$/ or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
/^\"([^\"]+)\" (.+)$/ or /^([^ ]+) (.+)$/
MC/DC0%0/2
/^\"([^\"]+)\" (.+)$/ or /^([^ ]+) (.+)$/
/^\"([^\"]+)\" (.+)$/
/^([^ ]+) (.+)$/
Truth table66%2/3
A or B
A= /^\"([^\"]+)\" (.+)$/B= /^([^ ]+) (.+)$/
| |||||||||||||||||||||||||||||
| 147 | 10 | $self->{desc}{$1} = $2; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 2 | close DESCRIPT; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 2 | $self->{changed} = 1; | |||||||||||||||||||||||||||
| 153 | } # end read_add | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 156 | # Write the 4DOS description file: | ||||||||||||||||||||||||||||
| 157 | # | ||||||||||||||||||||||||||||
| 158 | # Sets the CHANGED flag to 0 if writing to our FILE. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | sub write | ||||||||||||||||||||||||||||
| 161 | { | ||||||||||||||||||||||||||||
| 162 | 1 | ▶ | my ($self, $out) = @_; | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=9 SCAR=22.6
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 1 | ▶ | $out = $self->{file} unless $out; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 1 | my ($file, $desc); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 1 | unlink $out; | |||||||||||||||||||||||||||
| 167 | 1 | ▶ | if (keys %{$self->{desc}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 1 | ▶ | open(DESCRIPT,">$out") or croak "Unable to open $out for writing"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 1 | ▶ | while (($file,$desc) = each %{$self->{desc}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 4 | ▶ | next unless $desc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 4 | ▶ | $file = '"' . $file . '"' if $file =~ /\s/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 4 | print DESCRIPT $file,' ',$desc,"\n"; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 1 | close DESCRIPT; | |||||||||||||||||||||||||||
| 175 | 1 | ▶ | set_attribs('+h',$out) if $hide_descriptions; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 1 | ▶ | $self->{changed} = 0 if $out eq $self->{file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | } # end write | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 181 | # Save changes to descriptions: | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | sub update | ||||||||||||||||||||||||||||
| 184 | { | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | $_[0]->write if $_[0]->changed; | ||||||||||||||||||||||||||
|
Subroutine
update: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | } # end update | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | #===================================================================== | ||||||||||||||||||||||||||||
| 189 | # Package Return Value: | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 2 | 1; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||||||||||