App-Music-ChordPro

 view release on metacpan or  search on metacpan

lib/ChordPro/Files.pm  view on Meta::CPAN

#! perl

use v5.26;
use feature qw( signatures );
no warnings qw( experimental::signatures );
use utf8;

package ChordPro::Files;

# Generalize some file system operations so they use LongPath on Windows.
# This is necessary for long filenames and unicode filenames.

# NOTE: FILENAMES SHOULD AT ALL TIMES BE PERL STRINGS!

# Where do filenames come from?
#
# 1. Command line arguments. Decode ASAP.
# 2. File (and directory) dialogs: Always perl string.
# 3. Preferences, configs, recents: should all be perl strings.
# 4. From filelists. We expect these lists to have UTF8 filenames that
#    get decoded when the list is read.

use Encode qw( decode_utf8 encode_utf8 );
use Ref::Util qw(is_ref);

use Exporter 'import';
our @EXPORT;
our @EXPORT_OK;

################ Platforms ################

use constant MSWIN => $^O =~ /MSWin|Windows_NT/i ? 1 : 0;

sub is_msw ()   { MSWIN }
sub is_macos () { $^O =~ /darwin/ }
sub is_wx ()    { defined($Wx::wxVERSION) }

push( @EXPORT, qw( is_msw is_macos is_wx ) );

if ( is_msw ) {
    require Win32::LongPath;
}

################ ################

# General pattern:
# If Windows, call Windows specific function.
# Otherwise
#  If the filename contains UTF8 characters, encode.
#  Call standard perl function.

sub fs_open( $name, $mode = '<:utf8' ) {
    my $fd;
    if ( is_msw ) {
	Win32::LongPath::openL( \$fd, $mode, $name )
	    or die("$name: $^E\n");
	return $fd;
    }

    my $uname = $name;
    $uname = encode_utf8($name) if utf8::is_utf8($uname);

    open( $fd, $mode, $uname )
      or die("$name: $!\n");
    return $fd;
}

push( @EXPORT, qw(fs_open) );

sub fs_test( $tests, $name ) {
    my $res = 1;
    for my $test ( split( //, $tests ) ) {
	$res = _fs_test( $test, $name );
	return unless $res;
    }
    $res;
}

sub _fs_test( $test, $name ) {
    return Win32::LongPath::testL( $test, $name ) if is_msw;



( run in 0.655 second using v1.01-cache-2.11-cpan-5837b0d9d2c )