[sword-devel] SWIG for Sword!

Joachim Ansorg sword-devel@crosswire.org
Sun, 23 Jun 2002 00:32:57 +0200


--------------Boundary-00=_XAP4MQBKKQUPNQC4E3XX
Content-Type: text/plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I used SWIG 1.3.12.

I attached a small example how to use it in Perl, a mod2zmod program in P=
erl=20
:)

Joachim

> great work :) Does it require swig 1.3.13? it doesn't work with 1.3.11
>
> Regards,
> Daniel
>
> On Saturday 22 Jun 2002 1:35 am, you wrote:
> > Thank you for your kind words!
> >
> > Now I extended the interface so you can write all sorts of modules
> > (RawText, zText, RawLD, RawLD4, zLD, RawCom, RawGenBook etc.)! I thin=
k
> > this is usefuil for module creation.
> >
> > Is here some Automake expert? I need some help with the Makefile so i=
t
> > creates a shared lib from the SWIG file. I'd be glad for some help!
> >
> > Joachim
> >
> > > Wow,
> > > =09Even I can almost understand that Perl code!  Nice work.
> > >
> > > =09-Troy.
> > >
> > > Joachim Ansorg wrote:
> > > > Hi!
> > > >
> > > > Some time ago I read in the TWiki of Sword, that SWIG interface f=
iles
> > > > for Sword would be cool. I never heard about SWIG, but I decided =
to
> > > > try creating the necessary interface files after I read what SWIG=
 is.
> > > >
> > > > Now I have a basic set working. I added the files to bindings/swi=
g/.
> > > > The Makefile is not yet working properly with the automake system=
,
> > > > but I hope I can fix this soon. At the moment it's a default Make=
file
> > > > made on my system, a perl module is created.
> > > > The interface files have to be refined in future, but they're wor=
king
> > > > for the basic things.
> > > >
> > > > For the curious: With the Swig interface files I can write the
> > > > following in perl:
> > > > --------------
> > > > $localemgr =3D sword::LocaleMgr::systemLocaleMgr();
> > > > $localemgr->setDefaultLocaleName("de");
> > > >
> > > > $mgr =3D new sword::SWMgr();
> > > > $module =3D $mgr->module("WEB");
> > > > print "Description of module ", $module->Name(), ": \n\t",
> > > > $module->Description(), "\n";
> > > >
> > > > $key =3D new sword::VerseKey("Matthew 3:16");
> > > > $key->setPersist(1);
> > > > $module->SetKey($key);
> > > >
> > > > for ($i =3D 0; $i < 15; $i++) {
> > > >   print "(", $module->KeyText() ,")\t", $module->StripText(), "\n=
";
> > > >   $key->next();
> > > > }
> > > >
> > > > $module->write("This is a test entry! This tests the write abilit=
ies
> > > > of the Sword Perl classes");
> > > > ----------------
> > > >
> > > > As you can see the API is almost like Sword's C++ API, but operat=
ors
> > > > are not supported. I renamed them (e.g. SWModule::operator << to
> > > > SWModule::write).
> > > >
> > > > The last call writes into the module! I hope you find the interfa=
ce
> > > > files useful! I think we can even create bindings to Java, Phyton=
,
> > > > Ruby etc. using SWIG, although I have not yet tried this.
> > > >
> > > > Joachim

--------------Boundary-00=_XAP4MQBKKQUPNQC4E3XX
Content-Type: text/x-perl;
  charset="iso-8859-1";
  name="mod2zmod.pl"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="mod2zmod.pl"

#!/usr/bin/perl

# This program converts a given module into a compressed module of the same type.
# This is just an example to demomstrate the power of the Perl Sword bindings.

$appname = "mod2zmod.pl";

use sword;

sub printUsage()
{
	print "\n$appname - Convert a module into a compressed module of the same type.\n";
	print "Usage: $appname <module> <datapth> [blocktype [compresstype]]\n";
	print("datapath: the directory in which to write the zModule\n");
        print("blockType  : (default 4)\n\t2 - verses\n\t3 - chapters\n\t4 - books\n");
        print("compressType: (default 1):\n\t1 - LZSS\n\t2 - Zip\n\n");

	exit(-1);
}

#main part of the program
if (scalar(@ARGV) < 2 || scalar(@ARGV) > 4) {
	printUsage;
}

#initialization stuff
$datapath = $ARGV[1];
$blockType = defined $ARGV[2] ? $ARGV[2] : 4;
$compressType = defined $ARGV[3] ? $ARGV[3] : 1;
$mgr = new sword::SWMgr();
$module = $mgr->module($ARGV[0]);
$compressor = ($compressType == 1) ? new sword::LZSSCompress() : new sword::ZipCompress();

if ($module->Type() eq "Biblical Texts") {
	if (!sword::zText::createModule( $datapath, $blockType )) {
		print "$appname: Couldn't create module in $datapath";
		exit(-1);
	}
        $newmod = new sword::zText( $datapath, 0, 0, $blockType, $compressor );

} elsif ($module->Type() eq "Lexicons / Dictionaries") {
	if (!sword::zLD::createModule( $datapath )){
		print "$appname: Couldn't create module in $datapath";
		exit(-1);
	}
	$newmod = new sword::zLD( $datapath, 0, 0, $blockType, $compressor)
} elsif ($module->Type() eq "Commentaries") {
	if (!sword::zCom::createModule( $datapath, $blockType )){
		print "$appname: Couldn't create module in $datapath";
		exit(-1);
	}
	$newmod = new sword::zCom( $datapath, 0, 0, $blockType, $compressor)
}

#now copy the content!
$module->top();
$module->setSkipConsecutiveLinks(false);
do {
	$key = $module->Key();
	if (($buffer eq $module->getRawEntry()) &&($buffer ne "")) {
                print "Adding [", $key->getText(), "] link to: \n";
		$newmod->writeLink($key);
	}
	else {
		$buffer = $module->getRawEntry();
		if ($buffer ne "") {
		  $newmod->SetKey($key);
		  $newmod->write($buffer);
		  # print "Added ", $key->getText(), "\n";
		}
		else {
		  print "Skipping empty ", $key->getText(), "\n";
		}
	}
} while($module->next());

print "The new module is now available in $datapath!\n";

--------------Boundary-00=_XAP4MQBKKQUPNQC4E3XX--