-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarks.cpp
150 lines (139 loc) · 4.3 KB
/
marks.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "marks.h"
#include <QTextStream>
Marks::Marks()
{
_pos = QList<qint64>();
_marks = QList<MarkTypes>();
_samplesize = 3;
_startnr = 1;
timer = new QTimer();
connect(timer, &QTimer::timeout, this, &Marks::tmpSave);
}
void Marks::Save(QFile *file, QStringList label)
{
try {
validate();
file->open(QFile::WriteOnly);
QTextStream ts(file);
ts.setCodec("UTF-8");
ts << (_samplesize * 8) << ";" << _startnr << ";" << label.join("\\") << "\n";
for (int i = 0; i < _pos.count(); i++) {
if (_strings.at(i) != "")
ts << _pos.at(i) << "," << _marks.at(i) << "," << _strings.at(i) << "\n";
else
ts << _pos.at(i) << "," << _marks.at(i) << "\n";
}
} catch (...) {
emit Debug("Save " + file->fileName() + " " + label.join(";"));
}
tmpfilename = file->fileName() + ".save";
_label = label;
if (!timer->isActive())
timer->start(10000);
}
QStringList Marks::Read(QFile *file)
{
_pos.clear();
_marks.clear();
_startnr = 1;
QStringList ret;
if (file->exists()) {
file->open(QFile::ReadOnly);
if (!file->atEnd()) {
QString line = QString::fromUtf8(file->readLine());
bool ok;
_samplesize = line.section(';', 0, 0).toInt(&ok);
if (!ok) {
_samplesize = 3;
emit Debug("Mark read first line Error:" + line);
}
else {
_samplesize /= 8;
if (line.contains(';')) {
_startnr = line.section(';', 1, 1).toInt(&ok);
if (!ok) {
_startnr = 1;
emit Debug("Mark read first line Error:" + line);
}
else {
ret = line.section(';', 2, -1).split('\\');
}
}
}
}
while (!file->atEnd()) {
QString line = file->readLine();
if (line != "") {
qint64 ipos = line.section(',',0,0).toLongLong();
int typ = line.section(',', 1, 1).toInt();
if (ipos > -1) {
if (typ > -1 && typ < noFlag) {
if (line.count(',') > 1)
Add(ipos, static_cast<MarkTypes>(typ), line.section(',', 2, -1).simplified());
else
Add(ipos, static_cast<MarkTypes>(typ));
}
}
}
}
}
validate();
tmpfilename = file->fileName() + ".save";
_label = ret;
if (!timer->isActive())
timer->start(10000);
return ret;
}
QStringList Marks::ReadAutoSaved(QString filename)
{
QStringList ret = Read(new QFile(filename + ".save"));
tmpfilename = filename;
return ret;
}
void Marks::tmpSave()
{
try {
validate();
QFile *tmpfile = new QFile(tmpfilename);
tmpfile->open(QFile::WriteOnly);
QTextStream ts(tmpfile);
ts << (_samplesize * 8) << ";" << _startnr << ";" << _label.join("\\") << "\n";
for (int i = 0; i < _pos.count(); i++) {
if (_strings.at(i) != "")
ts << _pos.at(i) << "," << _marks.at(i) << "," << _strings.at(i) << "\n";
else
ts << _pos.at(i) << "," << _marks.at(i) << "\n";
}
} catch (...) {
emit Debug("tmpSave " + tmpfilename + " " + _label.join(";"));
}
}
void Marks::validate()
{
int len = _pos.length();
if (_marks.length() < len)
len = _marks.length();
if (_strings.length() < len)
len = _strings.length();
for (int i = 0; i < len; i++) {
if (_strings.at(i) != "") {
if (_marks.at(i) != StartTrack) {
QString txt = _strings.at(i);
QString old = "";
_strings[i] = "";
for (int j = i - 1; j >= 0 && txt != ""; j--) {
if (_marks.at(j) == StartTrack) {
if (_strings.at(j) == txt) {
txt = "";
}
else {
old = _strings.at(j);
_strings[j] = txt;
txt = old;
}
}
}
}
}
}
}