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
|
|
39566
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
90
|
|
22
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
70
|
|
23
|
2
|
|
|
2
|
|
11
|
use Carp qw(croak); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
144
|
|
24
|
2
|
|
|
2
|
|
1120
|
use Tie::CPHash (); |
|
2
|
|
|
|
|
1023
|
|
|
2
|
|
|
|
|
51
|
|
25
|
2
|
|
|
2
|
|
11
|
use vars qw($VERSION $hide_descriptions); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
187
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#===================================================================== |
28
|
|
|
|
|
|
|
# Package Startup: |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
BEGIN |
31
|
|
|
|
|
|
|
{ |
32
|
2
|
|
|
2
|
|
4
|
$VERSION = '1.05'; |
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
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
415
|
|
|
0
|
|
|
|
|
0
|
|
37
|
|
|
|
|
|
|
|
38
|
2
|
50
|
|
|
|
1856
|
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
|
45
|
my $self = {}; |
49
|
2
|
|
50
|
|
|
8
|
$self->{file} = $_[1] || '.'; |
50
|
2
|
50
|
|
|
|
29
|
$self->{file} =~ s![/\\]?$!/DESCRIPT.ION! if -d $self->{file}; |
51
|
2
|
|
|
|
|
3
|
tie %{$self->{desc}},'Tie::CPHash'; |
|
2
|
|
|
|
|
14
|
|
52
|
2
|
|
|
|
|
11
|
bless $self, $_[0]; |
53
|
2
|
|
|
|
|
7
|
$self->read; |
54
|
2
|
|
|
|
|
6
|
return $self; |
55
|
|
|
|
|
|
|
} # end new |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
58
|
|
|
|
|
|
|
# Destructor: |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub DESTROY |
61
|
|
|
|
|
|
|
{ |
62
|
2
|
50
|
|
2
|
|
506
|
$_[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
|
1877
|
$_[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
|
7794
|
my ($self, $file, $desc) = @_; |
89
|
|
|
|
|
|
|
|
90
|
28
|
|
|
|
|
47
|
$file =~ s/\.+$//; # Trailing dots don't count in MS-DOS |
91
|
28
|
100
|
|
|
|
60
|
if ($#_ > 1) { |
92
|
3
|
|
|
|
|
14
|
my $old = $self->{desc}{$file}; |
93
|
3
|
100
|
100
|
|
|
27
|
if (not defined($desc) or $desc eq '') { |
94
|
2
|
50
|
|
|
|
6
|
$self->{changed} = 1 if defined delete $self->{desc}{$file}; |
95
|
|
|
|
|
|
|
} else { |
96
|
1
|
|
|
|
|
3
|
$self->{desc}{$file} = $desc; |
97
|
1
|
50
|
33
|
|
|
11
|
$self->{changed} = 1 if not defined $old or $old ne $desc; |
98
|
|
|
|
|
|
|
} |
99
|
3
|
|
|
|
|
18
|
return $old; |
100
|
|
|
|
|
|
|
} |
101
|
25
|
|
|
|
|
104
|
$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
|
3
|
my ($self, $old, $new) = @_; |
110
|
1
|
|
|
|
|
4
|
$old =~ s/\.+$//; # Trailing dots don't count in MS-DOS |
111
|
1
|
|
|
|
|
3
|
$new =~ s/\.+$//; |
112
|
1
|
|
|
|
|
9
|
my $desc = delete $self->{desc}{$old}; |
113
|
1
|
50
|
|
|
|
35
|
if (defined $desc) { |
114
|
1
|
|
|
|
|
8
|
$self->{desc}{$new} = $desc; |
115
|
1
|
|
|
|
|
10
|
$self->{changed} = 1; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} # end rename |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
#--------------------------------------------------------------------- |
120
|
|
|
|
|
|
|
# Read the 4DOS description file: |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub read |
123
|
|
|
|
|
|
|
{ |
124
|
2
|
|
|
2
|
1
|
3
|
my ($self,$in) = @_; |
125
|
2
|
50
|
|
|
|
9
|
$in = $self->{file} unless $in; |
126
|
|
|
|
|
|
|
|
127
|
2
|
|
|
|
|
2
|
%{$self->{desc}} = (); |
|
2
|
|
|
|
|
9
|
|
128
|
2
|
|
|
|
|
16
|
$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
|
3
|
my ($self,$in) = @_; |
142
|
|
|
|
|
|
|
|
143
|
2
|
50
|
|
|
|
19
|
if (-r $in) { |
144
|
2
|
50
|
|
|
|
36
|
open(DESCRIPT, $in) or croak "Unable to open $in"; |
145
|
2
|
|
|
|
|
37
|
while () { |
146
|
10
|
50
|
66
|
|
|
105
|
m/^\"([^\"]+)\" (.+)$/ or m/^([^ ]+) (.+)$/ or die; |
147
|
10
|
|
|
|
|
45
|
$self->{desc}{$1} = $2; |
148
|
|
|
|
|
|
|
} |
149
|
2
|
|
|
|
|
28
|
close DESCRIPT; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
2
|
|
|
|
|
4
|
$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
|
1
|
my ($self, $out) = @_; |
163
|
1
|
50
|
|
|
|
472
|
$out = $self->{file} unless $out; |
164
|
1
|
|
|
|
|
2
|
my ($file, $desc); |
165
|
|
|
|
|
|
|
|
166
|
1
|
|
|
|
|
25
|
unlink $out; |
167
|
1
|
50
|
|
|
|
2
|
if (keys %{$self->{desc}}) { |
|
1
|
|
|
|
|
7
|
|
168
|
1
|
50
|
|
|
|
108
|
open(DESCRIPT,">$out") or croak "Unable to open $out for writing"; |
169
|
1
|
|
|
|
|
3
|
while (($file,$desc) = each %{$self->{desc}}) { |
|
5
|
|
|
|
|
11
|
|
170
|
4
|
50
|
|
|
|
42
|
next unless $desc; |
171
|
4
|
100
|
|
|
|
11
|
$file = '"' . $file . '"' if $file =~ /\s/; |
172
|
4
|
|
|
|
|
9
|
print DESCRIPT $file,' ',$desc,"\n"; |
173
|
|
|
|
|
|
|
} |
174
|
1
|
|
|
|
|
45
|
close DESCRIPT; |
175
|
1
|
50
|
|
|
|
4
|
set_attribs('+h',$out) if $hide_descriptions; |
176
|
|
|
|
|
|
|
} |
177
|
1
|
50
|
|
|
|
6
|
$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__ |