42 #include "Teuchos_MatrixMarket_Banner.hpp" 
   43 #include "Teuchos_MatrixMarket_split.hpp" 
   52   namespace MatrixMarket {
 
   55     using details::trim_and_lowercase;
 
   58     Banner::validateObjectType (
const std::string& objectType, 
const bool tolerant)
 
   61       std::string out = trim_and_lowercase (objectType);
 
   63       const char* 
const validValues[] = {
"matrix"};
 
   64       const int numValidValues = 1;
 
   68         return std::string (validValues[0]);
 
   69       else if (validValues + numValidValues ==
 
   70                std::find (validValues, validValues + numValidValues, out))
 
   71         throw std::invalid_argument(
"Object type \"" + out + 
"\" is " 
   72                                     "not one of the valid values");
 
   78     Banner::validateMatrixType (
const std::string& matrixType, 
const bool )
 
   81       std::string out = trim_and_lowercase (matrixType);
 
   83       const char* 
const validValues[] = {
"coordinate", 
"array"};
 
   84       const int numValidValues = 2;
 
   85       if (validValues + numValidValues == std::find (validValues, validValues + numValidValues, out))
 
   86         throw std::invalid_argument(
"Matrix type \"" + out + 
"\" is not one of the valid values");
 
   92     Banner::validateDataType (
const std::string& dataType, 
const bool )
 
   95       std::string out = trim_and_lowercase (dataType);
 
   97       const char* 
const validValues[] = {
"real", 
"complex", 
"integer", 
"pattern"};
 
   98       const int numValidValues = 4;
 
   99       if (validValues + numValidValues == std::find (validValues, validValues + numValidValues, out))
 
  100         throw std::invalid_argument(
"Data type \"" + out + 
"\" is not one of the valid values");
 
  106     Banner::validateSymmType (
const std::string& symmType, 
const bool tolerant)
 
  109       std::string out = trim_and_lowercase (symmType);
 
  113           const char* 
const validValues[] =
 
  114             {
"general", 
"nonsymmetric", 
"unsymmetric", 
"symmetric",
 
  115              "skew-symmetric", 
"skew", 
"hermitian"};
 
  116           const int numValidValues = 7;
 
  117           if (validValues + numValidValues == std::find (validValues, validValues + numValidValues, out))
 
  118             throw std::invalid_argument(
"Symmetry type \"" + out + 
"\" is not one of the valid values");
 
  121               if (out == 
"nonsymmetric" || out == 
"unsymmetric")
 
  122                 return std::string(
"general");
 
  123               else if (out == 
"skew")
 
  124                 return std::string(
"skew-symmetric");
 
  131           const char* 
const validValues[] = {
"general", 
"symmetric", 
"skew-symmetric", 
"hermitian"};
 
  132           const int numValidValues = 4;
 
  133           if (validValues + numValidValues == std::find (validValues, validValues + numValidValues, out))
 
  134             throw std::invalid_argument(
"Symmetry type \"" + out + 
"\" is not one of the valid values");
 
  142     Banner::setDefaults (
const int howMany)
 
  145         objectType_ = 
"matrix";
 
  147         matrixType_ = 
"coordinate";
 
  151         symmType_ = 
"general";
 
  164           throw std::invalid_argument (
"The banner line is empty");
 
  167       start = line.find_first_not_of (
" \t");
 
  168       if (start == std::string::npos) {
 
  174           throw std::invalid_argument (
"The banner line contains only " 
  175                                        "whitespace characters");
 
  178       else if (start != 0 && ! tolerant) {
 
  181         throw std::invalid_argument (
"The banner line is not allowed to start " 
  182                                      "with whitespace characters");
 
  189       size_t ppStart = line.find (
"%%", start);
 
  191       if (ppStart == std::string::npos) {
 
  197             "Market file's banner line should always start with \"%%\".  Here " 
  198             "is the offending line: " << std::endl << line);
 
  202         tokenStart = ppStart + 2;
 
  203         if (tokenStart >= line.size()) {
 
  211               "Market file's banner line needs to contain information after the " 
  212               "\"%%\" marker.  Here is the offending line: " << std::endl << line);
 
  221       std::vector<std::string> tokens = split (line, 
" \t", 2);
 
  222       const int numTokens = tokens.size();
 
  230             "Market file's banner line must always begin with the \"Matrix" 
  231             "Market\" keyword.  Here is the offending line: " << std::endl
 
  236       if (! tolerant && tokens[0] != 
"MatrixMarket") {
 
  238           "Market file's banner line must always begin with the \"Matrix" 
  239           "Market\" keyword.  Here is the offending line: " << std::endl
 
  244           setDefaults (5 - numTokens); 
 
  248             "Market file's banner line must always have 5 tokens, but yours " 
  249             "only has " << numTokens << 
"token" << (numTokens != 1 ? 
"s" : 
"")
 
  250             << 
".  Here is the offending line: " << std::endl << line);
 
  253       if (numTokens >= 2) {
 
  254         objectType_ = validateObjectType (tokens[1], tolerant);
 
  256       if (numTokens >= 3) {
 
  257         matrixType_ = validateMatrixType (tokens[2], tolerant);
 
  259       if (numTokens >= 4) {
 
  260         dataType_ = validateDataType (tokens[3], tolerant);
 
  262       if (numTokens >= 5) {
 
  263         symmType_ = validateSymmType (tokens[4], tolerant);
 
  268     operator<< (std::ostream& out, 
const Banner& banner)
 
  270       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.