From c3d8e03ff95f7dc5f97d052f203ed21bb4030ab4 Mon Sep 17 00:00:00 2001 From: daniel-Jones Date: Wed, 17 Oct 2018 17:37:53 +1030 Subject: added util.h/c files to store utility functions, removed config.h as I'm using cfg.h instead --- config.h | 23 ----------------------- main.c | 29 ++++++++++++++++++++++++++++- templates/template.txt | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ util.c | 39 +++++++++++++++++++++++++++++++++++++++ util.h | 25 +++++++++++++++++++++++++ 5 files changed, 140 insertions(+), 24 deletions(-) delete mode 100644 config.h create mode 100644 templates/template.txt create mode 100644 util.c create mode 100644 util.h diff --git a/config.h b/config.h deleted file mode 100644 index 19555bb..0000000 --- a/config.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * This file is part of websitegen, a website generator for https://danieljon.es - * Copyright (C) 2018 Daniel Jones daniel@danieljon.es - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/* blog */ -const char *blogindir = "content/blog/"; -const char *blogoutdir = "output/blog/"; -const int postsperpage = 10; -const char *blogtitle = "daniel's blog"; -const char *header = "

Blog

Rambles about programming, technology and life.

"; diff --git a/main.c b/main.c index f51541f..c1f1718 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ /* * This file is part of websitegen, a website generator for https://danieljon.es * Copyright (C) 2018 Daniel Jones daniel@danieljon.es + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -16,12 +17,38 @@ */ #include +#include +#include + +#include "util.h" #include "replace.h" -#include "config.h" #include "cfg.h" + int main(void) { + struct cfgfile *cfg = malloc(sizeof(struct cfgfile)); + if (!cfgsetup(cfg, "settings.cfg")) + { + perror("failed to create cfg structure"); + cfgfree(cfg); + exit(1); + } + char test[256]; + cfggetvalue(cfg, "blogtitle", test, sizeof(test)); + puts(test); + char val[3]; + cfggetvalue(cfg, "postsperpage", val, sizeof(val)); + printf("%d\n", atoi(val)*2); + cfgfree(cfg); + + char *content = readfilecontent("templates/template.txt"); + if (!content) + perror("unable to read file"); + char *replaced = replaceinmemory(content, "{CONTENT}", "test"); + puts(replaced); + free(content); + free(replaced); return 0; } diff --git a/templates/template.txt b/templates/template.txt new file mode 100644 index 0000000..0a9dd34 --- /dev/null +++ b/templates/template.txt @@ -0,0 +1,48 @@ + + + + + + + + + + +{TITLE} + + + + +
+{CONTENT} +
+

+
page generated {TIME} using sitegenerator
+ + + diff --git a/util.c b/util.c new file mode 100644 index 0000000..7615962 --- /dev/null +++ b/util.c @@ -0,0 +1,39 @@ +/* + * This file is part of websitegen, a website generator for https://danieljon.es + * Copyright (C) 2018 Daniel Jones daniel@danieljon.es + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + #include + #include + #include "util.h" + #include "replace.h" + +char * +readfilecontent(char *file) +{ + /* read file and return pointer to string containing the content */ + FILE *fp = fopen(file, "r"); + if (!fp) + return NULL; + size_t destsize = getfilelength(fp); + char *dest = malloc(destsize+1); + if (!dest) + goto skipfread; + fread(dest, destsize, 1, fp); +skipfread: + fclose(fp); + return dest; +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..767ac38 --- /dev/null +++ b/util.h @@ -0,0 +1,25 @@ +/* + * This file is part of websitegen, a website generator for https://danieljon.es + * Copyright (C) 2018 Daniel Jones daniel@danieljon.es + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef UTIL_H +#define UTIL_H + +char * +readfilecontent(char *file); + +#endif -- cgit v1.2.3