src/file_parser/MtxParser.cpp
74377719
 #include "MatrixElement.h"
dd3a9441
 #include "MtxParser.h"
 
8f9d4161
 #include "../utils/GapsAssert.h"
74377719
 
 #include <sstream>
 
 MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0)
 {
     mFile.open(path.c_str());
 
7de32bf9
     // read first line
     std::string line;
     std::getline(mFile, line);
     if (mFile.eof() || mFile.fail())
     {
         GAPS_ERROR("Invalid MTX file");
     }
 
74377719
     // skip over comments
     while (line.find('%') != std::string::npos)
     {
         std::getline(mFile, line);
7de32bf9
         if (mFile.eof() || mFile.fail())
         {
             GAPS_ERROR("Invalid MTX file");
         }
74377719
     }
     std::stringstream ss(line); // this line contains dimensions
17bd0769
 
74377719
     // store dimensions
     ss >> mNumRows >> mNumCols;
 }
 
 bool MtxParser::hasNext()
 {
7de32bf9
     mFile >> std::ws; // get rid of whitespace
74377719
     return mFile.peek() != EOF;
 }
 
 MatrixElement MtxParser::getNext()
 {
7de32bf9
     unsigned row = 0, col = 0;
     float val = 0.f;
 
     mFile >> row;
     mFile >> col;
     mFile >> val;
 
     return MatrixElement(row - 1, col - 1, val);
74377719
 }