aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/unarchiver/unarchiver.cpp
blob: 8351be3831cf011c4dd21a8f70c98df0120dc72b (plain) (blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * unarchiver.cpp
 * --------------
 * Purpose: archive loader
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */

#include "stdafx.h"

#include "unarchiver.h"
#include "../common/FileReader.h"


OPENMPT_NAMESPACE_BEGIN


CUnarchiver::CUnarchiver(FileReader &file)
	: impl(nullptr)
	, inFile(file)
	, emptyArchive(inFile)
#if (defined(MPT_WITH_ZLIB) && defined(MPT_WITH_MINIZIP)) || defined(MPT_WITH_MINIZ)
	, zipArchive(inFile)
#endif
#ifdef MPT_WITH_LHASA
	, lhaArchive(inFile)
#endif
#if defined(MPT_WITH_ZLIB) || defined(MPT_WITH_MINIZ)
	, gzipArchive(inFile)
#endif
#ifdef MPT_WITH_UNRAR
	, rarArchive(inFile)
#endif
#ifdef MPT_WITH_ANCIENT
	, ancientArchive(inFile)
#endif
{
	inFile.Rewind();
#if (defined(MPT_WITH_ZLIB) && defined(MPT_WITH_MINIZIP)) || defined(MPT_WITH_MINIZ)
	if(zipArchive.IsArchive()) { impl = &zipArchive; return; }
#endif
#ifdef MPT_WITH_LHASA
	if(lhaArchive.IsArchive()) { impl = &lhaArchive; return; }
#endif
#if defined(MPT_WITH_ZLIB) || defined(MPT_WITH_MINIZ)
	if(gzipArchive.IsArchive()) { impl = &gzipArchive; return; }
#endif
#ifdef MPT_WITH_UNRAR
	if(rarArchive.IsArchive()) { impl = &rarArchive; return; }
#endif
#ifdef MPT_WITH_ANCIENT
	if(ancientArchive.IsArchive()) { impl = &ancientArchive; return; }
#endif
	impl = &emptyArchive;
}


CUnarchiver::~CUnarchiver()
{
	return;
}


static inline std::string GetExtension(const std::string &filename)
{
	if(filename.find_last_of(".") != std::string::npos)
	{
		return mpt::ToLowerCaseAscii(filename.substr(filename.find_last_of(".") + 1));
	}
	return std::string();
}


std::size_t CUnarchiver::FindBestFile(const std::vector<const char *> &extensions)
{
	if(!IsArchive())
	{
		return failIndex;
	}
	uint64 biggestSize = 0;
	std::size_t bestIndex = failIndex;
	for(std::size_t i = 0; i < size(); ++i)
	{
		if(operator[](i).type != ArchiveFileType::Normal)
		{
			continue;
		}
		const std::string ext = GetExtension(operator[](i).name.ToUTF8());

		if(ext == "diz" || ext == "nfo" || ext == "txt")
		{
			// we do not want these
			continue;
		}

		// Compare with list of preferred extensions
		if(mpt::contains(extensions, ext))
		{
			bestIndex = i;
			break;
		}

		if(operator[](i).size >= biggestSize)
		{
			biggestSize = operator[](i).size;
			bestIndex = i;
		}
	}
	return bestIndex;
}


bool CUnarchiver::ExtractBestFile(const std::vector<const char *> &extensions)
{
	std::size_t bestFile = FindBestFile(extensions);
	if(bestFile == failIndex)
	{
		return false;
	}
	return ExtractFile(bestFile);
}


bool CUnarchiver::IsArchive() const
{
	return impl->IsArchive();
}


mpt::ustring CUnarchiver::GetComment() const
{
	return impl->GetComment();
}


bool CUnarchiver::ExtractFile(std::size_t index)
{
	return impl->ExtractFile(index);
}


FileReader CUnarchiver::GetOutputFile() const
{
	return impl->GetOutputFile();
}


std::size_t CUnarchiver::size() const
{
	return impl->size();
}


IArchive::const_iterator CUnarchiver::begin() const
{
	return impl->begin();
}


IArchive::const_iterator CUnarchiver::end() const
{
	return impl->end();
}


const ArchiveFileInfo & CUnarchiver::operator [] (std::size_t index) const
{
	return impl->operator[](index);
}


OPENMPT_NAMESPACE_END