#!/usr/bin/perl # Author : Damien KROTKINE (damien@tuxfamily.org) # # Contributors : # Charles LONGEAU (chl@tuxfamily.org) # # Copyright (C) 2002 Damien KROTKINE (damien@tuxfamily.org) # Copyright (C) 2002 Charles LONGEAU (chl@tuxfamily.org) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. package Libconf::Glueconf::Shell; use strict; use vars qw(@ISA); use Libconf::Glueconf; use Libconf::Libconf qw(all); #use Libconf::Glueconf::XF86Config::Wrapper; @ISA = qw(Libconf); sub new { my ($pkg, $filename) = @_; my $libconf = $pkg->Libconf::new($filename, 'shell', ''); tie my %wrapper, 'Libconf::Glueconf::Shell::Wrapper', $libconf; \%wrapper; } package Libconf::Glueconf::Shell::Wrapper; sub debug { $ENV{DEBUG} and print @_ } sub TIEHASH { my ($pkg, $libconf) = @_; debug "Wrapper - TIEHASH\n"; bless { libconf => $libconf }, $pkg; } sub CLEAR { my ($obj) = @_; debug "Wrapper - CLEAR\n"; $obj->{libconf}->clear(); } sub DELETE { my ($obj, $key) = @_; debug "Wrapper - DELETE - key: $key\n"; my @pos = $obj->{libconf}->findAtomPos( { type => 'KEY_VALUE', key => $key }); foreach (@pos) { $obj->{libconf}->deleteAtom($_); } } sub FIRSTKEY { my ($obj) = @_; debug "Wrapper - FIRSTKEY\n"; my $atom = $obj->{libconf}->getAtom(0); $atom->{key}; } sub EXISTS { my ($obj, $key) = @_; debug "Wrapper - EXISTS - key : $key\n"; my $pos = $obj->{libconf}->findAtomPos( { type => 'KEY_VALUE', key => $key }); defined $pos; } sub NEXTKEY { my ($obj, $lastkey) = @_; debug "Wrapper - NEXTKEY - lastkey : $lastkey\n"; my @pos = $obj->{libconf}->findAtomPos( { type => 'KEY_VALUE', key => $lastkey }); #FIXME : double entries should be removed elsewhere $pos[-1]+1 >= $obj->{libconf}->size() and return undef; $obj->{libconf}->getAtom($pos[-1]+1)->{key}; } sub STORE { my ($obj, $key, $value) = @_; debug "Wrapper - STORE - key : $key - value : $value\n"; ref $value eq '' or die 'trying to store anything else than a value'; my $index; my @pos = $obj->{libconf}->findAtomPos({ type => 'KEY_VALUE', key => $key }); if (@pos == 0) { $index = $obj->{libconf}->appendAtom({ type => 'KEY_VALUE', key => $key }); } else { $index = $pos[-1]; } $obj->{libconf}->editAtom($index, { type => 'KEY_VALUE', key => $key, value => $value }); } sub FETCH { my ($obj, $key) = @_; debug "Wrapper - FETCH - key : $key\n"; $key eq 'libconf' and return $obj->{libconf}; my @pos = $obj->{libconf}->findAtomPos({ type => 'KEY_VALUE', key => $key }); $obj->{libconf}->getAtom($pos[-1])->{value}; } 1;