|
STK++ 1.0
|
A IPage is an interface base class for reading and/or writing a page of option in a file. More...
#include <STK_IPage.h>


Public Types | |
| typedef std::vector< Option > | ContOption |
| Container for the options. | |
Public Member Functions | |
| IPage (String const &name, Integer const &level, bool isOptional) | |
| Constructor. | |
| IPage (IPage const &page) | |
| Copy constructor. | |
| virtual | ~IPage () |
| Destructor. | |
| String const & | name () const |
| name of the IPage. | |
| bool | isOptional () const |
| is this Option optional ?. | |
| ContOption const & | options () const |
| get the options of the IPage | |
| Option & | option (String const &name) |
| bookkeeping function. | |
| Option const & | option (String const &name) const |
| bookkeeping function. | |
| Option const & | option (Integer const &pos) const |
| get the ith constant option of the IPage | |
| Option & | option (Integer const &pos) |
| get the ith option of the IPage | |
| Option const & | operator[] (Integer const &pos) const |
| get the ith constant option of the IPage | |
| Option & | operator[] (Integer const &pos) |
| get the ith option of the IPage | |
| void | addOption (Option const &opt) |
| add an option to the page | |
| void | addPage (IPage const &page) |
| add a sub-page as an option to the page | |
| void | read (istream &is) |
| read in options from an input steam stream | |
| void | write (ostream &os) const |
| write out options in a stream | |
| virtual bool | validate () |
| validate the page. | |
| virtual IPage * | clone () const |
| validate the page. | |
Protected Attributes | |
| bool | isOptional_ |
true if the Page is optinal, false otherwise | |
| ContOption | options_ |
| array of the options | |
| String | msg_error_ |
| Contain the last error message. | |
Private Member Functions | |
| bool | findKeyword (istream &is) const |
| process the input stream until the keyword is encountered. | |
| bool | processLine (String const &line) |
| process the input line and set the value of the option. | |
Private Attributes | |
| String | name_ |
| name of the page of options | |
| Integer | level_ |
| level of the Page. | |
| String | keyword_ |
| keyword of the page of options. | |
A IPage is an interface base class for reading and/or writing a page of option in a file.
A page of option have the form
# My favorites [Favorites] computer = fruit color = vaguely grey [[softwares]] os = linux # error else ? ide = eclipse
The 'Favorites' page has a 'softwares' subpage. Pages are indicated using the page name in square brackets. Subpage are made by increasing the number of matching square brackets around the page name.
Note that the whitespace are not significants in this example, but as with code indentation visually shows the structure of the information.
Comments can be added using the symbol '#'. All remaining character on the same line are ignored.
Every Page of options of a STK program should derive from the IPage class as in the following example:
class Page2 : public IPage { public: Page2() : IPage("test2", 1, false) { options_.push_back(Option("opt1", real_, false)); options_.push_back(Option("opt2", string_, false)); options_.push_back(Option("opt3", lreal_, false)); options_.push_back(Option("opt4", lstring_, true)); options_.push_back(Option("opt5", lstring_, false)); } Real const& opt1() const { return options_[0].get(Real()); } String const& opt2() const { return options_[1].get(String()); } std::list<Real> const& opt3() const { return options_[2].get(std::list<Real>()); } };
Definition at line 102 of file STK_IPage.h.
| typedef std::vector<Option> STK::IPage::ContOption |
Container for the options.
Definition at line 106 of file STK_IPage.h.
Constructor.
The constructor will initialize the name of the page (in upper case), and the keyword usign the form [...[NAME]...] where the number of open/closing bracket are given by the level.
| name | name of the page |
| level | level of the page |
| isOptional | true if the page is optional, false otherwise |
Definition at line 77 of file STK_IPage.cpp.
References STK::createKeyWord(), keyword_, name_, and STK::toUpperString().
Referenced by clone().
: isOptional_(isOptional) , name_(name) , level_(level) , keyword_(name_) { toUpperString(name_); toUpperString(keyword_); createKeyWord(keyword_, level); }

| STK::IPage::IPage | ( | IPage const & | page | ) |
Copy constructor.
| page | the page to copy |
Definition at line 95 of file STK_IPage.cpp.
References isOptional_, keyword_, level_, name_, and options_.
{
name_ = page.name_;
level_ = page.level_;
isOptional_ = page.isOptional_;
keyword_ = page.keyword_;
options_ = page.options_;
}
| STK::IPage::~IPage | ( | ) | [virtual] |
| String const& STK::IPage::name | ( | ) | const [inline] |
| bool STK::IPage::isOptional | ( | ) | const [inline] |
is this Option optional ?.
true if the Option is optional, false otherwise Definition at line 133 of file STK_IPage.h.
References isOptional_.
{ return isOptional_;}
| ContOption const& STK::IPage::options | ( | ) | const [inline] |
get the options of the IPage
Definition at line 137 of file STK_IPage.h.
References options_.
{ return options_;};
bookkeeping function.
Find an Option given its name
| name | name of the Option to find |
Definition at line 217 of file STK_IPage.cpp.
References _T, msg_error_, name(), options_, and STK::toUpperString().
{
String Uname = toUpperString(name);
// read all pages
for (ContOption::iterator it = options_.begin(); it != options_.end(); it++)
{
// read curent page
if (it->name() == Uname) return *it;
}
msg_error_ = _T("In IPage::option(");
msg_error_ += name;
msg_error_ +=_T("); Option not found\n");
throw runtime_error(msg_error_);
}

bookkeeping function.
Find an Option given its name
| name | name of the Option to find |
Definition at line 198 of file STK_IPage.cpp.
References _T, msg_error_, name(), options_, and STK::toUpperString().
{
String Uname = toUpperString(name);
// read all pages
for (ContOption::const_iterator it = options_.begin(); it != options_.end(); it++)
{
// read curent page
if (it->name() == Uname) return *it;
}
msg_error_ = _T("In Ipage::Option(");
msg_error_ += name;
msg_error_ +=_T(") const; Option not found\n");
throw runtime_error(msg_error_);
}

| void STK::IPage::addOption | ( | Option const & | opt | ) | [inline] |
add an option to the page
| opt | the option to add |
Definition at line 182 of file STK_IPage.h.
References options_.
Referenced by addPage().
{ return options_.push_back(opt);}
| void STK::IPage::addPage | ( | IPage const & | page | ) |
add a sub-page as an option to the page
| page | the option to add |
Definition at line 107 of file STK_IPage.cpp.
References addOption(), STK::createKeyWord(), keyword_, and level_.
{
// look how many bracket
page.level_ = level_ +1;
createKeyWord(page.keyword_, page.level_);
addOption(Option(page));
}

| void STK::IPage::read | ( | istream & | is | ) |
read in options from an input steam stream
| is | input stream |
Definition at line 119 of file STK_IPage.cpp.
References _T, STK::CHAR_BLANK, STK::CHAR_COMMENT, STK::CHAR_EQUAL, STK::CHAR_OPENBRACKET, STK::CHAR_TAB, findKeyword(), isOptional_, keyword_, msg_error_, name_, options_, STK::Option::page_, processLine(), STK::DManager::removeCharBeforeAndAfter(), STK::removeComments(), STK::string, and validate().
Referenced by STK::Option::read().
{
// find page
if (!findKeyword(is))
{
if ( !isOptional_ ) // throw error message if the page is not optional
{
msg_error_ = "Page " + std::string(_T(keyword_.c_str())) + " not found \n";
throw runtime_error(msg_error_);
}
else return;
}
// get current position of the stream as we will need to pass twice
istream::pos_type pos = is.tellg();
// read standard options for that page until eof or a new option is discovered
String line;
while (std::getline(is, line))
{
// remove comments
removeComments(line, CHAR_COMMENT);
DManager::removeCharBeforeAndAfter(line, CHAR_BLANK);
DManager::removeCharBeforeAndAfter(line, CHAR_TAB);
// nothing to do
if (line.empty()) continue;
// we encounter a page name
if (line.at(0) == CHAR_OPENBRACKET) break;
// ignore line if it is not an option
if (line.find_first_of(CHAR_EQUAL) != line.npos)
{
if (!processLine(line))
throw runtime_error(msg_error_);
}
else
{
msg_error_ = "ERROR. In page " + std::string(_T(name_.c_str()))
+ ". Incorrect line.\n";
throw runtime_error(msg_error_);
}
}
// clear states
is.clear();
// read sub-option pages
for( ContOption::iterator it = options_.begin(); it != options_.end(); it++)
{
if (it->type() == Option::page_)
{ // set back iostream
is.seekg(pos);
// and read the sub-page
it->read(is);
}
}
// validate reading
validate();
}

| void STK::IPage::write | ( | ostream & | os | ) | const |
write out options in a stream
| os | output stream |
Definition at line 178 of file STK_IPage.cpp.
References STK::CHAR_BLANK, keyword_, level_, options_, STK::Option::page_, and STK::STRING_NL.
Referenced by STK::Option::write().
{
// padding
const Integer nbWhiteSpace = 2*(level_-1);
const String padding = String((int)nbWhiteSpace, CHAR_BLANK);
// write keyword
os << padding << keyword_ << STRING_NL;
// write options
for( ContOption::const_iterator it = options_.begin(); it != options_.end(); it++)
{
os << padding; it->write(os);
if (it->type() != Option::page_) os << STRING_NL;
}
os.flush();
}
| virtual bool STK::IPage::validate | ( | ) | [inline, virtual] |
validate the page.
Check if the options are coherent.
Reimplemented in STK::LocalVariancePage, and STK::AdditiveBSplineRegressionPage.
Definition at line 201 of file STK_IPage.h.
Referenced by STK::Option::read(), and read().
{ return true; }
| virtual IPage* STK::IPage::clone | ( | ) | const [inline, virtual] |
validate the page.
Check if the options are coherent.
Reimplemented in STK::LocalVariancePage, and STK::AdditiveBSplineRegressionPage.
Definition at line 205 of file STK_IPage.h.
References IPage().
Referenced by STK::ReadWritePages::addPage(), and STK::Option::setPage().
{ return new IPage(*this); }

| bool STK::IPage::findKeyword | ( | istream & | is | ) | const [private] |
process the input stream until the keyword is encountered.
| is | input stream to process |
true if the keyword have been found, false otherwise Definition at line 234 of file STK_IPage.cpp.
References STK::CHAR_BLANK, STK::CHAR_COMMENT, STK::CHAR_TAB, keyword_, STK::DManager::removeCharBeforeAndAfter(), STK::removeComments(), and STK::toUpperString().
Referenced by read().
{
// Reading lines
String line;
// search page
while (std::getline(is, line))
{
// remove comments and space characters before and after
removeComments(line, CHAR_COMMENT);
DManager::removeCharBeforeAndAfter(line, CHAR_BLANK);
DManager::removeCharBeforeAndAfter(line, CHAR_TAB);
toUpperString(line);
// check if the the keyword_ is encountered
if (line == keyword_) return true ;
}
// the keyword_ have not been found
return false;
}

| bool STK::IPage::processLine | ( | String const & | line | ) | [private] |
process the input line and set the value of the option.
| line | input string to process |
true if the keyword have been found, false otherwise Definition at line 257 of file STK_IPage.cpp.
References STK::CHAR_BLANK, STK::CHAR_EQUAL, STK::CHAR_TAB, msg_error_, name_, options_, STK::DManager::removeCharBeforeAndAfter(), and STK::toUpperString().
Referenced by read().
{
String::size_type pos = line.find_first_of(CHAR_EQUAL);
// get the option name before '='
String optName = line.substr(0,pos);
DManager::removeCharBeforeAndAfter(optName, CHAR_BLANK);
DManager::removeCharBeforeAndAfter(optName, CHAR_TAB);
toUpperString(optName);
// get the option value after character '='
String optValue = line.substr(pos+1);
DManager::removeCharBeforeAndAfter(optValue, CHAR_BLANK);
DManager::removeCharBeforeAndAfter(optValue, CHAR_TAB);
// iterate among all the options and find the option
for( ContOption::iterator it = options_.begin(); it != options_.end(); it++)
{
// compare the name of the option with those into the page
if (it->name().compare(optName) == 0)
{
it->setValue(optValue);
return true;
}
}
// the option name is not in the page
msg_error_ = "ERROR. In " + name_ + ", option: " + optName
+ " is unknown.\n";
return false;
}

bool STK::IPage::isOptional_ [protected] |
true if the Page is optinal, false otherwise
Definition at line 210 of file STK_IPage.h.
Referenced by IPage(), isOptional(), and read().
ContOption STK::IPage::options_ [protected] |
array of the options
Definition at line 212 of file STK_IPage.h.
Referenced by STK::AdditiveBSplineRegressionPage::AdditiveBSplineRegressionPage(), addOption(), IPage(), STK::LocalVariancePage::LocalVariancePage(), operator[](), option(), options(), processLine(), read(), STK::LocalVariancePage::validate(), STK::AdditiveBSplineRegressionPage::validate(), and write().
String STK::IPage::msg_error_ [mutable, protected] |
Contain the last error message.
Definition at line 215 of file STK_IPage.h.
Referenced by option(), processLine(), read(), STK::LocalVariancePage::validate(), and STK::AdditiveBSplineRegressionPage::validate().
String STK::IPage::name_ [private] |
name of the page of options
Definition at line 219 of file STK_IPage.h.
Referenced by IPage(), name(), processLine(), and read().
Integer STK::IPage::level_ [mutable, private] |
String STK::IPage::keyword_ [mutable, private] |
keyword of the page of options.
The keyword is constructed using the level_ and the name_. It is of the form [...[name_]...] where the number of open/closing bracket is given by level_.
Definition at line 229 of file STK_IPage.h.
Referenced by addPage(), findKeyword(), IPage(), read(), and write().