STK++ 0.9.13
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:
Inheritance graph

Public Types

typedef std::vector< OptionContOption
 Container for the options.
 

Public Member Functions

 IPage (String const &name, int const &level, bool isOptional)
 Constructor.
 
 IPage (IPage const &page)
 Copy constructor.
 
virtual ~IPage ()
 Destructor.
 
String constname () const
 name of the IPage.
 
bool isOptional () const
 is this Option optional ?.
 
ContOption constoptions () const
 get the options of the IPage
 
String constmsg_error () const
 name of the IPage.
 
Optionoption (String const &name)
 bookkeeping function.
 
Option constoption (String const &name) const
 bookkeeping function.
 
Option constoption (int const &pos) const
 get the ith constant option of the IPage
 
Optionoption (int const &pos)
 get the ith option of the IPage
 
Option constoperator[] (int const &pos) const
 get the ith constant option of the IPage
 
Optionoperator[] (int 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
 

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
 
int 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
os = linux # error else ?
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...

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>()); }
};
A IPage is an interface base class for reading and/or writing a page of option in a file.
Definition STK_IPage.h:94
the Option class allow to store the value of an option from a IPage.
Definition STK_Option.h:56
double Real
STK fundamental type of Real values.

Definition at line 93 of file STK_IPage.h.

Member Typedef Documentation

◆ ContOption

Container for the options.

Definition at line 97 of file STK_IPage.h.

Constructor & Destructor Documentation

◆ IPage() [1/2]

STK::IPage::IPage ( String const name,
int 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 72 of file STK_IPage.cpp.

74 , name_(name)
75 , level_(level)
77{
80 createKeyWord(keyword_, level);
81}
String const & name() const
name of the IPage.
Definition STK_IPage.h:120
String keyword_
keyword of the page of options.
Definition STK_IPage.h:218
int level_
level of the Page.
Definition STK_IPage.h:212
bool isOptional() const
is this Option optional ?.
Definition STK_IPage.h:124
String name_
name of the page of options
Definition STK_IPage.h:208
bool isOptional_
true if the Page is optinal, false otherwise
Definition STK_IPage.h:199
String const & toUpperString(String &s)
convert the characters of the String to upper case
Definition STK_String.h:134

References keyword_, name_, and STK::toUpperString().

◆ IPage() [2/2]

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

Copy constructor.

Parameters
pagethe page to copy

Definition at line 89 of file STK_IPage.cpp.

90{
91 name_ = page.name_;
92 level_ = page.level_;
93 isOptional_ = page.isOptional_;
94 keyword_ = page.keyword_;
95 options_ = page.options_;
96}
ContOption options_
array of the options
Definition STK_IPage.h:201

References isOptional_, keyword_, level_, name_, and options_.

◆ ~IPage()

STK::IPage::~IPage ( )
virtual

Destructor.

Definition at line 84 of file STK_IPage.cpp.

84{ }

Member Function Documentation

◆ addOption()

void STK::IPage::addOption ( Option const opt)
inline

add an option to the page

Parameters
optthe option to add

Definition at line 177 of file STK_IPage.h.

178 { return options_.push_back(opt);}

References options_.

Referenced by addPage().

◆ addPage()

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 101 of file STK_IPage.cpp.

102{
103 // look how many bracket
104 page.level_ = level_ +1;
105 createKeyWord(page.keyword_, page.level_);
106 addOption(Option(page));
107}
void addOption(Option const &opt)
add an option to the page
Definition STK_IPage.h:177

References addOption(), and level_.

◆ clone()

virtual IPage * STK::IPage::clone ( ) const
inlinevirtual
Returns
a clone of this. Overload this method if you add members

Reimplemented in STK::AdditiveBSplineRegressionPage, and STK::LocalVariancePage.

Definition at line 195 of file STK_IPage.h.

195{ return new IPage(*this); }
IPage(String const &name, int const &level, bool isOptional)
Constructor.
Definition STK_IPage.cpp:72

Referenced by STK::Option::setPage().

◆ findKeyword()

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 228 of file STK_IPage.cpp.

229{
230 // Reading lines
231 String line;
232 // search page
233 while (std::getline(is, line))
234 {
235 // remove comments and space characters before and after
236 removeComments(line, CHAR_COMMENT);
237 DManager::removeCharBeforeAndAfter(line, CHAR_BLANK);
239 toUpperString(line);
240 // check if the the keyword_ is encountered
241 if (line == keyword_) return true ;
242 }
243 // the keyword_ have not been found
244 return false;
245}
void removeCharBeforeAndAfter(String &str, Char c)
remove all occurrences of the char c at the beginning and the end of the string str.
std::basic_string< Char > String
STK fundamental type of a String.

References keyword_, STK::DManager::removeCharBeforeAndAfter(), and STK::toUpperString().

Referenced by read().

◆ isOptional()

bool STK::IPage::isOptional ( ) const
inline

is this Option optional ?.

Returns
true if the Option is optional, false otherwise

Definition at line 124 of file STK_IPage.h.

124{ return isOptional_;}

References isOptional_.

◆ msg_error()

String const & STK::IPage::msg_error ( ) const
inline

name of the IPage.

Returns
the name of the IPage

Definition at line 132 of file STK_IPage.h.

132{ return msg_error_;}
String msg_error_
Contain the last error message.
Definition STK_IPage.h:204

References msg_error_.

Referenced by STK::Option::read().

◆ name()

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

name of the IPage.

Returns
the name of the IPage

Definition at line 120 of file STK_IPage.h.

120{ return name_;}

References name_.

Referenced by option(), and option().

◆ operator[]() [1/2]

Option & STK::IPage::operator[] ( int 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 171 of file STK_IPage.h.

172 { return options_.at(ContOption::size_type(pos));}

References options_.

◆ operator[]() [2/2]

Option const & STK::IPage::operator[] ( int 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 164 of file STK_IPage.h.

165 { return options_.at(ContOption::size_type(pos));}

References options_.

◆ option() [1/4]

Option & STK::IPage::option ( int 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 157 of file STK_IPage.h.

158 { return options_.at(ContOption::size_type(pos));}

References options_.

◆ option() [2/4]

Option const & STK::IPage::option ( int 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 150 of file STK_IPage.h.

151 { return options_.at(ContOption::size_type(pos));}

References options_.

◆ option() [3/4]

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 211 of file STK_IPage.cpp.

212{
213 String Uname = toUpperString(name);
214 // read all pages
215 for (ContOption::iterator it = options_.begin(); it != options_.end(); it++)
216 {
217 // read curent page
218 if (it->name() == Uname) return *it;
219 }
220 msg_error_ = _T("In IPage::option(");
221 msg_error_ += name;
222 msg_error_ +=_T("); Option not found\n");
223 STKRUNTIME_ERROR_NO_ARG(Ipage::option, msg_error_);
224}
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
#define _T(x)
Let x unmodified.

References _T, msg_error_, name(), options_, STKRUNTIME_ERROR_NO_ARG, and STK::toUpperString().

◆ option() [4/4]

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 192 of file STK_IPage.cpp.

193{
194 String Uname = toUpperString(name);
195 // read all pages
196 for (ContOption::const_iterator it = options_.begin(); it != options_.end(); it++)
197 {
198 // read curent page
199 if (it->name() == Uname) return *it;
200 }
201 msg_error_ = _T("In Ipage::Option(");
202 msg_error_ += name;
203 msg_error_ +=_T(") const; Option not found\n");
204 STKRUNTIME_ERROR_NO_ARG(Ipage::option, msg_error_);
205}

References _T, msg_error_, name(), options_, STKRUNTIME_ERROR_NO_ARG, and STK::toUpperString().

◆ options()

ContOption const & STK::IPage::options ( ) const
inline

get the options of the IPage

Returns
the container with the options

Definition at line 128 of file STK_IPage.h.

128{ return options_;};

References options_.

◆ processLine()

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 251 of file STK_IPage.cpp.

252{
253 String::size_type pos = line.find_first_of(CHAR_EQUAL);
254 // get the option name before '='
255 String optName = line.substr(0,pos);
256 DManager::removeCharBeforeAndAfter(optName, CHAR_BLANK);
257 DManager::removeCharBeforeAndAfter(optName, CHAR_TAB);
258 toUpperString(optName);
259 // get the option value after character '='
260 String optValue = line.substr(pos+1);
261 DManager::removeCharBeforeAndAfter(optValue, CHAR_BLANK);
262 DManager::removeCharBeforeAndAfter(optValue, CHAR_TAB);
263 // iterate among all the options and find the option
264 for( ContOption::iterator it = options_.begin(); it != options_.end(); it++)
265 {
266 // compare the name of the option with those into the page
267 if (it->name().compare(optName) == 0)
268 {
269 it->setValue(optValue);
270 return true;
271 }
272 }
273 // the option name is not in the page
274 msg_error_ = "ERROR. In " + name_ + ", option: " + optName
275 + " is unknown.\n";
276 return false;
277}

References msg_error_, name_, options_, STK::DManager::removeCharBeforeAndAfter(), and STK::toUpperString().

Referenced by read().

◆ read()

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

read in options from an input steam stream

Parameters
isinput stream

Definition at line 113 of file STK_IPage.cpp.

114{
115 // find page
116 if (!findKeyword(is))
117 {
118 if ( !isOptional_ ) // throw error message if the page is not optional
119 {
120 msg_error_ = "Page " + std::string(_T(keyword_.c_str())) + " not found \n";
122 }
123 else return;
124 }
125 // get current position of the stream as we will need to pass twice
126 istream::pos_type pos = is.tellg();
127
128 // read standard options for that page until eof or a new option is discovered
129 String line;
130 while (std::getline(is, line))
131 {
132 // remove comments
133 removeComments(line, CHAR_COMMENT);
134 DManager::removeCharBeforeAndAfter(line, CHAR_BLANK);
136 // nothing to do
137 if (line.empty()) continue;
138 // we encounter a page name
139 if (line.at(0) == CHAR_OPENBRACKET) break;
140 // ignore line if it is not an option
141 if (line.find_first_of(CHAR_EQUAL) != line.npos)
142 {
143 if (!processLine(line))
145 }
146 else
147 {
148 msg_error_ = "ERROR. In page " + std::string(_T(name_.c_str()))
149 + ". Incorrect line.\n";
151 }
152 }
153 // clear states
154 is.clear();
155 // read sub-option pages
156 for( ContOption::iterator it = options_.begin(); it != options_.end(); it++)
157 {
158 if (it->type() == Option::page_)
159 { // set back iostream
160 is.seekg(pos);
161 // and read the sub-page
162 it->read(is);
163 }
164 }
165 // validate reading
166 validate();
167}
virtual bool validate()
validate the page.
Definition STK_IPage.h:193
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.
void read(istream &is)
read in options from an input steam stream
@ page_
a page option
Definition STK_Option.h:82

References _T, findKeyword(), isOptional_, keyword_, msg_error_, name_, options_, STK::Option::page_, processLine(), read(), STK::DManager::removeCharBeforeAndAfter(), STKRUNTIME_ERROR_NO_ARG, and validate().

Referenced by read(), and STK::Option::read().

◆ validate()

virtual bool STK::IPage::validate ( )
inlinevirtual

validate the page.

Check if the options are coherent.

Reimplemented in STK::AdditiveBSplineRegressionPage, and STK::LocalVariancePage.

Definition at line 193 of file STK_IPage.h.

193{ return true; }

Referenced by read(), and STK::Option::read().

◆ write()

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

write out options in a stream

Parameters
osoutput stream

Definition at line 172 of file STK_IPage.cpp.

173{
174 // padding
175 const int nbWhiteSpace = 2*(level_-1);
176 const String padding = String((int)nbWhiteSpace, CHAR_BLANK);
177 // write keyword
178 os << padding << keyword_ << _T("\n");
179 // write options
180 for( ContOption::const_iterator it = options_.begin(); it != options_.end(); it++)
181 {
182 os << padding; it->write(os);
183 if (it->type() != Option::page_) os << _T("\n");
184 }
185 os.flush();
186}

References _T, keyword_, level_, options_, and STK::Option::page_.

Referenced by STK::Option::write().

Member Data Documentation

◆ isOptional_

bool STK::IPage::isOptional_
protected

true if the Page is optinal, false otherwise

Definition at line 199 of file STK_IPage.h.

Referenced by IPage(), isOptional(), and read().

◆ keyword_

String STK::IPage::keyword_
mutableprivate

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 218 of file STK_IPage.h.

Referenced by findKeyword(), IPage(), IPage(), read(), and write().

◆ level_

int STK::IPage::level_
mutableprivate

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 212 of file STK_IPage.h.

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

◆ msg_error_

String STK::IPage::msg_error_
mutableprotected

Contain the last error message.

Definition at line 204 of file STK_IPage.h.

Referenced by msg_error(), option(), option(), processLine(), read(), STK::AdditiveBSplineRegressionPage::validate(), and STK::LocalVariancePage::validate().

◆ name_

String STK::IPage::name_
private

name of the page of options

Definition at line 208 of file STK_IPage.h.

Referenced by IPage(), IPage(), name(), processLine(), and read().

◆ options_


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