OpenMW
components/bsa/bsa_file.hpp
Go to the documentation of this file.
00001 /*
00002   OpenMW - The completely unofficial reimplementation of Morrowind
00003   Copyright (C) 2008-2010  Nicolay Korslund
00004   Email: < korslund@gmail.com >
00005   WWW: http://openmw.sourceforge.net/
00006 
00007   This file (bsa_file.h) is part of the OpenMW package.
00008 
00009   OpenMW is distributed as free software: you can redistribute it
00010   and/or modify it under the terms of the GNU General Public License
00011   version 3, as published by the Free Software Foundation.
00012 
00013   This program is distributed in the hope that it will be useful, but
00014   WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   General Public License for more details.
00017 
00018   You should have received a copy of the GNU General Public License
00019   version 3 along with this program. If not, see
00020   http://www.gnu.org/licenses/ .
00021 
00022  */
00023 
00024 #ifndef BSA_BSA_FILE_H
00025 #define BSA_BSA_FILE_H
00026 
00027 #include <libs/platform/stdint.h>
00028 #include <libs/platform/strings.h>
00029 #include <string>
00030 #include <vector>
00031 #include <map>
00032 
00033 #include <OgreDataStream.h>
00034 
00035 
00036 namespace Bsa
00037 {
00038 
00042 class BSAFile
00043 {
00044 public:
00046     struct FileStruct
00047     {
00048         // File size and offset in file. We store the offset from the
00049         // beginning of the file, not the offset into the data buffer
00050         // (which is what is stored in the archive.)
00051         uint32_t fileSize, offset;
00052 
00053         // Zero-terminated file name
00054         const char *name;
00055     };
00056     typedef std::vector<FileStruct> FileList;
00057 
00058 private:
00060     FileList files;
00061 
00063     std::vector<char> stringBuf;
00064 
00066     bool isLoaded;
00067 
00069     std::string filename;
00070 
00072     struct iltstr
00073     {
00074         bool operator()(const char *s1, const char *s2) const
00075         { return strcasecmp(s1,s2) < 0; }
00076     };
00077 
00082     typedef std::map<const char*, int, iltstr> Lookup;
00083     Lookup lookup;
00084 
00086     void fail(const std::string &msg);
00087 
00089     void readHeader();
00090 
00092     int getIndex(const char *str) const;
00093 
00094 public:
00095     /* -----------------------------------
00096      * BSA management methods
00097      * -----------------------------------
00098      */
00099 
00100     BSAFile()
00101       : isLoaded(false)
00102     { }
00103 
00105     void open(const std::string &file);
00106 
00107     /* -----------------------------------
00108      * Archive file routines
00109      * -----------------------------------
00110      */
00111 
00113     bool exists(const char *file) const
00114     { return getIndex(file) != -1; }
00115 
00119     Ogre::DataStreamPtr getFile(const char *file);
00120 
00122     const FileList &getList() const
00123     { return files; }
00124 };
00125 
00126 }
00127 
00128 #endif