10 #include "Teuchos_MatrixMarket_Banner.hpp"
11 #include "Teuchos_MatrixMarket_split.hpp"
20 namespace MatrixMarket {
23 using details::trim_and_lowercase;
26 Banner::validateObjectType (
const std::string& objectType,
const bool tolerant)
29 std::string out = trim_and_lowercase (objectType);
31 const char*
const validValues[] = {
"matrix"};
32 const int numValidValues = 1;
36 return std::string (validValues[0]);
37 else if (validValues + numValidValues ==
38 std::find (validValues, validValues + numValidValues, out))
39 throw std::invalid_argument(
"Object type \"" + out +
"\" is "
40 "not one of the valid values");
46 Banner::validateMatrixType (
const std::string& matrixType,
const bool )
49 std::string out = trim_and_lowercase (matrixType);
51 const char*
const validValues[] = {
"coordinate",
"array"};
52 const int numValidValues = 2;
53 if (validValues + numValidValues == std::find (validValues, validValues + numValidValues, out))
54 throw std::invalid_argument(
"Matrix type \"" + out +
"\" is not one of the valid values");
60 Banner::validateDataType (
const std::string& dataType,
const bool )
63 std::string out = trim_and_lowercase (dataType);
65 const char*
const validValues[] = {
"real",
"complex",
"integer",
"pattern"};
66 const int numValidValues = 4;
67 if (validValues + numValidValues == std::find (validValues, validValues + numValidValues, out))
68 throw std::invalid_argument(
"Data type \"" + out +
"\" is not one of the valid values");
74 Banner::validateSymmType (
const std::string& symmType,
const bool tolerant)
77 std::string out = trim_and_lowercase (symmType);
81 const char*
const validValues[] =
82 {
"general",
"nonsymmetric",
"unsymmetric",
"symmetric",
83 "skew-symmetric",
"skew",
"hermitian"};
84 const int numValidValues = 7;
85 if (validValues + numValidValues == std::find (validValues, validValues + numValidValues, out))
86 throw std::invalid_argument(
"Symmetry type \"" + out +
"\" is not one of the valid values");
89 if (out ==
"nonsymmetric" || out ==
"unsymmetric")
90 return std::string(
"general");
91 else if (out ==
"skew")
92 return std::string(
"skew-symmetric");
99 const char*
const validValues[] = {
"general",
"symmetric",
"skew-symmetric",
"hermitian"};
100 const int numValidValues = 4;
101 if (validValues + numValidValues == std::find (validValues, validValues + numValidValues, out))
102 throw std::invalid_argument(
"Symmetry type \"" + out +
"\" is not one of the valid values");
110 Banner::setDefaults (
const int howMany)
113 objectType_ =
"matrix";
115 matrixType_ =
"coordinate";
119 symmType_ =
"general";
132 throw std::invalid_argument (
"The banner line is empty");
135 start = line.find_first_not_of (
" \t");
136 if (start == std::string::npos) {
142 throw std::invalid_argument (
"The banner line contains only "
143 "whitespace characters");
146 else if (start != 0 && ! tolerant) {
149 throw std::invalid_argument (
"The banner line is not allowed to start "
150 "with whitespace characters");
157 size_t ppStart = line.find (
"%%", start);
159 if (ppStart == std::string::npos) {
165 "Market file's banner line should always start with \"%%\". Here "
166 "is the offending line: " << std::endl << line);
170 tokenStart = ppStart + 2;
171 if (tokenStart >= line.size()) {
179 "Market file's banner line needs to contain information after the "
180 "\"%%\" marker. Here is the offending line: " << std::endl << line);
189 std::vector<std::string> tokens = split (line,
" \t", 2);
190 const int numTokens = tokens.size();
198 "Market file's banner line must always begin with the \"Matrix"
199 "Market\" keyword. Here is the offending line: " << std::endl
204 if (! tolerant && tokens[0] !=
"MatrixMarket") {
206 "Market file's banner line must always begin with the \"Matrix"
207 "Market\" keyword. Here is the offending line: " << std::endl
212 setDefaults (5 - numTokens);
216 "Market file's banner line must always have 5 tokens, but yours "
217 "only has " << numTokens <<
"token" << (numTokens != 1 ?
"s" :
"")
218 <<
". Here is the offending line: " << std::endl << line);
221 if (numTokens >= 2) {
222 objectType_ = validateObjectType (tokens[1], tolerant);
224 if (numTokens >= 3) {
225 matrixType_ = validateMatrixType (tokens[2], tolerant);
227 if (numTokens >= 4) {
228 dataType_ = validateDataType (tokens[3], tolerant);
230 if (numTokens >= 5) {
231 symmType_ = validateSymmType (tokens[4], tolerant);
236 operator<< (std::ostream& out,
const Banner& banner)
238 out <<
"%%MatrixMarket"
const std::string & matrixType() const
Storage type of the matrix.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
const std::string & dataType() const
Data type of matrix entries.
const std::string & symmType() const
Symmetric storage type.
const std::string & objectType() const
The object type.
Banner(const std::string &line, const bool tolerant=false)
Standard test and throw macros.
Parse a Matrix Market banner line.