From 9424f3a1b4a4ba6b3ce87a6ce8acb1bff3c19c6d Mon Sep 17 00:00:00 2001 From: ReimarPB Date: Tue, 25 Apr 2023 21:18:08 +0200 Subject: [PATCH] Initial commit for CHP --- Makefile | 6 ++++ chp/chp.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 chp/chp.c diff --git a/Makefile b/Makefile index 9fab7e5..2cef4bd 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,12 @@ LOMMEREIMAR_OBJ = $(patsubst %.c, build/lommereimar/%.o, $(LOMMEREIMAR_SRC)) lommereimar: $(LOMMEREIMAR_OBJ) $(COMMON_OBJ) $(CC) -o bin/$@ $(CFLAGS) $^ -lm +CHP_SRC = chp.c +CHP_OBJ = $(patsubst %.c, build/chp/%.o, $(LOMMEREIMAR_SRC)) + +chp: $(CHP_OBJ) $(COMMON_OBJ) + $(CC) -o bin/$@ $(CFLAGS) $^ -lm + build/%.o: %.c $(shell find -name *.h) mkdir $(@D) -p $(CC) -c -o $@ $(CFLAGS) $< diff --git a/chp/chp.c b/chp/chp.c new file mode 100644 index 0000000..a322d83 --- /dev/null +++ b/chp/chp.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define BASE_DIR "/tmp/chp" +#define INTERMEDIATE_DIR "/tmp/chp/intermediate" +#define COMPILE_DIR "/tmp/chp/compiled" + +#define BUFFER_SIZE 25565 + +char *replace_tag(char *str, int pos) +{ + // TODO + + char *new = malloc(strlen(str) + 256); + memset(new, strlen(str) + 256, sizeof(char)); + + strncat(new, str, pos); +} + +void handle_file(char *filename) +{ + int filename_length = strlen(filename); + + if (strncmp(filename + filename_length - 4, ".chp", 4) != 0) + return; + + char *intermediate_filename = strdup(filename); + intermediate_filename[filename_length - 2] = '\0'; + + + FILE *input = fopen(filename, "r"); + FILE *output = fopen(intermediate_filename, "w"); + + char buffer[BUFFER_SIZE], newbuffer[BUFFER_SIZE]; + + while (fgets(buffer, BUFFER_SIZE, input) != NULL) { + char *str = buffer, *pos; + + // TODO + /*while ((pos = strstr(str, "d_name, ".", 1) || !strncmp(entry->d_name, "..", 2)) continue; + + int pathlength = strlen(dirname) + strlen(entry->d_name) + 2; + char *pathname = malloc(pathlength); + snprintf(pathname, pathlength, "%s/%s", dirname, entry->d_name); + + switch (entry->d_type) { + case DT_REG: + handle_file(pathname); + break; + case DT_DIR: + read_directory(pathname); + break; + } + + free(pathname); + } +} + +int main(void) +{ + puts("Compiling.."); + + mkdir(BASE_DIR, 0777); + mkdir(INTERMEDIATE_DIR, 0777); + mkdir(COMPILE_DIR, 0777); + + char current_dir[PATH_MAX]; + getcwd(current_dir, sizeof(current_dir)); + + read_directory(current_dir); + + puts("Ready"); + + return EXIT_SUCCESS; +} +