BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
test_IFile.cxx
Go to the documentation of this file.
1/// Test program for IFile facility within xmlBase package.
2
3#include "xmlBase/IFile.h"
4
5#include <iostream>
6#include <string>
7
8void lookFor( xmlBase::IFile* ifile, const char* section, const char* item );
9
10int main() {
11
12 std::string filename( "$(XMLBASEROOT)/xml/myIFile.xml" );
13 xmlBase::IFile* ifile = 0;
14
15 ifile = new xmlBase::IFile( filename.c_str() );
16 // fetch some stuff
17
18 if ( ifile )
19 {
20
21 lookFor( ifile, "section1", "section1-val1" );
22 lookFor( ifile, "section1", "section1-val2" );
23 lookFor( ifile, "subsection", "subsectionItem" );
24 lookFor( ifile, "section2", "subsectionItem" );
25 lookFor( ifile, "section2", "bad-int" );
26 }
27 else std::cout << "Unable to read file " << ifile << std::endl;
28
29 // Now do it again for IFile referencing schema rather than dtd
30 std::string filename2( "$(XMLBASEROOT)/xml/mySchemaIFile.xml" );
31
32 std::cout << std::endl << std::endl;
33 std::cout << "And for my next trick: process an IFile using XML Schema.";
34 std::cout << std::endl;
35 std::cout << "This file should be flagged bad; it doesn't follow schema content model."
36 << std::endl
37 << std::endl;
38
39 xmlBase::IFile* ifile2 = 0;
40
41 ifile2 = new xmlBase::IFile( filename2.c_str() );
42 // fetch some stuff
43
44 if ( ifile2 )
45 {
46
47 lookFor( ifile2, "section1", "section1-val1" );
48 lookFor( ifile2, "section1", "section1-val2" );
49 lookFor( ifile2, "subsection", "subsectionItem" );
50 lookFor( ifile2, "section2", "subsectionItem" );
51 lookFor( ifile2, "section2", "bad-int" );
52 }
53 else std::cout << "Unable to read file " << ifile2 << std::endl;
54
55 return ( 0 );
56}
57
58void lookFor( xmlBase::IFile* ifile, const char* section, const char* item ) {
59 bool haveItem = ifile->contains( section, item );
60 std::cout << "Item " << item << " in section " << section;
61
62 if ( haveItem )
63 {
64 std::cout << " was found." << std::endl;
65 // try writing in various formats
66 const char* charRep = ifile->getString( section, item );
67 std::cout << "..as a string = " << charRep << std::endl;
68
69 try
70 {
71 int intRep = ifile->getInt( section, item );
72 std::cout << "..as an int = " << intRep << std::endl;
73 } catch ( xmlBase::IFileException toCatch )
74 { std::cout << "**ERROR** int conversion failed " << std::endl << std::endl; }
75
76 try
77 {
78 double doubleRep = ifile->getDouble( section, item );
79 std::cout << "..as a double = " << doubleRep << std::endl;
80 } catch ( xmlBase::IFileException toCatch )
81 { std::cout << " **ERROR** double conversion failed " << std::endl << std::endl; }
82 }
83 else std::cout << " was NOT found." << std::endl;
84}
virtual int getInt(const char *section, const char *item)
Definition IFile.cxx:317
virtual bool contains(const char *section, const char *item)
Definition IFile.cxx:221
virtual const char * getString(const char *section, const char *item)
Definition IFile.cxx:274
virtual double getDouble(const char *section, const char *item)
Definition IFile.cxx:302
void lookFor(xmlBase::IFile *ifile, const char *section, const char *item)
Test program for IFile facility within xmlBase package.
int main()