Skip to content

Instantly share code, notes, and snippets.

@skullchap
Last active December 21, 2021 23:43
Show Gist options
  • Save skullchap/b88bcce04904927a464ab0f3d47a1366 to your computer and use it in GitHub Desktop.
Save skullchap/b88bcce04904927a464ab0f3d47a1366 to your computer and use it in GitHub Desktop.
simple dull way of http template generation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BFSZ 1024
char* template = "test.html";
char* generated = "new_test.html";
char* listFile = "zanimals.txt";
char *pattern = "<li><a href=\"\">$$</a></li>";
int main()
{
char pre[BFSZ] = {0};
char post[BFSZ] = {0};
char spaces[BFSZ] = {0};
memset(spaces, ' ', BFSZ);
int match = strcspn(pattern, "$$");
(match == 0) ? exit(1) : (1);
strncpy(pre, pattern, match);
strncpy(post, pattern + match + 2, strlen(pattern + match)); // 2 for $$
char buf[BFSZ] = {0};
FILE *fp = fopen(template, "rb");
FILE *txt_fp = fopen(listFile, "rb");
FILE *new_fp = fopen(generated, "wb");
int len = 0;
skip_line:
while (fgets(buf, BFSZ, fp) > 0)
{
len = strlen(buf);
for (int i = 0; i < len; ++i)
{
if (!strncmp(&buf[i], pattern, strlen(pattern)))
{
spaces[i] = '\0';
while (fgets(buf, BFSZ, txt_fp) > 0)
{
buf[strlen(buf) - 1] = '\0';
fprintf(new_fp, "%s%s%s%s\n", spaces, pre, buf, post);
}
memset(spaces, ' ', BFSZ);
goto skip_line;
}
}
fwrite(buf, 1, len, new_fp);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul>
<li><a href="">$$</a></li>
</ul>
</body>
</html>
Beaver
Cat
Dog
Goat
Horse
Human
Jaguar
Parrot
Rabbit
Sloth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment