summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Jones <admin@danieljon.es>2019-12-27 02:59:16 +1030
committerDaniel Jones <admin@danieljon.es>2019-12-27 02:59:16 +1030
commit61cdc43efe23719c9713ee3ae87818d7b9f2699d (patch)
treec065af81070f356981d3810dec65afe2a44711c5
parentd5d0883ec84653c13c01d35824c95fa3dd682348 (diff)
downloadwebsitegenerator-61cdc43efe23719c9713ee3ae87818d7b9f2699d.tar.gz
websitegenerator-61cdc43efe23719c9713ee3ae87818d7b9f2699d.zip
moved some code around and finished basic page generation
it is far too hacky though, createtmpfile() is an ugly hack that cannot stay. the problem is replaceinpage() and the functions it calls expects a file name that it opens, i cannot just pass it a string. i need to figure out a better solution
-rw-r--r--config.h1
-rw-r--r--pages.c69
-rw-r--r--pages.h3
3 files changed, 59 insertions, 14 deletions
diff --git a/config.h b/config.h
index 5e47583..d28ce62 100644
--- a/config.h
+++ b/config.h
@@ -37,6 +37,7 @@ static const char *info_string = "{INFO}";
static const char *time_string = "{TIME}";
static const char *template_file = "template.txt";
static const char *frontpage_title = "Index";
+static const char *frontpage_info = "<h1>Index</h1>";
static const char *frontpage_index = "content/index.txt";
static const char *frontpage_index_output = "index.html";
diff --git a/pages.c b/pages.c
index c4172ea..9ce7e76 100644
--- a/pages.c
+++ b/pages.c
@@ -46,7 +46,6 @@ createfile(const char *file)
FILE *out = fopen(filename, "w");
if (!out)
{
-
fprintf(stderr, "unable to open file '%s', unrecoverable failure\n", filename);
return 0;
}
@@ -190,7 +189,6 @@ deletebytes(const char *file, long offset, size_t bytes)
if (rename("tmp.tmp", filename) == -1)
{
-
fprintf(stderr, "unable to rename tmp file to '%s', unrecoverable failure\n", filename);
fclose(out);
return 0;
@@ -278,7 +276,6 @@ writefileatbyte(const char *dest, const char *source, long offset)
if (rename("tmp.tmp", filename) == -1)
{
-
fprintf(stderr, "unable to rename tmp file to '%s', unrecoverable failure\n", filename);
return 0;
}
@@ -287,32 +284,76 @@ writefileatbyte(const char *dest, const char *source, long offset)
}
int
-frontpage(int flags)
+replaceinpage(const char *outfile, const char *toreplace, const char *infile)
{
- if (!createfile(frontpage_index_output))
+ /*
+ * replace 'toreplace' in file 'outfile' taking 'infile' as input
+ * return 1 on success, 0 on failure
+ */
+ long substrpos = -1;
+
+ substrpos = findstring(frontpage_index_output, toreplace);
+
+ if (substrpos < 0)
{
- fprintf(stderr, "unable to generate frontpage\n");
return 0;
}
- long substrpos = -1;
+ if (!deletebytes(frontpage_index_output, substrpos, strlen(toreplace)))
+ {
+ return 0;
+ }
+
+ if (!writefileatbyte(outfile, infile, substrpos))
+ {
+ return 0;
+ }
+ return 1;
+}
- substrpos = findstring(frontpage_index_output, "cirno");
+char
+*gettime()
+{
+ return "10/10/2019";
+}
- if (substrpos < 0)
+int
+createtmpfile(const char *name, const char *content, size_t size)
+{
+ FILE *tmp = fopen(name, "w");
+ if (!tmp)
{
- fprintf(stderr, "unable to generate frontpage\n");
+ fprintf(stderr, "unable to create temp file '%s', unrecoverable failure\n", name);
return 0;
}
+ fwrite(content, 1, size, tmp);
+ fclose(tmp);
+ return 1;
+}
- printf("first occurance of '%s' is at byte %ld\n", "cirno", substrpos);
+int
+frontpage(int flags)
+{
+ if (!createfile(frontpage_index_output))
+ {
+ fprintf(stderr, "unable to generate frontpage\n");
+ return 0;
+ }
-
- if (!deletebytes(frontpage_index_output, substrpos, strlen("cirno")))
+ createtmpfile("title.tmp", frontpage_title, strlen(frontpage_title));
+ createtmpfile("info.tmp", frontpage_info, strlen(frontpage_info));
+ createtmpfile("date.tmp", gettime(), strlen(gettime()));
+ if (!replaceinpage(frontpage_index_output, content_string, frontpage_index) ||
+ !replaceinpage(frontpage_index_output, title_string, "title.tmp") ||
+ !replaceinpage(frontpage_index_output, info_string, "info.tmp") ||
+ !replaceinpage(frontpage_index_output, time_string, "date.tmp"))
{
fprintf(stderr, "unable to generate frontpage\n");
return 0;
}
- writefileatbyte(frontpage_index_output, "source.txt", substrpos);
+ remove("title.tmp");
+ remove("date.tmp");
+ remove("info.tmp");
+
return 1;
}
diff --git a/pages.h b/pages.h
index 658f17a..e141376 100644
--- a/pages.h
+++ b/pages.h
@@ -26,6 +26,9 @@ int createfile(const char *file);
long findstring(const char *file, const char *str);
int deletebytes(const char *file, long offset, size_t bytes);
int writefileatbyte(const char *dest, const char *source, long offset);
+int replaceinpage(const char *outfile, const char *toreplace, const char *infile);
+char *gettime();
+int createtmpfile(const char *name, const char *content, size_t size);
/* generators (to be put into the pages array) */
int frontpage(int flags);