This patch makes sure that MatrixElement is handling all of the
string parsing in the input data files. Previously, MtxParser was
doing this separetly and passing a float value. This by-passed the
error checking in MatrixElement and caused some errors to be
missed.
... | ... |
@@ -9,28 +9,41 @@ MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0) |
9 | 9 |
{ |
10 | 10 |
mFile.open(path.c_str()); |
11 | 11 |
|
12 |
- std::string line; |
|
13 |
- std::getline(mFile, line); |
|
14 |
- if (mFile.eof() || mFile.fail()) |
|
15 |
- { |
|
16 |
- GAPS_ERROR("Invalid MTX file"); |
|
17 |
- } |
|
18 |
- |
|
19 | 12 |
// skip over comments |
13 |
+ std::string line = "%"; |
|
20 | 14 |
while (line.find('%') != std::string::npos) |
21 | 15 |
{ |
22 | 16 |
std::getline(mFile, line); |
23 |
- if (mFile.eof() || mFile.fail()) |
|
24 |
- { |
|
25 |
- GAPS_ERROR("Invalid MTX file"); |
|
26 |
- } |
|
17 |
+ checkFileState(); |
|
27 | 18 |
} |
28 |
- std::stringstream ss(line); // this line contains dimensions |
|
29 | 19 |
|
30 |
- // store dimensions |
|
20 |
+ std::stringstream ss(line); // this line contains dimensions |
|
31 | 21 |
ss >> mNumRows >> mNumCols; |
32 | 22 |
} |
33 | 23 |
|
24 |
+MtxParser::~MtxParser() |
|
25 |
+{ |
|
26 |
+ mFile.close(); |
|
27 |
+} |
|
28 |
+ |
|
29 |
+unsigned MtxParser::nRow() const |
|
30 |
+{ |
|
31 |
+ return mNumRows; |
|
32 |
+} |
|
33 |
+ |
|
34 |
+unsigned MtxParser::nCol() const |
|
35 |
+{ |
|
36 |
+ return mNumCols; |
|
37 |
+} |
|
38 |
+ |
|
39 |
+void MtxParser::checkFileState() const |
|
40 |
+{ |
|
41 |
+ if (mFile.eof() || mFile.fail()) |
|
42 |
+ { |
|
43 |
+ GAPS_ERROR("Invalid MTX file"); |
|
44 |
+ } |
|
45 |
+} |
|
46 |
+ |
|
34 | 47 |
bool MtxParser::hasNext() |
35 | 48 |
{ |
36 | 49 |
mFile >> std::ws; // get rid of whitespace |
... | ... |
@@ -39,7 +52,8 @@ bool MtxParser::hasNext() |
39 | 52 |
|
40 | 53 |
MatrixElement MtxParser::getNext() |
41 | 54 |
{ |
42 |
- unsigned row = 0, col = 0; |
|
55 |
+ unsigned row = 0; |
|
56 |
+ unsigned col = 0; |
|
43 | 57 |
float val = 0.f; |
44 | 58 |
mFile >> row; |
45 | 59 |
mFile >> col; |
... | ... |
@@ -3,23 +3,10 @@ |
3 | 3 |
|
4 | 4 |
#include "../utils/GapsAssert.h" |
5 | 5 |
|
6 |
-//#include <cstdio> |
|
7 |
-//#include <cunistd> |
|
8 | 6 |
#include <sstream> |
9 | 7 |
|
10 | 8 |
MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0) |
11 | 9 |
{ |
12 |
- // check if file exists and read it in |
|
13 |
- //if (access(path, F_OK) == -1) |
|
14 |
- //{ |
|
15 |
- // GAPS_ERROR("Invalid MTX file"); |
|
16 |
- //} |
|
17 |
- //mFile = fopen(path, "r"); |
|
18 |
- |
|
19 |
- // read first line |
|
20 |
- //char line[1024]; |
|
21 |
- //fgets(line, 1024, mFile); |
|
22 |
- |
|
23 | 10 |
mFile.open(path.c_str()); |
24 | 11 |
|
25 | 12 |
std::string line; |
... | ... |
@@ -42,7 +29,6 @@ MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0) |
42 | 29 |
|
43 | 30 |
// store dimensions |
44 | 31 |
ss >> mNumRows >> mNumCols; |
45 |
- //mFile_p = fopen(path, "r"); |
|
46 | 32 |
} |
47 | 33 |
|
48 | 34 |
bool MtxParser::hasNext() |
... | ... |
@@ -55,10 +41,8 @@ MatrixElement MtxParser::getNext() |
55 | 41 |
{ |
56 | 42 |
unsigned row = 0, col = 0; |
57 | 43 |
float val = 0.f; |
58 |
- |
|
59 | 44 |
mFile >> row; |
60 | 45 |
mFile >> col; |
61 | 46 |
mFile >> val; |
62 |
- |
|
63 | 47 |
return MatrixElement(row - 1, col - 1, val); |
64 | 48 |
} |
... | ... |
@@ -3,26 +3,31 @@ |
3 | 3 |
|
4 | 4 |
#include "../utils/GapsAssert.h" |
5 | 5 |
|
6 |
-#include <cstdio> |
|
7 |
-#include <cunistd> |
|
6 |
+//#include <cstdio> |
|
7 |
+//#include <cunistd> |
|
8 | 8 |
#include <sstream> |
9 | 9 |
|
10 | 10 |
MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0) |
11 | 11 |
{ |
12 | 12 |
// check if file exists and read it in |
13 |
- if (access(path, F_OK) == -1) |
|
14 |
- { |
|
15 |
- GAPS_ERROR("Invalid MTX file"); |
|
16 |
- } |
|
17 |
- mFile = fopen(path, "r"); |
|
13 |
+ //if (access(path, F_OK) == -1) |
|
14 |
+ //{ |
|
15 |
+ // GAPS_ERROR("Invalid MTX file"); |
|
16 |
+ //} |
|
17 |
+ //mFile = fopen(path, "r"); |
|
18 | 18 |
|
19 | 19 |
// read first line |
20 |
- char line[1024]; |
|
21 |
- fgets(line, 1024, mFile); |
|
20 |
+ //char line[1024]; |
|
21 |
+ //fgets(line, 1024, mFile); |
|
22 | 22 |
|
23 |
+ mFile.open(path.c_str()); |
|
23 | 24 |
|
24 | 25 |
std::string line; |
25 | 26 |
std::getline(mFile, line); |
27 |
+ if (mFile.eof() || mFile.fail()) |
|
28 |
+ { |
|
29 |
+ GAPS_ERROR("Invalid MTX file"); |
|
30 |
+ } |
|
26 | 31 |
|
27 | 32 |
// skip over comments |
28 | 33 |
while (line.find('%') != std::string::npos) |
... | ... |
@@ -37,7 +42,7 @@ MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0) |
37 | 42 |
|
38 | 43 |
// store dimensions |
39 | 44 |
ss >> mNumRows >> mNumCols; |
40 |
- mFile_p = fopen(path, "r"); |
|
45 |
+ //mFile_p = fopen(path, "r"); |
|
41 | 46 |
} |
42 | 47 |
|
43 | 48 |
bool MtxParser::hasNext() |
... | ... |
@@ -51,13 +56,9 @@ MatrixElement MtxParser::getNext() |
51 | 56 |
unsigned row = 0, col = 0; |
52 | 57 |
float val = 0.f; |
53 | 58 |
|
54 |
- |
|
55 |
- |
|
56 |
- //mFile >> row; |
|
57 |
- //mFile >> col; |
|
58 |
- //mFile >> val; |
|
59 |
- |
|
60 |
- |
|
59 |
+ mFile >> row; |
|
60 |
+ mFile >> col; |
|
61 |
+ mFile >> val; |
|
61 | 62 |
|
62 | 63 |
return MatrixElement(row - 1, col - 1, val); |
63 | 64 |
} |
... | ... |
@@ -3,19 +3,26 @@ |
3 | 3 |
|
4 | 4 |
#include "../utils/GapsAssert.h" |
5 | 5 |
|
6 |
+#include <cstdio> |
|
7 |
+#include <cunistd> |
|
6 | 8 |
#include <sstream> |
7 | 9 |
|
8 | 10 |
MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0) |
9 | 11 |
{ |
10 |
- mFile.open(path.c_str()); |
|
12 |
+ // check if file exists and read it in |
|
13 |
+ if (access(path, F_OK) == -1) |
|
14 |
+ { |
|
15 |
+ GAPS_ERROR("Invalid MTX file"); |
|
16 |
+ } |
|
17 |
+ mFile = fopen(path, "r"); |
|
11 | 18 |
|
12 | 19 |
// read first line |
20 |
+ char line[1024]; |
|
21 |
+ fgets(line, 1024, mFile); |
|
22 |
+ |
|
23 |
+ |
|
13 | 24 |
std::string line; |
14 | 25 |
std::getline(mFile, line); |
15 |
- if (mFile.eof() || mFile.fail()) |
|
16 |
- { |
|
17 |
- GAPS_ERROR("Invalid MTX file"); |
|
18 |
- } |
|
19 | 26 |
|
20 | 27 |
// skip over comments |
21 | 28 |
while (line.find('%') != std::string::npos) |
... | ... |
@@ -30,6 +37,7 @@ MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0) |
30 | 37 |
|
31 | 38 |
// store dimensions |
32 | 39 |
ss >> mNumRows >> mNumCols; |
40 |
+ mFile_p = fopen(path, "r"); |
|
33 | 41 |
} |
34 | 42 |
|
35 | 43 |
bool MtxParser::hasNext() |
... | ... |
@@ -43,9 +51,13 @@ MatrixElement MtxParser::getNext() |
43 | 51 |
unsigned row = 0, col = 0; |
44 | 52 |
float val = 0.f; |
45 | 53 |
|
46 |
- mFile >> row; |
|
47 |
- mFile >> col; |
|
48 |
- mFile >> val; |
|
54 |
+ |
|
55 |
+ |
|
56 |
+ //mFile >> row; |
|
57 |
+ //mFile >> col; |
|
58 |
+ //mFile >> val; |
|
59 |
+ |
|
60 |
+ |
|
49 | 61 |
|
50 | 62 |
return MatrixElement(row - 1, col - 1, val); |
51 | 63 |
} |
... | ... |
@@ -1,10 +1,12 @@ |
1 |
-#include "MtxParser.h" |
|
2 | 1 |
#include "MatrixElement.h" |
2 |
+#include "MtxParser.h" |
|
3 |
+ |
|
3 | 4 |
#include "../GapsAssert.h" |
4 | 5 |
|
5 |
-#include <sstream> |
|
6 | 6 |
#include <Rcpp.h> |
7 | 7 |
|
8 |
+#include <sstream> |
|
9 |
+ |
|
8 | 10 |
MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0) |
9 | 11 |
{ |
10 | 12 |
mFile.open(path.c_str()); |
... | ... |
@@ -1,17 +1,30 @@ |
1 | 1 |
#include "MtxParser.h" |
2 | 2 |
#include "MatrixElement.h" |
3 |
+#include "../GapsAssert.h" |
|
3 | 4 |
|
4 | 5 |
#include <sstream> |
6 |
+#include <Rcpp.h> |
|
5 | 7 |
|
6 | 8 |
MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0) |
7 | 9 |
{ |
8 | 10 |
mFile.open(path.c_str()); |
9 | 11 |
|
12 |
+ // read first line |
|
13 |
+ std::string line; |
|
14 |
+ std::getline(mFile, line); |
|
15 |
+ if (mFile.eof() || mFile.fail()) |
|
16 |
+ { |
|
17 |
+ GAPS_ERROR("Invalid MTX file"); |
|
18 |
+ } |
|
19 |
+ |
|
10 | 20 |
// skip over comments |
11 |
- std::string line = "%"; |
|
12 | 21 |
while (line.find('%') != std::string::npos) |
13 | 22 |
{ |
14 | 23 |
std::getline(mFile, line); |
24 |
+ if (mFile.eof() || mFile.fail()) |
|
25 |
+ { |
|
26 |
+ GAPS_ERROR("Invalid MTX file"); |
|
27 |
+ } |
|
15 | 28 |
} |
16 | 29 |
std::stringstream ss(line); // this line contains dimensions |
17 | 30 |
|
... | ... |
@@ -21,17 +34,18 @@ MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0) |
21 | 34 |
|
22 | 35 |
bool MtxParser::hasNext() |
23 | 36 |
{ |
37 |
+ mFile >> std::ws; // get rid of whitespace |
|
24 | 38 |
return mFile.peek() != EOF; |
25 | 39 |
} |
26 | 40 |
|
27 | 41 |
MatrixElement MtxParser::getNext() |
28 | 42 |
{ |
29 |
- MatrixElement e(0, 0, 0.f); |
|
30 |
- unsigned buff; |
|
31 |
- mFile >> buff; |
|
32 |
- e.dim[0] = buff - 1; |
|
33 |
- mFile >> buff; |
|
34 |
- e.dim[1] = buff - 1; |
|
35 |
- mFile >> e.value; |
|
36 |
- return e; |
|
43 |
+ unsigned row = 0, col = 0; |
|
44 |
+ float val = 0.f; |
|
45 |
+ |
|
46 |
+ mFile >> row; |
|
47 |
+ mFile >> col; |
|
48 |
+ mFile >> val; |
|
49 |
+ |
|
50 |
+ return MatrixElement(row - 1, col - 1, val); |
|
37 | 51 |
} |
... | ... |
@@ -27,6 +27,11 @@ bool MtxParser::hasNext() |
27 | 27 |
MatrixElement MtxParser::getNext() |
28 | 28 |
{ |
29 | 29 |
MatrixElement e(0, 0, 0.f); |
30 |
- mFile >> e.dim[0] >> e.dim[1] >> e.value; |
|
30 |
+ unsigned buff; |
|
31 |
+ mFile >> buff; |
|
32 |
+ e.dim[0] = buff - 1; |
|
33 |
+ mFile >> buff; |
|
34 |
+ e.dim[1] = buff - 1; |
|
35 |
+ mFile >> e.value; |
|
31 | 36 |
return e; |
32 | 37 |
} |
... | ... |
@@ -14,7 +14,7 @@ MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0) |
14 | 14 |
std::getline(mFile, line); |
15 | 15 |
} |
16 | 16 |
std::stringstream ss(line); // this line contains dimensions |
17 |
- |
|
17 |
+ |
|
18 | 18 |
// store dimensions |
19 | 19 |
ss >> mNumRows >> mNumCols; |
20 | 20 |
} |
... | ... |
@@ -27,6 +27,6 @@ bool MtxParser::hasNext() |
27 | 27 |
MatrixElement MtxParser::getNext() |
28 | 28 |
{ |
29 | 29 |
MatrixElement e(0, 0, 0.f); |
30 |
- mFile >> e.row >> e.col >> e.value; |
|
30 |
+ mFile >> e.dim[0] >> e.dim[1] >> e.value; |
|
31 | 31 |
return e; |
32 | 32 |
} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,32 @@ |
1 |
+#include "MtxParser.h" |
|
2 |
+#include "MatrixElement.h" |
|
3 |
+ |
|
4 |
+#include <sstream> |
|
5 |
+ |
|
6 |
+MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0) |
|
7 |
+{ |
|
8 |
+ mFile.open(path.c_str()); |
|
9 |
+ |
|
10 |
+ // skip over comments |
|
11 |
+ std::string line = "%"; |
|
12 |
+ while (line.find('%') != std::string::npos) |
|
13 |
+ { |
|
14 |
+ std::getline(mFile, line); |
|
15 |
+ } |
|
16 |
+ std::stringstream ss(line); // this line contains dimensions |
|
17 |
+ |
|
18 |
+ // store dimensions |
|
19 |
+ ss >> mNumRows >> mNumCols; |
|
20 |
+} |
|
21 |
+ |
|
22 |
+bool MtxParser::hasNext() |
|
23 |
+{ |
|
24 |
+ return mFile.peek() != EOF; |
|
25 |
+} |
|
26 |
+ |
|
27 |
+MatrixElement MtxParser::getNext() |
|
28 |
+{ |
|
29 |
+ MatrixElement e(0, 0, 0.f); |
|
30 |
+ mFile >> e.row >> e.col >> e.value; |
|
31 |
+ return e; |
|
32 |
+} |