Source

lrc / src / include / ResourceManager.hxx

Full commit
//      ResourceManager.hxx
//
//      Copyright 2011, 2012 Andreas Tscharner <andy@vis.ethz.ch>
//
//      This program 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 3 of the
//      License, or (at your option) any later version.
//
//      This program 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 General Public License for more details.
//
//      You should have received a copy of the GNU Lesser General Public
//      License along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


/*! \file ResourceManager.hxx
 *
 * This file contains the declaration of the ResourceManager, the main class for
 * \c liblrc
 *
 * \author Andreas Tscharner
 * \date 2013-01-06
 */


#ifndef __RESOURCE_MANAGER_HXX__
#define __RESOURCE_MANAGER_HXX__


// Include files
#include <stddef.h>
#include "Resource.hxx"
#include "../ResourceData.hxx"
#include "../lrcExceptions.hxx"


namespace lrc
{
	/*! \class ResourceManager
	 *  \brief Manager class handling all resources of a resource file
	 *
	 * This is the main class of the \c liblrc library. It expects a
	 * filename of a resource file in the constructor and handles all
	 * resources in that given file.
	 */
	class ResourceManager
	{
		private:
			char *m_resourceFile;      //!< Filename of resource file
			resEntry *m_resEntries;    //!< Entries of the resource file
			int m_numResEntries;       //!< Number of entries in resource file
			unsigned char *m_resData;  //!< Pointer to data of resource file
			size_t m_resDataSize;      //!< Size of resource data
			CompressionType m_compType;//!< Compression type for complete file
			EncryptionType m_encType;  //!< Encryption type for complete file
			unsigned char *m_password; //!< Password is complete file is encrypted

			/*! \brief Method to load resource file
			 *
			 * This method loads all data from the resource file
			 *
			 * \retval NO_ERROR Resource file successfully loaded
			 * \retval ERROR_FILE_READ An error occurred while reading the
			 *                         file
			 */
			int load_from_file(void);

		public:
			/*! \brief Constructor expecting resource file name
			 *
			 * The constructor expects the name of a resource file. The file
			 * will be loaded at first use, but an exceptions will be thrown
			 * if it does not exist
			 *
			 * \param[in] p_resFilename Filename of resource file
			 * \param[in] p_compress Compression type if the complete file is
			 *                       compressed
			 * \param[in] p_encrypt Encryption type if the complete file is
			 *                      encrypted
			 * \param[in] p_key Password if whole file is encrypted
			 *
			 * \exception lrcFileNotFoundException Will be thrown if the
			 *                                     file does not exist
			 */
			ResourceManager(const char *, const CompressionType, const EncryptionType, const unsigned char *) throw (lrcFileNotFoundException);
			/*! \brief Destructor
			 *
			 * The destructor cleans up all the used memory of the class
			 */
			~ResourceManager(void);

			/*! \brief Returns a list of all resources by ID
			 *
			 * This method returns a list of all resources. The list
			 * contains all IDs of the resources
			 *
			 * \param[out] p_numRes Number of resources in list
			 *
			 * \remarks The caller is resposible to free the used memory
			 */
			char **get_resource_ids(int &);

			/*! \brief Returns the requested resource
			 *
			 * This method returns the requested resource defined by the
			 * given resource ID or \c nullptr if it does not exist
			 *
			 * \param[in] p_resID Resource ID
			 * \param[in] p_password Password if resource is encrypted
			 *
			 * \return Instance of Resource class of the requested resource
			 *         (or \c nullptr if it does not exist)
			 *
			 * \remarks The caller is responsible to free the used memory
			 */
			Resource *get_resource(const char *, const unsigned char *p_password = nullptr);

			/*! \brief Returns the resource at the index
			 *
			 * This method returns the requested resource defined by the
			 * given index or \c nullptr if the index is out of range
			 *
			 * \param[in] p_resIdx Index of resource
			 * \param[in] p_password Password if resource is encrypted
			 *
			 * \return Instance of Resource class of the resource at the
			 *         index (or \c nullptr if the index is out of range)
			 *
			 * \remarks Indices are zero based, i.e. valid indices are
			 *          between 0 and n-1 (n means the number of all
			 *          resources)
			 * \remarks The caller is responsible to free the used memory
			 */
			Resource *get_resource(unsigned int, const unsigned char *p_password = nullptr);
	};
}


#endif /* __RESOURCE_MANAGER_HXX__ */