Jive reference manual
DataParser.cpp
/************************************************************************
*
* This example demonstrates how to parse various types of data sets
* using the DataParser class.
*
***********************************************************************/
#include <jem/base/System.h>
#include <jem/io/FileInputStream.h>
#include <jem/io/FileOutputStream.h>
#include <jem/io/ObjectInputStream.h>
#include <jem/io/ObjectOutputStream.h>
#include <jem/util/Timer.h>
#include <jem/util/Properties.h>
#include <jive/util/Table.h>
#include <jive/util/ItemGroup.h>
#include <jive/util/Printer.h>
#include <jive/util/Database.h>
#include <jive/util/DataParser.h>
#include <jive/util/StdPointSet.h>
#include <jive/util/StdGroupSet.h>
using namespace jem;
using namespace jem::io;
using jive::IdxVector;
//-----------------------------------------------------------------------
// run
//-----------------------------------------------------------------------
int run ( int argc, char** argv )
{
Writer& out = System::out ();
Properties globdat ( "data" );
String baseName;
String fileName;
Timer timer;
IdxVector ipoints;
idx_t i, n;
timer.start ();
if ( argc > 1 )
{
fileName = argv[argc - 1];
}
else
{
fileName = "data.in";
}
i = fileName.rfind ( '.' );
if ( i >= 0 )
{
baseName = fileName[slice(BEGIN,i)];
}
else
{
baseName = fileName;
}
points = newInstance<StdPointSet> ( "points", "point" );
groups = newInstance<StdGroupSet> ( "segments", "segment", points );
points->store ( "points", globdat );
groups->store ( "segments", globdat );
parser = newInstance<DataParser> ();
parser->addPointSetParser ( points );
parser->addGroupSetParser ( groups );
parser->addItemGroupsParsers ( globdat );
parser->addTablesParsers ( globdat );
parser->addDbasesParsers ( globdat );
timer.reset ();
print ( out, "Parsing input file `", fileName, "\' ...\n\n" );
parser->parseFile ( fileName, &System::out() );
print ( out, "\nReady in ", timer, "\n\n" );
ItemGroup::printAll ( Printer::get(), globdat );
Table ::printAll ( Printer::get(), globdat );
Database ::printAll ( Printer::get(), globdat );
points = NIL;
groups = NIL;
Printer::flush ();
print ( out, "\n\n" );
fileName = baseName + ".dump";
dumpOut = newInstance<ObjectOutputStream> (
newInstance<FileOutputStream> ( fileName )
);
timer.reset ();
print ( out, "Writing binary data to `", fileName, "\' ...\n" );
encode ( *dumpOut, globdat );
dumpOut = NIL;
print ( out, "Ready in ", timer, "\n\n" );
dumpIn = newInstance<ObjectInputStream> (
newInstance<FileInputStream> ( fileName )
);
timer.reset ();
print ( out, "Reading data from binary file `",
fileName, "\' ...\n" );
decode ( *dumpIn, globdat );
print ( out, "Ready in ", timer, "\n\n" );
points = checkedCast<XPointSet> (
ItemSet::get ( "points", globdat, JEM_FUNC )
);
n = points->size() / 2;
ipoints.resize ( n );
for ( i = 0; i < n; i++ )
{
ipoints[i] = 2 * i;
}
timer.reset ();
print ( out, "Erasing all even-indexed points ...\n" );
points->erasePoints ( ipoints );
print ( out, "Ready in ", timer, "\n\n" );
groups = checkedCast<XGroupSet> (
ItemSet::get ( "segments", globdat, JEM_FUNC )
);
groups->printTo ( Printer::get() );
ItemGroup::printAll ( Printer::get(), globdat );
Table ::printAll ( Printer::get(), globdat );
Database ::printAll ( Printer::get(), globdat );
Printer::flush ();
print ( out, "\n\n" );
return 0;
}
//-----------------------------------------------------------------------
// main
//-----------------------------------------------------------------------
int main ( int argc, char** argv )
{
return System::exec ( & run, argc, argv );
}