Tpetra Matrix/Vector Services Version of the Day
MatrixMarket_generic.cpp
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //          Tpetra: Templated Linear Algebra Services Package
00005 //                 Copyright (2008) Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
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 Michael A. Heroux (maherou@sandia.gov) 
00038 // 
00039 // ************************************************************************
00040 // @HEADER
00041 
00042 #include "MatrixMarket_generic.hpp"
00043 #include "MatrixMarket_split.hpp"
00044 #include <algorithm>
00045 
00046 namespace Tpetra {
00047   namespace MatrixMarket {
00048 
00049     int maxLineLength() { return 1024; }
00050 
00051     bool 
00052     checkCommentLine (const std::string& line, 
00053           size_t& start, 
00054           size_t& size,
00055           const size_t lineNumber,
00056           const bool tolerant)
00057     {
00058       // In tolerant mode, empty lines are considered comment lines.
00059       if (line.empty()) 
00060   {
00061     if (tolerant)
00062       return true;
00063     else
00064       {
00065         std::ostringstream os;
00066         os << "Line " << lineNumber << " contains no characters";
00067         throw std::invalid_argument (os.str());
00068       }
00069   }
00070       // The line of comments or data "starts" after any whitespace
00071       // characters.  Whitespace-only lines are considered "empty."
00072       start = line.find_first_not_of (" \t");
00073       if (start == std::string::npos)
00074   { // It's a whitespace-only line.  We consider those comments
00075     // in tolerant mode, syntax errors otherwise.
00076     if (tolerant)
00077       return true;
00078     else
00079       {
00080         std::ostringstream os;
00081         os << "Line " << lineNumber << " contains only whitespace";
00082         throw std::invalid_argument (os.str());
00083       }
00084   }
00085       // Position of the first comment character (if any), relative to
00086       // the first non-whitespace character in the line.  (If we got
00087       // this far, then the line has at least one non-whitespace
00088       // character.)
00089       const size_t commentPos = line.find_first_of("%#", start);
00090       if (commentPos == std::string::npos)
00091   { // There are no comment characters in the line.
00092     // line.substr(start,npos) gives the substring of line
00093     // containing valid data.
00094     size = std::string::npos;
00095     return false;
00096   }
00097       else if (commentPos == start)
00098   { // The line has 0 or more whitespace characters, followed by
00099     // a start-of-comment character.  That means it's a comment.
00100     size = 0;
00101     return true;
00102   }
00103       else 
00104   { // [start, start+size-1] is the (inclusive) range of
00105     // characters (if any) between the first non-whitespace
00106     // character, and the first comment character.  That range
00107     // could contain valid data, so we don't consider this a
00108     // "comment line."
00109     size = commentPos - start;
00110     return false;
00111   }
00112     }
00113 
00114   } // namespace MatrixMarket
00115 } // namespace Tpetra
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines