first commit
This commit is contained in:
commit
8a543a4a5a
13 changed files with 9621 additions and 0 deletions
42
merger/Makefile
Normal file
42
merger/Makefile
Normal file
|
@ -0,0 +1,42 @@
|
|||
|
||||
|
||||
COMPILER = g++
|
||||
|
||||
NAME = codingame-ide-sync
|
||||
|
||||
FLAGS = -std=c++20 -Wall -Werror -Wextra
|
||||
|
||||
#ex: INCLUDES = -I libs/FTXUI/include -I libs/json-parser/include
|
||||
INCLUDES=
|
||||
|
||||
SOURCES = \
|
||||
main \
|
||||
File \
|
||||
merge \
|
||||
|
||||
SRC_WITHOUT_SUFFIX = $(addprefix src/, $(SOURCES))
|
||||
SRC = $(addsuffix .cpp, $(SRC_WITHOUT_SUFFIX))
|
||||
OBJ = $(SRC:src/%.cpp=obj/%.o)
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): dir $(OBJ)
|
||||
${COMPILER} $(FLAGS) $(OBJ) -o $(NAME)
|
||||
|
||||
dir:
|
||||
if [ ! -d "obj" ]; then mkdir obj; fi
|
||||
|
||||
obj/%.o: src/%.cpp src/*.h
|
||||
${COMPILER} $(FLAGS) $(INCLUDES) -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
|
||||
run: all
|
||||
./${NAME}
|
BIN
merger/codingame-ide-sync
Executable file
BIN
merger/codingame-ide-sync
Executable file
Binary file not shown.
48
merger/src/File.cpp
Normal file
48
merger/src/File.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include "File.h"
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
File::File(std::string filePath) : filePath(filePath) {
|
||||
content = "";
|
||||
std::ifstream file(filePath);
|
||||
if (!file.is_open()) {
|
||||
throw new std::runtime_error("Can't open file " + filePath);
|
||||
}
|
||||
std::string line;
|
||||
while(std::getline(file, line)) {
|
||||
content += line + '\n';
|
||||
lines.push_back(line);
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
std::string File::getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
std::string File::getName() const {
|
||||
auto lastSlashIndex = filePath.rfind('/');
|
||||
if (lastSlashIndex == std::string::npos) {
|
||||
return filePath;
|
||||
}
|
||||
return filePath.substr(lastSlashIndex + 1);
|
||||
}
|
||||
|
||||
const std::vector<std::string>& File::getLines() const {
|
||||
return lines;
|
||||
}
|
||||
|
||||
std::string File::getDirectoryPath() const {
|
||||
auto lastSlashIndex = filePath.rfind('/');
|
||||
if (lastSlashIndex == std::string::npos) {
|
||||
return ".";
|
||||
}
|
||||
return filePath.substr(0, lastSlashIndex);
|
||||
}
|
28
merger/src/File.h
Normal file
28
merger/src/File.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#pragma once
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
class File {
|
||||
|
||||
public:
|
||||
|
||||
File(std::string filePath);
|
||||
|
||||
std::string getName() const;
|
||||
std::string getContent();
|
||||
const std::vector<std::string>& getLines() const;
|
||||
std::string getDirectoryPath() const;
|
||||
|
||||
private:
|
||||
|
||||
bool contentAlreadyRead = false;
|
||||
std::string filePath;
|
||||
std::string content;
|
||||
std::vector<std::string> lines;
|
||||
};
|
5
merger/src/codingame-sync.h
Normal file
5
merger/src/codingame-sync.h
Normal file
|
@ -0,0 +1,5 @@
|
|||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
std::string merge(const std::string& entryFilePath);
|
||||
std::optional<std::string> extractFilePathFromInclude(const std::string& line);
|
9286
merger/src/httplib.h
Normal file
9286
merger/src/httplib.h
Normal file
File diff suppressed because it is too large
Load diff
31
merger/src/main.cpp
Normal file
31
merger/src/main.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
#include "codingame-sync.h"
|
||||
#include "httplib.h"
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc != 2) {
|
||||
std::cout << "You must provide the entry file as first argument" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Merger starter successfully" << std::endl;
|
||||
|
||||
httplib::Server server;
|
||||
|
||||
server.Get("/code", [&](const httplib::Request &, httplib::Response &res) {
|
||||
try {
|
||||
auto mergeResult = merge(argv[1]);
|
||||
std::cout << "Code merged successfully" << std::endl;
|
||||
res.set_content(mergeResult, "text/plain");
|
||||
} catch (std::exception* e) {
|
||||
std::cout << "Exception : " << e->what() << std::endl;
|
||||
res.status = 500;
|
||||
}
|
||||
});
|
||||
|
||||
server.listen("0.0.0.0", 8080);
|
||||
return 0;
|
||||
}
|
50
merger/src/merge.cpp
Normal file
50
merger/src/merge.cpp
Normal file
|
@ -0,0 +1,50 @@
|
|||
#include "codingame-sync.h"
|
||||
#include "File.h"
|
||||
#include <regex>
|
||||
|
||||
void copyFileTo(std::string* to, const File& file, std::map<std::string, bool>* includedFiles) {
|
||||
(*to) += "/** cg-sync -> " + file.getName() + " **/\n\n";
|
||||
for (auto& line : file.getLines()) {
|
||||
auto includeFilePath = extractFilePathFromInclude(line);
|
||||
if (includeFilePath.has_value()) {
|
||||
if ((*includedFiles).find(includeFilePath.value()) != (*includedFiles).end())
|
||||
continue;
|
||||
(*includedFiles)[includeFilePath.value()] = true;
|
||||
std::string relativeIncludedFilePath = file.getDirectoryPath();
|
||||
File includedFile(relativeIncludedFilePath + "/" + includeFilePath.value());
|
||||
copyFileTo(to, includedFile, includedFiles);
|
||||
} else {
|
||||
(*to) += line + "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string merge(const std::string& entryFilePath) {
|
||||
std::string mergedFileContent = "";
|
||||
std::map<std::string, bool> includedFiles;
|
||||
|
||||
std::string currentFilePath = entryFilePath;
|
||||
File file(currentFilePath);
|
||||
std::string currentFileContent = file.getContent();
|
||||
|
||||
copyFileTo(&mergedFileContent, file, &includedFiles);
|
||||
|
||||
std::ofstream newFile("merge.cpp");
|
||||
if (!newFile.is_open()) {
|
||||
newFile << mergedFileContent;
|
||||
}
|
||||
newFile.close();
|
||||
return mergedFileContent;
|
||||
}
|
||||
|
||||
// Returns a string_view from the line provided as argument
|
||||
std::optional<std::string> extractFilePathFromInclude(const std::string& line) {
|
||||
std::smatch res;
|
||||
std::regex re("#include \"(.+)\"");
|
||||
std::regex_match(line, res, re);
|
||||
|
||||
if (res.size() == 2) {
|
||||
return std::make_optional(res.str(1));
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue