DenseLinAlgPack_DVectorInFunc.cpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include <sstream>
00030 
00031 #include "DenseLinAlgPack_DVectorInFunc.hpp"
00032 #include "DenseLinAlgPack_DVectorClass.hpp"
00033 #include "DenseLinAlgPack_AssertOp.hpp"
00034 
00035 namespace { // Local implementation
00036 std::istream& input_vs(std::istream& is, DenseLinAlgPack::DVectorSlice* vs, const char func[]);
00037 }
00038 
00039 std::istream& DenseLinAlgPack::input(std::istream& is, DVector* v, LinAlgPackIO::fmtflags extra_flags) {
00040   if( !(extra_flags & LinAlgPackIO::ignore_dim_bit) ) {
00041     size_type n;
00042     is >> n;
00043     if(is.fail())
00044       throw LinAlgPackIO::InputException("DenseLinAlgPack::input() {DVector}:  Input operation of vector dimension failed.  Check that the constant n is a valid integer.");
00045     if(is.bad())
00046       throw std::ios_base::failure("DenseLinAlgPack::input() {DVector}: Input operation failed because the stream became currupted.");
00047     v->resize(n);
00048   }
00049   return input_vs(is,&(*v)(),"DenseLinAlgPack::input() {DVector}");
00050 }
00051 
00052 std::istream& DenseLinAlgPack::input(std::istream& is, DVectorSlice* vs, LinAlgPackIO::fmtflags extra_flags) {
00053   if( !(extra_flags & LinAlgPackIO::ignore_dim_bit) ) {
00054     size_type n;
00055     is >> n;
00056     if(is.fail())
00057       throw LinAlgPackIO::InputException("DenseLinAlgPack::input() {DVectorSlice}:  Input operation of vector dimension failed.  Check that the constant n is a valid integer.");
00058     if(is.bad())
00059       throw std::ios_base::failure("DenseLinAlgPack::input() {DVectorSlice}: Input operation failed because the stream became currupted.");
00060     DenseLinAlgPack::Vp_V_assert_sizes( vs->dim(), n );
00061   }
00062   return input_vs(is,vs,"DenseLinAlgPack::input() {DVectorSlice}");
00063 }
00064 
00065 
00066 // ///////////////////////////////////
00067 // Local implementation
00068 
00069 namespace {
00070 
00071 // Read in a specified number of elements into a DVectorSlice object.
00072 // The dim of vs is not checked.  If an element input operation fails or the end of the file
00073 // is reached before all of the elements are read in then a LinAlgPackIO::InputException is thrown.
00074 // If the stream becomes currupted durring the input then a std::ios_base::failure exception
00075 // is thrown.  The state of the input steam remains the same on return accept for the char's
00076 // that have been extracted.
00077 std::istream& input_vs(std::istream& is, DenseLinAlgPack::DVectorSlice* vs, const char func[]) {
00078   using std::ios_base;
00079   using DenseLinAlgPack::DVectorSlice;
00080   if(!vs->dim()) return is; // If there are no elements to read in just return
00081   ios_base::iostate old_state = is.exceptions();    // save the old state
00082   is.exceptions(ios_base::badbit | ios_base::failbit);
00083   try {
00084     // Read in the elements
00085     for(DVectorSlice::iterator itr = vs->begin(); itr != vs->end(); ++itr)
00086       is >> *itr;
00087   }
00088   catch(std::ios_base::failure& excpt) {
00089     is.exceptions(old_state);
00090     if(is.bad()) throw; // The stream was bad so rethrow the exception
00091     if(is.fail()) {
00092       std::ostringstream os;
00093       os << func << ":  An vector element input failed.  Check that the vector element is a valid C number.  "
00094          << excpt.what();
00095       throw DenseLinAlgPack::LinAlgPackIO::InputException(os.str());      
00096     }
00097     if(is.eof()) {
00098       std::ostringstream os;
00099       os << func << ":  DVector input failed.  The end of the file was found before all of the elements where read in.  "
00100          << excpt.what();;
00101       throw DenseLinAlgPack::LinAlgPackIO::InputException(os.str());      
00102     }
00103   }
00104   catch(...) {
00105     is.exceptions(old_state);
00106     throw;
00107   }
00108   is.exceptions(old_state);
00109   return is;
00110 }
00111 
00112 } // end namespace

Generated on Tue Jul 13 09:28:46 2010 for DenseLinAlgPack: Concreate C++ Classes for Dense Blas-Compatible Linear Algebra by  doxygen 1.4.7