#!/usr/bin/perl
#/*
#	openisis - an open implementation of the CDS/ISIS database
#	Version 0.8.x (patchlevel see file Version)
#	Copyright (C) 2001-2003 by Erik Grziwotz, erik@openisis.org
#
#	This library is free software; you can redistribute it and/or
#	modify it under the terms of the GNU Lesser General Public
#	License as published by the Free Software Foundation; either
#	version 2.1 of the License, or (at your option) any later version.
#
#	This library 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
#	Lesser General Public License for more details.
#
#	You should have received a copy of the GNU Lesser General Public
#	License along with this library; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#	see README for more information
#EOH */
#
#	$Id: demo.pl,v 1.4 2003/04/08 00:20:53 kripke Exp $
#	demo script for openisis perl XSUB
#
#	usage:
#	perl/demo.pl db/cds/cds
#

BEGIN {
	# prepend OpenIsis dir to INClude array
	# OpenIsis dir should contain OpenIsis.pm and auto/OpenIsis/OpenIsis.so
	# get dir of script, ending with /
	my ($dir) = ($0 =~ /(.*\/)/);
	# example of OpenIsis dir relative to script dir, e.g. cgi-bin/../OpenIsis
	# unshift @INC, $dir.'../OpenIsis';
	# include the build dirs
	# unshift @INC, $dir.'perl/blib/arch', $dir.'perl/blib';
	# include the tmptree created by 'make perltree'
	unshift @INC, $dir.'tmptree';
	# prepend hardcoded absolute directory
	# unshift @INC, '/tmp/OpenIsis';
	# print STDERR "INC is ", join( ':', @INC ), "\n";
	# now 'use OpenIsis' should do ...
}
use OpenIsis;

my $mode = 'dump';

sub pr {
	my $rec = shift;
	return unless $rec->{'mfn'};
	print '<isisrec id="', $rec->{'mfn'}, '">', "\n";
	for $t (keys(%$rec)) {
		my $flds = $rec->{$t}; # all fields for tag 
		for $f (@$flds) {
			# encoding conversion generated by encoding.pl
			# convert cp850 to Latin1
			$f =~ y/\x80-\xff/\xc7\xfc\xe9\xe2\xe4\xe0\xe5\xe7\xea\xeb\xe8\xef\xee\xec\xc4\xc5\xc9\xe6\xc6\xf4\xf6\xf2\xfb\xf9\xff\xd6\xdc\xf8\xa3\xd8\xd7?\xe1\xed\xf3\xfa\xf1\xd1\xaa\xba\xbf\xae\xac\xbd\xbc\xa1\xab\xbb?????\xc1\xc2\xc0\xa9????\xa2\xa5???????\xe3\xc3???????\xa4\xf0\xd0\xca\xcb\xc8?\xcd\xce\xcf????\xa6\xcc?\xd3\xdf\xd4\xd2\xf5\xd5\xb5\xfe\xde\xda\xdb\xd9\xfd\xdd\xaf\xb4\xad\xb1?\xbe\xb6\xa7\xf7\xb8\xb0\xa8\xb7\xb9\xb3\xb2?\xa0/;
			print "\t<v$t";
			if ( $f =~ /\^/ ) { # split subfields
				$ind = '';
				$sub = OpenIsis::subfields( $f );
				for $s (keys(%$sub)) {
					if ( $s ) {
						print ' ', $s, '="', $sub->{$s}, '"';
					} else {
						$ind = $sub->{$s};
					}
				}
				if ( '' eq $ind ) {
					print "/>\n";
				} else {
					print ">$ind</v$t>\n";
				}
			} elsif ( $f =~ /^<.*>$/ ) { # probably <kw><kw>
				print ">\n";
				for $kw (split(/>[^<]*</,substr($f,1,-1))) {
					print "\t\t<key>", $kw, "</key>\n";
				}
				print "\t</v$t>\n";
			} else {
				OpenIsis::MHL( $f );
				print '>', $f, '</v', $t, ">\n";
			}
		}
	}
	print '</isisrec>', "\n";
}	# pr


# OpenIsis::log(0,''); # turn off logging
$db = OpenIsis::open( $ARGV[0] || 'db/cds/cds' );

if ( 'dump' eq $mode ) {
	for (1..OpenIsis::maxRowid( $db )) {
		pr OpenIsis::read( $db, $_ );
	}
}
