aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8.1/source/Irrlicht/CFileList.cpp
blob: 1b8ff4f69a40d8a8c2ac3950d3a013e71be70b63 (plain)
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
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h

#include "CFileList.h"
#include "IrrCompileConfig.h"
#include "irrArray.h"
#include "coreutil.h"

#include "os.h"

namespace irr
{
namespace io
{

static const io::path emptyFileListEntry;

CFileList::CFileList(const io::path& path, bool ignoreCase, bool ignorePaths)
 : IgnorePaths(ignorePaths), IgnoreCase(ignoreCase), Path(path)
{
	#ifdef _DEBUG
	setDebugName("CFileList");
	#endif

	Path.replace('\\', '/');
}

CFileList::~CFileList()
{
	Files.clear();
}

u32 CFileList::getFileCount() const
{
	return Files.size();
}

void CFileList::sort()
{
	Files.sort();
}

const io::path& CFileList::getFileName(u32 index) const
{
	if (index >= Files.size())
		return emptyFileListEntry;

	return Files[index].Name;
}


//! Gets the full name of a file in the list, path included, based on an index.
const io::path& CFileList::getFullFileName(u32 index) const
{
	if (index >= Files.size())
		return emptyFileListEntry;

	return Files[index].FullName;
}

//! adds a file or folder
u32 CFileList::addItem(const io::path& fullPath, u32 offset, u32 size, bool isDirectory, u32 id)
{
	SFileListEntry entry;
	entry.ID   = id ? id : Files.size();
	entry.Offset = offset;
	entry.Size = size;
	entry.Name = fullPath;
	entry.Name.replace('\\', '/');
	entry.IsDirectory = isDirectory;

	// remove trailing slash
	if (entry.Name.lastChar() == '/')
	{
		entry.IsDirectory = true;
		entry.Name[entry.Name.size()-1] = 0;
		entry.Name.validate();
	}

	if (IgnoreCase)
		entry.Name.make_lower();

	entry.FullName = entry.Name;

	core::deletePathFromFilename(entry.Name);

	if (IgnorePaths)
		entry.FullName = entry.Name;

	//os::Printer::log(Path.c_str(), entry.FullName);

	Files.push_back(entry);

	return Files.size() - 1;
}

//! Returns the ID of a file in the file list, based on an index.
u32 CFileList::getID(u32 index) const
{
	return index < Files.size() ? Files[index].ID : 0;	
}

bool CFileList::isDirectory(u32 index) const
{
	bool ret = false;
	if (index < Files.size())
		ret = Files[index].IsDirectory;

	_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
	return ret;
}

//! Returns the size of a file
u32 CFileList::getFileSize(u32 index) const
{
	return index < Files.size() ? Files[index].Size : 0;
}

//! Returns the size of a file
u32 CFileList::getFileOffset(u32 index) const
{
	return index < Files.size() ? Files[index].Offset : 0;
}


//! Searches for a file or folder within the list, returns the index
s32 CFileList::findFile(const io::path& filename, bool isDirectory = false) const
{
	SFileListEntry entry;
	// we only need FullName to be set for the search
	entry.FullName = filename;
	entry.IsDirectory = isDirectory;

	// exchange
	entry.FullName.replace('\\', '/');

	// remove trailing slash
	if (entry.FullName.lastChar() == '/')
	{
		entry.IsDirectory = true;
		entry.FullName[entry.FullName.size()-1] = 0;
		entry.FullName.validate();
	}

	if (IgnoreCase)
		entry.FullName.make_lower();

	if (IgnorePaths)
		core::deletePathFromFilename(entry.FullName);

	return Files.binary_search(entry);
}


//! Returns the base path of the file list
const io::path& CFileList::getPath() const
{
	return Path;
}


} // end namespace irr
} // end namespace io