This commit is contained in:
cozis
2022-05-05 14:35:47 +02:00
parent 6337a7f7ca
commit cd8f9fd6a7
7 changed files with 578 additions and 205 deletions
+62 -14
View File
@@ -1,25 +1,73 @@
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "c2html.h"
static char *load_file(const char *file, long *size)
{
FILE *fp = fopen(file, "rb");
if(fp == NULL)
return NULL;
fseek(fp, 0, SEEK_END);
long size2 = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *data = malloc(size2+1);
if(data == NULL) {
fclose(fp);
return NULL;
}
fread(data, 1, size2, fp);
fclose(fp);
if(size)
*size = size2;
data[size2] = '\0';
return data;
}
int main(int argc, char **argv)
{
assert(argc > 0);
if(argc < 3) {
fprintf(stderr,
"Missing input ad output files\n"
"\n"
"Usage:\n"
" $ %s input.c output.html\n"
"\n", argv[0]);
fprintf(stderr, "ERROR: Missing input and output file\n");
return -1;
}
char *src_file = argv[1];
char *dst_file = argv[2];
long src_size;
char *src_data = load_file(src_file, &src_size);
if(src_data == NULL) {
fprintf(stderr, "ERROR: Failed to open \"%s\"\n", src_file);
return -1;
}
const char *err;
_Bool table_mode = 0;
const char *class_prefix = "c2h-";
err = c2html(argv[1], argv[2]);
if(err != NULL)
fprintf(stderr, "ERROR: %s\n", err);
fprintf(stderr, "OK");
return err == NULL ? -1 : 0;
const char *error;
char *dst_data = c2html(src_data, src_size, table_mode, class_prefix, &error);
if(dst_data == NULL)
fprintf(stderr, "ERROR: %s\n", error);
else {
FILE *fp = fopen(dst_file, "wb");
if(fp == NULL)
fprintf(stderr, "ERROR: Failed to open \"%s\"\n", dst_file);
else {
fwrite(dst_data, 1, strlen(dst_data), fp);
fclose(fp);
fprintf(stderr, "OK\n");
}
free(dst_data);
}
free(src_data);
return 0;
}