File: dat1.cpp

package info (click to toggle)
fife 0.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,012 kB
  • ctags: 26,660
  • sloc: cpp: 84,903; sh: 10,269; python: 8,753; xml: 1,213; makefile: 265; objc: 245; ansic: 229
file content (152 lines) | stat: -rw-r--r-- 5,227 bytes parent folder | download | duplicates (2)
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
/***************************************************************************
 *   Copyright (C) 2005-2013 by the FIFE team                              *
 *   http://www.fifengine.net                                              *
 *   This file is part of FIFE.                                            *
 *                                                                         *
 *   FIFE is free software; you can redistribute it and/or                 *
 *   modify it under the terms of the GNU Lesser General Public            *
 *   License as published by the Free Software Foundation; either          *
 *   version 2.1 of the License, or (at your option) any later version.    *
 *                                                                         *
 *   This library 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     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

// Standard C++ library includes

// 3rd party library includes

// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "vfs/raw/rawdata.h"
#include "util/base/exception.h"
#include "util/log/logger.h"

#include "dat1.h"

namespace FIFE {
	static Logger _log(LM_FO_LOADERS);

	DAT1::DAT1(VFS* vfs, const std::string& file) : VFSSource(vfs), m_datpath(file), m_data(vfs->open(file))  {
		FL_LOG(_log, LMsg("MFFalloutDAT1") 
			<< "loading: " << file 
			<< " filesize: " << m_data->getDataLength());

		m_data->setIndex(0);

		const uint32_t dircount = m_data->read32Big();
		m_data->moveIndex(4*3);

		FL_LOG(_log, LMsg("MFFalloutDAT1") 
			<< "number of directories " << dircount);

		// Sanity check. Each dir entry needs min. 16 bytes.
		if( dircount*16 > m_data->getDataLength() ) {
			throw InvalidFormat("directory count larger than filesize.");
		}

		std::list<std::string> dir_names;
		for (uint32_t i = 0; i < dircount; ++i) {
			std::string name = readString();
			if (name == ".") {
				name = "";
			}
			dir_names.push_back(name);
		}

		for(std::list<std::string>::iterator i= dir_names.begin(); i!= dir_names.end(); ++i)
			loadFileList(*i);
	}

	void DAT1::loadFileList(const std::string& dirname) {
		const uint32_t filecount = m_data->read32Big();
		m_data->moveIndex(4*3);
		for (uint32_t i = 0; i < filecount; ++i) {
			RawDataDAT1::s_info info;
			info.name = fixPath(dirname + "/" + readString());
			info.type = m_data->read32Big();
			info.offset = m_data->read32Big();
			info.unpackedLength = m_data->read32Big();
			info.packedLength = m_data->read32Big();

			m_filelist.insert(std::make_pair(info.name, info));
		}
	}

	std::string DAT1::readString() {
		uint8_t length = m_data->read8();
		return m_data->readString(length);
	}

	RawData* DAT1::open(const std::string& file) const {
		const RawDataDAT1::s_info& info = getInfo(file);
		return new RawData(new RawDataDAT1(getVFS(), m_datpath, info));
	}

	bool DAT1::fileExists(const std::string& name) const {
		return m_filelist.find(name) != m_filelist.end();
	}

	const RawDataDAT1::s_info& DAT1::getInfo(const std::string& name) const {
		type_filelist::const_iterator i = m_filelist.find(name);
		if (i == m_filelist.end())
			throw NotFound(name);

		return i->second;
	}

	std::set<std::string> DAT1::listFiles(const std::string& pathstr) const {
		return list(pathstr, false);
	}

	std::set<std::string> DAT1::listDirectories(const std::string& pathstr) const {
		return list(pathstr, true);
	}

	std::set<std::string> DAT1::list(const std::string& pathstr, bool dirs) const {
		std::set<std::string> list;
		std::string path = pathstr;

		// Normalize the path
		if (path.find("./") == 0) {
			path.erase(0, 2);
		}
		
		size_t lastIndex = path.size();
		if (lastIndex != 0 && path[lastIndex-1] != '/') {
			path += '/';
		}

		type_filelist::const_iterator end = m_filelist.end();
		for (type_filelist::const_iterator i = m_filelist.begin(); i != end; ++i) {
			const std::string& file = i->first;
			if (file.find(path) == 0) {
				std::string cleanedfile = file.substr(path.size(), file.size()); // strip the pathstr
				bool isdir = cleanedfile.find('/') != std::string::npos; // if we still have a / it's a subdir

				if (isdir) {
					cleanedfile = cleanedfile.substr(0, cleanedfile.find('/'));
					if (cleanedfile.find('/') != cleanedfile.rfind('/')) {
						// check if this is a direct subdir
						continue;
					}
				}

				if (isdir == dirs) {
					list.insert(cleanedfile);
				}
			}
		}

		return list;
	}
} // FIFE