Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_MatrixMarket_generic.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
12 #include <algorithm>
13 
14 namespace Teuchos {
15  namespace MatrixMarket {
16 
17  int maxLineLength() { return 1024; }
18 
19  bool
20  checkCommentLine (const std::string& line,
21  size_t& start,
22  size_t& size,
23  const size_t lineNumber,
24  const bool tolerant,
25  const bool maybeBannerLine)
26  {
27  // In tolerant mode, empty lines are considered comment lines.
28  if (line.empty ()) {
29  if (tolerant) {
30  return true;
31  }
32  else {
33  std::ostringstream os;
34  os << "Line " << lineNumber << " contains no characters";
35  throw std::invalid_argument (os.str());
36  }
37  }
38  // The line of comments or data "starts" after any whitespace
39  // characters. Whitespace-only lines are considered "empty."
40  start = line.find_first_not_of (" \t");
41  if (start == std::string::npos) {
42  // It's a whitespace-only line. We consider those comments
43  // in tolerant mode, syntax errors otherwise.
44  if (tolerant) {
45  return true;
46  }
47  else {
48  std::ostringstream os;
49  os << "Line " << lineNumber << " contains only whitespace";
50  throw std::invalid_argument (os.str());
51  }
52  }
53  // Position of the first comment character (if any), relative to
54  // the first non-whitespace character in the line. (If we got
55  // this far, then the line has at least one non-whitespace
56  // character.)
57  const size_t commentPos = line.find_first_of("%#", start);
58  if (commentPos == std::string::npos) {
59  // There are no comment characters in the line.
60  // line.substr(start,npos) gives the substring of line
61  // containing valid data.
62  size = std::string::npos;
63  return false;
64  }
65  else if (commentPos == start) {
66  // The line has 0 or more whitespace characters, followed by a
67  // start-of-comment character. However, the Matrix Market
68  // banner line starts with "%%MatrixMarket", so we have to
69  // look for this, if the caller allows this.
70  if (maybeBannerLine) {
71  const size_t bannerStart =
72  line.substr (commentPos).find ("%%MatrixMarket");
73  if (bannerStart != std::string::npos) { // It's a banner line!
74  size = line.size() - commentPos;
75  return false;
76  }
77  else { // It's a comment line. Ah well.
78  size = 0;
79  return true;
80  }
81  }
82  else {
83  size = 0;
84  return true;
85  }
86  }
87  else {
88  // [start, start+size-1] is the (inclusive) range of
89  // characters (if any) between the first non-whitespace
90  // character, and the first comment character. That range
91  // could contain valid data, so we don't consider this a
92  // "comment line."
93  size = commentPos - start;
94  return false;
95  }
96  }
97 
98  } // namespace MatrixMarket
99 } // namespace Teuchos
bool checkCommentLine(const std::string &line, size_t &start, size_t &size, const size_t lineNumber, const bool tolerant, const bool maybeBannerLine)
True if the line is a comment line, false otherwise.
int maxLineLength()
Maximum line length for a Matrix Market file.