80 lines
2.9 KiB
C++
80 lines
2.9 KiB
C++
#include "preprocessor.h"
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <fstream>
|
|
#include <filesystem>
|
|
#include <regex>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
// Funkcja pomocnicza do wczytania pliku
|
|
std::string loadFileContent(const std::string& path) {
|
|
std::ifstream in(path);
|
|
if (!in) {
|
|
std::cerr << "[PREPROCESSOR] Error: Could not open included file: " << path << "\n";
|
|
return "";
|
|
}
|
|
return std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
|
}
|
|
|
|
std::string preprocessSource(const std::string& src, const std::string& projectDir, const std::string& compilerDir) {
|
|
std::istringstream iss(src);
|
|
std::string line;
|
|
std::stringstream output;
|
|
|
|
while (std::getline(iss, line)) {
|
|
// Szukamy: #include "..." lub #include <...>
|
|
// U¿ywamy prostego find, ¿eby by³o szybko
|
|
std::string trimLine = line;
|
|
// Usuwamy bia³e znaki z pocz¹tku
|
|
size_t first = trimLine.find_first_not_of(" \t");
|
|
if (first != std::string::npos) trimLine = trimLine.substr(first);
|
|
|
|
if (trimLine.rfind("#include", 0) == 0) {
|
|
// Mamy include!
|
|
size_t openQuote = trimLine.find('"');
|
|
size_t closeQuote = trimLine.rfind('"');
|
|
size_t openAngle = trimLine.find('<');
|
|
size_t closeAngle = trimLine.rfind('>');
|
|
|
|
std::string includePath;
|
|
bool isStdLib = false;
|
|
|
|
// Wersja: #include "plik.pcc"
|
|
if (openQuote != std::string::npos && closeQuote > openQuote) {
|
|
includePath = trimLine.substr(openQuote + 1, closeQuote - openQuote - 1);
|
|
}
|
|
// Wersja: #include <plik.pcc>
|
|
else if (openAngle != std::string::npos && closeAngle > openAngle) {
|
|
includePath = trimLine.substr(openAngle + 1, closeAngle - openAngle - 1);
|
|
isStdLib = true;
|
|
}
|
|
|
|
if (!includePath.empty()) {
|
|
std::string fullPath;
|
|
if (isStdLib) {
|
|
fullPath = compilerDir + "/std/" + includePath;
|
|
std::cout << "[PREPROCESSOR] Including STD lib: " << fullPath << "\n";
|
|
}
|
|
else {
|
|
// Plik lokalny: Szukamy w folderze projektu
|
|
if (projectDir.empty()) fullPath = includePath;
|
|
else fullPath = projectDir + "/" + includePath;
|
|
std::cout << "[PREPROCESSOR] Including local file: " << fullPath << "\n";
|
|
}
|
|
std::string content = loadFileContent(fullPath);
|
|
std::string processedContent = preprocessSource(content, projectDir, compilerDir);
|
|
|
|
output << "\n// --- BEGIN INCLUDE: " << includePath << " ---\n";
|
|
output << processedContent;
|
|
output << "\n// --- END INCLUDE ---\n";
|
|
}
|
|
}
|
|
else {
|
|
output << line << "\n";
|
|
}
|
|
}
|
|
|
|
return output.str();
|
|
}
|