diff options
| author | manuel <manuel@nc8430.lan> | 2009-04-27 00:25:16 +0200 |
|---|---|---|
| committer | manuel <manuel@nc8430.lan> | 2009-04-27 00:25:16 +0200 |
| commit | aa139a7d2b3f26af7590edbf413df67195c5d900 (patch) | |
| tree | ba99ea3b2af9aa191386550f025520117f18f4f8 /ue2/imgsynth2/cscriptparser.cpp | |
| parent | 384539f7cc9feaa7ef7cee385cce472c6966c843 (diff) | |
| download | ooprog-aa139a7d2b3f26af7590edbf413df67195c5d900.tar.gz ooprog-aa139a7d2b3f26af7590edbf413df67195c5d900.tar.bz2 ooprog-aa139a7d2b3f26af7590edbf413df67195c5d900.zip | |
Adding ue2
Diffstat (limited to 'ue2/imgsynth2/cscriptparser.cpp')
| -rw-r--r-- | ue2/imgsynth2/cscriptparser.cpp | 209 |
1 files changed, 209 insertions, 0 deletions
diff --git a/ue2/imgsynth2/cscriptparser.cpp b/ue2/imgsynth2/cscriptparser.cpp new file mode 100644 index 0000000..df3df1e --- /dev/null +++ b/ue2/imgsynth2/cscriptparser.cpp | |||
| @@ -0,0 +1,209 @@ | |||
| 1 | /** | ||
| 2 | * @module cscriptparser | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief class for parsing simple scriptfiles | ||
| 5 | * @date 17.04.2009 | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <fstream> | ||
| 9 | #include <boost/tokenizer.hpp> | ||
| 10 | #include <boost/algorithm/string.hpp> | ||
| 11 | #include "cscriptparser.h" | ||
| 12 | #include "cbitmap.h" | ||
| 13 | |||
| 14 | using namespace std; | ||
| 15 | using namespace boost; | ||
| 16 | |||
| 17 | CScriptparser::CScriptparser(const std::string& scriptfile) | ||
| 18 | : m_scriptfile(scriptfile), m_handler(NULL) | ||
| 19 | { | ||
| 20 | /* add our handlers */ | ||
| 21 | m_handlers.insert(new CBitmap); | ||
| 22 | } | ||
| 23 | |||
| 24 | /*----------------------------------------------------------------------------*/ | ||
| 25 | |||
| 26 | CScriptparser::~CScriptparser() | ||
| 27 | { | ||
| 28 | /* delete image handlers */ | ||
| 29 | set<CFile *>::iterator it; | ||
| 30 | for (it = m_handlers.begin(); it != m_handlers.end(); it++) | ||
| 31 | delete *it; | ||
| 32 | m_handler = NULL; | ||
| 33 | } | ||
| 34 | |||
| 35 | /*----------------------------------------------------------------------------*/ | ||
| 36 | |||
| 37 | void CScriptparser::parse() | ||
| 38 | { | ||
| 39 | /* open and read file */ | ||
| 40 | ifstream file(m_scriptfile.c_str(), ios::in); | ||
| 41 | if (!file) | ||
| 42 | throw ParserError("Unable to open scriptfile '" + m_scriptfile + "'."); | ||
| 43 | |||
| 44 | while (!file.eof() && file.good()) | ||
| 45 | { | ||
| 46 | /* read file pre line */ | ||
| 47 | getline(file, m_curline); | ||
| 48 | if (m_curline.empty()) | ||
| 49 | continue; | ||
| 50 | |||
| 51 | trim(m_curline); | ||
| 52 | |||
| 53 | /* ignore comments */ | ||
| 54 | if (m_curline.find_first_of('#') == 0) | ||
| 55 | continue; | ||
| 56 | |||
| 57 | /* line has no function call */ | ||
| 58 | size_t pos1 = m_curline.find_first_of('('); | ||
| 59 | size_t pos2 = m_curline.find_last_of(')'); | ||
| 60 | if (pos1 == string::npos || pos2 == string::npos) | ||
| 61 | throw ParserError("Invalid syntax. Not a function", m_curline); | ||
| 62 | |||
| 63 | /* first parse function name and tokenize all parameters */ | ||
| 64 | string func = m_curline.substr(0, pos1); | ||
| 65 | string params = m_curline.substr(pos1 + 1, pos2 - pos1 - 1); | ||
| 66 | list<string> funcparams; | ||
| 67 | tokenizer< char_separator<char> > tokens(params, char_separator<char>(",")); | ||
| 68 | /* BOOST_FOREACH isn't available on OOP-servers... */ | ||
| 69 | for (tokenizer< char_separator<char> >::iterator it = tokens.begin(); | ||
| 70 | it != tokens.end(); | ||
| 71 | it++) | ||
| 72 | { | ||
| 73 | string tok(*it); | ||
| 74 | trim(tok); | ||
| 75 | if (tok.find_first_of(' ') != string::npos) | ||
| 76 | { | ||
| 77 | if (tok.find_first_of('"') == string::npos) | ||
| 78 | throw ParserError("Invalid syntax", m_curline); | ||
| 79 | } | ||
| 80 | trim_if(tok, is_any_of("\"")); | ||
| 81 | funcparams.push_back(tok); | ||
| 82 | } | ||
| 83 | |||
| 84 | /* then call the corresponding function */ | ||
| 85 | callFunc(func, funcparams); | ||
| 86 | } | ||
| 87 | |||
| 88 | file.close(); | ||
| 89 | } | ||
| 90 | |||
| 91 | /*----------------------------------------------------------------------------*/ | ||
| 92 | |||
| 93 | void CScriptparser::callFunc(const std::string& func, const std::list<std::string>& funcparams) | ||
| 94 | { | ||
| 95 | if (func.empty()) | ||
| 96 | throw ParserError("Function name is empty.", m_curline); | ||
| 97 | |||
| 98 | if (func == "read") | ||
| 99 | read(funcparams); | ||
| 100 | else if (func == "write") | ||
| 101 | write(funcparams); | ||
| 102 | else | ||
| 103 | { | ||
| 104 | if (m_handler == NULL) | ||
| 105 | throw ParserError("No image is being processed.", m_curline); | ||
| 106 | |||
| 107 | /* call function from handler */ | ||
| 108 | try | ||
| 109 | { | ||
| 110 | m_handler->callFunc(func, funcparams); | ||
| 111 | } | ||
| 112 | catch(CFile::FileError& ex) | ||
| 113 | { | ||
| 114 | throw ParserError(ex.what(), m_curline); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | /*----------------------------------------------------------------------------*/ | ||
| 120 | |||
| 121 | void CScriptparser::read(std::list<std::string> funcparams) | ||
| 122 | { | ||
| 123 | /* check prerequirements */ | ||
| 124 | if (funcparams.size() != 2) | ||
| 125 | throw ParserError("Invalid number of function parameters (must be 2).", m_curline); | ||
| 126 | if (m_handler != NULL) | ||
| 127 | throw ParserError("An image is already being processed. Unable to open another.", m_curline); | ||
| 128 | |||
| 129 | string type = funcparams.front(); | ||
| 130 | to_upper(type); | ||
| 131 | funcparams.pop_front(); | ||
| 132 | string filename = funcparams.front(); | ||
| 133 | |||
| 134 | /* fetch image handler supporting requested filetype */ | ||
| 135 | m_handler = NULL; | ||
| 136 | set<CFile *>::iterator it; | ||
| 137 | for (it = m_handlers.begin(); it != m_handlers.end(); it++) | ||
| 138 | { | ||
| 139 | if ((*it)->supportsType(type)) | ||
| 140 | { | ||
| 141 | m_handler = *it; | ||
| 142 | break; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | if (m_handler == NULL) | ||
| 146 | throw ParserError("Unknown filetype.", m_curline); | ||
| 147 | |||
| 148 | /* open file in binary mode */ | ||
| 149 | ifstream file(filename.c_str(), ios::in | ios::binary); | ||
| 150 | if (!file) | ||
| 151 | throw ParserError("Unable to read file.", m_curline); | ||
| 152 | |||
| 153 | /* let handlers read() parse the file */ | ||
| 154 | try | ||
| 155 | { | ||
| 156 | m_handler->read(file); | ||
| 157 | if (!file.good()) | ||
| 158 | throw ParserError("Error while reading image file.", m_curline); | ||
| 159 | file.close(); | ||
| 160 | } | ||
| 161 | catch(CFile::FileError& ex) | ||
| 162 | { | ||
| 163 | file.close(); | ||
| 164 | throw ParserError(ex.what(), m_curline); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | /*----------------------------------------------------------------------------*/ | ||
| 169 | |||
| 170 | void CScriptparser::write(std::list<std::string> funcparams) | ||
| 171 | { | ||
| 172 | /* check prerequirements */ | ||
| 173 | if (funcparams.size() != 2) | ||
| 174 | throw ParserError("Invalid number of function parameters (must be 2).", m_curline); | ||
| 175 | if (m_handler == NULL) | ||
| 176 | throw ParserError("No image is being processed.", m_curline); | ||
| 177 | |||
| 178 | string type = funcparams.front(); | ||
| 179 | to_upper(type); | ||
| 180 | funcparams.pop_front(); | ||
| 181 | string filename = funcparams.front(); | ||
| 182 | |||
| 183 | /* do we have an image handler supporting the filetype? */ | ||
| 184 | if (!m_handler->supportsType(type)) | ||
| 185 | throw ParserError("Unknown filetype.", m_curline); | ||
| 186 | |||
| 187 | /* open file in binary mode */ | ||
| 188 | ofstream file(filename.c_str(), ios::out | ios::binary); | ||
| 189 | if (!file) | ||
| 190 | throw ParserError("Unable to open file.", m_curline); | ||
| 191 | |||
| 192 | /* let handlers write() parse the file */ | ||
| 193 | try | ||
| 194 | { | ||
| 195 | m_handler->write(file); | ||
| 196 | if (!file.good()) | ||
| 197 | throw ParserError("Error while writing image file.", m_curline); | ||
| 198 | file.close(); | ||
| 199 | m_handler = NULL; | ||
| 200 | } | ||
| 201 | catch(CFile::FileError& ex) | ||
| 202 | { | ||
| 203 | file.close(); | ||
| 204 | m_handler = NULL; | ||
| 205 | throw ParserError(ex.what(), m_curline); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | /* vim: set et sw=2 ts=2: */ | ||
