| 1 | #!/usr/bin/perl | ||
| 2 | # $Id: dbm.pm,v 1.3 2000/08/11 00:05:58 zarahg Exp $ | ||
| 3 | # Perl module for note | ||
| 4 | # DBM database backend. see docu: perldoc NOTEDB::dbm | ||
| 5 | # | ||
| 6 | |||
| 7 | package NOTEDB::dbm; | ||
| 8 | |||
| 9 | $NOTEDB::dbm::VERSION = "1.42"; | ||
| 10 | |||
| 11 | use DB_File; | ||
| 12 | use NOTEDB; | ||
| 13 | use strict; | ||
| 14 | use Exporter (); | ||
| 15 | use vars qw(@ISA @EXPORT %note %date); | ||
| 16 | @ISA = qw(NOTEDB Exporter); | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | sub new | ||
| 24 | { | ||
| 25 | my($this, %param) = @_; | ||
| 26 | my $class = ref($this) || $this; | ||
| 27 | my $self = {}; | ||
| 28 | bless($self,$class); | ||
| 29 | |||
| 30 | my $notefile = "note.dbm"; | ||
| 31 | my $timefile = "date.dbm"; | ||
| 32 | my $dbm_dir = $self->{dbname} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb.dbm"); | ||
| 33 | |||
| 34 | if (! -d $dbm_dir) { | ||
| 35 | # try to make it | ||
| 36 | mkdir $dbm_dir || die "Could not create $dbm_dir: $!\n"; | ||
| 37 | } | ||
| 38 | |||
| 39 | tie %note, "DB_File", "$dbm_dir/$notefile" || die "Could not tie $dbm_dir/$notefile: $!\n"; | ||
| 40 | tie %date, "DB_File", "$dbm_dir/$timefile" || die "Could not tie $dbm_dir/$timefile: $!\n"; | ||
| 41 | |||
| 42 | $self->{LOCKFILE} = $param{dbname} . "~LOCK"; | ||
| 43 | |||
| 44 | return $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub DESTROY | ||
| 49 | { | ||
| 50 | # clean the desk! | ||
| 51 | untie %note, %date; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub version { | ||
| 55 | my $this = shift; | ||
| 56 | return $this->{version}; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub get_single | ||
| 61 | { | ||
| 62 | my($this, $num) = @_; | ||
| 63 | my($note, $date); | ||
| 64 | return $this->ude ($note{$num}), $this->ude($date{$num}); | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | sub get_all | ||
| 69 | { | ||
| 70 | my $this = shift; | ||
| 71 | my($num, $note, $date, %res, $real); | ||
| 72 | foreach $num (sort {$a <=> $b} keys %date) { | ||
| 73 | $res{$num}->{'note'} = $this->ude($note{$num}); | ||
| 74 | $res{$num}->{'date'} = $this->ude($date{$num}); | ||
| 75 | } | ||
| 76 | return %res; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub import_data { | ||
| 80 | my ($this, $data) = @_; | ||
| 81 | foreach my $num (keys %{$data}) { | ||
| 82 | my $pos = $this->get_nextnum(); | ||
| 83 | $note{$pos} = $this->ude($note{$num}->{note}); | ||
| 84 | $date{$pos} = $this->ude($date{$num}->{date}); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | sub get_nextnum | ||
| 89 | { | ||
| 90 | my($this, $num); | ||
| 91 | foreach (sort {$a <=> $b} keys %date) { | ||
| 92 | $num = $_; | ||
| 93 | } | ||
| 94 | $num++; | ||
| 95 | return $num; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub get_search | ||
| 99 | { | ||
| 100 | my($this, $searchstring) = @_; | ||
| 101 | my($num, $note, $date, %res, $match); | ||
| 102 | |||
| 103 | my $regex = $this->generate_search($searchstring); | ||
| 104 | eval $regex; | ||
| 105 | if ($@) { | ||
| 106 | print "invalid expression: \"$searchstring\"!\n"; | ||
| 107 | return; | ||
| 108 | } | ||
| 109 | $match = 0; | ||
| 110 | foreach $num (sort {$a <=> $b} keys %date) { | ||
| 111 | $_ = $this->ude($note{$num}); | ||
| 112 | eval $regex; | ||
| 113 | if ($match) { | ||
| 114 | $res{$num}->{'note'} = $this->ude($note{$num}); | ||
| 115 | $res{$num}->{'date'} = $this->ude($date{$num}); | ||
| 116 | } | ||
| 117 | $match = 0; | ||
| 118 | } | ||
| 119 | |||
| 120 | return %res; | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | sub set_recountnums | ||
| 126 | { | ||
| 127 | my $this = shift; | ||
| 128 | my(%Note, %Date, $num, $setnum); | ||
| 129 | $setnum = 1; | ||
| 130 | foreach $num (sort {$a <=> $b} keys %note) { | ||
| 131 | $Note{$setnum} = $note{$num}; | ||
| 132 | $Date{$setnum} = $date{$num}; | ||
| 133 | $setnum++; | ||
| 134 | } | ||
| 135 | %note = %Note; | ||
| 136 | %date = %Date; | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | sub set_edit | ||
| 142 | { | ||
| 143 | my($this, $num, $note, $date) = @_; | ||
| 144 | $note{$num} = $this->uen($note); | ||
| 145 | $date{$num} = $this->uen($date); | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | sub set_new | ||
| 150 | { | ||
| 151 | my($this, $num, $note, $date) = @_; | ||
| 152 | $this->set_edit($num, $note, $date); # just the same thing | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | sub set_del | ||
| 157 | { | ||
| 158 | my($this, $num) = @_; | ||
| 159 | my($note, $date, $T); | ||
| 160 | ($note, $date) = $this->get_single($num); | ||
| 161 | return "ERROR" if ($date !~ /^\d/); | ||
| 162 | delete $note{$num}; | ||
| 163 | delete $date{$num}; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub set_del_all | ||
| 167 | { | ||
| 168 | my($this) = @_; | ||
| 169 | %note = (); | ||
| 170 | %date = (); | ||
| 171 | return; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub uen | ||
| 175 | { | ||
| 176 | my $this = shift; | ||
| 177 | my($T); | ||
| 178 | if($NOTEDB::crypt_supported == 1) { | ||
| 179 | eval { | ||
| 180 | $T = pack("u", $this->{cipher}->encrypt($_[0])); | ||
| 181 | }; | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | $T = $_[0]; | ||
| 185 | } | ||
| 186 | chomp $T; | ||
| 187 | return $T; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub ude | ||
| 191 | { | ||
| 192 | my $this = shift; | ||
| 193 | my($T); | ||
| 194 | if($NOTEDB::crypt_supported == 1) { | ||
| 195 | eval { | ||
| 196 | $T = $this->{cipher}->decrypt(unpack("u",$_[0])) | ||
| 197 | }; | ||
| 198 | return $T; | ||
| 199 | } | ||
| 200 | else { | ||
| 201 | return $_[0]; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | |||
| 207 | 1; # keep this! | ||
| 208 | |||
| 209 | __END__ |