#include <stdio.h>
#include <QDebug>
#include "lob.h"
void printUsage ( char* commandName )
{
qDebug( "Usage: %s [-adDetuv] <file.lurch>", commandName );
qDebug( "\t-u show urn (default with no switches)" );
qDebug( "\t-a show author" );
qDebug( "\t-d show description" );
qDebug( "\t-e show encoding language" );
qDebug( "\t-t show title" );
qDebug( "\t-v show version" );
qDebug( "\t-T show dependency list (Titles; supercedes -U)" );
qDebug( "\t-U show dependency list (URNs)" );
}
int main ( int argc, char** argv )
{
// ensure they passed a filename
if ( argc < 2 ) {
printUsage( argv[0] );
return 1;
}
// process any command-line switches
bool showURN = false;
bool showAuthor = false;
bool showTitle = false;
bool showEncoding = false;
bool showVersion = false;
bool showDependencies = false;
bool showDependencyTitles = false;
bool showDescription = false;
for ( int i = 1 ; i < argc - 1 ; i++ ) {
QString compare( argv[i] );
if ( compare.startsWith( "-" ) ) {
for ( int j = 1 ; j < compare.length() ; j++ ) {
QChar cj = compare[j];
if ( cj == 'U' ) {
showDependencies = true;
} else if ( cj == 'T' ) {
showDependencyTitles = true;
} else if ( cj == 'd' ) {
showDescription = true;
} else if ( cj == 'a' ) {
showAuthor = true;
} else if ( cj == 'e' ) {
showEncoding = true;
} else if ( cj == 't' ) {
showTitle = true;
} else if ( cj == 'u' ) {
showURN = true;
} else if ( cj == 'v' ) {
showVersion = true;
} else {
qDebug() << "Unknown switch:" << cj;
printUsage( argv[0] );
return 1;
}
}
} else {
printUsage( argv[0] );
return 1;
}
}
// load the file and print any errors that happen while doing so
QFile f( argv[argc-1] );
QString maybeError;
Lob loaded = Lob::fromXML( f, &maybeError );
if ( maybeError.length() > 0 ) {
qDebug( "%s", maybeError.toLatin1().constData() );
return 2;
}
if ( loaded.numChildren() < 1 ) {
qDebug( "The file %s contains no OpenMath objects",
f.fileName().toLatin1().constData() );
return 3;
}
if ( loaded.numChildren() > 1 ) {
qDebug( "The file %s contains more than one OpenMath object",
f.fileName().toLatin1().constData() );
return 4;
}
loaded.set( loaded.child() );
if ( !loaded.isDocument() ) {
qDebug( "The file %s does not contain a Lurch Document",
f.fileName().toLatin1().constData() );
return 5;
}
// success; print what they asked for from the file
// or the URN if they didn't ask for anything
if ( showURN || (!showDependencies && !showDescription && !showDependencyTitles
&& !showAuthor && !showEncoding && !showTitle && !showVersion) ) {
printf( "%s\n", loaded.getURN().toLatin1().constData() );
}
if ( showTitle ) {
printf( "Title: %s\n", loaded.getTitle().toLatin1().constData() );
}
if ( showAuthor ) {
printf( "Author: %s\n", loaded.getAuthor().toLatin1().constData() );
}
if ( showVersion ) {
printf( "Version: %s\n", loaded.getVersion().toLatin1().constData() );
}
if ( showEncoding ) {
printf( "Encoding Language: %s\n", loaded.getEncodingLanguage().toLatin1().constData() );
}
if ( showDescription ) {
Lob key = Lob::fromXML( "<OMS cd=\"LurchCore\" "
"name=\"topic_description\"/>" ).child();
Lob desc = loaded.attribute( key );
if ( desc.isEmpty() ) {
printf( "no description\n" );
} else if ( desc.nodeType() != OmStringType ) {
printf( "document description not a string" );
} else {
printf( "Description: %s\n",
desc.toVariant().toString().toLatin1().constData() );
}
}
if ( showDependencies || showDependencyTitles ) {
QStringList deps = loaded.getDependencies();
for ( int i = 0 ; i < deps.count() ; i++ ) {
if ( showDependencyTitles ) {
QStringList depPieces = Lob::splitLurchURN( deps[i] );
printf( "Dependency %d: %s\n", i,
depPieces[0].toLatin1().constData() );
} else {
printf( "Dependency %d: %s\n", i,
deps[i].toLatin1().constData() );
}
}
}
// done, no errors
return 0;
}