2024-05-19 23:13:32 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2024-07-11 17:51:51 +01:00
|
|
|
#include "string.h"
|
2024-05-19 23:13:32 +01:00
|
|
|
#include "parsers/json.h"
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
music = 0,
|
|
|
|
art = 1,
|
|
|
|
blog = 2,
|
|
|
|
} MediaType;
|
|
|
|
|
|
|
|
MediaType read_media_type(char *name) {
|
|
|
|
char *type_names[] = {"music", "art", "blog"};
|
|
|
|
for (int i = 0; i < sizeof(type_names) / sizeof(char*); i++) {
|
|
|
|
if (strcmp(name, type_names[i]) == 0) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_help() {
|
2024-07-11 17:51:51 +01:00
|
|
|
printf("usage: compile <-t media_type> <-j json_file> <-h html_file> [output_file]\n");
|
2024-05-19 23:13:32 +01:00
|
|
|
printf("valid media types: music, art, blog\n");
|
|
|
|
printf("if output_file is not specified, the output will be written to stdout.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
if (argc <= 1) {
|
|
|
|
print_help();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaType media_type = -1;
|
2024-07-11 17:51:51 +01:00
|
|
|
FILE *json_file = NULL;
|
|
|
|
FILE *html_file = NULL;
|
2024-05-19 23:13:32 +01:00
|
|
|
size_t optind = 0;
|
|
|
|
|
|
|
|
for (optind = 1; optind < argc && argv[optind][0] == '-'; optind++) {
|
|
|
|
switch (argv[optind][1]) {
|
|
|
|
case 't':
|
|
|
|
media_type = read_media_type(argv[++optind]);
|
|
|
|
if (media_type < 0) {
|
|
|
|
fprintf(stderr, "media type `%s` is not implemented!\n\n", argv[optind]);
|
|
|
|
print_help();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
2024-07-11 17:51:51 +01:00
|
|
|
case 'j':
|
2024-05-19 23:13:32 +01:00
|
|
|
if (optind++ >= argc) {
|
2024-07-11 17:51:51 +01:00
|
|
|
fprintf(stderr, "-j was passed, but no file path was given!\n\n");
|
2024-05-19 23:13:32 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2024-07-11 17:51:51 +01:00
|
|
|
json_file = fopen(argv[optind], "r");
|
|
|
|
if (json_file == NULL) {
|
|
|
|
fprintf(stderr, "failed to open file %s!\n\n", argv[optind]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
if (optind++ >= argc) {
|
|
|
|
fprintf(stderr, "-h was passed, but no file path was given!\n\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
html_file = fopen(argv[optind], "r");
|
|
|
|
if (html_file == NULL) {
|
2024-05-19 23:13:32 +01:00
|
|
|
fprintf(stderr, "failed to open file %s!\n\n", argv[optind]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
print_help();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (media_type < 0) {
|
|
|
|
fprintf(stderr, "media type not provided!\n\n");
|
|
|
|
print_help();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2024-07-11 17:51:51 +01:00
|
|
|
if (json_file == NULL) {
|
|
|
|
fprintf(stderr, "JSON file not provided!\n\n");
|
|
|
|
print_help();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (html_file == NULL) {
|
|
|
|
fprintf(stderr, "HTML file not provided!\n\n");
|
2024-05-19 23:13:32 +01:00
|
|
|
print_help();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get length of file
|
2024-07-11 17:51:51 +01:00
|
|
|
fseek(json_file, (long)-sizeof(char), SEEK_END);
|
|
|
|
size_t json_length = ftell(json_file);
|
|
|
|
fseek(json_file, 0, SEEK_SET);
|
2024-05-19 23:13:32 +01:00
|
|
|
|
|
|
|
// read file to buffer
|
2024-07-11 17:51:51 +01:00
|
|
|
char json_buf[json_length + 1];
|
|
|
|
fread(json_buf, 1, json_length, json_file);
|
|
|
|
json_buf[json_length] = '\0';
|
2024-05-19 23:13:32 +01:00
|
|
|
|
2024-07-11 14:41:47 +01:00
|
|
|
// parse the file
|
2024-05-19 23:13:32 +01:00
|
|
|
size_t offset = 0;
|
2024-07-11 17:51:51 +01:00
|
|
|
JsonObject *json = json_parse(json_buf, &offset);
|
2024-05-19 23:13:32 +01:00
|
|
|
if (json == NULL) {
|
|
|
|
fprintf(stderr, "failed to parse input file!\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2024-07-11 17:51:51 +01:00
|
|
|
char *title = (char*)((JsonObject*)json_get(json, "title"))->value;
|
|
|
|
char *type = (char*)((JsonObject*)json_get(json, "type"))->value;
|
|
|
|
float year = *(float*)((JsonObject*)json_get(json, "year"))->value;
|
|
|
|
char *artwork = (char*)((JsonObject*)json_get(json, "artwork"))->value;
|
|
|
|
char *buylink = (char*)((JsonObject*)json_get(json, "buylink"))->value;
|
|
|
|
printf("title: %s\n", title);
|
|
|
|
printf("type: %s\n", type);
|
|
|
|
printf("year: %.0f\n", year);
|
|
|
|
printf("artwork: %s\n", artwork);
|
|
|
|
printf("buylink: %s\n", buylink);
|
2024-05-19 23:13:32 +01:00
|
|
|
|
2024-07-11 14:41:47 +01:00
|
|
|
JsonObject *links = (JsonObject*)json_get(json, "links");
|
|
|
|
for (size_t i = 0; i < links->length; i++) {
|
|
|
|
char path[256];
|
|
|
|
sprintf(path, "links.%zu.title", i);
|
2024-07-11 17:51:51 +01:00
|
|
|
char *title = (char*)((JsonObject*)json_get(json, path))->value;
|
2024-07-11 14:41:47 +01:00
|
|
|
sprintf(path, "links.%zu.url", i);
|
2024-07-11 17:51:51 +01:00
|
|
|
char *url = (char*)((JsonObject*)json_get(json, path))->value;
|
|
|
|
printf("links.%zu.title: %s\n", i, title);
|
|
|
|
printf("links.%zu.url: %s\n", i, url);
|
2024-05-19 23:13:32 +01:00
|
|
|
}
|
|
|
|
|
2024-07-11 17:51:51 +01:00
|
|
|
// get length of file
|
|
|
|
fseek(html_file, (long)-sizeof(char), SEEK_END);
|
|
|
|
size_t html_length = ftell(html_file);
|
|
|
|
fseek(html_file, 0, SEEK_SET);
|
|
|
|
|
|
|
|
// read file to buffer
|
|
|
|
char html_buf[html_length + 1];
|
|
|
|
fread(html_buf, 1, html_length, html_file);
|
|
|
|
html_buf[html_length] = '\0';
|
|
|
|
|
|
|
|
char *html_out = strrep(html_buf, "{title}", title);
|
|
|
|
|
|
|
|
printf("%s\n", html_out);
|
|
|
|
|
|
|
|
json_free(json);
|
|
|
|
|
2024-05-19 23:13:32 +01:00
|
|
|
return 0;
|
|
|
|
}
|