1. Herbert Breunung
  2. Kephra

Source

Kephra / lib / Kephra / File / Local.pm

use v5.10;
use strict;
use warnings;
use Kephra::API;
use Kephra::File;

package Kephra::File::Local;


sub read {
	my $doc = shift;
	my $file = Kephra::File::normalize_path( shift ) || $doc->file_path;
	return Kephra::Log::warning("can't load nonexising file") unless $file and -e $file;
	return Kephra::Log::warning("can't read $file") unless -r $file;
	my $ed = $doc->editor;
	CORE::open my $FH, '<', $file;
	binmode($FH);
	my $content = do { local $/; <$FH> };
	if ($content) {
		$ed->SetText( $content );
		$ed->SetSavePoint;
	}
}

sub write {
	my $doc = shift;
	my $file = Kephra::File::normalize_path( shift ) || $doc->file_path;
	return Kephra::Log::warning("need a file path") unless $file;
	return Kephra::Log::warning("can't overwrite $file") if -e $file and not -w $file;
	$doc->file_path($file) unless $doc->file_path eq $file;
	my $ed = $doc->editor;
	CORE::open my $FH, '>', $file;
	print $FH $ed->GetText;
	$ed->SetSavePoint;
}


1;