| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Win32::AbsPath; |
|
2
|
|
|
|
|
|
|
require Exporter; |
|
3
|
|
|
|
|
|
|
@ISA = (Exporter); |
|
4
|
|
|
|
|
|
|
@EXPORT = qw(); #&new); |
|
5
|
|
|
|
|
|
|
@EXPORT_OK = qw(RelativeToAbsolute Relative2Absolute canonpath FixPath FixPaths FullPath FullPaths); |
|
6
|
1
|
|
|
1
|
|
891
|
use Cwd; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
1266
|
|
|
7
|
|
|
|
|
|
|
$VERSION = '1.0'; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub Relative2Absolute { |
|
10
|
0
|
|
|
0
|
1
|
|
local $_; |
|
11
|
0
|
|
|
|
|
|
foreach $_ (@_) { |
|
12
|
|
|
|
|
|
|
#print "DO: $_\n"; |
|
13
|
0
|
|
|
|
|
|
my $root; |
|
14
|
0
|
|
|
|
|
|
s#\\#/#g; |
|
15
|
0
|
0
|
|
|
|
|
if (m#^(\w:)(/.*)$#) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
16
|
0
|
|
|
|
|
|
$root = $1; |
|
17
|
0
|
|
|
|
|
|
$_ = $2; |
|
18
|
|
|
|
|
|
|
} elsif (m#^(//[^/]+/[^/]+)(.*)#) { |
|
19
|
0
|
|
|
|
|
|
$root = $1; |
|
20
|
0
|
|
|
|
|
|
$_ = $2; |
|
21
|
|
|
|
|
|
|
} elsif (m#^(/.*)#) { |
|
22
|
0
|
|
|
|
|
|
$root = getcwd(); |
|
23
|
0
|
|
|
|
|
|
$root =~ s#^(\w:|//[^/]+/[^/]+).*#$1#; |
|
24
|
|
|
|
|
|
|
} elsif (m#^(\w:)(.*)$#) { |
|
25
|
0
|
|
|
|
|
|
$root = $1; |
|
26
|
0
|
|
|
|
|
|
$_ = $2; |
|
27
|
0
|
|
|
|
|
|
my $oldcwd = getcwd(); |
|
28
|
0
|
0
|
|
|
|
|
chdir($root) or return; |
|
29
|
0
|
|
|
|
|
|
$_ = substr( getcwd(), 2).'/'.$_; |
|
30
|
0
|
0
|
|
|
|
|
chdir($oldcwd) or return; |
|
31
|
|
|
|
|
|
|
} else { |
|
32
|
0
|
|
|
|
|
|
$_ = getcwd().'/'.$_; |
|
33
|
0
|
|
|
|
|
|
($root,$_) = m#^(\w:|//[^/]+/[^/]+)(.*)$#; |
|
34
|
|
|
|
|
|
|
} |
|
35
|
0
|
|
|
|
|
|
s#//+#/#g; |
|
36
|
0
|
|
|
|
|
|
s#(/\.)+(?=/|$)##g; |
|
37
|
0
|
|
|
|
|
|
s#/\.\.(\.+)(?=/|$)#'/..'x(length($1)+1)#ge; |
|
|
0
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
while(s{/[^/]+/\.\.(/|$)}{/}){}; |
|
39
|
0
|
|
|
|
|
|
s#(/\.\.)*/?$##; |
|
40
|
0
|
|
|
|
|
|
s#^(/\.\.)+##; |
|
41
|
0
|
|
|
|
|
|
$_=$root.$_; |
|
42
|
0
|
|
|
|
|
|
s#^(\w:)$#$1\\#; |
|
43
|
0
|
|
|
|
|
|
s#/#\\#g; |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
@_ |
|
46
|
0
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
*FixPaths = \&Relative2Absolute; |
|
48
|
|
|
|
|
|
|
*FullPaths=\&Relative2Absolute; |
|
49
|
|
|
|
|
|
|
*rel2abs = \&Relative2Absolute; |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
*canonpath=\&RelativeToAbsolute; |
|
52
|
|
|
|
|
|
|
*Fix=\&RelativeToAbsolute; |
|
53
|
|
|
|
|
|
|
*FixPath=\&RelativeToAbsolute; |
|
54
|
|
|
|
|
|
|
*FullPath=\&RelativeToAbsolute; |
|
55
|
|
|
|
|
|
|
*reltoabs = \&RelativeToAbsolute; |
|
56
|
|
|
|
|
|
|
sub RelativeToAbsolute ($) { |
|
57
|
0
|
|
|
0
|
1
|
|
my $str = shift; |
|
58
|
0
|
|
|
|
|
|
Relative2Absolute $str; |
|
59
|
0
|
|
|
|
|
|
$str; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
1; |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 NAME |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Win32::AbsPath - convert relative to absolute paths |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Version 1.0 |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
use Win32::AbsPath; |
|
73
|
|
|
|
|
|
|
$path = Win32::AbsPath::Fix '../some\dir\file.doc' |
|
74
|
|
|
|
|
|
|
system("winword $path"); |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
use Win32::AbsPath qw(Relative2Absolute); |
|
77
|
|
|
|
|
|
|
@paths = qw( |
|
78
|
|
|
|
|
|
|
..\dir\file.txt |
|
79
|
|
|
|
|
|
|
./other.doc |
|
80
|
|
|
|
|
|
|
c:\boot.ini |
|
81
|
|
|
|
|
|
|
); |
|
82
|
|
|
|
|
|
|
Relative2Absolute @paths; |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Convert relative paths to absolute. Understands UNC paths. |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
The functions understands many different types of paths |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
dir\file.txt |
|
91
|
|
|
|
|
|
|
..\dir\file.txt |
|
92
|
|
|
|
|
|
|
c:\dir\file.txt |
|
93
|
|
|
|
|
|
|
c:\dir\..\file.txt |
|
94
|
|
|
|
|
|
|
\dir\file.txt |
|
95
|
|
|
|
|
|
|
\\server\share\dir\..\file.txt |
|
96
|
|
|
|
|
|
|
c:dir\file.txt |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
and of course you may pepper these with whatever mixtures of \.\ and |
|
99
|
|
|
|
|
|
|
\..\ you like. You may use both forward and backward slashes, the result |
|
100
|
|
|
|
|
|
|
will be in backward slashes. |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
! The ussage of paths of type c:file.txt is slightly deprecated. It IS |
|
103
|
|
|
|
|
|
|
supported, but may lead to a change of current directory. The function |
|
104
|
|
|
|
|
|
|
first chdir()s top the current directory on the drive mentioned in the |
|
105
|
|
|
|
|
|
|
path and then back to cwd() in time it was called. If any of those |
|
106
|
|
|
|
|
|
|
chdir()s fails, the result of the function will be undef. |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
This is likely to happen if one of the drives is a floppy or CD, or |
|
109
|
|
|
|
|
|
|
if one of the drives was a network drive and was disconnected. |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 Functions |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=over 2 |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=item Relative2Absolute |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Relative2Absolute @list; |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Converts all paths in @list to absolute paths C. |
|
120
|
|
|
|
|
|
|
That is the function changes the list you pass in. |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item RelativeToAbsolute |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
$abspath = RelativeToAbsolute $relpath; |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Converts the relative path to absolute. Returns the absolute path, but |
|
127
|
|
|
|
|
|
|
doesn't change the parameter. It takes exactly one parameter! |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
print join(' ',RelativeToAbsolute '_file.txt','_other.txt'); |
|
130
|
|
|
|
|
|
|
prints |
|
131
|
|
|
|
|
|
|
c:\_file.txt _other.txt |
|
132
|
|
|
|
|
|
|
instead of |
|
133
|
|
|
|
|
|
|
c:\_file.txt c:\_other.txt |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item Win32::AbsPath::Fix $path |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
The same as RelativeToAbsolute. |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=back |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 AUTHOR |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
and Mike |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |