feat(ltm): support custom data reader functions
This commit is contained in:
parent
f37cfc30af
commit
845ec95bf1
4 changed files with 90 additions and 8 deletions
|
|
@ -3,9 +3,18 @@
|
|||
|
||||
#include "ltm/template.h"
|
||||
|
||||
const char *s = "<body><pre><code>\n"
|
||||
"{{ paste }}\n"
|
||||
"</code></pre></body>";
|
||||
const char *s = "<head><link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css\">\n"
|
||||
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js\"></script>\n"
|
||||
"<script>hljs.highlightAll();</script></head>\n"
|
||||
"<body><pre><code>{{ paste }}</code></pre></body>";
|
||||
|
||||
ltm_err reader(size_t *written, char *buf, size_t len, void *data) {
|
||||
FILE *f = data;
|
||||
|
||||
*written = fread(buf, 1, len, f);
|
||||
|
||||
return ltm_err_ok;
|
||||
}
|
||||
|
||||
int main() {
|
||||
ltm_template *template;
|
||||
|
|
@ -13,17 +22,22 @@ int main() {
|
|||
ltm_instance *instance;
|
||||
ltm_template_instantiate(&instance, template);
|
||||
|
||||
const char *filename = "src/ltm_instance.c";
|
||||
|
||||
struct stat sb;
|
||||
stat("Makefile", &sb);
|
||||
stat(filename, &sb);
|
||||
|
||||
FILE *f = fopen("Makefile", "rb");
|
||||
FILE *f = fopen(filename, "rb");
|
||||
|
||||
ltm_instance_block_add_var(instance, "paste", ltm_instance_block_type_file_owned, f, sb.st_size);
|
||||
ltm_instance_block_add_var_fn(instance, "paste", reader, f, sb.st_size);
|
||||
/* ltm_instance_block_add_var(instance, "paste", ltm_instance_block_type_file_owned, f, sb.st_size); */
|
||||
/* ltm_instance_block_add_var(instance, "paste", ltm_instance_block_type_buf, "hello\n", 6); */
|
||||
/* ltm_instance_block_add_var(instance, "paste", ltm_instance_block_type_buf, "world\n", 6); */
|
||||
|
||||
char buf[1024];
|
||||
char buf[128];
|
||||
size_t written = 0;
|
||||
|
||||
while (ltm_instance_write(&written, buf, 1024, instance) != ltm_err_done) {
|
||||
while (ltm_instance_write(&written, buf, 128, instance) != ltm_err_done) {
|
||||
printf("%.*s", (int)written, buf);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue