rewrite split and join to C99

main
Dominik Madarász 2024-08-19 10:28:32 +02:00
parent 885a2d4c8d
commit 888340b57a
6 changed files with 205 additions and 8 deletions

View File

@ -272,11 +272,11 @@ if "%1"=="prep" (
rem shortcuts for split & join amalgamation scripts rem shortcuts for split & join amalgamation scripts
if "%1"=="split" ( if "%1"=="split" (
call tools\split call tools\split.bat
exit /b exit /b
) )
if "%1"=="join" ( if "%1"=="join" (
call tools\join call tools\join.bat
exit /b exit /b
) )
if "%1"=="3rd" ( if "%1"=="3rd" (

View File

@ -1,5 +1,5 @@
#!/bin/bash 2>nul #!/bin/bash 2>nul
python tools/join.py --template engine/split/v4k.h.inl --path ./engine/split/ --output ./engine/v4k.h tools\join.exe --template engine/split/v4k.h.inl --path ./engine/split/ --output ./engine/v4k.h
python tools/join.py --template engine/split/v4k.c.inl --path ./engine/split/ --output ./engine/v4k.c tools\join.exe --template engine/split/v4k.c.inl --path ./engine/split/ --output ./engine/v4k.c
python tools/join.py --template engine/split/v4k.x.inl --path ./engine/split/ --output ./engine/v4k tools\join.exe --template engine/split/v4k.x.inl --path ./engine/split/ --output ./engine/v4k

95
tools/join.c 100644
View File

@ -0,0 +1,95 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MAX_PATH 1024
#define MAX_LINE 4096
void process_template(const char* template_path, const char* base_path, const char* output_path) {
FILE *template_file, *output_file;
char line[MAX_LINE];
char file_path[MAX_PATH];
template_file = fopen(template_path, "r");
if (!template_file) {
fprintf(stderr, "Error opening template file: %s\n", template_path);
exit(1);
}
output_file = fopen(output_path, "w");
if (!output_file) {
fprintf(stderr, "Error opening output file: %s\n", output_path);
fclose(template_file);
exit(1);
}
while (fgets(line, sizeof(line), template_file)) {
if (strncmp(line, "{{FILE:", 7) == 0) {
char* end = strchr(line + 7, '}');
if (end) {
*end = '\0';
snprintf(file_path, sizeof(file_path), "%s%s", base_path, line + 7);
printf("Appending file: %s\n", line + 7);
FILE* include_file = fopen(file_path, "r");
if (include_file) {
fprintf(output_file, "#line 1 \"%s\"\n", line + 7);
char include_line[MAX_LINE];
while (fgets(include_line, sizeof(include_line), include_file)) {
fputs(include_line, output_file);
}
fprintf(output_file, "#line 0\n");
fclose(include_file);
} else {
fprintf(stderr, "Warning: Unable to open included file: %s\n", file_path);
fputs(line, output_file);
}
} else {
fputs(line, output_file);
}
} else {
fputs(line, output_file);
}
}
fclose(template_file);
fclose(output_file);
}
int main(int argc, char* argv[]) {
char template_path[MAX_PATH] = "";
char base_path[MAX_PATH] = "";
char output_path[MAX_PATH] = "";
for (int i = 1; i < argc; i += 2) {
if (i + 1 < argc) {
if (strcmp(argv[i], "--template") == 0) {
strncpy(template_path, argv[i + 1], sizeof(template_path) - 1);
} else if (strcmp(argv[i], "--path") == 0) {
strncpy(base_path, argv[i + 1], sizeof(base_path) - 1);
} else if (strcmp(argv[i], "--output") == 0) {
strncpy(output_path, argv[i + 1], sizeof(output_path) - 1);
}
}
}
if (template_path[0] == '\0' || base_path[0] == '\0' || output_path[0] == '\0') {
fprintf(stderr, "Usage: %s --template <template_file> --path <base_path> --output <output_file>\n", argv[0]);
return 1;
}
process_template(template_path, base_path, output_path);
printf("OK\n");
return 0;
}
/*
compiled with:
cc -ObjC join.c -I../engine -o join.osx -framework Cocoa -framework IOKit -framework audiotoolbox -framework coreaudio -O3
cc join.c -I../engine -o join.linux -lm -lpthread -ldl -lX11 -O3
tcc join.c -I..\engine
cl join.c -I..\engine /openmp /Os /Ox /O2 /Oy /MT /DNDEBUG /GL /GF /Gw /arch:AVX2 /link /OPT:ICF /LTCG
del *.o & del *.obj & del *.lib & del *.exp & del *.pdb
*/

BIN
tools/join.exe 100644

Binary file not shown.

View File

@ -1,5 +1,5 @@
#!/bin/bash 2>nul #!/bin/bash 2>nul
python tools/split.py --input ./engine/v4k.h --output-path ./engine/split/ tools/split.exe --input ./engine/v4k.h --output-path ./engine/split/
python tools/split.py --input ./engine/v4k.c --output-path ./engine/split/ tools/split.exe --input ./engine/v4k.c --output-path ./engine/split/
python tools/split.py --input ./engine/v4k --output-path ./engine/split/ tools/split.exe --input ./engine/v4k --output-path ./engine/split/

102
tools/split.c 100644
View File

@ -0,0 +1,102 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MAX_FILENAME_LENGTH 256
#define MAX_LINE_LENGTH 1024
void print_usage(const char* program_name) {
fprintf(stderr, "Usage: %s --input <input_file> --output-path <output_path>\n", program_name);
}
int main(int argc, char* argv[]) {
char* input_file = NULL;
char* output_path = NULL;
// Parse command-line arguments
for (int i = 1; i < argc; i += 2) {
if (i + 1 >= argc) {
fprintf(stderr, "Error: Missing argument value\n");
print_usage(argv[0]);
return 1;
}
if (strcmp(argv[i], "--input") == 0) {
input_file = argv[i + 1];
} else if (strcmp(argv[i], "--output-path") == 0) {
output_path = argv[i + 1];
} else {
fprintf(stderr, "Error: Unknown argument %s\n", argv[i]);
print_usage(argv[0]);
return 1;
}
}
if (!input_file || !output_path) {
fprintf(stderr, "Error: Missing required arguments\n");
print_usage(argv[0]);
return 1;
}
FILE* input = fopen(input_file, "r");
if (!input) {
fprintf(stderr, "Error: Unable to open input file %s\n", input_file);
return 1;
}
char line[MAX_LINE_LENGTH];
char current_filename[MAX_FILENAME_LENGTH] = "";
FILE* current_output = NULL;
while (fgets(line, sizeof(line), input)) {
if (strncmp(line, "#line 1 ", 8) == 0) {
// Close previous output file if open
if (current_output) {
fclose(current_output);
current_output = NULL;
}
// Extract new filename
sscanf(line, "#line 1 \"%[^\"]\"", current_filename);
// Open new output file
char output_filename[MAX_FILENAME_LENGTH * 2];
snprintf(output_filename, sizeof(output_filename), "%s/%s", output_path, current_filename);
current_output = fopen(output_filename, "w");
if (!current_output) {
fprintf(stderr, "Error: Unable to open output file %s\n", output_filename);
fclose(input);
return 1;
}
printf("Writing file: %s\n", current_filename);
} else if (strncmp(line, "#line 0", 7) == 0) {
// Close current output file
if (current_output) {
fclose(current_output);
current_output = NULL;
}
} else if (current_output) {
// Write line to current output file
fputs(line, current_output);
}
}
// Close any open files
if (current_output) {
fclose(current_output);
}
fclose(input);
printf("OK\n");
return 0;
}
/*
compiled with:
cc -ObjC join.c -I../engine -o join.osx -framework Cocoa -framework IOKit -framework audiotoolbox -framework coreaudio -O3
cc join.c -I../engine -o join.linux -lm -lpthread -ldl -lX11 -O3
tcc join.c -I..\engine
cl join.c -I..\engine /openmp /Os /Ox /O2 /Oy /MT /DNDEBUG /GL /GF /Gw /arch:AVX2 /link /OPT:ICF /LTCG
del *.o & del *.obj & del *.lib & del *.exp & del *.pdb
*/