STK++ 1.0
STK::IPage Class Reference

A IPage is an interface base class for reading and/or writing a page of option in a file. More...

#include <STK_IPage.h>

Inheritance diagram for STK::IPage:
Collaboration diagram for STK::IPage:

List of all members.

Public Types

typedef std::vector< OptionContOption
 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
Optionoption (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
Optionoption (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
Optionoperator[] (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 IPageclone () 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.

Detailed Description

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.


Member Typedef Documentation

typedef std::vector<Option> STK::IPage::ContOption

Container for the options.

Definition at line 106 of file STK_IPage.h.


Constructor & Destructor Documentation

STK::IPage::IPage ( String const &  name,
Integer const &  level,
bool  isOptional 
)

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.

Parameters:
namename of the page
levellevel of the page
isOptionaltrue 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().

Here is the call graph for this function:

STK::IPage::IPage ( IPage const &  page)

Copy constructor.

Parameters:
pagethe 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]

Destructor.

Definition at line 89 of file STK_IPage.cpp.

{ }

Member Function Documentation

String const& STK::IPage::name ( ) const [inline]

name of the IPage.

Returns:
the name of the IPage

Definition at line 129 of file STK_IPage.h.

References name_.

Referenced by option().

{ return name_;}
bool STK::IPage::isOptional ( ) const [inline]

is this Option optional ?.

Returns:
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

Returns:
the container with the options

Definition at line 137 of file STK_IPage.h.

References options_.

{ return options_;};
Option & STK::IPage::option ( String const &  name)

bookkeeping function.

Find an Option given its name

Parameters:
namename of the Option to find
Returns:
NULL if the variable is not found, the page otherwise

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_);
}

Here is the call graph for this function:

Option const & STK::IPage::option ( String const &  name) const

bookkeeping function.

Find an Option given its name

Parameters:
namename of the Option to find
Returns:
NULL if the variable is not found, the page otherwise

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_);
}

Here is the call graph for this function:

Option const& STK::IPage::option ( Integer const &  pos) const [inline]

get the ith constant option of the IPage

Parameters:
posthe position of the option we want to get
Returns:
the pos-th Option

Definition at line 155 of file STK_IPage.h.

References options_.

    { return options_.at(ContOption::size_type(pos));}
Option& STK::IPage::option ( Integer const &  pos) [inline]

get the ith option of the IPage

Parameters:
posthe position of the option we want to get
Returns:
the pos-th Option

Definition at line 162 of file STK_IPage.h.

References options_.

    { return options_.at(ContOption::size_type(pos));}
Option const& STK::IPage::operator[] ( Integer const &  pos) const [inline]

get the ith constant option of the IPage

Parameters:
posthe position of the option we want to get
Returns:
the pos-th Option

Definition at line 169 of file STK_IPage.h.

References options_.

    { return options_.at(ContOption::size_type(pos));}
Option& STK::IPage::operator[] ( Integer const &  pos) [inline]

get the ith option of the IPage

Parameters:
posthe position of the option we want to get
Returns:
the pos-th Option

Definition at line 176 of file STK_IPage.h.

References options_.

    { return options_.at(ContOption::size_type(pos));}
void STK::IPage::addOption ( Option const &  opt) [inline]

add an option to the page

Parameters:
optthe 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

Parameters:
pagethe 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));
}

Here is the call graph for this function:

void STK::IPage::read ( istream is)

read in options from an input steam stream

Parameters:
isinput 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();
}

Here is the call graph for this function:

void STK::IPage::write ( ostream os) const

write out options in a stream

Parameters:
osoutput 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); }

Here is the call graph for this function:

bool STK::IPage::findKeyword ( istream is) const [private]

process the input stream until the keyword is encountered.

Parameters:
isinput stream to process
Returns:
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;
}

Here is the call graph for this function:

bool STK::IPage::processLine ( String const &  line) [private]

process the input line and set the value of the option.

Parameters:
lineinput string to process
Returns:
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;
}

Here is the call graph for this function:


Member Data Documentation

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().

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().

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]

level of the Page.

This is a mutable element as it depends only of its position in the Option file and not of the values.

Definition at line 223 of file STK_IPage.h.

Referenced by addPage(), IPage(), and write().

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().


The documentation for this class was generated from the following files: