-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistory.cpp
97 lines (71 loc) · 1.74 KB
/
History.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
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include "History.hpp"
History::History(std::string filepath)
: mFilepath(filepath), mData()
{
std::ifstream file(mFilepath.c_str());
if (file) {
std::string line;
while (getline(file, line)) {
if (line.size() == 0)
continue;
char trackpath[2048];
float value;
const char * cstring = line.c_str();
sscanf(cstring, "%f", &value);
int i = 0;
while (cstring[i] != ' ' && cstring[i] != '\0')
++i;
if (cstring[i] == ' ')
++i;
sprintf(trackpath, "%s", &(cstring[i]));
Track track;
track.path = std::string(trackpath);
track.value = value;
mData.push_back(track);
}
} else {
std::cerr << "Warning : can't open file in reading mode '" << mFilepath << "'." << std::endl;
}
}
History::~History()
{
std::ofstream file(mFilepath.c_str());
if (file) {
for(it = mData.begin(); it != mData.end() ; ++it)
file << (*it).value << " " << (*it).path << std::endl;
} else {
std::cerr << "Error : can't write file : '" << mFilepath << "'." << std::endl;
}
}
float History::getTime(std::string trackpath)
{
for(it = mData.begin(); it != mData.end() ; ++it)
if ((*it).path == trackpath)
return (*it).value;
return 0.0f;
}
void History::setTime(std::string trackpath, float value)
{
for(it = mData.begin(); it != mData.end() ; ++it) {
if ((*it).path == trackpath) {
//(*it).value = value;
it=mData.erase(it);
break;
}
}
if (mData.size() >= MAX_TRACK)
mData.erase(mData.begin());
Track track;
track.path = trackpath;
track.value = value;
mData.push_back(track);
}
std::string History::getLast()
{
if (mData.size() == 0)
return std::string();
return mData.at(mData.size()-1).path;
}