Commits

Andreas Tscharner committed a1ee794

RCParser: Re-factor the parse method and clean up the include list

  • Participants
  • Parent commits 5e8368c

Comments (0)

Files changed (2)

src/compiler/RCParser.cxx

 //      RCParser.cxx
 //
-//      Copyright 2011 Andreas Tscharner <andy@vis.ethz.ch>
+//      Copyright 2011, 2014 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
 //      MA 02110-1301, USA.
 
 
-#include <vector>
-#include <tuple>
-#include <cstring>
-#include <cstdio>
-#include "../lrcExceptions.hxx"
-#include "../Utils.hxx"
+#include <regex>
 #include "../StatusCodes.hxx"
 #include "RCParser.hxx"
 
 	unsigned char *passwd = nullptr;
 	lrc::EncryptionType resEnc;
 	lrc::CompressionType resComp;
+	std::regex lrcID(R"rgx(^\s*([\w\-]+))rgx");
+	std::regex lrcMandatory(R"rgx(^\s*([\w\-]+)\s+"([\w\-\/\. ]+)")rgx");
+	std::regex lrcCompression(R"rgx(\s+(None|zLib|bzip2))rgx");
+	std::regex lrcEncryption(R"rgx(\s+(None|Serpent)\s+"(.*)")rgx");
+	std::cmatch mandatoryTokens;
+	std::smatch compressionToken;
+	std::smatch encryptionTokens;
 
 
 	rcFile = fopen(m_filename, "r");
 			continue;
 		};
 
-		tok = oneLine;
-		resIdent = strtok(tok, " \t");
-		if (!resIdent) {
+		if (! std::regex_search(oneLine, mandatoryTokens, lrcMandatory)) {
 			fclose(rcFile);
-
-			m_lastError = ERROR_PARSE;
-			m_internalError = ieIdentNotFound;
-			std::get<0>(m_errorPosition) = lineCount;
-			std::get<1>(m_errorPosition) = 1;
+			this->m_lastError = ERROR_PARSE;
+			std::get<0>(this->m_errorPosition) = lineCount;
+			if (std::regex_search(oneLine, lrcID)) {
+				this->m_internalError = ieFilenameNotFound;
+				std::get<1>(this->m_errorPosition) = 2;
+			} else {
+				this->m_internalError = ieIdentNotFound;
+				std::get<1>(this->m_errorPosition) = 1;
+			};
 			return ERROR_PARSE;
 		};
 
-		tok = strtok(NULL, "\"");      // Parse up to (including) starting " of filename
-		resFile = strtok(NULL, "\"");  // Parse filename without "
-		if (!resFile) {
-			fclose(rcFile);
+		// Fill in identifier and file here
 
-			m_lastError = ERROR_PARSE;
-			m_internalError = ieFilenameNotFound;
-			std::get<0>(m_errorPosition) = lineCount;
-			std::get<1>(m_errorPosition) = 2;
-			return ERROR_PARSE;
-		};
+		resComp = lrc::NoneCompression;
+		resEnc = lrc::NoneEncryption;
 
-		tok = strtok(NULL, " \t");
-		if (!tok) {
-			resComp = lrc::NoneCompression;
-			resEnc = lrc::NoneEncryption;
-		} else {
-			resComp = this->eval_compression_type(tok);
+		if (! ((std::string)mandatoryTokens.suffix()).empty()) {
+			if (std::regex_search((std::string)mandatoryTokens.suffix(), compressionToken, lrcCompression)) {
+				resComp = this->eval_compression_type(static_cast<const char *>(compressionToken[1].str().c_str()));
 
-			tok = strtok(NULL, " \t\n");
-			if (!tok) {
-				resEnc = lrc::NoneEncryption;
-			} else {
-				resEnc = this->eval_encryption_type(tok);
+				if (! ((std::string)compressionToken.suffix()).empty()) {
+					if (std::regex_search((std::string)compressionToken.suffix(), encryptionTokens, lrcEncryption)) {
+						resEnc = this->eval_encryption_type(static_cast<const char *>(encryptionTokens[1].str().c_str()));
 
-				if (resEnc != lrc::NoneEncryption) {
-					tok = strtok(NULL, "\"");
-					tok = strtok(NULL, "\"");
-					DEBUG_PRINT(("Password (un-handled): %s\n", tok))
-					try {
-						passwd = this->get_password(tok);
-					} catch (lrcFileNotFoundException const &fnfEx) {
-						m_lastError = ERROR_PARSE;
-						m_internalError = iePasswordFileNotFound;
-						std::get<0>(m_errorPosition) = lineCount;
-						std::get<1>(m_errorPosition) = 5;
-						return ERROR_PARSE;
+						if (resEnc != lrc::NoneEncryption) {
+							try {
+								passwd = this->get_password(encryptionTokens[2].str().c_str());
+							} catch (lrcFileNotFoundException const &fnfEx) {
+								fclose(rcFile);
+
+								this->m_lastError = ERROR_PARSE;
+								this->m_internalError = iePasswordFileNotFound;
+								std::get<0>(this->m_errorPosition) = lineCount;
+								std::get<1>(this->m_errorPosition) = 5;
+								return ERROR_PARSE;
+							};
+
+							if (!passwd) {
+								fclose(rcFile);
+
+								this->m_lastError = ERROR_PARSE;
+								this->m_internalError = ieMissingPassword;
+								std::get<0>(this->m_errorPosition) = lineCount;
+								std::get<1>(this->m_errorPosition) = 5;
+								return ERROR_PARSE;
+							};
+						};
 					};
-
-					if (!passwd) {
-						fclose(rcFile);
-
-						m_lastError = ERROR_PARSE;
-						m_internalError = ieMissingPassword;
-						std::get<0>(m_errorPosition) = lineCount;
-						std::get<1>(m_errorPosition) = 5;
-						return ERROR_PARSE;
-					}; // if (!passwd)
-				}; // if (resEnc != lrc::NoneEncryption)
+				};
 			};
 		};
 
 		entry = new ResourceData();
-		entry->set_ident(resIdent);
-		entry->set_file(resFile);
+		entry->set_ident(mandatoryTokens[1].str().c_str());
+		entry->set_file(mandatoryTokens[2].str().c_str());
 		entry->set_encryption(resEnc, passwd);
 		entry->set_compression(resComp);
 		entry->set_rc_position(lineCount, -1);
 		m_resEntries->push_back(entry);
 
-		if (passwd) {
-			delete[] passwd;
-			passwd = nullptr;
-		};
 	};
 	fclose(rcFile);
 

src/compiler/RCParser.hxx

 //      RCParser.hxx
 //
-//      Copyright 2011 Andreas Tscharner <andy@vis.ethz.ch>
+//      Copyright 2011, 2014 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
  * format
  *
  * \author Andreas Tscharner
- * \date 2011-09-26
+ * \date 2014-08-23
  */
 
  #ifndef __RCPARSER_HXX__
 
 
 // Includefiles
-#include <vector>
-#include "../lrcExceptions.hxx"
-#include "../ResourceData.hxx"
 #include "InFileParser.hxx"