Update for new keydata interface.
[catacomb-perl] / Catacomb / Key.pm
CommitLineData
fcd15e0b 1# -*-perl-*-
2#
3# $Id$
4#
5# Key management
6#
7# (c) 2004 Straylight/Edgeware
8#
9
10#----- Licensing notice -----------------------------------------------------
11#
12# This file is part of the Perl interface to Catacomb.
13#
14# Catacomb/Perl is free software; you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation; either version 2 of the License, or
17# (at your option) any later version.
18#
19# Catacomb/Perl is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with Catacomb/Perl; if not, write to the Free Software Foundation,
26# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
28#----- Key data -------------------------------------------------------------
29
bfdf19cb 30package Catacomb::Key::Data::StructTie;
fcd15e0b 31use Carp;
32use Catacomb::Base;
33
34sub TIEHASH { bless [$_[1], []], $_[0]; }
bfdf19cb 35sub FETCH { $_[0][0]->find($_[1]); }
36sub EXISTS { !!$_[0][0]->find($_[1]); }
37sub DELETE { $_[0][0]->del($_[1]); }
38sub STORE { $_[0][0]->find($_[1], $_[2]); }
39
40sub CLEAR {
41 my ($me) = @_;
42 my $kd = $me->[0];
43 my $i = $kd->iterate();
44 $kd->del($k) while my $k = $i->next();
45 1;
fcd15e0b 46}
bfdf19cb 47
fcd15e0b 48sub FIRSTKEY {
49 my ($me) = @_;
bfdf19cb 50 my $kd = $me->[0];
51 my $i = $kd->iterate();
fcd15e0b 52 my @k = ();
53 while (my $k = $i->next()) {
54 push(@k, $k);
55 }
56 $me->[1] = \@k;
57 return shift(@k);
58}
59sub NEXTKEY { shift(@{$_[0][1]}); }
60
bfdf19cb 61package Catacomb::Key::Data::Structured;
62sub open { my %h; tie %h, Catacomb::Key::Data::StructTie, $_[0]; \%h; }
fcd15e0b 63
bfdf19cb 64foreach $i (qw(Binary Encrypted MP EC String Structured)) {
65 @{"Catacomb::Key::Data::${i}::ISA"} = qw(Catacomb::Key::Data);
fcd15e0b 66}
fcd15e0b 67
68#----- Actual keys ----------------------------------------------------------
69
70package Catacomb::Key::Attrs;
71use Carp;
72
73sub TIEHASH { bless [$_[1], []], $_[0]; }
74sub FETCH { $_[0][0]->getattr($_[1]); }
75sub EXISTS { defined($_[0][0]->getattr($_[1])); }
76sub STORE { $_[0][0]->putattr($_[1], $_[2]); }
77sub DELETE { $_[0][0]->putattr($_[1]); }
78sub FIRSTKEY {
79 my ($me) = @_;
80 my $k = $me->[0];
81 my $i = $k->attriter();
82 my @a;
83 while (my $a = $i->next()) { push(@a, $a); }
84 $me->[1] = \@a;
85 return shift(@a);
86}
87sub NEXTKEY { shift(@{$_[0][1]}); }
88
89package Catacomb::Key;
90
91sub attrs {
92 croak("Usage: Catacomb::Key::attrs(k)") unless @_ == 1;
93 my %a;
94 tie %a, Catacomb::Key::Attrs, @_;
95 return \%a;
96}
97
fcd15e0b 98#----- That's all, folks ----------------------------------------------------
99
1001;