-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbfileentry.cpp
executable file
·92 lines (82 loc) · 2.29 KB
/
dbfileentry.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
#include "dbfileentry.h"
#include "stringhelper.h"
#include <QTextStream>
#include <QStringList>
#include <QFileInfo>
#include <QDebug>
QString DBFileEntry::dateTimeFormat= "yyyyMMddThh:mm:ss.zzz";
QChar DBFileEntry::fieldSeparator = ',';
DBFileEntry::DBFileEntry() : m_size(0), m_linkType('C')
{
}
DBFileEntry::DBFileEntry(const DBFileEntry& entry) : DBFileEntry()
{
operator=(entry);
}
DBFileEntry::DBFileEntry(const QFileInfo& info, const QString& rootPath) : m_size(info.size()), m_time(info.lastModified()), m_path(info.canonicalFilePath())
{
if (rootPath.length() > 0)
{
if (m_path.startsWith(rootPath, Qt::CaseSensitive)) {
// Remove the root-path from the full path.
m_path = m_path.right(m_path.length() - rootPath.length());
} else {
// Unsure how this happened, the full path should always contain the root path.
qDebug() << QString("File path (%1) does not contain root path (%2)").arg(m_path, rootPath);
}
}
}
DBFileEntry::~DBFileEntry()
{
}
bool DBFileEntry::readLine(QTextStream& stream)
{
if (stream.status() != QTextStream::Ok || stream.atEnd()) {
return false;
}
stream.skipWhiteSpace();
QString line = stream.readLine();
if (line.isNull()) {
return false;
}
//This fails if the path contains a comma.
//QStringList tokens = line.split(fieldSeparator);
QStringList tokens = StringHelper::split(fieldSeparator, line, 5);
if (tokens.count() < 5) {
return false;
}
if (tokens[0].length() > 0) {
m_linkType = tokens[0].at(0);
}
m_time = QDateTime::fromString(tokens[1], dateTimeFormat);
m_hash = tokens[2];
m_hash = m_hash.toUpper();
bool ok;
m_size = tokens[3].toULongLong(&ok, 10);
if (!ok) {
return false;
}
m_path = tokens[4];
return true;
}
bool DBFileEntry::writeLine(QTextStream& stream)
{
stream << m_linkType << fieldSeparator;
stream << m_time.toString(dateTimeFormat) << fieldSeparator;
stream << m_hash << fieldSeparator;
stream << m_size << fieldSeparator;
stream << m_path << "\n";
return (stream.status() == QTextStream::Ok);
}
DBFileEntry& DBFileEntry::operator=(const DBFileEntry& entry)
{
if (&entry != this)
{
m_size = entry.m_size;
m_linkType = entry.m_linkType;
m_time = entry.m_time;
m_path = entry.m_path;
m_hash = entry.m_hash;
}
return *this;
}