Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_MatrixMarket_generic.cpp
1// @HEADER
2// ***********************************************************************
3//
4// Tpetra: Templated Linear Algebra Services Package
5// Copyright (2008) Sandia Corporation
6//
7// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8// the U.S. Government retains certain rights in this software.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ************************************************************************
40// @HEADER
41
42#include "Teuchos_MatrixMarket_generic.hpp"
43#include "Teuchos_MatrixMarket_split.hpp"
44#include <algorithm>
45
46namespace Teuchos {
47 namespace MatrixMarket {
48
49 int maxLineLength() { return 1024; }
50
51 bool
52 checkCommentLine (const std::string& line,
53 size_t& start,
54 size_t& size,
55 const size_t lineNumber,
56 const bool tolerant,
57 const bool maybeBannerLine)
58 {
59 // In tolerant mode, empty lines are considered comment lines.
60 if (line.empty ()) {
61 if (tolerant) {
62 return true;
63 }
64 else {
65 std::ostringstream os;
66 os << "Line " << lineNumber << " contains no characters";
67 throw std::invalid_argument (os.str());
68 }
69 }
70 // The line of comments or data "starts" after any whitespace
71 // characters. Whitespace-only lines are considered "empty."
72 start = line.find_first_not_of (" \t");
73 if (start == std::string::npos) {
74 // It's a whitespace-only line. We consider those comments
75 // in tolerant mode, syntax errors otherwise.
76 if (tolerant) {
77 return true;
78 }
79 else {
80 std::ostringstream os;
81 os << "Line " << lineNumber << " contains only whitespace";
82 throw std::invalid_argument (os.str());
83 }
84 }
85 // Position of the first comment character (if any), relative to
86 // the first non-whitespace character in the line. (If we got
87 // this far, then the line has at least one non-whitespace
88 // character.)
89 const size_t commentPos = line.find_first_of("%#", start);
90 if (commentPos == std::string::npos) {
91 // There are no comment characters in the line.
92 // line.substr(start,npos) gives the substring of line
93 // containing valid data.
94 size = std::string::npos;
95 return false;
96 }
97 else if (commentPos == start) {
98 // The line has 0 or more whitespace characters, followed by a
99 // start-of-comment character. However, the Matrix Market
100 // banner line starts with "%%MatrixMarket", so we have to
101 // look for this, if the caller allows this.
102 if (maybeBannerLine) {
103 const size_t bannerStart =
104 line.substr (commentPos).find ("%%MatrixMarket");
105 if (bannerStart != std::string::npos) { // It's a banner line!
106 size = line.size() - commentPos;
107 return false;
108 }
109 else { // It's a comment line. Ah well.
110 size = 0;
111 return true;
112 }
113 }
114 else {
115 size = 0;
116 return true;
117 }
118 }
119 else {
120 // [start, start+size-1] is the (inclusive) range of
121 // characters (if any) between the first non-whitespace
122 // character, and the first comment character. That range
123 // could contain valid data, so we don't consider this a
124 // "comment line."
125 size = commentPos - start;
126 return false;
127 }
128 }
129
130 } // namespace MatrixMarket
131} // namespace Teuchos
Matrix Market file utilities.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...