STK++ 0.9.13
STK_ReadWritePages.cpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2010 Serge Iovleff
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: 27 sept. 2010
28 * Purpose: Implementation of the class ReadWritePages.
29 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
30 *
31 **/
32
37#include "../include/STK_ReadWritePages.h"
38
39namespace STK
40{
41
43 : file_name_(file_name)
44 , msg_error_("")
45{ }
46
48{
49 // delete all pages
50 for (ContPage::size_type it = 0; it < pages_.size(); it++)
51 {
52 delete pages_[it];
53 }
54}
55
56/* @brief Add a page of option to read and/or write.
57 * @param page the page of option to add
58 */
60{ pages_.push_back(page.clone());}
61
62/* @brief Attempts to write the ReadWritePage to the location specified by
63 * file_name.
64 * @param file_name name of the file to write
65 * @return @c true if successful, @c false if an error is encountered.
66 **/
67bool ReadWritePages::write( std::string const& file_name) const
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}
86
87/* @brief Attempts to write the ReadWritePage to the specified output
88 * stream.
89 * @param os name of output stream to write
90 * @return @c true if successful, @c false if an error is encountered.
91 **/
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}
105
106/* @brief Attempts to reads the specified file.
107 * @param file_name name of the file to read
108 * @return @c true if successful, @c false if an error is encountered.
109 **/
110bool ReadWritePages::read(std::string const& file_name)
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}
135
136
137/* @brief Attempts to read the pages from an input stream.
138 * @param is name of the input stream to read
139 * @return @c true if successful, @c false if an error is encountered.
140 **/
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}
161
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}
172
173/* internal bookkeeping.
174 * @param name name of the Page to find
175 * @return NULL if the variable is not found, the page otherwise
176 **/
177IPage const* ReadWritePages::p_page( String const& name) const
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}
189
190/* internal bookkeeping.
191 * @param name name of the Page to find
192 * @return NULL if the variable is not found, the page otherwise
193 **/
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}
206
207} // namespace STK
Sdk class for all library Exceptions.
A IPage is an interface base class for reading and/or writing a page of option in a file.
Definition STK_IPage.h:94
String const & error() const
get the last error message.
Definition STK_IRunner.h:82
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
ContPage pages_
The list of page 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.
ReadWritePages(std::string const &file_name=std::string())
default constructor.
stringstream buffer_
the string buffer containing the file with all the options.
void addPage(IPage const &page)
Add a page of option to read and/or write.
virtual ~ReadWritePages()
Destructor.
bool read(std::string const &file_name=std::string())
Attempts to read the specified file.
String msg_error_
The last error message.
bool validate()
validate all the pages.
std::string file_name_
Name of the Current file to read/write.
IPage const * p_page(int const &pos) const
get a constant page of option.
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.
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
std::basic_ifstream< Char > ifstream
ifstream for Char
Definition STK_Stream.h:71
std::basic_istream< Char > istream
istream for Char
Definition STK_Stream.h:55
std::basic_ofstream< Char > ofstream
ofstream for Char
Definition STK_Stream.h:73
The namespace STK is the main domain space of the Statistical ToolKit project.