Initial commit for CHP

This commit is contained in:
ReimarPB 2023-04-25 21:18:08 +02:00
parent e9ebefd5bb
commit 9424f3a1b4
2 changed files with 104 additions and 0 deletions

View File

@ -29,6 +29,12 @@ LOMMEREIMAR_OBJ = $(patsubst %.c, build/lommereimar/%.o, $(LOMMEREIMAR_SRC))
lommereimar: $(LOMMEREIMAR_OBJ) $(COMMON_OBJ) lommereimar: $(LOMMEREIMAR_OBJ) $(COMMON_OBJ)
$(CC) -o bin/$@ $(CFLAGS) $^ -lm $(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) build/%.o: %.c $(shell find -name *.h)
mkdir $(@D) -p mkdir $(@D) -p
$(CC) -c -o $@ $(CFLAGS) $< $(CC) -c -o $@ $(CFLAGS) $<

98
chp/chp.c Normal file
View File

@ -0,0 +1,98 @@
#include <stdlib.h>
#include <stdio.h>
#include <linux/limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#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, "<?c")) != NULL)
str = replace_tag(str, pos - str);*/
fputs(newbuffer, output);
}
free(intermediate_filename);
}
void read_directory(char *dirname)
{
DIR *dir = opendir(dirname);
if (!dir) return;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (!strncmp(entry->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;
}