Source

lrc / src / compiler / InFileParser.hxx

Full commit
//      InFileParser.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 InFileParser.hxx
 *
 * This file contains the base class for all parsers that read and parse an
 * input file for the Linux Resource Compiler
 *
 * \author Andreas Tscharner
 * \date 2012-09-01
 */


#ifndef __INFILEPARSER_HXX__
#define __INFILEPARSER_HXX__


// Include files
#include <vector>
#include "../ResourceData.hxx"
#include "../lrcExceptions.hxx"


/*! \class InFileParser
 *  \brief Base class for all \c lrc input files
 *
 * This class is the base class for all input files for the Linux Resource
 * Compiler. All input file parser must derive from it.
 */
class InFileParser
{
	protected:
		/*! \brief Possible internal errors
		 *
		 * An enumeration of all possible internal errors
		 */
		enum internalErrorType {
			ieNone,                    //!< No internal error
			ieInvalidElement,          //!< The root or any other element has a wrong name
			ieIdentNotFound,           //!< The identifier could not be found
			ieMissingPassword,         //!< An encryption is used, but no password is given
			ieFilenameNotFound,        //!< The actual resource filename is missing
			iePasswordFileNotFound     //!< The password file could not be found
		};

		char *m_filename;                             //!< Filename of the RC file
		std::vector<ResourceData *> *m_resEntries;    //!< List of resource data entries
		inFilePosition m_errorPosition;               //!< Line and column of error
		int m_lastError;                              //!< Error code of last error
		internalErrorType m_internalError;            //!< Internal error


		/*! \brief Clear all internal errors
		 *
		 * This method is used to clear all internal errors. It is virtual
		 * and should be overwritten by derived classes
		 */
		virtual void clear_internal_error(void);

	public:
		/*! \brief Constructor
		 *
		 * This is the constructor. It expects the name of the file to parse
		 * and initializes all the members of the class
		 *
		 * \param[in] p_filename Filename of input file
		 *
		 * \exception lrcFileNotFoundException Exception that is thrown if
		 * the given file could not be found
		 */
		InFileParser(char *) throw(lrcFileNotFoundException);
		/*! \brief Destructor
		 *
		 * Cleans up the memory and resources the parser class needed
		 *
		 * \exception lrcFileNotFoundException This exception will be thrown
		 *            if the given file for parsing could not be found
		 */
		~InFileParser(void);

		/*! \brief Evaluate compression type from string
		 *
		 * This method evaluates which compression type should be used,
		 * defined by the given string
		 *
		 * \param[in] p_compStr Compression type as string
		 *
		 * \retval lrc::NoneComression No compression
		 * \retval lrc::zLibCompression zlib compression
		 *
		 * \remarks lrc::NoneCompression is the default if the given string
		 *          defines no other compression type
		 */
		static lrc::CompressionType eval_compression_type(const char *);
		/*! \brief Evaluate encryption type from string
		 *
		 * This method evaluated which encryption type should be used
		 * defined by the given string
		 *
		 * \param[in] p_encStr Encryption type as string
		 *
		 * \retval lrc::NoneEncryption No encryption
		 * \retval lrc::SerpentEncryption Serpent encryption
		 *
		 * \remarks lrc::NoneEncryption is the default if the ginve string
		 *          defines no other encryption type
		 */
		static lrc::EncryptionType eval_encryption_type(const char *);
		/*! \brief Get password
		 *
		 * This method gets the password, either directly from the .rc or
		 * .rif file or it is getting the password from a different file if
		 * the "@" notation (re-direct to a file) is used.
		 *
		 * \param[in] p_passwdStr Password string
		 *
		 * \return Password directly from input file or from re-directed
		 *         file
		 *
		 * \exception lrcFileNotFoundException This exception will be thrown
		 *            if the password should be read from a file and the
		 *            given file could not be found
		 *
		 * \remarks The caller is responsible to free the used memory that
		 *          the returned password needs
		 */
		 static unsigned char *get_password(const char *) throw(lrcFileNotFoundException);

		/*! \brief Parses the file
		 *
		 * This method parses the file, creates a ResourceData class for
		 * each entry and adds them to the internal structure
		 *
		 * \remarks If the method returns ERROR_PARSE, get_internal_error
		 *          will provide more information
		 *
		 * \retval NO_ERROR File successfully parsed
		 * \retval ERROR_FILE_OPEN An error occurred while trying to open
		 *                         the file
		 * \retval ERROR_PARSE An error occurred while trying to parse the
		 *                     file
		 */
		virtual int parse(void) = 0;

		/*! \brief Returns internal error
		 *
		 * This method returns the state of the last internal error together
		 * with the line and column position where it happened and an error
		 * message
		 *
		 * \remarks The internal error state will be cleared after that call
		 *
		 * \param[out] p_errPos Line and column of error
		 * \param[out] p_errMsg Error message
		 *
		 * \return Error code of last error
		 */
		virtual int get_internal_error(inFilePosition &, char **);

		/*! \brief Return all resource entries
		 *
		 * This method returns all parsed resource entries in a vector of
		 * pointers to a ResourceData class
		 *
		 * \return Resource entries
		 */
		std::vector<ResourceData *> *get_resource_entries(void);
};

#endif /* __INFILEPARSER_HXX__ */