Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "parameter_processing.h"
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <vector>
- #include <sstream>
- #include <unordered_map>
- std::vector<std::string> processQuestFile(const std::string& filePath) {
- std::vector<std::string> processedLines;
- std::unordered_map<std::string, std::string> defineTable;
- bool inMultilineComment = false;
- try {
- std::ifstream inputFile(filePath);
- if (!inputFile.is_open()) {
- throw std::runtime_error("Fehler beim Öffnen der Datei: " + filePath);
- }
- std::string line;
- while (std::getline(inputFile, line)) {
- // Entferne Kommentarblöcke oder auskommentierte Quests
- if (inMultilineComment) {
- if (line.find("]]--") != std::string::npos || line.find("--]]") != std::string::npos) {
- inMultilineComment = false; // Ende des Kommentarblocks
- }
- continue; // Überspringe die gesamte Zeile
- }
- // Prüfen, ob die Zeile einen mehrzeiligen Kommentarblock oder auskommentierte Quests beginnt
- if (line.find("--[[") != std::string::npos) {
- inMultilineComment = true;
- continue; // Überspringe die gesamte Zeile
- }
- // Entferne einzeilige Kommentare
- std::string strippedLine = line.substr(0, line.find("--"));
- // Verarbeite Define-Anweisungen
- if (strippedLine.find("define") == 0) {
- std::string define, key, value;
- size_t equalPos = strippedLine.find('=');
- if (equalPos != std::string::npos) {
- // Fall: "define KEY = VALUE"
- key = strippedLine.substr(7, equalPos - 7); // Extrahiere KEY
- value = strippedLine.substr(equalPos + 1); // Extrahiere VALUE
- } else {
- // Fall: "define KEY VALUE" (ohne "=")
- std::istringstream iss(strippedLine);
- iss >> define >> key >> value;
- }
- // Entferne unnötige Leerzeichen
- key = key.substr(0, key.find_last_not_of(" \t") + 1);
- value = value.substr(value.find_first_not_of(" \t"));
- // Entferne Anführungszeichen von VALUE (falls nötig)
- if (!value.empty() && value.front() == '"' && value.back() == '"') {
- value = value.substr(1, value.length() - 2);
- }
- // Speichere KEY und VALUE in der Tabelle
- if (!key.empty() && !value.empty()) {
- defineTable[key] = value;
- }
- continue; // Überspringe die Zeile mit der Define-Anweisung
- }
- // Sortiere die Define-Tabelle nach Schlüssellänge (längere Schlüssel zuerst)
- std::vector<std::pair<std::string, std::string>> sortedDefines(defineTable.begin(), defineTable.end());
- std::sort(sortedDefines.begin(), sortedDefines.end(), [](const auto& a, const auto& b) {
- return a.first.length() > b.first.length();
- });
- // Ersetze Tokens mit Werten aus der Define-Tabelle
- for (const auto& [key, value] : sortedDefines) {
- size_t pos = 0;
- while ((pos = strippedLine.find(key, pos)) != std::string::npos) {
- // Stelle sicher, dass wir uns nicht in einem Lua-Schlüssel oder einer Punkt-Notation befinden
- size_t dotPos = strippedLine.rfind('.', pos);
- size_t bracketPos = strippedLine.rfind("[\"", pos);
- if ((dotPos != std::string::npos && dotPos + 1 == pos) || // Prüfe auf Punkt-Notation
- (bracketPos != std::string::npos && pos > bracketPos && // Prüfe auf Schlüssel in ["..."]
- strippedLine.find("\"]", bracketPos) > pos)) {
- pos += key.length(); // Überspringe diesen Schlüssel
- continue;
- }
- // Prüfen, ob der Token vollständig übereinstimmt
- if ((pos == 0 || !isalnum(strippedLine[pos - 1])) && // Kein Buchstabe/Zahl davor
- (pos + key.length() == strippedLine.length() || // Ende der Zeile
- !isalnum(strippedLine[pos + key.length()]))) { // Kein Buchstabe/Zahl danach
- strippedLine.replace(pos, key.length(), value);
- pos += value.length();
- } else {
- pos += key.length();
- }
- }
- }
- // Nur nicht-leere Zeilen hinzufügen
- if (!strippedLine.empty()) {
- processedLines.push_back(strippedLine);
- }
- }
- inputFile.close();
- } catch (const std::exception& e) {
- std::cerr << "Fehler beim Verarbeiten der Datei: " << filePath << " - " << e.what() << std::endl;
- throw; // Fehler weiterwerfen, um die Kompilierung zu stoppen
- }
- return processedLines;
- }
Advertisement
Add Comment
Please, Sign In to add comment