/
fileid.cpp
105 lines (91 loc) · 2.52 KB
/
fileid.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
/** @file fileid.cpp
*
* Implements a file identifier in terms of a MD5 hash of its absolute path.
*
* @ingroup types
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <QDir>
#include <QCryptographicHash>
#include <de/App>
#include <de/Log>
#include "doomsday/filesys/fs_main.h"
#include "doomsday/filesys/fileid.h"
using namespace de;
FileId::FileId(Md5Hash _md5)
: md5_(_md5.left(16))
#ifdef DENG_DEBUG
, path_("unknown-path")
#endif
{}
FileId::FileId(FileId const &other)
: LogEntry::Arg::Base()
, md5_(other.md5())
#ifdef DENG_DEBUG
, path_(other.path())
#endif
{}
FileId &FileId::operator = (FileId other)
{
swap(*this, other);
return *this;
}
bool FileId::operator < (FileId const &other) const
{
return md5_ < other.md5_;
}
bool FileId::operator == (FileId const &other) const
{
return md5_ == other.md5_;
}
bool FileId::operator != (FileId const &other) const
{
return md5_ != other.md5_;
}
String FileId::asText() const
{
String txt;
txt.reserve(32);
for(int i = 0; i < 16; ++i)
{
txt += String("%1").arg(String::number((byte)md5_.at(i), 16), 2, '0');
}
return txt;
}
FileId FileId::fromPath(String path)
{
FileId fileId = FileId(hash(path));
#ifdef DENG_DEBUG
fileId.setPath(path);
#endif
return fileId;
}
FileId::Md5Hash FileId::hash(String path)
{
// Ensure we've a normalized path.
if(QDir::isRelativePath(path))
{
path = App_BasePath() / path;
}
#if defined(WIN32) || defined(MACOSX)
// This is a case insensitive operation.
path = path.toUpper();
#endif
return QCryptographicHash::hash(path.toUtf8(), QCryptographicHash::Md5);
}