-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileparser.cpp
128 lines (125 loc) · 3.96 KB
/
fileparser.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "fileparser.h"
#include "boost/filesystem/path.hpp"
#include "boost/filesystem/fstream.hpp"
#include "axis.h"
#include <sstream>
#include "graph.h"
#include <QMessageBox>
// process file to get axis from
Graph* FileParser::processFile(boost::filesystem::path path)
{
// temp path with removed ext
boost::filesystem::path temp_path(path);
// create stream to open file
boost::filesystem::fstream in;
// open file using path
in.open(temp_path, std::ios::in);
// remove ext so later we can use filename for photo name without the .xxx
temp_path.replace_extension(".png");
// temp Axis to pass between methods and set in graph object
Axis xy;
// temp graph
Graph* graph = new Graph();
// set filename
graph->setFileName(QString::fromStdString(temp_path.string()));
// error
if (in.fail())
{
// error message
QMessageBox messageBox;
messageBox.critical(0,"Error","Failed to parse file at it's location");
messageBox.setFixedSize(500,200);
}
// used to skip not needed lines
int counter = 0;
// used to get x,y min,max
float xmin = 0, ymin = 0, xmax = 0, ymax = 0;
// temp string to hold file
std::string temp = "";
// prase each file line
while (getline(in, temp))
{
// if the line is the time
if(counter == 11)
// get the time
graph->setTime(temp);
// if we are reading the data part of the file
if(counter > 14)
{
// process string to get xy axis
processString(temp, xy);
// get x,y coordinates
graph->setXAxisVectorPoint(xy.x);
graph->setYAxisVectorPoint(xy.y);
// first real line of data
if(counter == 15)
{
// set up default min and max as the firts value
xmin = xmax = xy.x;
ymin = ymax = xy.y;
// one more so it does not enter this if statement agian
counter++;
}
else
{
//compare to find min and max for each x and y
if(xy.x < xmin)
xmin = xy.x;
if(xy.x > xmax)
xmax = xy.x;
if(xy.y < ymin)
ymin = xy.y;
if(xy.y > ymax)
ymax = xy.y;
}
}
else
{
// increase counter
counter++;
}
}
// set min and max values for x and y
graph->setXMax(xmax);
graph->setXMin(xmin);
graph->setYMax(ymax);
graph->setYMin(ymin);
// return graph object
return graph;
}
// process string to get the x and y points
void FileParser::processString(std::string& file, Axis& ax)
{
// if string has data (assuming axis)
if (!file.empty())
{
// parse string enough to be able to
partialParse(file);
// use a stream to gt values
std::istringstream ss{file};
// right here
ss >> ax.x >> ax.y;
}
else
{
// error message
QMessageBox messageBox;
messageBox.critical(0,"Error","Can't process string, empty line in file");
messageBox.setFixedSize(500,200);
}
}
// parse file enough to us stream to get x and y axis
void FileParser::partialParse(std::string& str)
{
// get iterator to stop at first [
auto i{ std::find(std::begin(str), std::end(str), '[') };
// remove everything between start and that [ iterator
// so this removes the first - and not the - used to prepresent
// negative numbers
std::remove(std::begin(str), i, '-');
// after first -, remove any char that matches condition below
std::remove_if(std::begin(str), std::end(str), [](char c)
{
return c == ',' || c == '[' || c == ']';
});
}