Commits

Herbert Breunung committed 21c0059

just one sub should be able to read files

Comments (0)

Files changed (1)

lib/Kephra/File.pm

 
 		my $doc = Kephra::App::Part::Editor::create_document($file);
 		next unless ref $doc eq 'Kephra::Document';
-		CORE::open my $FH, '<', $file;
-		my $file_content = do { local $/; <$FH> };
+		my $file_content = _read_file( $file );
 		my $ed = $doc->editor;
 		$ed->SetText( $file_content ) if $file_content;
 		$ed->EmptyUndoBuffer;
 		next unless ref $doc eq 'Kephra::Document';
 		Kephra::Log::warning("can't reopen nonexising file"), next
 			unless $doc->file_path and -e $doc->file_path;
-		CORE::open my $FH, '<', $doc->file_path;
-		my $file_content = do { local $/; <$FH> };
-		my $ed = $doc->editor;
-		$ed->SetText( $file_content ) if $file_content;
+		my $file_content = _read_file( $doc->file_path );
+		$doc->editor->SetText( $file_content ) if $file_content;
 	}
 }
 sub reopen_active { reopen( Kephra::API::document() ) }
 sub reopen_all    { reopen( Kephra::API::all_documents()   ) }
 
-
-sub _store_under_filename {
+sub _read_file {
+	my $file = shift;
+	return Kephra::Log::warning("can't load nonexising file") unless $file and -e $file;
+	CORE::open my $FH, '<', $file;
+	binmode($FH);
+	return do { local $/; <$FH> };
+}
+sub _write_under_filename {
 	my $doc = shift;
 	my $file = shift || $doc->file_path;
 	CORE::open my $FH, '>', $file;
 		next unless ref $doc eq 'Kephra::Document';
 		next unless $doc->editor->GetModify;
 		if ($doc->file_path){
-			_store_under_filename($doc);
+			_write_under_filename($doc);
 		} else {
 			$doc->raise;
 			save_as($doc);
 	my $doc = shift || Kephra::API::document();
 	my $file = normalize_name( Kephra::App::Dialog::get_file_save() );
 	return unless $file;
-	_store_under_filename($doc, $file);
+	_write_under_filename($doc, $file);
 	$doc->assign_file_path($file);
 	Kephra::API::docbar()->set_page_title( $doc->file_name);
 	Kephra::API::main_window()->refresh_title();