-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmifserializer.cpp
57 lines (49 loc) · 1.57 KB
/
mifserializer.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
#include "mifserializer.h"
int MifSerializer::MifToVector(QFile& f, QVector<quint32>& vec, QString& errstr)
{
QByteArray ba = f.readLine();
int lnum = 0;
while(ba.length())
{
lnum++;
// Split the line in the tab character
QString line(ba);
QVector<QStringRef> split = line.splitRef(QChar('\t'));
if(split.length() != 2)
{
errstr = QObject::tr("More than two columns detected in input file");
return lnum;
}
bool ok;
int currentmempos = (int)split.at(0).toUShort(&ok, 2);
if(!ok)
{
errstr = QObject::tr("Could not parse memory address as a number");
return lnum;
}
unsigned long val = split.at(1).toULong(&ok, 2);
if(!ok)
{
errstr = QObject::tr("Could not parse memory data as a number");
return lnum;
}
// Because they used a hashmap originally, we need to check if we need to expand the vector
// as the addresses might skip, and then insert the line
if(vec.size() < currentmempos + 1)
vec.resize(currentmempos + 1);
vec[currentmempos] = val;
ba = f.readLine();
}
return 0;
}
// This file must be opened in binary mode
int MifSerializer::VectorToMif(QFile& f, QVector<quint32>& vec)
{
for(int i = 0; i < vec.length(); i++)
{
QString str = QString("%1\t%2\n").arg(i, 13, 2, QChar('0')).arg(vec.at(i), 20, 2, QChar('0'));
if(!f.write(str.toUtf8()))
return i + 1;
}
return 0;
}