MOOCHO (Single Doxygen Collection) Version of the Day
DenseLinAlgPack_DVectorInFunc.cpp
Go to the documentation of this file.
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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include <sstream>
00043 
00044 #include "DenseLinAlgPack_DVectorInFunc.hpp"
00045 #include "DenseLinAlgPack_DVectorClass.hpp"
00046 #include "DenseLinAlgPack_AssertOp.hpp"
00047 
00048 namespace { // Local implementation
00049 std::istream& input_vs(std::istream& is, DenseLinAlgPack::DVectorSlice* vs, const char func[]);
00050 }
00051 
00052 std::istream& DenseLinAlgPack::input(std::istream& is, DVector* v, 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() {DVector}:  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() {DVector}: Input operation failed because the stream became currupted.");
00060     v->resize(n);
00061   }
00062   return input_vs(is,&(*v)(),"DenseLinAlgPack::input() {DVector}");
00063 }
00064 
00065 std::istream& DenseLinAlgPack::input(std::istream& is, DVectorSlice* vs, LinAlgPackIO::fmtflags extra_flags) {
00066   if( !(extra_flags & LinAlgPackIO::ignore_dim_bit) ) {
00067     size_type n;
00068     is >> n;
00069     if(is.fail())
00070       throw LinAlgPackIO::InputException("DenseLinAlgPack::input() {DVectorSlice}:  Input operation of vector dimension failed.  Check that the constant n is a valid integer.");
00071     if(is.bad())
00072       throw std::ios_base::failure("DenseLinAlgPack::input() {DVectorSlice}: Input operation failed because the stream became currupted.");
00073     DenseLinAlgPack::Vp_V_assert_sizes( vs->dim(), n );
00074   }
00075   return input_vs(is,vs,"DenseLinAlgPack::input() {DVectorSlice}");
00076 }
00077 
00078 
00079 // ///////////////////////////////////
00080 // Local implementation
00081 
00082 namespace {
00083 
00084 // Read in a specified number of elements into a DVectorSlice object.
00085 // The dim of vs is not checked.  If an element input operation fails or the end of the file
00086 // is reached before all of the elements are read in then a LinAlgPackIO::InputException is thrown.
00087 // If the stream becomes currupted durring the input then a std::ios_base::failure exception
00088 // is thrown.  The state of the input steam remains the same on return accept for the char's
00089 // that have been extracted.
00090 std::istream& input_vs(std::istream& is, DenseLinAlgPack::DVectorSlice* vs, const char func[]) {
00091   using std::ios_base;
00092   using DenseLinAlgPack::DVectorSlice;
00093   if(!vs->dim()) return is; // If there are no elements to read in just return
00094   ios_base::iostate old_state = is.exceptions();    // save the old state
00095   is.exceptions(ios_base::badbit | ios_base::failbit);
00096   try {
00097     // Read in the elements
00098     for(DVectorSlice::iterator itr = vs->begin(); itr != vs->end(); ++itr)
00099       is >> *itr;
00100   }
00101   catch(std::ios_base::failure& excpt) {
00102     is.exceptions(old_state);
00103     if(is.bad()) throw; // The stream was bad so rethrow the exception
00104     if(is.fail()) {
00105       std::ostringstream os;
00106       os << func << ":  An vector element input failed.  Check that the vector element is a valid C number.  "
00107          << excpt.what();
00108       throw DenseLinAlgPack::LinAlgPackIO::InputException(os.str());      
00109     }
00110     if(is.eof()) {
00111       std::ostringstream os;
00112       os << func << ":  DVector input failed.  The end of the file was found before all of the elements where read in.  "
00113          << excpt.what();;
00114       throw DenseLinAlgPack::LinAlgPackIO::InputException(os.str());      
00115     }
00116   }
00117   catch(...) {
00118     is.exceptions(old_state);
00119     throw;
00120   }
00121   is.exceptions(old_state);
00122   return is;
00123 }
00124 
00125 } // end namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines