From ad9be0e4d703aab42b2721762a03373e98977515 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sun, 13 Jan 2013 10:18:34 +0100 Subject: [PATCH] Bugfix: crash when reading/writing files to paths containing non-ASCII characters on Windows. #651 #865 --- Build.PL | 1 + lib/Slic3r.pm | 8 ++++++++ lib/Slic3r/Config.pm | 6 +++--- lib/Slic3r/Format/AMF.pm | 4 ++-- lib/Slic3r/Format/OBJ.pm | 2 +- lib/Slic3r/Format/STL.pm | 4 ++-- lib/Slic3r/Print.pm | 4 ++-- lib/Slic3r/SVG.pm | 2 +- 8 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Build.PL b/Build.PL index 53dccbbba..e77e31fba 100644 --- a/Build.PL +++ b/Build.PL @@ -8,6 +8,7 @@ my $build = Module::Build->new( license => 'perl', requires => { 'Boost::Geometry::Utils' => '0', + 'Encode::Locale' => '0', 'File::Basename' => '0', 'File::Spec' => '0', 'Getopt::Long' => '0', diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index 4a0b1bb37..4ca5b4383 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -27,7 +27,10 @@ warn "Running Slic3r under Perl >= 5.16 is not supported nor recommended\n" use FindBin; our $var = "$FindBin::Bin/var"; +use Encode; +use Encode::Locale; use Moo 0.091009; + use Slic3r::Config; use Slic3r::ExPolygon; use Slic3r::Extruder; @@ -88,4 +91,9 @@ sub parallelize { } } +sub open { + my ($fh, $mode, $filename) = @_; + return CORE::open $$fh, $mode, encode('locale_fs', $filename); +} + 1; diff --git a/lib/Slic3r/Config.pm b/lib/Slic3r/Config.pm index a310361d5..d79fdf9c4 100644 --- a/lib/Slic3r/Config.pm +++ b/lib/Slic3r/Config.pm @@ -946,7 +946,7 @@ sub new_from_cli { if ($args{$opt_key}) { die "Invalid value for --${_}-gcode: file does not exist\n" if !-e $args{$opt_key}; - open my $fh, "<", $args{$opt_key} + Slic3r::open(\my $fh, "<", $args{$opt_key}) or die "Failed to open $args{$opt_key}\n"; binmode $fh, ':utf8'; $args{$opt_key} = do { local $/; <$fh> }; @@ -1251,7 +1251,7 @@ sub write_ini { my $class = shift; my ($file, $ini) = @_; - open my $fh, '>', $file; + Slic3r::open(\my $fh, '>', $file); binmode $fh, ':utf8'; my $localtime = localtime; printf $fh "# generated by Slic3r $Slic3r::VERSION on %s\n", "$localtime"; @@ -1269,7 +1269,7 @@ sub read_ini { my ($file) = @_; local $/ = "\n"; - open my $fh, '<', $file; + Slic3r::open(\my $fh, '<', $file); binmode $fh, ':utf8'; my $ini = { _ => {} }; diff --git a/lib/Slic3r/Format/AMF.pm b/lib/Slic3r/Format/AMF.pm index 0e44353cf..0b3bbc1ed 100644 --- a/lib/Slic3r/Format/AMF.pm +++ b/lib/Slic3r/Format/AMF.pm @@ -13,7 +13,7 @@ sub read_file { 1; } or die "AMF parsing requires XML::SAX\n"; - open my $fh, '<', $file or die "Failed to open $file\n"; + Slic3r::open(\my $fh, '<', $file) or die "Failed to open $file\n"; my $model = Slic3r::Model->new; XML::SAX::ParserFactory @@ -30,7 +30,7 @@ sub write_file { my %vertices_offset = (); - open my $fh, '>', $file; + Slic3r::open(\my $fh, '>', $file); binmode $fh, ':utf8'; printf $fh qq{\n}; printf $fh qq{\n}; diff --git a/lib/Slic3r/Format/OBJ.pm b/lib/Slic3r/Format/OBJ.pm index d663cfb5a..c5cc08555 100644 --- a/lib/Slic3r/Format/OBJ.pm +++ b/lib/Slic3r/Format/OBJ.pm @@ -5,7 +5,7 @@ sub read_file { my $self = shift; my ($file) = @_; - open my $fh, '<', $file or die "Failed to open $file\n"; + Slic3r::open(\my $fh, '<', $file) or die "Failed to open $file\n"; my $vertices = []; my $facets = []; while (my $_ = <$fh>) { diff --git a/lib/Slic3r/Format/STL.pm b/lib/Slic3r/Format/STL.pm index 6e9665185..5c992230e 100644 --- a/lib/Slic3r/Format/STL.pm +++ b/lib/Slic3r/Format/STL.pm @@ -7,7 +7,7 @@ sub read_file { my $self = shift; my ($file) = @_; - open my $fh, '<', $file or die "Failed to open $file\n"; + Slic3r::open(\my $fh, '<', $file) or die "Failed to open $file\n"; # let's detect whether file is ASCII or binary my $mode; @@ -103,7 +103,7 @@ sub write_file { my $self = shift; my ($file, $model, %params) = @_; - open my $fh, '>', $file; + Slic3r::open(\my $fh, '>', $file); $params{binary} ? _write_binary($fh, $model->mesh) diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index e3f3c3adf..5540632db 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -472,7 +472,7 @@ sub export_svg { my $output_file = $self->expanded_output_filepath($params{output_file}); $output_file =~ s/\.gcode$/.svg/i; - open my $fh, ">", $output_file or die "Failed to open $output_file for writing\n"; + Slic3r::open(\my $fh, ">", $output_file) or die "Failed to open $output_file for writing\n"; print "Exporting to $output_file..."; my $print_size = $self->size; print $fh sprintf <<"EOF", unscale($print_size->[X]), unscale($print_size->[Y]); @@ -647,7 +647,7 @@ sub write_gcode { if (ref $file eq 'IO::Scalar') { $fh = $file; } else { - open $fh, ">", $file + Slic3r::open(\$fh, ">", $file) or die "Failed to open $file for writing\n"; } diff --git a/lib/Slic3r/SVG.pm b/lib/Slic3r/SVG.pm index 8621614b5..0179ea769 100644 --- a/lib/Slic3r/SVG.pm +++ b/lib/Slic3r/SVG.pm @@ -130,7 +130,7 @@ sub output_lines { sub write_svg { my ($svg, $filename) = @_; - open my $fh, '>', $filename; + Slic3r::open(\my $fh, '>', $filename); print $fh $svg->xmlify; close $fh; printf "SVG written to %s\n", $filename;