version 1.6, 2010/01/09 05:15:20 |
version 1.8, 2010/01/09 06:26:43 |
|
|
package Text::Todo; |
package Text::Todo; |
|
|
# $RedRiver: Todo.pm,v 1.5 2010/01/09 05:00:21 andrew Exp $ |
# $RedRiver: Todo.pm,v 1.6 2010/01/09 05:15:20 andrew Exp $ |
|
|
use warnings; |
use warnings; |
use strict; |
use strict; |
|
|
|
|
my %path_of; |
my %path_of; |
my %list_of; |
my %list_of; |
|
my %loaded_of; |
|
|
sub new { |
sub new { |
my ( $class, $options ) = @_; |
my ( $class, $options ) = @_; |
|
|
my ( $self, $file ) = @_; |
my ( $self, $file ) = @_; |
my $ident = ident($self); |
my $ident = ident($self); |
|
|
|
$loaded_of{$ident} = undef; |
|
|
|
if ( $list_of{$ident} = $self->listfile($file) ) { |
|
$loaded_of{$ident} = $file; |
|
return 1; |
|
} |
|
|
|
return; |
|
} |
|
|
|
sub listfile { |
|
my ( $self, $file ) = @_; |
|
|
$file = $self->file($file); |
$file = $self->file($file); |
|
|
if ( !defined $file ) { |
if ( !defined $file ) { |
croak q{todo file can't be found}; |
carp q{file can't be found}; |
|
return; |
} |
} |
|
|
if ( !-e $file ) { |
if ( !-e $file ) { |
carp "todo file [$file] does not exist"; |
carp "file [$file] does not exist"; |
return; |
return; |
} |
} |
|
|
my @list; |
my @list; |
my $line = 1; |
|
open my $fh, '<', $file or croak "Couldn't open [$file]: $!"; |
open my $fh, '<', $file or croak "Couldn't open [$file]: $!"; |
while (<$fh>) { |
while (<$fh>) { |
s/\r?\n$//xms; |
s/\r?\n$//xms; |
|
next if !length $_; |
push @list, Text::Todo::Entry->new($_); |
push @list, Text::Todo::Entry->new($_); |
} |
} |
close $fh or croak "Couldn't close [$file]: $!"; |
close $fh or croak "Couldn't close [$file]: $!"; |
$list_of{$ident} = \@list; |
|
|
|
return 1; |
return wantarray ? @list : \@list; |
} |
} |
|
|
sub save { |
sub save { |
|
|
return wantarray ? @projects : \@projects; |
return wantarray ? @projects : \@projects; |
} |
} |
|
|
sub archive { carp "unsupported\n"; return } |
sub archive { carp 'unsupported'; return } |
sub addto { carp "unsupported\n"; return } |
|
sub listfile { carp "unsupported\n"; return } |
sub addto { |
|
my ( $self, $file, $entry ) = @_; |
|
my $ident = ident($self); |
|
|
|
$file = $self->file($file); |
|
if ( !defined $file ) { |
|
croak q{file can't be found}; |
|
} |
|
|
|
open my $fh, '>>', $file or croak "Couldn't open [$file]: $!"; |
|
print {$fh} $entry, "\n" |
|
or croak "Couldn't print to [$file]: $!"; |
|
close $fh or croak "Couldn't close [$file]: $!"; |
|
|
|
if ( defined $loaded_of{$ident} && $file eq $loaded_of{$ident} ) { |
|
return $self->load($file); |
|
} |
|
|
|
return 1; |
|
} |
|
|
sub _find_entry_id { |
sub _find_entry_id { |
my ( $self, $entry ) = @_; |
my ( $self, $entry ) = @_; |