OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
bsa_file.hpp
Go to the documentation of this file.
1 /*
2  OpenMW - The completely unofficial reimplementation of Morrowind
3  Copyright (C) 2008-2010 Nicolay Korslund
4  Email: < korslund@gmail.com >
5  WWW: http://openmw.sourceforge.net/
6 
7  This file (bsa_file.h) is part of the OpenMW package.
8 
9  OpenMW is distributed as free software: you can redistribute it
10  and/or modify it under the terms of the GNU General Public License
11  version 3, as published by the Free Software Foundation.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  version 3 along with this program. If not, see
20  http://www.gnu.org/licenses/ .
21 
22  */
23 
24 #ifndef BSA_BSA_FILE_H
25 #define BSA_BSA_FILE_H
26 
27 #include <stdint.h>
28 #include <string>
29 #include <vector>
30 #include <map>
31 
33 
35 
36 
37 namespace Bsa
38 {
39 
43 class BSAFile
44 {
45 public:
47  struct FileStruct
48  {
49  // File size and offset in file. We store the offset from the
50  // beginning of the file, not the offset into the data buffer
51  // (which is what is stored in the archive.)
52  uint32_t fileSize, offset;
53 
54  // Zero-terminated file name
55  const char *name;
56  };
57  typedef std::vector<FileStruct> FileList;
58 
59 private:
62 
64  std::vector<char> stringBuf;
65 
67  bool isLoaded;
68 
70  std::string filename;
71 
73  struct iltstr
74  {
75  bool operator()(const char *s1, const char *s2) const
76  { return Misc::StringUtils::ciLess(s1, s2); }
77  };
78 
83  typedef std::map<const char*, int, iltstr> Lookup;
85 
87  void fail(const std::string &msg);
88 
90  void readHeader();
91 
94  int getIndex(const char *str) const;
95 
96 public:
97  /* -----------------------------------
98  * BSA management methods
99  * -----------------------------------
100  */
101 
103  : isLoaded(false)
104  { }
105 
107  void open(const std::string &file);
108 
109  /* -----------------------------------
110  * Archive file routines
111  * -----------------------------------
112  */
113 
115  bool exists(const char *file) const
116  { return getIndex(file) != -1; }
117 
122  Files::IStreamPtr getFile(const char *file);
123 
127  Files::IStreamPtr getFile(const FileStruct* file);
128 
131  const FileList &getList() const
132  { return files; }
133 };
134 
135 }
136 
137 #endif
std::vector< char > stringBuf
Filename string buffer.
Definition: bsa_file.hpp:64
static bool ciLess(const std::string &x, const std::string &y)
Definition: stringops.hpp:58
void open(const std::string &file)
Open an archive file.
Definition: bsa_file.cpp:163
std::map< const char *, int, iltstr > Lookup
Definition: bsa_file.hpp:83
uint32_t fileSize
Definition: bsa_file.hpp:52
void readHeader()
Read header information from the input source.
Definition: bsa_file.cpp:42
uint32_t offset
Definition: bsa_file.hpp:52
Definition: bsa_file.hpp:43
const char * name
Definition: bsa_file.hpp:55
std::string filename
Used for error messages.
Definition: bsa_file.hpp:70
void fail(const std::string &msg)
Error handling.
Definition: bsa_file.cpp:36
bool exists(const char *file) const
Check if a file exists.
Definition: bsa_file.hpp:115
BSAFile()
Definition: bsa_file.hpp:102
bool isLoaded
True when an archive has been loaded.
Definition: bsa_file.hpp:67
boost::shared_ptr< std::istream > IStreamPtr
Definition: constrainedfilestream.hpp:20
int getIndex(const char *str) const
Get the index of a given file name, or -1 if not found.
Definition: bsa_file.cpp:151
std::vector< FileStruct > FileList
Definition: bsa_file.hpp:57
bool operator()(const char *s1, const char *s2) const
Definition: bsa_file.hpp:75
Files::IStreamPtr getFile(const char *file)
Definition: bsa_file.cpp:169
Case insensitive string comparison.
Definition: bsa_file.hpp:73
Lookup lookup
Definition: bsa_file.hpp:84
FileList files
Table of files in this archive.
Definition: bsa_file.hpp:61
Represents one file entry in the archive.
Definition: bsa_file.hpp:47
const FileList & getList() const
Definition: bsa_file.hpp:131