line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
2
|
|
|
|
|
|
|
package MSDOS::Descript; |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Copyright 1997-2008 Christopher J. Madsen |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Author: Christopher J. Madsen |
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
|
|
|
|
|
|
|
require 5.006; |
21
|
2
|
|
|
2
|
|
72819
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
77
|
|
22
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
61
|
|
23
|
2
|
|
|
2
|
|
10
|
use Carp qw(croak); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
143
|
|
24
|
2
|
|
|
2
|
|
1873
|
use Tie::CPHash (); |
|
2
|
|
|
|
|
1025
|
|
|
2
|
|
|
|
|
51
|
|
25
|
2
|
|
|
2
|
|
14
|
use vars qw($VERSION $hide_descriptions); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
224
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#===================================================================== |
28
|
|
|
|
|
|
|
# Package Startup: |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
BEGIN |
31
|
|
|
|
|
|
|
{ |
32
|
2
|
|
|
2
|
|
6
|
$VERSION = '1.04'; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# RECOMMEND PREREQ: MSDOS::Attrib |
35
|
|
|
|
|
|
|
# Try to load MSDOS::Attrib, but keep going without it: |
36
|
2
|
|
|
|
|
3
|
$hide_descriptions = do { local $@; eval { require MSDOS::Attrib; 1 } }; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
779
|
|
|
0
|
|
|
|
|
0
|
|
37
|
|
|
|
|
|
|
|
38
|
2
|
50
|
|
|
|
1977
|
MSDOS::Attrib->import('set_attribs') if $hide_descriptions; |
39
|
|
|
|
|
|
|
} # end BEGIN |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
#===================================================================== |
42
|
|
|
|
|
|
|
# Methods: |
43
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
44
|
|
|
|
|
|
|
# Constructor |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub new |
47
|
|
|
|
|
|
|
{ |
48
|
2
|
|
|
2
|
1
|
69
|
my $self = {}; |
49
|
2
|
|
50
|
|
|
11
|
$self->{file} = $_[1] || '.'; |
50
|
2
|
50
|
|
|
|
39
|
$self->{file} =~ s![/\\]?$!/DESCRIPT.ION! if -d $self->{file}; |
51
|
2
|
|
|
|
|
4
|
tie %{$self->{desc}},'Tie::CPHash'; |
|
2
|
|
|
|
|
16
|
|
52
|
2
|
|
|
|
|
13
|
bless $self, $_[0]; |
53
|
2
|
|
|
|
|
8
|
$self->read; |
54
|
2
|
|
|
|
|
5
|
return $self; |
55
|
|
|
|
|
|
|
} # end new |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
58
|
|
|
|
|
|
|
# Destructor: |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub DESTROY |
61
|
|
|
|
|
|
|
{ |
62
|
2
|
50
|
|
2
|
|
598
|
$_[0]->update if $_[0]->{autoupdate}; |
63
|
|
|
|
|
|
|
} # end DESTROY |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
66
|
|
|
|
|
|
|
# Enable or disable automatic updates: |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub autoupdate |
69
|
|
|
|
|
|
|
{ |
70
|
0
|
0
|
|
0
|
1
|
0
|
$_[0]->{autoupdate} = (($#_ > 0) ? $_[1] : 1); |
71
|
|
|
|
|
|
|
} # end autoupdate |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
74
|
|
|
|
|
|
|
# Return true if the descriptions have changed: |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub changed |
77
|
|
|
|
|
|
|
{ |
78
|
6
|
|
|
6
|
1
|
1503
|
$_[0]->{changed}; |
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
|
|
|
28
|
1
|
5596
|
my ($self, $file, $desc) = @_; |
89
|
|
|
|
|
|
|
|
90
|
28
|
|
|
|
|
41
|
$file =~ s/\.+$//; # Trailing dots don't count in MS-DOS |
91
|
28
|
100
|
|
|
|
58
|
if ($#_ > 1) { |
92
|
3
|
|
|
|
|
12
|
my $old = $self->{desc}{$file}; |
93
|
3
|
100
|
100
|
|
|
31
|
if (not defined($desc) or $desc eq '') { |
94
|
2
|
50
|
|
|
|
9
|
$self->{changed} = 1 if defined delete $self->{desc}{$file}; |
95
|
|
|
|
|
|
|
} else { |
96
|
1
|
|
|
|
|
5
|
$self->{desc}{$file} = $desc; |
97
|
1
|
50
|
33
|
|
|
18
|
$self->{changed} = 1 if not defined $old or $old ne $desc; |
98
|
|
|
|
|
|
|
} |
99
|
3
|
|
|
|
|
22
|
return $old; |
100
|
|
|
|
|
|
|
} |
101
|
25
|
|
|
|
|
100
|
$self->{desc}{$file}; |
102
|
|
|
|
|
|
|
} # end description |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
105
|
|
|
|
|
|
|
# Transfer the description when a file is renamed: |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub rename |
108
|
|
|
|
|
|
|
{ |
109
|
1
|
|
|
1
|
1
|
2
|
my ($self, $old, $new) = @_; |
110
|
1
|
|
|
|
|
2
|
$old =~ s/\.+$//; # Trailing dots don't count in MS-DOS |
111
|
1
|
|
|
|
|
2
|
$new =~ s/\.+$//; |
112
|
1
|
|
|
|
|
6
|
my $desc = delete $self->{desc}{$old}; |
113
|
1
|
50
|
|
|
|
26
|
if (defined $desc) { |
114
|
1
|
|
|
|
|
4
|
$self->{desc}{$new} = $desc; |
115
|
1
|
|
|
|
|
7
|
$self->{changed} = 1; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} # end rename |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
120
|
|
|
|
|
|
|
# Read the 4DOS description file: |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub read |
123
|
|
|
|
|
|
|
{ |
124
|
2
|
|
|
2
|
1
|
4
|
my ($self,$in) = @_; |
125
|
2
|
50
|
|
|
|
11
|
$in = $self->{file} unless $in; |
126
|
|
|
|
|
|
|
|
127
|
2
|
|
|
|
|
3
|
%{$self->{desc}} = (); |
|
2
|
|
|
|
|
13
|
|
128
|
2
|
|
|
|
|
17
|
$self->read_add($in); |
129
|
|
|
|
|
|
|
|
130
|
2
|
50
|
|
|
|
9
|
delete $self->{changed} if $in eq $self->{file}; |
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
|
|
|
2
|
1
|
4
|
my ($self,$in) = @_; |
142
|
|
|
|
|
|
|
|
143
|
2
|
50
|
|
|
|
25
|
if (-r $in) { |
144
|
2
|
50
|
|
|
|
65
|
open(DESCRIPT, $in) or croak "Unable to open $in"; |
145
|
2
|
|
|
|
|
49
|
while () { |
146
|
10
|
50
|
66
|
|
|
120
|
m/^\"([^\"]+)\" (.+)$/ or m/^([^ ]+) (.+)$/ or die; |
147
|
10
|
|
|
|
|
53
|
$self->{desc}{$1} = $2; |
148
|
|
|
|
|
|
|
} |
149
|
2
|
|
|
|
|
37
|
close DESCRIPT; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
2
|
|
|
|
|
6
|
$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
|
|
|
1
|
1
|
3
|
my ($self, $out) = @_; |
163
|
1
|
50
|
|
|
|
3
|
$out = $self->{file} unless $out; |
164
|
1
|
|
|
|
|
2
|
my ($file, $desc); |
165
|
|
|
|
|
|
|
|
166
|
1
|
|
|
|
|
35
|
unlink $out; |
167
|
1
|
50
|
|
|
|
2
|
if (keys %{$self->{desc}}) { |
|
1
|
|
|
|
|
9
|
|
168
|
1
|
50
|
|
|
|
165
|
open(DESCRIPT,">$out") or croak "Unable to open $out for writing"; |
169
|
1
|
|
|
|
|
3
|
while (($file,$desc) = each %{$self->{desc}}) { |
|
5
|
|
|
|
|
13
|
|
170
|
4
|
50
|
|
|
|
51
|
next unless $desc; |
171
|
4
|
100
|
|
|
|
11
|
$file = '"' . $file . '"' if $file =~ /\s/; |
172
|
4
|
|
|
|
|
10
|
print DESCRIPT $file,' ',$desc,"\n"; |
173
|
|
|
|
|
|
|
} |
174
|
1
|
|
|
|
|
60
|
close DESCRIPT; |
175
|
1
|
50
|
|
|
|
4
|
set_attribs('+h',$out) if $hide_descriptions; |
176
|
|
|
|
|
|
|
} |
177
|
1
|
50
|
|
|
|
5
|
$self->{changed} = 0 if $out eq $self->{file}; |
178
|
|
|
|
|
|
|
} # end write |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
181
|
|
|
|
|
|
|
# Save changes to descriptions: |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub update |
184
|
|
|
|
|
|
|
{ |
185
|
0
|
0
|
|
0
|
1
|
|
$_[0]->write if $_[0]->changed; |
186
|
|
|
|
|
|
|
} # end update |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
#===================================================================== |
189
|
|
|
|
|
|
|
# Package Return Value: |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
1; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
__END__ |