STK++ 0.9.13
STK::ReadWritePages Class Reference

The ReadWritePages class allow to handle a file of option containing pages. More...

#include <STK_ReadWritePages.h>

Public Member Functions

 ReadWritePages (std::string const &file_name=std::string())
 default constructor.
 
virtual ~ReadWritePages ()
 Destructor.
 
IPage constp_page (int const &pos) const
 get a constant page of option.
 
IPagep_page (int const &pos)
 get a page of option.
 
IPage constp_page (String const &name) const
 constant bookkeeping function.
 
IPagep_page (String const &name)
 bookkeeping function.
 
IPage constoperator[] (int const &pos) const
 get a constant page of option.
 
IPageoperator[] (int const &pos)
 get a page of option.
 
void addPage (IPage const &page)
 Add a page of option to read and/or write.
 
bool write (std::string const &file_name=std::string()) const
 Attempts to write the ReadWritePage to the location specified by file_name.
 
bool write (ostream &os) const
 Attempts to write the ReadWritePage to the specified output stream.
 
bool read (std::string const &file_name=std::string())
 Attempts to read the specified file.
 
bool read (istream &is)
 Attempts to read the pages from an input stream.
 
bool validate ()
 validate all the pages.
 
String consterror () const
 give the last error message encountered
 

Protected Attributes

ContPage pages_
 The list of page to read and/or write.
 
stringstream buffer_
 the string buffer containing the file with all the options.
 
std::string file_name_
 Name of the Current file to read/write.
 
String msg_error_
 The last error message.
 

Private Types

typedef std::vector< IPage * > ContPage
 type of the container for the pages.
 

Detailed Description

The ReadWritePages class allow to handle a file of option containing pages.

See also
IPage.

A file of pages of option have the form

# My favorites
os = linux # error else ?
# My personal informations
age = 40
weight = 75.5
measures = 60, 90, 60 # A list is separated with commas
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
String const & error() const
give the last error message encountered

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.

Definition at line 77 of file STK_ReadWritePages.h.

Member Typedef Documentation

◆ ContPage

type of the container for the pages.

Definition at line 81 of file STK_ReadWritePages.h.

Constructor & Destructor Documentation

◆ ReadWritePages()

STK::ReadWritePages::ReadWritePages ( std::string const file_name = std::string())

default constructor.

Instantiates an instance of ReadWritePage and reads the specified file.

Parameters
file_namename of the file to read/write

Definition at line 42 of file STK_ReadWritePages.cpp.

43 : file_name_(file_name)
44 , msg_error_("")
45{ }
String msg_error_
The last error message.
std::string file_name_
Name of the Current file to read/write.

◆ ~ReadWritePages()

STK::ReadWritePages::~ReadWritePages ( )
virtual

Destructor.

Definition at line 47 of file STK_ReadWritePages.cpp.

48{
49 // delete all pages
50 for (ContPage::size_type it = 0; it < pages_.size(); it++)
51 {
52 delete pages_[it];
53 }
54}
ContPage pages_
The list of page to read and/or write.

References pages_.

Member Function Documentation

◆ addPage()

void STK::ReadWritePages::addPage ( IPage const page)

Add a page of option to read and/or write.

Parameters
pagethe page of option to add

Definition at line 59 of file STK_ReadWritePages.cpp.

60{ pages_.push_back(page.clone());}

References pages_.

◆ error()

String const & STK::ReadWritePages::error ( ) const
inline

give the last error message encountered

Returns
the last message error encountered

Definition at line 171 of file STK_ReadWritePages.h.

171{ return msg_error_;}

References msg_error_.

◆ operator[]() [1/2]

IPage * STK::ReadWritePages::operator[] ( int const pos)
inline

get a page of option.

Parameters
posthe position of the page of option
Returns
the page at the position pos

Definition at line 130 of file STK_ReadWritePages.h.

131 { return pages_.at(ContPage::size_type(pos));}

References pages_.

◆ operator[]() [2/2]

IPage const * STK::ReadWritePages::operator[] ( int const pos) const
inline

get a constant page of option.

Parameters
posthe position of the page of option
Returns
the page at the position pos

Definition at line 123 of file STK_ReadWritePages.h.

124 { return pages_.at(ContPage::size_type(pos));}

References pages_.

◆ p_page() [1/4]

IPage * STK::ReadWritePages::p_page ( int const pos)
inline

get a page of option.

Parameters
posthe position of the page of option
Returns
the page at the position pos

Definition at line 104 of file STK_ReadWritePages.h.

105 { return pages_.at(ContPage::size_type(pos));}

References pages_.

◆ p_page() [2/4]

IPage const * STK::ReadWritePages::p_page ( int const pos) const
inline

get a constant page of option.

Parameters
posthe position of the page of option
Returns
the page at the position pos

Definition at line 97 of file STK_ReadWritePages.h.

98 { return pages_.at(ContPage::size_type(pos));}

References pages_.

◆ p_page() [3/4]

IPage * STK::ReadWritePages::p_page ( String const name)

bookkeeping function.

Find a page given its name

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

Definition at line 194 of file STK_ReadWritePages.cpp.

195{
196 String Uname = toUpperString(name);
197 // read all pages
198 for (ContPage::iterator it = pages_.begin(); it != pages_.end(); it++)
199 {
200 // read curent page
201 if ((*it)->name() == Uname) return *it;
202 }
203 // return null pointer
204 return 0;
205}
String const & toUpperString(String &s)
convert the characters of the String to upper case
Definition STK_String.h:134
std::basic_string< Char > String
STK fundamental type of a String.

References pages_, and STK::toUpperString().

◆ p_page() [4/4]

IPage const * STK::ReadWritePages::p_page ( String const name) const

constant bookkeeping function.

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

Definition at line 177 of file STK_ReadWritePages.cpp.

178{
179 String Uname = toUpperString(name);
180 // read all pages
181 for (ContPage::const_iterator it = pages_.begin(); it != pages_.end(); it++)
182 {
183 // read curent page
184 if ((*it)->name() == Uname) return *it;
185 }
186 // return null pointer
187 return 0;
188}

References pages_, and STK::toUpperString().

◆ read() [1/2]

bool STK::ReadWritePages::read ( istream is)

Attempts to read the pages from an input stream.

Parameters
isname of the input stream to read
Returns
true if successful, false if an error is encountered.

Definition at line 141 of file STK_ReadWritePages.cpp.

142{
143 try
144 {
145 // read all pages
146 for (ContPage::iterator it = pages_.begin(); it != pages_.end(); it++)
147 {
148 // go to the beginning of the stream
149 is.seekg(0, ios::beg);
150 // read curent page
151 (*it)->read(is);
152 }
153 validate();
154 // no error catch
155 return true;
156 }
157 catch(const Exception& e)
158 { msg_error_ = e.error();}
159 return false;
160}
bool validate()
validate all the pages.

References STK::IRunnerBase::error(), msg_error_, pages_, and validate().

◆ read() [2/2]

bool STK::ReadWritePages::read ( std::string const file_name = std::string())

Attempts to read the specified file.

Parameters
file_namename of the file to read
Returns
true if successful, false if an error is encountered.

Definition at line 110 of file STK_ReadWritePages.cpp.

111{
112 // save file_name
113 if (!file_name.empty()) file_name_ = file_name;
114 try
115 {
116 // try to open the file
117 ifstream is(file_name_.c_str());
118 if (!is.is_open())
119 {
120 msg_error_ = "In ReadWritePages::read(" + file_name
121 + "). Could not open file.";
122 return false;
123 }
124 // copy integrally the file in the buffer
125 buffer_ << is.rdbuf();
126 // close file
127 is.close();
128 // read buffer
129 return read(buffer_);
130 }
131 catch(const Exception& e)
132 { msg_error_ = e.error(); }
133 return false;
134}
stringstream buffer_
the string buffer containing the file with all the options.
bool read(std::string const &file_name=std::string())
Attempts to read the specified file.
std::basic_ifstream< Char > ifstream
ifstream for Char
Definition STK_Stream.h:71

References buffer_, STK::IRunnerBase::error(), file_name_, msg_error_, and read().

Referenced by read().

◆ validate()

bool STK::ReadWritePages::validate ( )

validate all the pages.

return true if all the page are validated, false otherwise.

Definition at line 162 of file STK_ReadWritePages.cpp.

163{
164 // read all pages
165 for (ContPage::iterator it = pages_.begin(); it != pages_.end(); it++)
166 {
167 // the method valdite should throw an Exception
168 (*it)->validate();
169 }
170 return true;
171}

References pages_.

Referenced by read().

◆ write() [1/2]

bool STK::ReadWritePages::write ( ostream os) const

Attempts to write the ReadWritePage to the specified output stream.

Parameters
osname of output stream to write
Returns
true if successful, false if an error is encountered.

Definition at line 92 of file STK_ReadWritePages.cpp.

93{
94 try
95 {
96 // Write all pages
97 for (ContPage::const_iterator it = pages_.begin(); it != pages_.end(); it++)
98 { (*it)->write(os);}
99 return true;
100 }
101 catch(const Exception& e)
102 { msg_error_ = e.error();}
103 return false;
104}

References STK::IRunnerBase::error(), msg_error_, and pages_.

◆ write() [2/2]

bool STK::ReadWritePages::write ( std::string const file_name = std::string()) const

Attempts to write the ReadWritePage to the location specified by file_name.

Parameters
file_namename of the file to write
Returns
true if successful, false if an error is encountered.

Definition at line 67 of file STK_ReadWritePages.cpp.

68{
69 // save file_name
70 if (!file_name.empty()) file_name_ = file_name;
71 try
72 {
73 ofstream os(file_name.c_str());
74 if (os.fail())
75 {
76 msg_error_ = "In ReadWritePages::write(" + file_name
77 + "). Could not open file.";
78 return false;
79 }
80 return write(os);
81 }
82 catch(const Exception& e)
83 { msg_error_ = e.error(); }
84 return false;
85}
bool write(std::string const &file_name=std::string()) const
Attempts to write the ReadWritePage to the location specified by file_name.
std::basic_ofstream< Char > ofstream
ofstream for Char
Definition STK_Stream.h:73

References STK::IRunnerBase::error(), file_name_, msg_error_, and write().

Referenced by write().

Member Data Documentation

◆ buffer_

stringstream STK::ReadWritePages::buffer_
protected

the string buffer containing the file with all the options.

Definition at line 177 of file STK_ReadWritePages.h.

Referenced by read().

◆ file_name_

std::string STK::ReadWritePages::file_name_
mutableprotected

Name of the Current file to read/write.

Definition at line 179 of file STK_ReadWritePages.h.

Referenced by read(), and write().

◆ msg_error_

String STK::ReadWritePages::msg_error_
mutableprotected

The last error message.

Definition at line 181 of file STK_ReadWritePages.h.

Referenced by error(), read(), read(), write(), and write().

◆ pages_

ContPage STK::ReadWritePages::pages_
protected

The list of page to read and/or write.

Definition at line 175 of file STK_ReadWritePages.h.

Referenced by addPage(), operator[](), operator[](), p_page(), p_page(), p_page(), p_page(), read(), validate(), write(), and ~ReadWritePages().


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