[go: up one dir, main page]

Menu

[80c337]: / utils / geturn / main.cpp  Maximize  Restore  History

Download this file

141 lines (134 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#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;
}