-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSVReader.cpp
85 lines (70 loc) · 1.5 KB
/
CSVReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// Created by mfbut on 1/20/2018.
//
#include <string>
#include "CSVReader.h"
#include <sstream>
Monopoly::CSVReader::CSVReader(const std::string& fileName) : file(fileName), fileName(fileName) {
}
Monopoly::CSVReader::~CSVReader() {
if (file.is_open()) {
file.close();
}
}
/**
* Read the next field from the csv file and give it back as a string
* @return
*/
std::string Monopoly::CSVReader::get_next_field() {
std::string field;
char c;
while (file.get(c)) {
if (c == ',' || c == '\n') {
break;
} else {
field.push_back(c);
}
}
return field;
}
/**
* Read the next field from the stream as an int
* @return
*/
int Monopoly::CSVReader::get_next_field_as_int() {
int num;
std::stringstream field(get_next_field());
field >> num;
return num;
}
/**
* Read characters from the file until one of the stop characters is encountered
* @param stopChars
*/
void Monopoly::CSVReader::skip_until(const std::string& stopChars) {
char c;
while (file.get(c)) {
//if we found one of the stop characters
if (stopChars.find_first_of(c) != std::string::npos) {
break; //stop searching
}
}
}
/**
* Skip the next field in the file
*/
void Monopoly::CSVReader::skip_field() {
skip_until(",\n");
}
/**
* Skip the next line of the file
*/
void Monopoly::CSVReader::skip_line() {
skip_until("\n");
}
bool Monopoly::CSVReader::good() const {
return file.good();
}
const std::string& Monopoly::CSVReader::getFileName() const {
return fileName;
}