STK++ 1.0
STK_ReadWritePages.cpp
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2010  Serge Iovleff
00003 
00004  This program is free software; you can redistribute it and/or modify
00005  it under the terms of the GNU Lesser General Public License as
00006  published by the Free Software Foundation; either version 2 of the
00007  License, or (at your option) any later version.
00008 
00009  This program is distributed in the hope that it will be useful,
00010  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  GNU Lesser General Public License for more details.
00013 
00014  You should have received a copy of the GNU Lesser General Public
00015  License along with this program; if not, write to the
00016  Free Software Foundation, Inc.,
00017  59 Temple Place,
00018  Suite 330,
00019  Boston, MA 02111-1307
00020  USA
00021 
00022  Contact : Serge.Iovleff@stkpp.org
00023  */
00024 
00025 /*
00026  * Project:  stkpp::DManager
00027  * created on: 27 sept. 2010
00028  * Purpose: Implementation of the class ReadWritePages.
00029  * Author:   iovleff, serge.iovleff@stkpp.org
00030  *
00031  **/
00032 
00037 #include "../include/STK_ReadWritePages.h"
00038 #include "../../STKernel/include/STK_Stream.h"
00039 #include "../../STKernel/include/STK_Exceptions.h"
00040 
00041 namespace STK
00042 {
00043 
00044 ReadWritePages::ReadWritePages( std::string const& file_name)
00045                               : file_name_(file_name)
00046                               , msg_error_("")
00047 { }
00048 
00049 ReadWritePages::~ReadWritePages()
00050 {
00051   // delete all pages
00052   for (ContPage::size_type it = 0; it < pages_.size(); it++)
00053   {
00054     delete pages_[it];
00055   }
00056 }
00057 
00058 /* @brief Add a page of option to read and/or write.
00059  * @param page the page of option to add
00060  */
00061 void ReadWritePages::addPage( IPage const& page)
00062 { pages_.push_back(page.clone());}
00063 
00064 /* @brief Attempts to write the ReadWritePage to the location specified by
00065  *  file_name.
00066  *  @param file_name name of the file to write
00067  *  @return  @c true if successful, @c false if an error is encountered.
00068  **/
00069 bool ReadWritePages::write( std::string const& file_name) const
00070 {
00071   // save file_name
00072   if (!file_name.empty()) file_name_  = file_name;
00073   try
00074   {
00075     ofstream os(file_name.c_str());
00076     if (os.fail())
00077     {
00078       msg_error_ = "In ReadWritePages::write(" + file_name
00079                  + "). Could not open file.";
00080       return false;
00081     }
00082     return write(os);
00083   }
00084   catch(const Exception& e)
00085   { msg_error_ = e.error(); }
00086   return false;
00087 }
00088 
00089 /* @brief Attempts to write the ReadWritePage to the specified output
00090  *  stream.
00091  *  @param os name of output stream to write
00092  *  @return  @c true if successful, @c false if an error is encountered.
00093  **/
00094 bool ReadWritePages::write( ostream& os) const
00095 {
00096   try
00097   {
00098     // Write all pages
00099     for (ContPage::const_iterator it = pages_.begin(); it != pages_.end(); it++)
00100     { (*it)->write(os);}
00101     return true;
00102   }
00103   catch(const Exception& e)
00104   { msg_error_ = e.error();}
00105   return false;
00106 }
00107 
00108 /* @brief Attempts to reads the specified file.
00109  *  @param file_name name of the file to read
00110  *  @return  @c true if successful, @c false if an error is encountered.
00111  **/
00112 bool ReadWritePages::read(std::string const& file_name)
00113 {
00114   // save file_name
00115   if (!file_name.empty()) file_name_  = file_name;
00116   try
00117   {
00118     // try to open the file
00119     ifstream is(file_name_.c_str());
00120     if (!is.is_open())
00121     {
00122       msg_error_ = "In ReadWritePages::read(" + file_name
00123                  + "). Could not open file.";
00124       return false;
00125     }
00126     // copy integrally the file in the buffer
00127     buffer_ << is.rdbuf();
00128     // close file
00129     is.close();
00130     // read buffer
00131     return read(buffer_);
00132   }
00133   catch(const Exception& e)
00134   { msg_error_ = e.error(); }
00135   return false;
00136 }
00137 
00138 
00139 /* @brief Attempts to read the pages from an input stream.
00140  *  @param is name of the input stream to read
00141  *  @return  @c true if successful, @c false if an error is encountered.
00142  **/
00143 bool ReadWritePages::read( istream& is)
00144 {
00145   try
00146   {
00147     // read all pages
00148     for (ContPage::iterator it = pages_.begin(); it != pages_.end(); it++)
00149     {
00150       // go to the beginning of the stream
00151       is.seekg(0, ios::beg);
00152       // read curent page
00153       (*it)->read(is);
00154     }
00155     validate();
00156     // no error catch
00157     return true;
00158   }
00159   catch(const Exception& e)
00160   { msg_error_ = e.error();}
00161   return false;
00162 }
00163 
00164 bool ReadWritePages::validate()
00165 {
00166   // read all pages
00167   for (ContPage::iterator it = pages_.begin(); it != pages_.end(); it++)
00168   {
00169     // the method valdite should throw an Exception
00170     (*it)->validate();
00171   }
00172   return true;
00173 }
00174 
00175 /*  internal bookkeeping.
00176  *  @param name name of the Page to find
00177  *  @return NULL if the variable is not found, the page otherwise
00178  **/
00179 IPage const* ReadWritePages::p_page( String const& name) const
00180 {
00181   String Uname = toUpperString(name);
00182   // read all pages
00183   for (ContPage::const_iterator it = pages_.begin(); it != pages_.end(); it++)
00184   {
00185     // read curent page
00186     if ((*it)->name() == Uname) return *it;
00187   }
00188   // return null pointer
00189   return 0;
00190 }
00191 
00192 /*  internal bookkeeping.
00193  *  @param name name of the Page to find
00194  *  @return NULL if the variable is not found, the page otherwise
00195  **/
00196 IPage * ReadWritePages::p_page( String const& name)
00197 {
00198   String Uname = toUpperString(name);
00199   // read all pages
00200   for (ContPage::iterator it = pages_.begin(); it != pages_.end(); it++)
00201   {
00202     // read curent page
00203     if ((*it)->name() == Uname) return *it;
00204   }
00205   // return null pointer
00206   return 0;
00207 }
00208 
00209 } // namespace STK