| 1 | package Config::Find::WinAny; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use Config::Find::Any; | ||
| 8 | use Win32 qw( | ||
| 9 | CSIDL_LOCAL_APPDATA | ||
| 10 | CSIDL_APPDATA | ||
| 11 | CSIDL_DESKTOPDIRECTORY); | ||
| 12 | |||
| 13 | our @ISA = qw(Config::Find::Any); | ||
| 14 | |||
| 15 | sub app_dir { | ||
| 16 | my ($class, $name)=@_; | ||
| 17 | |||
| 18 | $name=$class->guess_script_name | ||
| 19 | unless defined $name; | ||
| 20 | |||
| 21 | my $ename = uc($name).'_HOME'; | ||
| 22 | return $ENV{$ename} if (exists $ENV{$ename}); | ||
| 23 | $class->guess_script_dir; | ||
| 24 | } | ||
| 25 | |||
| 26 | my $winlocalappdir=Win32::GetFolderPath(CSIDL_LOCAL_APPDATA); | ||
| 27 | my $winappdir=Win32::GetFolderPath(CSIDL_APPDATA); | ||
| 28 | |||
| 29 | my $windesktop=Win32::GetFolderPath(CSIDL_DESKTOPDIRECTORY); | ||
| 30 | |||
| 31 | if (defined $windesktop and $windesktop ne '') { | ||
| 32 | undef $winlocalappdir if (defined($winlocalappdir) and index($winlocalappdir, $windesktop) == 0); | ||
| 33 | undef $winappdir if (defined($winappdir) and index($winappdir, $windesktop) == 0); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub app_user_dir { | ||
| 37 | my ($class, $name)=@_; | ||
| 38 | return ( (defined $winlocalappdir) && ($winlocalappdir ne "") ? $winlocalappdir : | ||
| 39 | (defined $winappdir) && ($winappdir ne "") ? $winappdir : | ||
| 40 | File::Spec->catdir($class->app_dir($name), | ||
| 41 | 'Users', | ||
| 42 | $class->my_getlogin)); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub system_temp { | ||
| 46 | my $class=shift; | ||
| 47 | |||
| 48 | return $ENV{TEMP} if defined $ENV{TEMP}; | ||
| 49 | return $ENV{TMP} if defined $ENV{TMP}; | ||
| 50 | |||
| 51 | return File::Spec->catfile($ENV{windir}, 'Temp') if defined $ENV{windir}; | ||
| 52 | |||
| 53 | return 'C:\Temp'; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _var_dir { | ||
| 57 | my ($class, $name, $more_name, $scope)=@_; | ||
| 58 | if ($scope eq 'user') { | ||
| 59 | File::Spec->catdir($class->app_user_dir($name), $name, 'Data', $more_name) | ||
| 60 | } else { | ||
| 61 | File::Spec->catdir($class->app_dir($name), 'Data', $more_name); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _bin_dir { | ||
| 66 | my ($class, $name, $more_name, $scope)=@_; | ||
| 67 | if ($scope eq 'app') { | ||
| 68 | $class->app_dir($name); | ||
| 69 | } else { | ||
| 70 | die "unimplemented option scope => $scope"; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | sub look_for_helper { | ||
| 75 | my ($class, $dir, $helper)=@_; | ||
| 76 | |||
| 77 | my @ext=('', ( defined $ENV{PATHEXT} | ||
| 78 | ? (split /;/, $ENV{PATHEXT}) | ||
| 79 | : qw(.COM .EXE .BAT .CMD))); | ||
| 80 | |||
| 81 | for my $ext (@ext) { | ||
| 82 | my $path=File::Spec->catfile($dir, $helper.$ext); | ||
| 83 | -e $path and -x $path and return $path; | ||
| 84 | } | ||
| 85 | |||
| 86 | croak "helper '$helper' not found"; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub look_for_file { | ||
| 90 | my ($class, $name, $write, $global)=@_; | ||
| 91 | my $fn; | ||
| 92 | my $fnwe=$class->add_extension($name, 'cfg'); | ||
| 93 | |||
| 94 | if ($write) { | ||
| 95 | return File::Spec->catfile($class->app_dir($name), $fnwe) if ($global); | ||
| 96 | # my $login=getlogin(); | ||
| 97 | return File::Spec->catfile($class->app_user_dir($name), $fnwe ); | ||
| 98 | |||
| 99 | } else { | ||
| 100 | unless ($global) { | ||
| 101 | $fn=File::Spec->catfile($class->app_user_dir, $fnwe ); | ||
| 102 | return $fn if -f $fn; | ||
| 103 | } | ||
| 104 | |||
| 105 | $fn=File::Spec->catfile($class->app_dir($name), $fnwe); | ||
| 106 | return $fn if -f $fn; | ||
| 107 | } | ||
| 108 | |||
| 109 | return; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub look_for_dir_file { | ||
| 113 | my ($class, $dir, $name, $write, $global)=@_; | ||
| 114 | my $fn; | ||
| 115 | my $fnwe=$class->add_extension($name, 'cfg'); | ||
| 116 | |||
| 117 | if ($write) { | ||
| 118 | return File::Spec->catfile($class->app_dir($dir), $dir, $fnwe) if ($global); | ||
| 119 | |||
| 120 | # my $login=getlogin(); | ||
| 121 | return File::Spec->catfile($class->app_user_dir($dir), $dir, $fnwe ); | ||
| 122 | |||
| 123 | } else { | ||
| 124 | unless ($global) { | ||
| 125 | $fn=File::Spec->catfile($class->app_user_dir($name), $dir, $fnwe ); | ||
| 126 | return $fn if -f $fn; | ||
| 127 | } | ||
| 128 | $fn=File::Spec->catfile($class->app_dir($name), $fnwe); | ||
| 129 | return $fn if -f $fn; | ||
| 130 | } | ||
| 131 | |||
| 132 | return; | ||
| 133 | } | ||
| 134 | |||
| 135 | 1; | ||
| 136 | |||
| 137 | __END__ |