NOTEDB/text.pm untested

filter: Statementstmt 0.0% 0 / 160 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 243 SCAR 53.5
CC
14
Cov
0%
get_search
30.0
new
12.0
_retrieve
12.0
CRAP 210.0
1 # Perl module for note
2 # text database backend. see docu: perldoc NOTEDB::text
3 # using Storable as backend.
4  
5 package NOTEDB::text;
6  
7 $NOTEDB::text::VERSION = "1.04";
8  
9 use strict;
10 #use Data::Dumper;
11 use File::Spec;
12 use Storable qw(lock_nstore lock_retrieve);
13 use MIME::Base64;
14  
15 use NOTEDB;
16  
17 use Fcntl qw(LOCK_EX LOCK_UN);
18  
19 use Exporter ();
20 use vars qw(@ISA @EXPORT);
21 @ISA = qw(NOTEDB Exporter);
22  
23  
24  
25  
26 sub new {
27 my($this, %param) = @_;
28  
29 my $class = ref($this) || $this;
30 my $self = {};
31 bless($self,$class);
32  
33 $self->{NOTEDB} = $self->{dbname} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb");
34  
35 if(! -e $param{dbname}) {
36 open(TT,">$param{dbname}") or die "Could not create $param{dbname}: $!\n";
37 close (TT);
38 }
39 elsif(! -w $param{dbname}) {
40 print "$param{dbname} is not writable!\n";
41 exit(1);
42 }
43  
44 $self->{LOCKFILE} = $param{dbname} . "~LOCK";
45 $self->{mtime} = $self->get_stat();
46 $self->{unread} = 1;
47 $self->{data} = {};
48  
49 return $self;
50 }
51  
52  
53 sub DESTROY
54 {
55 # clean the desk!
56 }
57  
58 sub version {
59 my $this = shift;
60 return $NOTEDB::text::VERSION;
61 }
62  
63 sub get_stat {
64 my ($this) = @_;
65 my $mtime = (stat($this->{dbname}))[9];
66 return $mtime;
67 }
68  
69  
70 sub set_del_all {
71 my $this = shift;
72 unlink $this->{NOTEDB};
73 open(TT,">$this->{NOTEDB}") or die "Could not create $this->{NOTEDB}: $!\n";
74 close (TT);
75 }
76  
77  
78 sub get_single {
79 my($this, $num) = @_;
80 my($address, $note, $date, $n, $t, $buffer, );
81  
82 my %data = $this->get_all();
83  
84 return ($data{$num}->{note}, $data{$num}->{date});
85 }
86  
87  
88 sub get_all {
89 my $this = shift;
90 my($num, $note, $date, %res);
91  
92 if ($this->unchanged) {
93 return %{$this->{cache}};
94 }
95  
96 my %data = $this->_retrieve();
97  
98 foreach my $num (keys %data) {
99 $res{$num}->{note} = $this->ude($data{$num}->{note});
100 $res{$num}->{date} = $this->ude($data{$num}->{date});
101 }
102  
103 $this->cache(%res);
104 return %res;
105 }
106  
107 sub import_data {
108 my ($this, $data) = @_;
109 my %res = $this->_retrieve();
110 my $pos = (scalar keys %res) + 1;
111 foreach my $num (keys %{$data}) {
112 $res{$pos}->{note} = $this->uen($data->{$num}->{note});
113 $res{$pos}->{date} = $this->uen($data->{$num}->{date});
114 $pos++;
115 }
116 $this->_store(\%res);
117 }
118  
119 sub get_nextnum {
120 my $this = shift;
121 my($num, $te, $me, $buffer);
122  
123 if ($this->unchanged) {
124 my @numbers = sort { $a <=> $b } keys %{$this->{cache}};
125 $num = pop @numbers;
126 $num++;
127 return $num;
128 }
129  
130 my %data = $this->get_all();
131 my @numbers = sort { $a <=> $b } keys %data;
132 $num = pop @numbers;
133 $num++;
134 return $num;
135 }
136  
137 sub get_search {
138 my($this, $searchstring) = @_;
139 my($buffer, $num, $note, $date, %res, $t, $n, $match);
140  
141 my $regex = $this->generate_search($searchstring);
142 eval $regex;
143 if ($@) {
144 print "invalid expression: \"$searchstring\"!\n";
145 return;
146 }
147 $match = 0;
148  
149 if ($this->unchanged) {
150 foreach my $num (keys %{$this->{cache}}) {
151 $_ = $this->{cache}{$num}->{note};
152 eval $regex;
153 if ($match) {
154 $res{$num}->{note} = $this->{cache}{$num}->{note};
155 $res{$num}->{date} = $this->{cache}{$num}->{date}
156 }
157 $match = 0;
158 }
159 return %res;
160 }
161  
162 my %data = $this->get_all();
163  
164 foreach my $num(sort keys %data) {
165 $_ = $data{$num}->{note};
166 eval $regex;
167 if($match)
168 {
169 $res{$num}->{note} = $data{$num}->{note};
170 $res{$num}->{date} = $data{$num}->{data};
171 }
172 $match = 0;
173 }
174  
175 return %res;
176 }
177  
178  
179  
180  
181 sub set_edit {
182 my($this, $num, $note, $date) = @_;
183  
184 my %data = $this->_retrieve();
185  
186 $data{$num} = {
187 note => $this->uen($note),
188 date => $this->uen($date)
189 };
190  
191 $this->_store(\%data);
192  
193 $this->changed;
194 }
195  
196  
197 sub set_new {
198 my($this, $num, $note, $date) = @_;
199 $this->set_edit($num, $note, $date);
200 }
201  
202  
203 sub set_del {
204 my($this, $num) = @_;
205 my(%data, $note, $date, $T, $setnum, $buffer, $n, $N, $t);
206  
207 $setnum = 1;
208  
209 %data = $this->_retrieve();
210 return "ERROR" if (! exists $data{$num});
211  
212 delete $data{$num};
213  
214 $this->_store(\%data);
215  
216 $this->changed;
217  
218 return;
219 }
220  
221 sub set_recountnums {
222 # not required here
223 return;
224 }
225  
226 sub uen {
227 my ($this, $raw) = @_;
228 my($crypted);
229 if($NOTEDB::crypt_supported == 1) {
230 eval {
231 $crypted = $this->{cipher}->encrypt($raw);
232 };
233 }
234 else {
235 $crypted = $raw;
236 }
237 my $coded = encode_base64($crypted);
238 return $coded;
239 }
240  
241 sub ude {
242 my ($this, $crypted) = @_;
243 my($raw);
244 if($NOTEDB::crypt_supported == 1) {
245 eval {
246 $raw = $this->{cipher}->decrypt(decode_base64($crypted));
247 };
248 }
249 else {
250 $raw = decode_base64($crypted)
251 }
252 return $raw;
253 }
254  
255  
256 sub _store {
257 my ($this, $data) = @_;
258 lock_nstore($data, $this->{NOTEDB});
259 }
260  
261 sub _retrieve {
262 my $this = shift;
263 if (-s $this->{NOTEDB}) {
264 if ($this->changed() || $this->{unread}) {
265 my $data = lock_retrieve($this->{NOTEDB});
266 $this->{unread} = 0;
267 $this->{data} = $data;
268 return %{$data};
269 }
270 }
271 else {
272 return ();
273 }
274 }
275  
276  
277 1; # keep this!
278  
279 __END__