-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceAccess.cpp
More file actions
69 lines (55 loc) · 1.58 KB
/
SourceAccess.cpp
File metadata and controls
69 lines (55 loc) · 1.58 KB
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
//Submodule file
#include <iostream>
#include <fstream>
#include "SourceAccess.h"
int SourceAccess::Open(const std::string& fileName) {
std::ifstream inFile;
unsigned int size = 0;
DataPoint newData;
fs::path newFilePath = PARENT_DIRECTORY + fileName;
if(newFilePath.extension() == "") {
newFilePath += ".txt";
}
inFile.open(newFilePath);
if(inFile.is_open() == false) {
return -1;
}
filePath = newFilePath;
inFile >> size;
inFile.ignore();
dataPoints.resize(size);
for(unsigned int i = 0; i < size; i++) {
inFile >> newData.id;
inFile.ignore();
getline(inFile, newData.value);
dataPoints.at(i) = newData;
}
inFile.close();
return 0;
}
std::string SourceAccess::GetValue(const std::string& dataId) const {
int index = IndexFromId(dataId);
if(index == -1) {//IndexFromId returns -1 if a data point was not found
return "ERROR: No data point of ID [" + dataId + "] was found in " + filePath.generic_string();
}
return dataPoints.at(index).value;
}
void SourceAccess::Print(bool printIds) const {
for(DataPoint data : dataPoints) {
if(printIds == true){
std::cout << data.id << " ";
}
std::cout << data.value << std::endl;
}
}
int SourceAccess::Size() const {
return dataPoints.size();
}
int SourceAccess::IndexFromId(const std::string& dataId) const {
for(unsigned int i = 0; i < dataPoints.size(); ++i) {
if(dataPoints.at(i).id == dataId) {
return i;
}
}
return -1;
}