1 package DirectoryManager
;
3 # ************************************************************
4 # Description : This module provides directory related methods
5 # Author : Chad Elliott
6 # Create Date : 5/13/2004
7 # ************************************************************
9 # ************************************************************
11 # ************************************************************
17 # ************************************************************
19 # ************************************************************
21 my $onVMS = ($^O
eq 'VMS');
22 my $case_insensitive = File
::Spec
->case_tolerant();
23 my $cwd = Cwd
::getcwd
();
24 if ($^O
eq 'cygwin' && $cwd !~ /[A-Za-z]:/) {
25 my $cyg = `cygpath -w $cwd`;
30 $case_insensitive = 1;
32 elsif ($^O
eq 'msys' && $cwd !~ /[A-Za-z]:/) {
33 my $mp = Win32
::GetCwd
();
40 $cwd = VMS
::Filespec
::unixify
($cwd);
45 # ************************************************************
47 # ************************************************************
51 my $status = chdir($dir);
53 if ($status && $dir ne '.') {
54 ## First strip out any /./ or ./ or /.
55 $dir =~ s/\/\.\//\//g
;
59 ## If the new directory contains a relative directory
60 ## then we just get the real working directory
61 if (index($dir, '..') >= 0) {
63 if ($^O
eq 'cygwin' && $cwd !~ /[A-Za-z]:/) {
64 ## We're using Cygwin perl, use cygpath to get the windows path
65 ## and then fix up the slashes.
66 my $cyg = `cygpath -w $cwd`;
72 elsif ($^O
eq 'msys' && $cwd !~ /[A-Za-z]:/) {
73 ## We're using Mingw32 perl, use Win32::GetCwd() to get the windows
74 ## path and then fix up the slashes.
75 my $mp = Win32
::GetCwd
();
82 ## On VMS, we need to get the UNIX style path and remove the
84 $cwd = VMS
::Filespec
::unixify
($cwd);
89 if ($dir =~ /^(\/|[a
-z
]:)/i
) {
90 ## It was a full path, just store it.
94 ## This portion was relative, add it onto the current working
105 my($self, $path) = @_;
107 ## When needing a full path, it's usually because the build system requires
108 ## it. If that's the case, it is unlikely to understand cygwin or mingw32
109 ## paths. For these, we will return the full path for Win32 specifically.
110 return Win32
::GetFullPathName
($path) if ($^O
eq 'cygwin' || $^O
eq 'msys');
112 ## For all others, we will just use Cwd::abs_path
113 return Cwd
::abs_path
($path);
132 return substr($_[1], rindex($_[1], '/') + 1);
137 my($self, $dir) = @_;
139 ## The dirname() on VMS doesn't work as we expect it to.
141 ## If the directory contains multiple parts, we need to get the
142 ## dirname in a UNIX style format and then remove the slash from the
144 if (index($dir, '/') >= 0) {
145 $dir = VMS
::Filespec
::unixify
(dirname
($dir));
150 ## There's no directory portion, so just return '.'
155 ## Get the directory portion of the original directory or file path.
156 $dir = dirname
($dir);
158 ## If the result is just a drive specification, we need to append a
159 ## slash to the end of the path so that cygwin perl can use this
160 ## return value within a chdir() call.
161 $dir .= '/' if ($dir =~ /^[a
-z
]:$/i
);
169 my($self, $pattern) = @_;
171 ## glob() provided by OpenVMS does not understand [] within
172 ## the pattern. So, we implement our own through recursive calls
174 if ($onVMS && $pattern =~ /(.*)\[([^\]]+)\](.*)/) {
176 my($pre, $mid, $post) = ($1, $2, $3);
177 for(my $i = 0; $i < length($mid); $i++) {
178 StringProcessor
::merge
(\
@files,
179 [$self->mpc_glob($pre . substr($mid, $i, 1)
185 ## Otherwise, we just return the globbed pattern.
186 return glob($pattern);
195 sub path_is_relative
{
196 ## To determine if the path is relative, we just determine if it is not
198 #my($self, $path) = @_;
199 return (index($_[1], '/') != 0 && $_[1] !~ /^[A
-Z
]:[\
/\\]/i && $_[1] !~ /^\$\(\w+\)/);
202 sub path_to_relative
{
203 my($self, $check, $path) = @_;
205 ## See if it's already relative. If it is, there's nothing to do.
206 if ($path !~ s/^.[\/]+// && !$self->path_is_relative($path)) {
207 ## See how many times we have to chop off a directory until we find that
208 ## the provided path contains part of the current working directory.
210 while($check ne '.' && index($path, $check) != 0) {
212 $check = $self->mpc_dirname($check);
215 ## If we didn't go all the way back up the current working directory, we
216 ## can create a relative path from it based on the number of directories
219 $path = ('../' x
$dircount) . substr($path, length($check) + 1);
226 # ************************************************************
227 # Virtual Methods To Be Overridden
228 # ************************************************************
230 sub translate_directory
{
231 my($self, $dir) = @_;
233 ## Remove the current working directory from $dir (if it is contained)
234 my $cwd = $self->getcwd();
235 $cwd =~ s/\//\\/g
if ($self->convert_slashes());
236 if (index($dir, $cwd) == 0) {
237 my $cwdl = length($cwd);
238 return '.' if (length($dir) == $cwdl);
239 $dir = substr($dir, $cwdl + 1);
242 ## Translate .. to $dd
243 if (index($dir, '..') >= 0) {
245 $dir =~ s/^\.\.([\/\\])/$dd$1/;
246 $dir =~ s/([\/\\])\.\.$/$1$dd/;
247 $dir =~ s/([\/\\])\.\.(?=[\/\\])/$1$dd$2/g;
248 $dir =~ s/^\.\.$/$dd/;
255 sub convert_slashes
{
261 sub case_insensitive
{
263 return $case_insensitive;