Bsoft  2.1.0
Bernard's software package
bxml.cpp
Go to the documentation of this file.
1 
9 #include "rwxml.h"
10 #include "utilities.h"
11 #include "options.h"
12 #include "timer.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
17 // Usage assistence
18 const char* use[] = {
19 " ",
20 "Usage: bxml [options] input.xml [input.xml]",
21 "-------------------------------------------",
22 "Reads, writes and validates XML files.",
23 " ",
24 "Parameters:",
25 "-verbose 7 Verbosity of output.",
26 " ",
27 "Input:",
28 "-validate mg.xsd Validate XML input file(s) with this schema.",
29 " ",
30 "Output:",
31 "-output file.xml Output XML format file.",
32 " ",
33 NULL
34 };
35 
36 int main(int argc, char **argv)
37 {
38  // Initialize variables
39  Bstring xsdfile; // XML schema file
40  Bstring outfile; // Output XML format file
41 
42  int optind;
43  Boption* option = get_option_list(use, argc, argv, optind);
44  Boption* curropt;
45  for ( curropt = option; curropt; curropt = curropt->next ) {
46  if ( curropt->tag == "validate" )
47  xsdfile = curropt->filename();
48  if ( curropt->tag == "output" )
49  outfile = curropt->filename();
50  }
51  option_kill(option);
52 
53  double ti = timer_start();
54 
55  // Read all the parameter files
56  Bstring* file_list = NULL;
57  while ( optind < argc ) string_add(&file_list, argv[optind++]);
58  if ( !file_list ) {
59  cerr << "Error: No XML files specified!" << endl;
60  bexit(-1);
61  }
62 
63  Bstring* thisfile;
64  xmlDocPtr doc;
65 
66  for ( thisfile = file_list; thisfile; thisfile = thisfile->next ) {
67  doc = xmlParseFile(thisfile->c_str());
68  if ( !doc ) {
69  cerr << "Error: " << *thisfile << " not parsed!" << endl;
70  bexit(-1);
71  } else {
72  if ( verbose & VERB_PROCESS )
73  cout << "# Reading file: " << *thisfile << endl;
74  if ( xsdfile.length() ) xml_validate(doc, xsdfile);
75  }
76  if ( outfile.length() ) xmlSaveFormatFile(outfile.c_str(), doc, 1);
77  xmlFreeDoc(doc);
78  }
79 
80  if ( verbose & VERB_TIME )
81  timer_report(ti);
82 
83  bexit(0);
84 }
85 
Boption
A class for a command line input option.
Definition: options.h:26
rwxml.h
Reads and writes micrograph XML files.
Bstring
Definition: Bstring.h:29
Boption::next
Boption * next
Definition: options.h:28
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
verbose
int verbose
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
options.h
Header file for option handlers.
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Boption::tag
Bstring tag
Definition: options.h:29
xml_validate
int xml_validate(const xmlDocPtr doc, Bstring &xsdfile)
timer.h
Utilities for timing functions.
main
int main(int argc, char **argv)
Definition: bxml.cpp:36
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bstring::next
Bstring * next
Definition: Bstring.h:31
use
const char * use[]
Definition: bxml.cpp:18
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713