NOTEDB/general.pm untested

filter: Statementstmt 0.0% 0 / 196 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 294 SCAR 56.1
CC
16
Cov
0%
get_search
30.0
new
12.0
_retrieve
12.0
CRAP 272.0
1 # Perl module for note
2 # general database backend. see docu: perldoc NOTEDB::general
3 # using Config::General as backend.
4  
5 package NOTEDB::general;
6  
7 $NOTEDB::general::VERSION = "1.04";
8  
9 use strict;
10 #use Data::Dumper;
11 use File::Spec;
12 use Config::General qw(ParseConfig SaveConfig SaveConfigString);
13 use MIME::Base64;
14 use FileHandle;
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  
27 sub new {
28 my($this, %param) = @_;
29  
30 my $class = ref($this) || $this;
31 my $self = {};
32 bless($self,$class);
33  
34 $self->{dbname} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb");
35  
36 if(! -e $param{dbname}) {
37 open(TT,">$param{dbname}") or die "Could not create $param{dbname}: $!\n";
38 close (TT);
39 }
40 elsif(! -w $param{dbname}) {
41 print "$param{dbname} is not writable!\n";
42 exit(1);
43 }
44  
45 $self->{mtime} = $self->get_stat();
46 $self->{unread} = 1;
47 $self->{changed} = 1;
48 $self->{data} = {};
49 $self->{LOCKFILE} = $param{dbname} . "~LOCK";
50  
51 return $self;
52 }
53  
54  
55 sub DESTROY {
56 # clean the desk!
57 }
58  
59 sub version {
60 my $this = shift;
61 return $NOTEDB::general::VERSION;
62 }
63  
64 sub get_stat {
65 my ($this) = @_;
66 my $mtime = (stat($this->{dbname}))[9];
67 return $mtime;
68 }
69  
70 sub changed {
71 my ($this) = @_;
72 my $current = $this->get_stat();
73 if ($current > $this->{mtime}) {
74 $this->{mtime} = $current;
75 return $current;
76 }
77 else {
78 return 0;
79 }
80 }
81  
82 sub set_del_all {
83 my $this = shift;
84 unlink $this->{dbname};
85 open(TT,">$this->{dbname}") or die "Could not create $this->{dbname}: $!\n";
86 close (TT);
87 }
88  
89  
90 sub get_single {
91 my($this, $num) = @_;
92 my($address, $note, $date, $n, $t, $buffer, );
93  
94 my %data = $this->get_all();
95  
96 return ($data{$num}->{note}, $data{$num}->{date});
97 }
98  
99  
100 sub get_all {
101 my $this = shift;
102 my($num, $note, $date, %res);
103  
104 if ($this->unchanged) {
105 return %{$this->{cache}};
106 }
107  
108 my %data = $this->_retrieve();
109  
110 foreach my $num (keys %data) {
111 $res{$num}->{note} = $this->ude($data{$num}->{note});
112 $res{$num}->{date} = $this->ude($data{$num}->{date});
113 }
114  
115 $this->cache(%res);
116 return %res;
117 }
118  
119 sub import_data {
120 my ($this, $data) = @_;
121 my %res = $this->_retrieve();
122 my $pos = (scalar keys %res) + 1;
123 foreach my $num (keys %{$data}) {
124 $res{$pos}->{note} = $this->uen($data->{$num}->{note});
125 $res{$pos}->{date} = $this->uen($data->{$num}->{date});
126 $pos++;
127 }
128 $this->_store(\%res);
129 }
130  
131 sub get_nextnum {
132 my $this = shift;
133 my($num, $te, $me, $buffer);
134  
135 if ($this->unchanged) {
136 $num = 1;
137 foreach (keys %{$this->{cache}}) {
138 $num++;
139 }
140 return $num;
141 }
142  
143 my %data = $this->get_all();
144 my @numbers = sort { $a <=> $b } keys %data;
145 $num = pop @numbers;
146 $num++;
147  
148 return $num;
149 }
150  
151 sub get_search {
152 my($this, $searchstring) = @_;
153 my($buffer, $num, $note, $date, %res, $t, $n, $match);
154  
155 my $regex = $this->generate_search($searchstring);
156 eval $regex;
157 if ($@) {
158 print "invalid expression: \"$searchstring\"!\n";
159 return;
160 }
161 $match = 0;
162  
163 if ($this->unchanged) {
164 foreach my $num (keys %{$this->{cache}}) {
165 $_ = $this->{cache}{$num}->{note};
166 eval $regex;
167 if ($match) {
168 $res{$num}->{note} = $this->{cache}{$num}->{note};
169 $res{$num}->{date} = $this->{cache}{$num}->{date}
170 }
171 $match = 0;
172 }
173 return %res;
174 }
175  
176 my %data = $this->get_all();
177  
178 foreach my $num(sort keys %data) {
179 $_ = $data{$num}->{note};
180 eval $regex;
181 if($match)
182 {
183 $res{$num}->{note} = $data{$num}->{note};
184 $res{$num}->{date} = $data{$num}->{data};
185 }
186 $match = 0;
187 }
188  
189 return %res;
190 }
191  
192  
193  
194  
195 sub set_edit {
196 my($this, $num, $note, $date) = @_;
197  
198 my %data = $this->_retrieve();
199  
200 $data{$num} = {
201 note => $this->uen($note),
202 date => $this->uen($date)
203 };
204  
205 $this->_store(\%data);
206  
207 $this->changed;
208 }
209  
210  
211 sub set_new {
212 my($this, $num, $note, $date) = @_;
213 $this->set_edit($num, $note, $date);
214 }
215  
216  
217 sub set_del {
218 my($this, $num) = @_;
219 my(%data, $note, $date, $T, $setnum, $buffer, $n, $N, $t);
220  
221 $setnum = 1;
222  
223 %data = $this->_retrieve();
224 return "ERROR" if (! exists $data{$num});
225  
226 delete $data{$num};
227  
228 $this->_store(\%data);
229  
230 $this->changed;
231  
232 return;
233 }
234  
235 sub set_recountnums {
236 my($this) = @_;
237 my(%orig, %data, $note, $date, $T, $setnum, $buffer, $n, $N, $t);
238  
239 $setnum = 1;
240 %orig = $this->_retrieve();
241  
242 foreach $N (sort {$a <=> $b} keys %orig) {
243 $data{$setnum} = {
244 note => $orig{$N}->{note},
245 date => $orig{$N}->{date}
246 };
247 $setnum++;
248 }
249  
250 $this->_store(\%data);
251  
252 $this->changed;
253  
254 return;
255 }
256  
257 sub uen {
258 my ($this, $raw) = @_;
259 my($crypted);
260 if($NOTEDB::crypt_supported == 1) {
261 eval {
262 $crypted = $this->{cipher}->encrypt($raw);
263 };
264 print $@;
265 }
266 else {
267 $crypted = $raw;
268 }
269 my $coded = encode_base64($crypted);
270 chomp $coded;
271 return $coded;
272 }
273  
274 sub ude {
275 my ($this, $crypted) = @_;
276 my($raw);
277 if($NOTEDB::crypt_supported == 1) {
278 eval {
279 $raw = $this->{cipher}->decrypt(decode_base64($crypted));
280 };
281 }
282 else {
283 $raw = decode_base64($crypted)
284 }
285 return $raw;
286 }
287  
288  
289  
290 sub _store {
291 my ($this, $data) = @_;
292 open NOTE, ">$this->{dbname}" or die "could not open $this->{dbname}: $!\n";
293 flock NOTE, LOCK_EX;
294  
295 if (%{$data}) {
296 my $content = SaveConfigString($data) or die "could not serialize data: $!\n";
297 print NOTE $content;
298 }
299 else {
300 print NOTE "";
301 }
302  
303 flock NOTE, LOCK_UN;
304 close NOTE;
305  
306 # finally re-read the db, so that we always have the latest data
307 $this->_retrieve();
308 }
309  
310 sub _retrieve {
311 my ($this) = @_;
312 my $file = $this->{dbname};
313 if (-s $file) {
314 if ($this->{changed} || $this->{unread}) {
315 my $fh = new FileHandle "<$this->{dbname}" or die "could not open $this->{dbname}\n";
316 flock $fh, LOCK_EX;
317  
318 my %data = ParseConfig(-ConfigFile => $fh) or die "could not read to database: $!\n";
319  
320 flock $fh, LOCK_UN;
321 $fh->close();
322  
323 $this->{unread} = 0;
324 $this->{data} = \%data;
325 return %data;
326 }
327 else {
328 return %{$this->{data}};
329 }
330 }
331 else {
332 return ();
333 }
334 }
335  
336  
337 1; # keep this!
338  
339 __END__