###############################################################################
#
# GDBM.pm -- utility functions for writing to gdbm databases
#
# A component of the Greenstone digital library software from the New Zealand
# Digital Library Project at the University of Waikato, New Zealand.
#
# Copyright (C) 1999-2015 New Zealand Digital Library Project
#
# 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 of the License, 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., 675 Mass
# Ave, Cambridge, MA 02139, USA.
#
###############################################################################

package DBDrivers::GDBM;

# Pragma
use strict;

# Libraries
use util;
use FileUtils;
use parent 'DBDrivers::70HyphenFormat';


## @function constructor
#
sub new
{
    my $class = shift(@_);
    my $self = DBDrivers::70HyphenFormat->new(@_);
    $self->{'default_file_extension'} = 'gdb';
    # note: file separator agnostic
    $self->{'executable_path'} = $ENV{'GSDLHOME'} . '/bin/' . $ENV{'GSDLOS'};
    $self->{'read_executable'} = 'db2txt';
    $self->{'keyread_executable'} = 'gdbmkeys';
    $self->{'write_executable'} = 'txt2db';
    $self->{'supports_set'} = 1;
    bless($self, $class);
    return $self;
}
## new(void) => GDBM ##


# -----------------------------------------------------------------------------
#   GDBM IMPLEMENTATION
# -----------------------------------------------------------------------------

# Handled by BaseDBDriver
# sub get_infodb_file_path(string, string)

# Handled by 70HyphenFormat
# sub close_infodb_write_handle(filehandle) => void
# sub delete_infodb_entry(filehandle, string) => void
# sub read_infodb_entry(string, string) => hashmap
# sub read_infodb_file(string, hashmap) => void
# sub read_infodb_keys(string, hashmap) => void
# sub read_infodb_rawentry(string, string) => string
# sub set_infodb_entry(string, string, hashmap) => integer
# sub write_infodb_entry(filehandle, string, hashmap) => void
# sub write_infodb_rawentry(filehandle, string, string) => void


## @function open_infodb_write_handle(string, string*) => filehandle
#
#  Handles legacy use of optional 'append' argument where '-append' is required
#
sub open_infodb_write_handle
{
    my $self = shift(@_);
    my $infodb_file_path = shift(@_);
    my $opt_append = shift(@_);
    my $infodb_file_handle;
    if (defined $opt_append) {
	if ($opt_append eq 'append') {
	    $opt_append = '-append';
	}
	$infodb_file_handle = $self->SUPER::open_infodb_write_handle($infodb_file_path, $opt_append);
    }
    else
    {
	$infodb_file_handle = $self->SUPER::open_infodb_write_handle($infodb_file_path);
    }
    return $infodb_file_handle;
}
## open_infodb_write_handle(string, string*) => filehandle ##

1;
