Download | Plain Text | No Line Numbers


  1. /**
  2.  * @module cscriptparser
  3.  * @author Manuel Mausz, 0728348
  4.  * @brief class for parsing simple scriptfiles
  5.  * @date 17.04.2009
  6.  */
  7.  
  8. #ifndef CSCRIPTPARSER_H
  9. #define CSCRIPTPARSER_H
  10.  
  11. #include <stdexcept>
  12. #include <string>
  13. #include <list>
  14. #include <set>
  15. #include "cfile.h"
  16.  
  17. /**
  18.  * @class CScriptparser
  19.  *
  20.  * Parses a simple line based scriptfile with some limitations:
  21.  * first function (starting a block) must be a read-command,
  22.  * last must be a write-command (ending this block).
  23.  *
  24.  * read- and write-commands have hard coded parameters, number#1 being a filetype.
  25.  * Classes handling certain filetypes must be of type CFile.
  26.  * Custom functions will be passed to CFile::callFunc().
  27.  *
  28.  * On error ParserError will be thrown.
  29.  */
  30. class CScriptparser
  31. {
  32. public:
  33. /**
  34.   * @class ParserError
  35.   * @brief Exception thrown by CScriptparser
  36.   */
  37. class ParserError : public std::invalid_argument {
  38. public:
  39. /**
  40.   * @method ParserError
  41.   * @brief Default exception ctor
  42.   * @param what message to pass along
  43.   * @return -
  44.   * @globalvars none
  45.   * @exception none
  46.   * @conditions none
  47.   */
  48. ParserError(const std::string& what)
  49. : std::invalid_argument(what), m_line("")
  50. {}
  51.  
  52. /**
  53.   * @method ParserError
  54.   * @brief Custom exception ctor
  55.   * @param what message to pass along
  56.   * @param line scriptline which is currently being parsed
  57.   * @return -
  58.   * @globalvars none
  59.   * @exception none
  60.   * @conditions none
  61.   */
  62. ParserError(const std::string& what, const std::string& line)
  63. : std::invalid_argument(what), m_line(line)
  64. {}
  65.  
  66. /**
  67.   * @method ~ParserError
  68.   * @brief Default dtor
  69.   * @param -
  70.   * @return -
  71.   * @globalvars none
  72.   * @exception not allowed
  73.   * @conditions none
  74.   */
  75. ~ParserError() throw()
  76. {}
  77.  
  78. /**
  79.   * @method getLine
  80.   * @brief returns reference to currently parsed scriptline (if set)
  81.   * @return reference to currently parsed scriptline (maybe empty string)
  82.   * @globalvars none
  83.   * @exception none
  84.   * @conditions none
  85.   */
  86. const std::string &getLine()
  87. {
  88. return m_line;
  89. }
  90.  
  91. private:
  92. /* members*/
  93. std::string m_line;
  94. };
  95.  
  96. /**
  97.   * @method CScriptparser
  98.   * @brief Default ctor
  99.   * @param scriptfile filename of script to parse
  100.   * @return -
  101.   * @globalvars none
  102.   * @exception bad_alloc
  103.   * @conditions none
  104.   */
  105. CScriptparser(const std::string& scriptfile);
  106.  
  107. /**
  108.   * @method ~CScriptparser
  109.   * @brief Default dtor
  110.   * @param -
  111.   * @return -
  112.   * @globalvars none
  113.   * @exception none
  114.   * @conditions none
  115.   */
  116. ~CScriptparser();
  117.  
  118. /**
  119.   * @method parse
  120.   * @brief Start parsing the scriptfile
  121.   * @param -
  122.   * @return -
  123.   * @globalvars none
  124.   * @exception ParserError
  125.   * @conditions none
  126.   */
  127. void parse();
  128.  
  129. protected:
  130. /**
  131.   * @method callFunc
  132.   * @brief Delegates the function and its parameters to the correct
  133.   * method (internal or handler)
  134.   * @param func function name
  135.   * @param funcparams function parameters as list
  136.   * @return -
  137.   * @globalvars none
  138.   * @exception ParserError
  139.   * @conditions none
  140.   */
  141. void callFunc(const std::string& func, const std::list<std::string>& funcparams);
  142.  
  143. /**
  144.   * @method read
  145.   * @brief Handles/wrappes read-command. according to the filetype the
  146.   * read-method of the corresponding handler will be called inside.
  147.   * @param funcparams function parameters as list
  148.   * @return -
  149.   * @globalvars none
  150.   * @exception ParserError
  151.   * @conditions none
  152.   *
  153.   * Scriptfile syntax: read(<FILETYPE>, <FILENAME>)
  154.   */
  155. void read(std::list<std::string> funcparams);
  156.  
  157. /**
  158.   * @method write
  159.   * @brief Handles/wrappes write-command. according to the filetype the
  160.   * write-method of the corresponding handler will be called inside.
  161.   * @param funcparams function parameters as list
  162.   * @return -
  163.   * @globalvars none
  164.   * @exception ParserError
  165.   * @conditions none
  166.   *
  167.   * Scriptfile syntax: write(<FILETYPE>, <FILENAME>)
  168.   */
  169. void write(std::list<std::string> funcparams);
  170.  
  171. private:
  172. /* members */
  173. std::set<CFile *> m_handlers;
  174. std::string m_scriptfile;
  175. std::string m_curline;
  176. CFile *m_handler;
  177. };
  178.  
  179. #endif
  180.  
  181. /* vim: set et sw=2 ts=2: */
  182.