Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_MatrixMarket_generic.cpp
Go to the documentation of this file.
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 "Teuchos_MatrixMarket_generic.hpp"
00043 #include "Teuchos_MatrixMarket_split.hpp"
00044 #include <algorithm>
00045 
00046 namespace Teuchos {
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                       const bool maybeBannerLine)
00058     {
00059       // In tolerant mode, empty lines are considered comment lines.
00060       if (line.empty ()) {
00061         if (tolerant) {
00062           return true;
00063         }
00064         else {
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         }
00079         else {
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 a
00099         // start-of-comment character.  However, the Matrix Market
00100         // banner line starts with "%%MatrixMarket", so we have to
00101         // look for this, if the caller allows this.
00102         if (maybeBannerLine) {
00103           const size_t bannerStart =
00104             line.substr (commentPos).find ("%%MatrixMarket");
00105           if (bannerStart != std::string::npos) { // It's a banner line!
00106             size = line.size() - commentPos;
00107             return false;
00108           }
00109           else { // It's a comment line.  Ah well.
00110             size = 0;
00111             return true;
00112           }
00113         }
00114         else {
00115           size = 0;
00116           return true;
00117         }
00118       }
00119       else {
00120         // [start, start+size-1] is the (inclusive) range of
00121         // characters (if any) between the first non-whitespace
00122         // character, and the first comment character.  That range
00123         // could contain valid data, so we don't consider this a
00124         // "comment line."
00125         size = commentPos - start;
00126         return false;
00127       }
00128     }
00129 
00130   } // namespace MatrixMarket
00131 } // namespace Teuchos
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines