From 57aa67d435a11f3ffd0ac0d3472ca9288115a8d0 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Mon, 27 Mar 2023 05:31:45 +0200 Subject: Header-only version; replace Python code with C code --- gen_inl.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 gen_inl.c (limited to 'gen_inl.c') diff --git a/gen_inl.c b/gen_inl.c new file mode 100644 index 0000000..dab36dc --- /dev/null +++ b/gen_inl.c @@ -0,0 +1,134 @@ +#if 0 +gcc gen_inl.c -o gen_inl && ./gen_inl +exit +#endif + +#include +#include +#include +#include + +char const *const SOURCES[] = { + "./source/kit/status.h", "./source/kit/allocator.h", + "./source/kit/array_ref.h", "./source/kit/dynamic_array.h", + "./source/kit/string_ref.h", "./source/kit/file.h", + "./source/kit/allocator.c", "./source/kit/array_ref.c", + "./source/kit/dynamic_array.c", "./source/kit/file.c" +}; + +char const *repeat(int n, char c) { + static char buf[200]; + for (int i = 0; i < n && i < 199; i++) buf[i] = c; + buf[n] = '\0'; + return buf; +} + +int skip_whitespaces(char const *const line, int i) { + while (line[i] == ' ') i++; + return i; +} + +int skip(char const *const line, int i, char const *const s) { + int j = 0; + while (s[j] != '\0') { + if (line[i] != s[j]) + return -1; + i++; + j++; + } + return i; +} + +int is_local_include(char const *const line) { + if (line[0] != '#') + return 0; + int i = skip_whitespaces(line, 1); + i = skip(line, i, "include"); + if (i == -1) + return 0; + i = skip_whitespaces(line, i); + if (line[i] != '"') + return 0; + return 1; +} + +int is_empty_line(char const *const line) { + int i = skip_whitespaces(line, 0); + return line[i] == '\0' || line[i] == '\n'; +} + +int write_file(FILE *out, char const *const source) { + assert(out != NULL); + assert(source != NULL); + + FILE *in = fopen(source, "rt"); + + if (in == NULL) { + fprintf(stderr, "File not found: %s\n", source); + return 1; + } + + fprintf(out, "/*%s*\n", repeat(70, '*')); + fprintf(out, " *%s*\n", repeat(70, ' ')); + fprintf(out, " *%s*\n", repeat(70, ' ')); + fprintf(out, " * FILE: %s%s*\n", source, + repeat(58 - strlen(source), ' ')); + fprintf(out, " *%s*\n", repeat(70, ' ')); + fprintf(out, " *%s*\n", repeat(70, ' ')); + fprintf(out, " *%s*/\n", repeat(70, '*')); + + char line[200]; + char buf[400]; + int prev_empty = 0; + + while (fgets(line, 199, in) != NULL) { + int empty = is_empty_line(line) || is_local_include(line); + + if (empty) { + if (!prev_empty) + fprintf(out, "\n"); + } else { + int j = 0; + for (int i = 0; line[i] != '\0'; i++) { + if (line[i] == '\t') { + buf[j++] = ' '; + buf[j++] = ' '; + } else if (line[i] != '\n' && line[i] != '\r') + buf[j++] = line[i]; + } + buf[j] = '\0'; + fprintf(out, "%s\n", buf); + } + + prev_empty = empty; + } + + fclose(in); + fprintf(out, "\n"); + return 0; +} + +int main(int argc, char **argv) { + char const *const out_file = "./include/kit.inl.h"; + FILE *out = fopen(out_file, "wt"); + if (out == NULL) { + fprintf(stderr, "Can't write: %s\n", out_file); + return 1; + } + + fprintf(out, "#ifndef KIT_INL_H\n"); + fprintf(out, "#define KIT_INL_H\n"); + fprintf(out, "\n"); + + for (int i = 0; i < sizeof SOURCES / sizeof *SOURCES; i++) + if (write_file(out, SOURCES[i]) != 0) { + fclose(out); + return 1; + } + + fprintf(out, "\n"); + fprintf(out, "#endif\n"); + + fclose(out); + return 0; +} -- cgit v1.2.3