STK++ 0.9.13
STK_IPage.cpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::DManager
27 * created on: 22 avr. 2010
28 * Purpose: Public interface of the IPage class.
29 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
30 **/
31
35#include <algorithm>
36#include <Sdk.h>
37
38#include "../include/STK_IPage.h"
39
40namespace STK
41{
42// remove all occurrences of the char @c c
43//static void removeAllChar( STK::String & str, STK::Char c )
44//{
45// str.erase( std::remove( str.begin(), str.end(), c ), str.end() );
46//}
47
48// remove all occurrences after the char @c c
49static void removeComments( String & str, Char c )
50{
51 String::size_type pos = str.find(c);
52 if (pos < str.size())
53 {
54 str.resize(pos);
55 }
56}
57
58// create pagename_
59static void createKeyWord( String & str, int const& level )
60{
61 // remove all occurrences of CHAR_OPENBRACKET
62 str.erase( std::remove( str.begin(), str.end(), CHAR_OPENBRACKET ), str.end() );
63 // remove all occurrences of CHAR_CLOSEBRACKET
64 str.erase( std::remove( str.begin(), str.end(), CHAR_CLOSEBRACKET ), str.end() );
65 // add CHAR_OPENBRACKET and CHAR_CLOSEBRACKET
66 str.insert(0, level, CHAR_OPENBRACKET).append(level, CHAR_CLOSEBRACKET);
67}
68
69
70/* Constructor.
71 * @param level level of the page */
72IPage::IPage( String const& name, int const& level, bool isOptional)
73 : isOptional_(isOptional)
74 , name_(name)
75 , level_(level)
76 , keyword_(name_)
77{
80 createKeyWord(keyword_, level);
81}
82
83/* Destructor */
85
86/* Copy constructor.
87 * @param page the page to copy
88 **/
90{
91 name_ = page.name_;
92 level_ = page.level_;
93 isOptional_ = page.isOptional_;
94 keyword_ = page.keyword_;
95 options_ = page.options_;
96}
97
102{
103 // look how many bracket
104 page.level_ = level_ +1;
105 createKeyWord(page.keyword_, page.level_);
107}
108
109
110/* @brief read in options in a stream
111 * @param is input stream
112 */
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);
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}
168
169/* @brief write out options in a stream
170 * @param os output stream
171 */
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}
187
188/* internal bookkeeping.
189 * @param name name of the Page to find
190 * @return NULL if the variable is not found, the page otherwise
191 **/
192Option const& IPage::option( String const& name) const
193{
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}
206
207/* internal bookkeeping.
208 * @param name name of the Page to find
209 * @return NULL if the variable is not found, the page otherwise
210 **/
212{
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}
225
226
227
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);
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}
246
247/* process the input line in order to obtain a .
248 * @param is input stringstream to process
249 * @return @c true if the keyword have been found, @c false otherwise
250 */
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);
259 // get the option value after character '='
260 String optValue = line.substr(pos+1);
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}
278
279
280} // namespace STK
281
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
#define _T(x)
Let x unmodified.
This file include all the other header files of the project Sdk.
A IPage is an interface base class for reading and/or writing a page of option in a file.
Definition STK_IPage.h:94
void write(ostream &os) const
write out options in a stream
String const & name() const
name of the IPage.
Definition STK_IPage.h:120
virtual bool validate()
validate the page.
Definition STK_IPage.h:193
String keyword_
keyword of the page of options.
Definition STK_IPage.h:218
void addPage(IPage const &page)
add a sub-page as an option to the page
String msg_error_
Contain the last error message.
Definition STK_IPage.h:204
bool findKeyword(istream &is) const
process the input stream until the keyword is encountered.
ContOption options_
array of the options
Definition STK_IPage.h:201
Option & option(String const &name)
bookkeeping function.
void addOption(Option const &opt)
add an option to the page
Definition STK_IPage.h:177
virtual ~IPage()
Destructor.
Definition STK_IPage.cpp:84
int level_
level of the Page.
Definition STK_IPage.h:212
bool processLine(String const &line)
process the input line and set the value of the option.
String name_
name of the page of options
Definition STK_IPage.h:208
IPage(String const &name, int const &level, bool isOptional)
Constructor.
Definition STK_IPage.cpp:72
void read(istream &is)
read in options from an input steam stream
bool isOptional_
true if the Page is optinal, false otherwise
Definition STK_IPage.h:199
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
the Option class allow to store the value of an option from a IPage.
Definition STK_Option.h:56
String const & toUpperString(String &s)
convert the characters of the String to upper case
Definition STK_String.h:134
void removeCharBeforeAndAfter(String &str, Char c)
remove all occurrences of the char c at the beginning and the end of the string str.
@ page_
a page option
Definition STK_Option.h:82
char Char
STK fundamental type of a Char.
std::basic_string< Char > String
STK fundamental type of a String.
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
std::basic_istream< Char > istream
istream for Char
Definition STK_Stream.h:55
The namespace STK is the main domain space of the Statistical ToolKit project.