source: main/trunk/greenstone2/perllib/dbutil/gdbm.pm@ 22076

Last change on this file since 22076 was 22076, checked in by sjm84, 14 years ago

Added error messages to these files that are printed when a write handle cannot be opened

File size: 3.9 KB
Line 
1###########################################################################
2#
3# dbutil::gdbm -- utility functions for writing to gdbm databases
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 2009
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27package dbutil::gdbm;
28
29use strict;
30
31use dbutil::gdbmtxtgz;
32
33# -----------------------------------------------------------------------------
34# GDBM IMPLEMENTATION
35# -----------------------------------------------------------------------------
36
37sub open_infodb_write_handle
38{
39 my $infodb_file_path = shift(@_);
40 my $opt_append = shift(@_);
41
42 my $txt2db_exe = &util::filename_cat($ENV{'GSDLHOME'},"bin",$ENV{'GSDLOS'}, "txt2db" . &util::get_os_exe());
43 my $infodb_file_handle = undef;
44 my $cmd = "\"$txt2db_exe\"";
45 if ((defined $opt_append) && ($opt_append eq "append")) {
46 $cmd .= " -append";
47 }
48 $cmd .= " \"$infodb_file_path\"";
49
50 if (!-e "$txt2db_exe")
51 {
52 print STDERR "Error: Unable to find $txt2db_exe\n";
53 return undef;
54 }
55
56 if(!open($infodb_file_handle, "| $cmd"))
57 {
58 print STDERR "Error: Failed to open pipe to $cmd\n";
59 print STDERR " $!\n";
60 return undef;
61 }
62
63 return $infodb_file_handle;
64}
65
66
67
68sub close_infodb_write_handle
69{
70 my $infodb_handle = shift(@_);
71
72 close($infodb_handle);
73}
74
75
76sub get_infodb_file_path
77{
78 my $collection_name = shift(@_);
79 my $infodb_directory_path = shift(@_);
80
81 my $infodb_file_extension = ".gdb";
82 my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
83 return &util::filename_cat($infodb_directory_path, $infodb_file_name);
84}
85
86
87sub read_infodb_file
88{
89 my $infodb_file_path = shift(@_);
90 my $infodb_map = shift(@_);
91
92 open (PIPEIN, "db2txt \"$infodb_file_path\" |") || die "couldn't open pipe from db2txt \$infodb_file_path\"\n";
93 my $infodb_line = "";
94 my $infodb_key = "";
95 my $infodb_value = "";
96 while (defined ($infodb_line = <PIPEIN>))
97 {
98 if ($infodb_line =~ /^\[([^\]]+)\]$/)
99 {
100 $infodb_key = $1;
101 }
102 elsif ($infodb_line =~ /^-{70}$/)
103 {
104 $infodb_map->{$infodb_key} = $infodb_value;
105 $infodb_key = "";
106 $infodb_value = "";
107 }
108 else
109 {
110 $infodb_value .= $infodb_line;
111 }
112 }
113
114 close (PIPEIN);
115}
116
117sub read_infodb_keys
118{
119 my $infodb_file_path = shift(@_);
120 my $infodb_map = shift(@_);
121
122 open (PIPEIN, "gdbmkeys \"$infodb_file_path\" |") || die "couldn't open pipe from gdbmkeys \$infodb_file_path\"\n";
123 my $infodb_line = "";
124 my $infodb_key = "";
125 my $infodb_value = "";
126 while (defined ($infodb_line = <PIPEIN>))
127 {
128 chomp $infodb_line; # remove end of line
129
130 $infodb_map->{$infodb_line} = 1;
131 }
132
133 close (PIPEIN);
134}
135
136sub write_infodb_entry
137{
138 # With infodb_handle already set up, works the same as _gdbm_txtgz version
139 &dbutil::gdbmtxtgz::write_infodb_entry(@_);
140}
141
142sub write_infodb_rawentry
143{
144 # With infodb_handle already set up, works the same as _gdbm_txtgz version
145 &dbutil::gdbmtxtgz::write_infodb_rawentry(@_);
146}
147
148
149sub delete_infodb_entry
150{
151 # With infodb_handle already set up, works the same as _gdbm_txtgz version
152 &dbutil::gdbmtxtgz::delete_infodb_entry(@_);
153}
154
155
156
1571;
Note: See TracBrowser for help on using the repository browser.