Compare commits
20 Commits
4089570293
...
65dcbef0b1
Author | SHA1 | Date |
---|---|---|
Jef Roosens | 65dcbef0b1 | |
Jef Roosens | a68e32c949 | |
Jef Roosens | f789847fc5 | |
Jef Roosens | 97d4008db2 | |
Jef Roosens | 83072d5441 | |
Jef Roosens | 90826c3644 | |
Jef Roosens | b74e8a7b84 | |
Jef Roosens | 9cd5d386cd | |
Jef Roosens | f781fbdb89 | |
Jef Roosens | dae8a2f30e | |
Jef Roosens | 53c2313953 | |
Jef Roosens | c7a1fec6c2 | |
Jef Roosens | 11325bdbcd | |
Jef Roosens | 959be10264 | |
Jef Roosens | 6f817307cf | |
Jef Roosens | 1e34cb8c2d | |
Jef Roosens | 9a207d0b0b | |
Jef Roosens | 1bb0e7f119 | |
Jef Roosens | 0c8c6c2b63 | |
Jef Roosens | 4f4b780b28 |
10
CHANGELOG.md
10
CHANGELOG.md
|
@ -20,6 +20,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
* Automatically support HEAD requests for all GET requests
|
* Automatically support HEAD requests for all GET requests
|
||||||
* Event loop uses `epoll` instead of `poll`
|
* Event loop uses `epoll` instead of `poll`
|
||||||
* Configurable multithreading using `epoll`
|
* Configurable multithreading using `epoll`
|
||||||
|
* LSM
|
||||||
|
* Binary tree iterators
|
||||||
|
* Trie iterators
|
||||||
|
* Store iterators
|
||||||
|
* LTM - Lander Template Library
|
||||||
|
* Simple string templates
|
||||||
|
* Fully streamable template instances
|
||||||
|
* Lander
|
||||||
|
* Simple paste pages with syntax highlighting
|
||||||
|
* Index pages that group elements under the same namespace into an overview
|
||||||
* Landerctl
|
* Landerctl
|
||||||
* `-c` flag to use custom config file (useful for testing)
|
* `-c` flag to use custom config file (useful for testing)
|
||||||
|
|
||||||
|
|
8
Makefile
8
Makefile
|
@ -42,7 +42,11 @@ liblsm:
|
||||||
liblnm:
|
liblnm:
|
||||||
$(MAKE) -C lnm
|
$(MAKE) -C lnm
|
||||||
|
|
||||||
$(BIN): liblsm liblnm $(OBJS)
|
.PHONY: libltm
|
||||||
|
libltm:
|
||||||
|
$(MAKE) -C ltm
|
||||||
|
|
||||||
|
$(BIN): liblsm liblnm libltm $(OBJS)
|
||||||
$(CC) -o $@ $(OBJS) $(_LDFLAGS)
|
$(CC) -o $@ $(OBJS) $(_LDFLAGS)
|
||||||
|
|
||||||
$(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c
|
$(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c
|
||||||
|
@ -67,7 +71,7 @@ run: $(BIN)
|
||||||
valgrind: $(BIN)
|
valgrind: $(BIN)
|
||||||
LANDER_API_KEY=test \
|
LANDER_API_KEY=test \
|
||||||
LANDER_DATA_DIR=data \
|
LANDER_DATA_DIR=data \
|
||||||
valgrind --track-origins=yes '$(BUILD_DIR)/$(BIN_FILENAME)'
|
valgrind --track-origins=yes --leak-check=full '$(BUILD_DIR)/$(BIN_FILENAME)'
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: $(TARGETS_TEST)
|
test: $(TARGETS_TEST)
|
||||||
|
|
|
@ -7,16 +7,16 @@ SRC_DIR = src
|
||||||
TEST_DIR = test
|
TEST_DIR = test
|
||||||
THIRDPARTY_DIR = thirdparty
|
THIRDPARTY_DIR = thirdparty
|
||||||
|
|
||||||
INC_DIRS = include $(THIRDPARTY_DIR)/include lsm/include lnm/include
|
INC_DIRS = include $(THIRDPARTY_DIR)/include lsm/include lnm/include ltm/include
|
||||||
LIBS = lsm lnm
|
LIBS = lsm lnm ltm
|
||||||
LIB_DIRS = ./lsm/build ./lnm/build
|
LIB_DIRS = ./lsm/build ./lnm/build ./ltm/build
|
||||||
|
|
||||||
# -MMD: generate a .d file for every source file. This file can be imported by
|
# -MMD: generate a .d file for every source file. This file can be imported by
|
||||||
# make and makes make aware that a header file has been changed, ensuring an
|
# make and makes make aware that a header file has been changed, ensuring an
|
||||||
# object file is also recompiled if only a header is changed.
|
# object file is also recompiled if only a header is changed.
|
||||||
# -MP: generate a dummy target for every header file (according to the docs it
|
# -MP: generate a dummy target for every header file (according to the docs it
|
||||||
# prevents some errors when removing header files)
|
# prevents some errors when removing header files)
|
||||||
CFLAGS ?= -MMD -MP -g
|
CFLAGS ?= -MMD -MP -g -Wall
|
||||||
|
|
||||||
# When compiling release builds, these flags are better
|
# When compiling release builds, these flags are better
|
||||||
# CLAGS = -O3
|
# CLAGS = -O3
|
||||||
|
|
|
@ -4,16 +4,22 @@
|
||||||
#include "lnm/common.h"
|
#include "lnm/common.h"
|
||||||
#include "lnm/http/loop.h"
|
#include "lnm/http/loop.h"
|
||||||
#include "lsm/store.h"
|
#include "lsm/store.h"
|
||||||
|
#include "ltm/template.h"
|
||||||
|
|
||||||
extern const char lander_key_charset[];
|
extern const char lander_key_charset[];
|
||||||
|
|
||||||
typedef struct lander_gctx {
|
typedef struct lander_gctx {
|
||||||
const char *data_dir;
|
const char *data_dir;
|
||||||
lsm_store *store;
|
lsm_store *store;
|
||||||
|
struct {
|
||||||
|
ltm_template *paste;
|
||||||
|
ltm_template *index;
|
||||||
|
} templates;
|
||||||
} lander_gctx;
|
} lander_gctx;
|
||||||
|
|
||||||
typedef struct lander_ctx {
|
typedef struct lander_ctx {
|
||||||
lsm_entry_handle *entry;
|
lsm_entry_handle *entry;
|
||||||
|
ltm_instance *instance;
|
||||||
} lander_ctx;
|
} lander_ctx;
|
||||||
|
|
||||||
typedef enum lander_attr_type : uint8_t {
|
typedef enum lander_attr_type : uint8_t {
|
||||||
|
@ -27,6 +33,7 @@ typedef enum lander_entry_type : uint8_t {
|
||||||
lander_entry_type_redirect = 0,
|
lander_entry_type_redirect = 0,
|
||||||
lander_entry_type_paste = 1,
|
lander_entry_type_paste = 1,
|
||||||
lander_entry_type_file = 2,
|
lander_entry_type_file = 2,
|
||||||
|
lander_entry_type_index = 3,
|
||||||
} lander_entry_type;
|
} lander_entry_type;
|
||||||
|
|
||||||
void *lander_gctx_init();
|
void *lander_gctx_init();
|
||||||
|
@ -37,7 +44,7 @@ void lander_ctx_reset(lander_ctx *ctx);
|
||||||
|
|
||||||
void lander_ctx_free(lander_ctx *ctx);
|
void lander_ctx_free(lander_ctx *ctx);
|
||||||
|
|
||||||
lnm_http_step_err lander_get_index(lnm_http_conn *conn);
|
lnm_http_step_err lander_get_root(lnm_http_conn *conn);
|
||||||
|
|
||||||
lnm_http_step_err lander_get_entry(lnm_http_conn *conn);
|
lnm_http_step_err lander_get_entry(lnm_http_conn *conn);
|
||||||
|
|
||||||
|
@ -53,6 +60,8 @@ lnm_http_step_err lander_remove_entry(lnm_http_conn *conn);
|
||||||
|
|
||||||
lnm_http_step_err lander_post_file(lnm_http_conn *conn);
|
lnm_http_step_err lander_post_file(lnm_http_conn *conn);
|
||||||
|
|
||||||
|
lnm_http_step_err lander_post_index(lnm_http_conn *conn);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store the requested header as an attribute, if it's present.
|
* Store the requested header as an attribute, if it's present.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -27,6 +27,7 @@ typedef enum lsm_error {
|
||||||
lsm_error_null_value = 4,
|
lsm_error_null_value = 4,
|
||||||
lsm_error_failed_io = 5,
|
lsm_error_failed_io = 5,
|
||||||
lsm_error_lock_busy = 6,
|
lsm_error_lock_busy = 6,
|
||||||
|
lsm_error_done = 7,
|
||||||
} lsm_error;
|
} lsm_error;
|
||||||
|
|
||||||
/*typedef struct lsm_string { */
|
/*typedef struct lsm_string { */
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef LSM_BT
|
#ifndef LSM_BT
|
||||||
#define LSM_BT
|
#define LSM_BT
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "lsm.h"
|
#include "lsm.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,6 +10,11 @@
|
||||||
*/
|
*/
|
||||||
typedef struct lsm_bt lsm_bt;
|
typedef struct lsm_bt lsm_bt;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A node inside an `lsm_bt` binary tree.
|
||||||
|
*/
|
||||||
|
typedef struct lsm_bt_node lsm_bt_node;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize a new binary tree
|
* Initialize a new binary tree
|
||||||
*
|
*
|
||||||
|
@ -68,4 +75,33 @@ lsm_error lsm_bt_remove(void **out, lsm_bt *bt, char key);
|
||||||
*/
|
*/
|
||||||
lsm_error lsm_bt_replace(void **out, lsm_bt *bt, char key, void *data);
|
lsm_error lsm_bt_replace(void **out, lsm_bt *bt, char key, void *data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Struct representing an in-flight iterator over the binary tree
|
||||||
|
*/
|
||||||
|
typedef struct lsm_bt_iterator {
|
||||||
|
lsm_bt_node *next;
|
||||||
|
} lsm_bt_iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the given iterator for the binary tree.
|
||||||
|
*
|
||||||
|
* The iterator is explicitely allowed to be allocated by the user, as these are
|
||||||
|
* commonly used inside functions where they can simply be stored on the stack.
|
||||||
|
*
|
||||||
|
* @param out iterator to initialize
|
||||||
|
* @param bt binary tree to iterate
|
||||||
|
*/
|
||||||
|
void lsm_bt_iter(lsm_bt_iterator *out, const lsm_bt *bt);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Advance the iterator to the next element.
|
||||||
|
*
|
||||||
|
* @param out where to store pointer to data; ignored if NULL
|
||||||
|
* @param key_out where to store key; ignored if NULL
|
||||||
|
* @param iter iterator to advance
|
||||||
|
* @return true if a new entry was returned, false if the iterator has no more
|
||||||
|
* entries to return
|
||||||
|
*/
|
||||||
|
bool lsm_bt_iter_next(void **out, char *key_out, lsm_bt_iterator *iter);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -98,6 +98,11 @@ lsm_error lsm_entry_attr_insert_uint8_t(lsm_entry_handle *handle, uint8_t type,
|
||||||
lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle,
|
lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle,
|
||||||
uint8_t type);
|
uint8_t type);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a pointer to the entry's key.
|
||||||
|
*/
|
||||||
|
void lsm_entry_key(const lsm_str **out, lsm_entry_handle *handle);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A store consisting of LSM entries.
|
* A store consisting of LSM entries.
|
||||||
*
|
*
|
||||||
|
@ -228,4 +233,40 @@ lsm_error lsm_entry_data_read(uint64_t *out, char *buf,
|
||||||
*/
|
*/
|
||||||
uint64_t lsm_entry_data_len(lsm_entry_handle *handle);
|
uint64_t lsm_entry_data_len(lsm_entry_handle *handle);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an in-flight iterator over an LSM store.
|
||||||
|
*/
|
||||||
|
typedef struct lsm_store_iterator lsm_store_iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize an iterator to iterate over all entries with keys starting
|
||||||
|
* with the given prefix.
|
||||||
|
*
|
||||||
|
* @param out pointer to store iterator pointer in
|
||||||
|
* @param trie trie to iterate
|
||||||
|
* @param prefix prefix of the keys; a zero-length string means iterating over
|
||||||
|
* the entire trie; NULL is interpreted as a zero-length string
|
||||||
|
*/
|
||||||
|
lsm_error lsm_store_iter(lsm_store_iterator **out, lsm_store *store,
|
||||||
|
const lsm_str *prefix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Advance the iterator, returning a read handle to the next entry. The caller
|
||||||
|
* is responsible for closing this handle.
|
||||||
|
*/
|
||||||
|
lsm_error lsm_store_iter_next_read(lsm_entry_handle **out,
|
||||||
|
lsm_store_iterator *iter);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Advance the iterator, returning a write handle to the next entry. The caller
|
||||||
|
* is responsible for closing this handle.
|
||||||
|
*/
|
||||||
|
/* lsm_error lsm_store_iter_next_write(lsm_entry_handle **out,
|
||||||
|
* lsm_store_iterator *iter); */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Free the given iterator.
|
||||||
|
*/
|
||||||
|
void lsm_store_iter_free(lsm_store_iterator *iter);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -178,6 +178,15 @@ lsm_error lsm_str_split(lsm_str *s, lsm_str *s2, uint64_t index);
|
||||||
* @param s string to append s2 to
|
* @param s string to append s2 to
|
||||||
* @param s2 string to append to s
|
* @param s2 string to append to s
|
||||||
*/
|
*/
|
||||||
lsm_error lsm_str_append(lsm_str *s, lsm_str *s2);
|
lsm_error lsm_str_append(lsm_str *s, const lsm_str *s2);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Same as `lsm_str_append`, but it takes a C-style string instead.
|
||||||
|
*
|
||||||
|
* @param s string to append c_str to
|
||||||
|
* @param c_str char buffer to append
|
||||||
|
* @param len length of c_str
|
||||||
|
*/
|
||||||
|
lsm_error lsm_str_append_c(lsm_str *s, const char *c_str, uint64_t len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
#ifndef LSM_TRIE
|
#ifndef LSM_TRIE
|
||||||
#define LSM_TRIE
|
#define LSM_TRIE
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "lsm.h"
|
#include "lsm.h"
|
||||||
|
#include "lsm/bt.h"
|
||||||
#include "lsm/str.h"
|
#include "lsm/str.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -9,6 +12,11 @@
|
||||||
*/
|
*/
|
||||||
typedef struct lsm_trie lsm_trie;
|
typedef struct lsm_trie lsm_trie;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A node inside an `lsm_trie` trie
|
||||||
|
*/
|
||||||
|
typedef struct lsm_trie_node lsm_trie_node;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize a new trie.
|
* Initialize a new trie.
|
||||||
*
|
*
|
||||||
|
@ -57,4 +65,36 @@ lsm_error lsm_trie_remove(void **out, lsm_trie *trie, const lsm_str *key);
|
||||||
*/
|
*/
|
||||||
uint64_t lsm_trie_size(const lsm_trie *trie);
|
uint64_t lsm_trie_size(const lsm_trie *trie);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an in-flight iterator over a trie
|
||||||
|
*/
|
||||||
|
typedef struct lsm_trie_iterator lsm_trie_iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize an iterator to iterate over all entries with keys starting
|
||||||
|
* with the given prefix.
|
||||||
|
*
|
||||||
|
* @param out pointer to store iterator pointer in
|
||||||
|
* @param trie trie to iterate
|
||||||
|
* @param prefix prefix of the keys; a zero-length string means iterating over
|
||||||
|
* the entire trie; NULL is interpreted as a zero-length string
|
||||||
|
*/
|
||||||
|
lsm_error lsm_trie_iter(lsm_trie_iterator **out, const lsm_trie *trie,
|
||||||
|
const lsm_str *prefix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Advance the given iterator.
|
||||||
|
*
|
||||||
|
* @param out pointer to store next data pointer in
|
||||||
|
* @param iter iterator to advance
|
||||||
|
* @return `lsm_error_ok` if next element has been returned, `lsm_error_done` if
|
||||||
|
* no more elements are present, or some other error code
|
||||||
|
*/
|
||||||
|
lsm_error lsm_trie_iter_next(void **out, lsm_trie_iterator *iter);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Free the given iterator.
|
||||||
|
*/
|
||||||
|
void lsm_trie_iter_free(lsm_trie_iterator *iter);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -9,12 +9,13 @@
|
||||||
/**
|
/**
|
||||||
* Node inside a binary tree
|
* Node inside a binary tree
|
||||||
*/
|
*/
|
||||||
typedef struct lsm_bt_node {
|
struct lsm_bt_node {
|
||||||
struct lsm_bt_node *left;
|
struct lsm_bt_node *left;
|
||||||
struct lsm_bt_node *right;
|
struct lsm_bt_node *right;
|
||||||
|
struct lsm_bt_node *parent;
|
||||||
void *data;
|
void *data;
|
||||||
char key;
|
char key;
|
||||||
} lsm_bt_node;
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize a new binary tree node
|
* Initialize a new binary tree node
|
||||||
|
|
|
@ -34,6 +34,11 @@ typedef struct lsm_entry {
|
||||||
uint64_t idx_file_offset;
|
uint64_t idx_file_offset;
|
||||||
} lsm_entry;
|
} lsm_entry;
|
||||||
|
|
||||||
|
struct lsm_store_iterator {
|
||||||
|
lsm_trie_iterator *iter;
|
||||||
|
lsm_store *store;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allocate and initialize a new lsm_entry object.
|
* Allocate and initialize a new lsm_entry object.
|
||||||
*
|
*
|
||||||
|
|
|
@ -5,14 +5,11 @@
|
||||||
#include "lsm/str_internal.h"
|
#include "lsm/str_internal.h"
|
||||||
#include "lsm/trie.h"
|
#include "lsm/trie.h"
|
||||||
|
|
||||||
/**
|
struct lsm_trie_node {
|
||||||
* A node inside a trie structure
|
|
||||||
*/
|
|
||||||
typedef struct lsm_trie_node {
|
|
||||||
lsm_bt bt;
|
lsm_bt bt;
|
||||||
lsm_str skip;
|
lsm_str skip;
|
||||||
void *data;
|
void *data;
|
||||||
} lsm_trie_node;
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allocate and initialize a new trie node
|
* Allocate and initialize a new trie node
|
||||||
|
@ -33,4 +30,13 @@ struct lsm_trie {
|
||||||
uint64_t size;
|
uint64_t size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct lsm_trie_iterator {
|
||||||
|
lsm_trie_node *next;
|
||||||
|
struct {
|
||||||
|
lsm_bt_iterator *arr;
|
||||||
|
size_t len;
|
||||||
|
size_t cap;
|
||||||
|
} stack;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "lsm/bt.h"
|
||||||
#include "lsm/bt_internal.h"
|
#include "lsm/bt_internal.h"
|
||||||
|
|
||||||
lsm_error lsm_bt_node_init(lsm_bt_node **ptr, const char key, void *data) {
|
lsm_error lsm_bt_node_init(lsm_bt_node **ptr, const char key, void *data) {
|
||||||
|
@ -60,10 +61,12 @@ void lsm_bt_free(lsm_bt *bt) {
|
||||||
uint64_t lsm_bt_size(const lsm_bt *bt) { return bt->size; }
|
uint64_t lsm_bt_size(const lsm_bt *bt) { return bt->size; }
|
||||||
|
|
||||||
lsm_error lsm_bt_insert(lsm_bt *bt, char key, void *data) {
|
lsm_error lsm_bt_insert(lsm_bt *bt, char key, void *data) {
|
||||||
|
lsm_bt_node *parent = NULL;
|
||||||
lsm_bt_node **dest = &bt->root;
|
lsm_bt_node **dest = &bt->root;
|
||||||
|
|
||||||
// Traverse down the tree until we reach the new point to insert our node
|
// Traverse down the tree until we reach the new point to insert our node
|
||||||
while ((*dest != NULL) && ((*dest)->key != key)) {
|
while ((*dest != NULL) && ((*dest)->key != key)) {
|
||||||
|
parent = *dest;
|
||||||
dest = key < (*dest)->key ? &(*dest)->left : &(*dest)->right;
|
dest = key < (*dest)->key ? &(*dest)->left : &(*dest)->right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,9 +74,8 @@ lsm_error lsm_bt_insert(lsm_bt *bt, char key, void *data) {
|
||||||
return lsm_error_already_present;
|
return lsm_error_already_present;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lsm_bt_node_init(dest, key, data) != lsm_error_ok) {
|
LSM_RES(lsm_bt_node_init(dest, key, data));
|
||||||
return lsm_error_failed_alloc;
|
(*dest)->parent = parent;
|
||||||
}
|
|
||||||
|
|
||||||
bt->size++;
|
bt->size++;
|
||||||
|
|
||||||
|
@ -125,13 +127,27 @@ lsm_error lsm_bt_remove(void **out, lsm_bt *bt, char key) {
|
||||||
(*dest)->data = (*succ)->data;
|
(*dest)->data = (*succ)->data;
|
||||||
|
|
||||||
lsm_bt_node *succ_replacement = (*succ)->right;
|
lsm_bt_node *succ_replacement = (*succ)->right;
|
||||||
|
lsm_bt_node *parent = (*succ)->parent;
|
||||||
|
|
||||||
lsm_bt_node_free(*succ);
|
lsm_bt_node_free(*succ);
|
||||||
|
|
||||||
*succ = succ_replacement;
|
*succ = succ_replacement;
|
||||||
|
|
||||||
|
if (*succ != NULL) {
|
||||||
|
(*succ)->parent = parent;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
lsm_bt_node *replacement =
|
lsm_bt_node *replacement =
|
||||||
(*dest)->left != NULL ? (*dest)->left : (*dest)->right;
|
(*dest)->left != NULL ? (*dest)->left : (*dest)->right;
|
||||||
|
lsm_bt_node *parent = (*dest)->parent;
|
||||||
|
|
||||||
lsm_bt_node_free(*dest);
|
lsm_bt_node_free(*dest);
|
||||||
|
|
||||||
*dest = replacement;
|
*dest = replacement;
|
||||||
|
|
||||||
|
if (*dest != NULL) {
|
||||||
|
(*dest)->parent = parent;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return lsm_error_ok;
|
return lsm_error_ok;
|
||||||
|
@ -156,3 +172,45 @@ lsm_error lsm_bt_replace(void **out, lsm_bt *bt, char key, void *data) {
|
||||||
|
|
||||||
return lsm_error_ok;
|
return lsm_error_ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void lsm_bt_iter(lsm_bt_iterator *out, const lsm_bt *bt) {
|
||||||
|
out->next = bt->root;
|
||||||
|
|
||||||
|
if (bt->root != NULL) {
|
||||||
|
// Initialize the iterator to the smallest element in the tree
|
||||||
|
while (out->next->left != NULL) {
|
||||||
|
out->next = out->next->left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool lsm_bt_iter_next(void **out, char *key_out, lsm_bt_iterator *iter) {
|
||||||
|
if (iter->next == NULL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (out != NULL) {
|
||||||
|
*out = iter->next->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key_out != NULL) {
|
||||||
|
*key_out = iter->next->key;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (iter->next->right != NULL) {
|
||||||
|
iter->next = iter->next->right;
|
||||||
|
|
||||||
|
while (iter->next->left != NULL) {
|
||||||
|
iter->next = iter->next->left;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while ((iter->next->parent != NULL) &&
|
||||||
|
(iter->next->parent->right == iter->next)) {
|
||||||
|
iter->next = iter->next->parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
iter->next = iter->next->parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
|
@ -316,3 +316,7 @@ lsm_error lsm_entry_data_open_read(lsm_entry_handle *handle) {
|
||||||
|
|
||||||
return lsm_error_ok;
|
return lsm_error_ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void lsm_entry_key(const lsm_str **out, lsm_entry_handle *handle) {
|
||||||
|
*out = handle->wrapper->entry->key;
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
#include "lsm/store.h"
|
||||||
|
#include "lsm/store_internal.h"
|
||||||
|
|
||||||
|
lsm_error lsm_store_iter(lsm_store_iterator **out, lsm_store *store,
|
||||||
|
const lsm_str *prefix) {
|
||||||
|
lsm_trie_iterator *trie_iter;
|
||||||
|
LSM_RES(lsm_trie_iter(&trie_iter, store->trie, prefix));
|
||||||
|
|
||||||
|
lsm_store_iterator *iter = calloc(1, sizeof(lsm_store_iterator));
|
||||||
|
|
||||||
|
if (iter == NULL) {
|
||||||
|
lsm_trie_iter_free(trie_iter);
|
||||||
|
|
||||||
|
return lsm_error_failed_alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
iter->iter = trie_iter;
|
||||||
|
iter->store = store;
|
||||||
|
|
||||||
|
*out = iter;
|
||||||
|
|
||||||
|
return lsm_error_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
lsm_error lsm_store_iter_next_read(lsm_entry_handle **out,
|
||||||
|
lsm_store_iterator *iter) {
|
||||||
|
lsm_entry_wrapper *wrapper = NULL;
|
||||||
|
|
||||||
|
// Traverse through the trie until a node is found with a filled data field
|
||||||
|
do {
|
||||||
|
// Exits function if iterator is done
|
||||||
|
LSM_RES(lsm_trie_iter_next((void **)&wrapper, iter->iter));
|
||||||
|
|
||||||
|
// TODO error handling?
|
||||||
|
pthread_rwlock_rdlock(&wrapper->lock);
|
||||||
|
|
||||||
|
if (wrapper->entry == NULL) {
|
||||||
|
pthread_rwlock_unlock(&wrapper->lock);
|
||||||
|
|
||||||
|
wrapper = NULL;
|
||||||
|
}
|
||||||
|
} while (wrapper == NULL);
|
||||||
|
|
||||||
|
lsm_entry_handle *handle;
|
||||||
|
LSM_RES2(lsm_entry_handle_init(&handle),
|
||||||
|
pthread_rwlock_unlock(&wrapper->lock));
|
||||||
|
|
||||||
|
handle->wrapper = wrapper;
|
||||||
|
handle->store = iter->store;
|
||||||
|
*out = handle;
|
||||||
|
|
||||||
|
return lsm_error_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
void lsm_store_iter_free(lsm_store_iterator *iter) {
|
||||||
|
lsm_trie_iter_free(iter->iter);
|
||||||
|
free(iter);
|
||||||
|
}
|
|
@ -215,15 +215,15 @@ bool lsm_str_eq(const lsm_str *s1, const lsm_str *s2) {
|
||||||
return memcmp(lsm_str_ptr(s1), lsm_str_ptr(s2), s1->len) == 0;
|
return memcmp(lsm_str_ptr(s1), lsm_str_ptr(s2), s1->len) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
lsm_error lsm_str_append(lsm_str *s, lsm_str *s2) {
|
lsm_error lsm_str_append_c(lsm_str *s, const char *c_str, uint64_t len) {
|
||||||
if (s2->len == 0) {
|
if (len == 0) {
|
||||||
return lsm_error_ok;
|
return lsm_error_ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t new_len = s->len + s2->len;
|
uint64_t new_len = s->len + len;
|
||||||
|
|
||||||
if (new_len <= 8) {
|
if (new_len <= 8) {
|
||||||
memcpy(&s->data.val[s->len], s2->data.val, s2->len);
|
memcpy(&s->data.val[s->len], c_str, len);
|
||||||
} else {
|
} else {
|
||||||
char *buf;
|
char *buf;
|
||||||
|
|
||||||
|
@ -243,11 +243,15 @@ lsm_error lsm_str_append(lsm_str *s, lsm_str *s2) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(&buf[s->len], lsm_str_ptr(s2), s2->len);
|
memcpy(&buf[s->len], c_str, len);
|
||||||
s->data.ptr = buf;
|
s->data.ptr = buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->len += s2->len;
|
s->len += len;
|
||||||
|
|
||||||
return lsm_error_ok;
|
return lsm_error_ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lsm_error lsm_str_append(lsm_str *s, const lsm_str *s2) {
|
||||||
|
return lsm_str_append_c(s, lsm_str_ptr(s2), s2->len);
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,117 @@
|
||||||
|
#include "lsm.h"
|
||||||
|
#include "lsm/trie_internal.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Advance the iterator so that `next` now points to a node containing data. If
|
||||||
|
* no more data nodes are found, `next` is set to NULL. The initial value of
|
||||||
|
* `next` is ignored.
|
||||||
|
*/
|
||||||
|
lsm_error lsm_trie_iter_next_data_node(lsm_trie_iterator *iter) {
|
||||||
|
do {
|
||||||
|
// Traverse back up the stack until we reach either the end of the subtree,
|
||||||
|
// or an iterator that hasn't been exhausted yet
|
||||||
|
while ((iter->stack.len > 0) &&
|
||||||
|
!lsm_bt_iter_next((void **)&iter->next, NULL,
|
||||||
|
&iter->stack.arr[iter->stack.len - 1])) {
|
||||||
|
iter->stack.len--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (iter->stack.len == 0) {
|
||||||
|
iter->next = NULL;
|
||||||
|
|
||||||
|
return lsm_error_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add node's subtree to the stack
|
||||||
|
if (iter->stack.len == iter->stack.cap) {
|
||||||
|
lsm_bt_iterator *arr = realloc(
|
||||||
|
iter->stack.arr, iter->stack.cap * 2 * sizeof(lsm_bt_iterator));
|
||||||
|
|
||||||
|
if (arr == NULL) {
|
||||||
|
return lsm_error_failed_alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
iter->stack.arr = arr;
|
||||||
|
iter->stack.cap *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
lsm_bt_iter(&iter->stack.arr[iter->stack.len], &iter->next->bt);
|
||||||
|
iter->stack.len++;
|
||||||
|
|
||||||
|
} while (iter->next->data == NULL);
|
||||||
|
|
||||||
|
return lsm_error_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
lsm_error lsm_trie_iter(lsm_trie_iterator **out, const lsm_trie *trie,
|
||||||
|
const lsm_str *prefix) {
|
||||||
|
uint64_t prefix_len = prefix == NULL ? 0 : lsm_str_len(prefix);
|
||||||
|
|
||||||
|
uint64_t index = 0;
|
||||||
|
lsm_trie_node *node = trie->root;
|
||||||
|
|
||||||
|
// Simplified traversal down the trie to find the root of the subtree
|
||||||
|
// contaiing all nodes with the given prefix
|
||||||
|
while ((index < prefix_len) && (node != NULL)) {
|
||||||
|
char c = lsm_str_char(prefix, index);
|
||||||
|
lsm_error res = lsm_bt_search((void **)&node, &node->bt, c);
|
||||||
|
|
||||||
|
switch (res) {
|
||||||
|
case lsm_error_ok:
|
||||||
|
index += 1 + lsm_str_len(&node->skip);
|
||||||
|
break;
|
||||||
|
case lsm_error_not_found:
|
||||||
|
node = NULL;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
lsm_trie_iterator *iter = calloc(1, sizeof(lsm_trie_iterator));
|
||||||
|
|
||||||
|
if (iter == NULL) {
|
||||||
|
return lsm_error_failed_alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
iter->next = node;
|
||||||
|
|
||||||
|
if (node != NULL) {
|
||||||
|
lsm_bt_iterator *arr = malloc(sizeof(lsm_bt_iterator));
|
||||||
|
|
||||||
|
if (arr == NULL) {
|
||||||
|
free(iter);
|
||||||
|
|
||||||
|
return lsm_error_failed_alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
iter->stack.arr = arr;
|
||||||
|
iter->stack.cap = 1;
|
||||||
|
iter->stack.len = 1;
|
||||||
|
|
||||||
|
lsm_bt_iter(&iter->stack.arr[0], &node->bt);
|
||||||
|
|
||||||
|
if (node->data == NULL) {
|
||||||
|
LSM_RES(lsm_trie_iter_next_data_node(iter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = iter;
|
||||||
|
|
||||||
|
return lsm_error_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
lsm_error lsm_trie_iter_next(void **out, lsm_trie_iterator *iter) {
|
||||||
|
if (iter->next == NULL) {
|
||||||
|
return lsm_error_done;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = iter->next->data;
|
||||||
|
|
||||||
|
return lsm_trie_iter_next_data_node(iter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lsm_trie_iter_free(lsm_trie_iterator *iter) {
|
||||||
|
free(iter->stack.arr);
|
||||||
|
free(iter);
|
||||||
|
}
|
|
@ -104,6 +104,33 @@ void test_remove_multiple() {
|
||||||
lsm_bt_free(bt);
|
lsm_bt_free(bt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_iter() {
|
||||||
|
char chars[] = "falcoep";
|
||||||
|
size_t char_count = sizeof(chars) / sizeof(char) - 1;
|
||||||
|
|
||||||
|
char sorted_chars[] = "aceflop";
|
||||||
|
|
||||||
|
BT_INIT();
|
||||||
|
|
||||||
|
for (size_t i = 0; i < char_count; i++) {
|
||||||
|
TEST_CHECK(lsm_bt_insert(bt, chars[i], (void *)(i + 1)) == lsm_error_ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
lsm_bt_iterator iter;
|
||||||
|
lsm_bt_iter(&iter, bt);
|
||||||
|
|
||||||
|
char key;
|
||||||
|
void *data;
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
while (lsm_bt_iter_next(&data, &key, &iter)) {
|
||||||
|
TEST_CHECK_(key == sorted_chars[i], "%c == %c", key, sorted_chars[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CHECK(i == char_count);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_LIST = {
|
TEST_LIST = {
|
||||||
{ "bt init", test_init },
|
{ "bt init", test_init },
|
||||||
{ "bt insert first", test_insert_first },
|
{ "bt insert first", test_insert_first },
|
||||||
|
@ -111,5 +138,6 @@ TEST_LIST = {
|
||||||
{ "bt insert multiple", test_insert_multiple },
|
{ "bt insert multiple", test_insert_multiple },
|
||||||
{ "bt remove root", test_remove_root },
|
{ "bt remove root", test_remove_root },
|
||||||
{ "bt remove multiple", test_remove_multiple },
|
{ "bt remove multiple", test_remove_multiple },
|
||||||
|
{ "bt iter", test_iter },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
|
@ -25,8 +25,64 @@ void test_insert_one() {
|
||||||
TEST_CHECK(data == (void *)1);
|
TEST_CHECK(data == (void *)1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_iter() {
|
||||||
|
TRIE_INIT();
|
||||||
|
|
||||||
|
lsm_str *s;
|
||||||
|
|
||||||
|
lsm_str_init_copy(&s, "hello");
|
||||||
|
TEST_CHECK(lsm_trie_insert(trie, s, (void *)1) == lsm_error_ok);
|
||||||
|
|
||||||
|
lsm_str_init_copy(&s, "hella");
|
||||||
|
TEST_CHECK(lsm_trie_insert(trie, s, (void *)2) == lsm_error_ok);
|
||||||
|
|
||||||
|
lsm_str_init_copy(&s, "hel");
|
||||||
|
TEST_CHECK(lsm_trie_insert(trie, s, (void *)3) == lsm_error_ok);
|
||||||
|
|
||||||
|
lsm_str_init_copy(&s, "wow");
|
||||||
|
TEST_CHECK(lsm_trie_insert(trie, s, (void *)4) == lsm_error_ok);
|
||||||
|
|
||||||
|
lsm_str_init_copy(&s, "hel");
|
||||||
|
|
||||||
|
lsm_trie_iterator *iter;
|
||||||
|
TEST_CHECK(lsm_trie_iter(&iter, trie, s) == lsm_error_ok);
|
||||||
|
|
||||||
|
void *data;
|
||||||
|
|
||||||
|
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||||
|
TEST_CHECK_(data == (void *)3, "%p == %p", data, (void *)3);
|
||||||
|
|
||||||
|
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||||
|
TEST_CHECK_(data == (void *)2, "%p == %p", data, (void *)2);
|
||||||
|
|
||||||
|
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||||
|
TEST_CHECK_(data == (void *)1, "%p == %p", data, (void *)1);
|
||||||
|
|
||||||
|
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_done);
|
||||||
|
lsm_trie_iter_free(iter);
|
||||||
|
|
||||||
|
// Test full trie iterator
|
||||||
|
TEST_CHECK(lsm_trie_iter(&iter, trie, NULL) == lsm_error_ok);
|
||||||
|
|
||||||
|
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||||
|
TEST_CHECK_(data == (void *)3, "%p == %p", data, (void *)3);
|
||||||
|
|
||||||
|
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||||
|
TEST_CHECK_(data == (void *)2, "%p == %p", data, (void *)2);
|
||||||
|
|
||||||
|
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||||
|
TEST_CHECK_(data == (void *)1, "%p == %p", data, (void *)1);
|
||||||
|
|
||||||
|
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
|
||||||
|
TEST_CHECK_(data == (void *)4, "%p == %p", data, (void *)4);
|
||||||
|
|
||||||
|
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_done);
|
||||||
|
lsm_trie_iter_free(iter);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_LIST = {
|
TEST_LIST = {
|
||||||
{ "trie init", test_init },
|
{ "trie init", test_init },
|
||||||
{ "trie insert one", test_insert_one },
|
{ "trie insert one", test_insert_one },
|
||||||
|
{ "trie iter", test_iter },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,133 @@
|
||||||
|
# https://spin.atomicobject.com/2016/08/26/makefile-c-projects/ was a great
|
||||||
|
# base for this Makefile
|
||||||
|
|
||||||
|
-include config.mk
|
||||||
|
|
||||||
|
LIB := $(BUILD_DIR)/$(LIB_FILENAME)
|
||||||
|
|
||||||
|
SRCS != find '$(SRC_DIR)' -iname '*.c'
|
||||||
|
SRCS_H != find include -iname '*.h'
|
||||||
|
SRCS_H_INTERNAL != find $(SRC_DIR) -iname '*.h'
|
||||||
|
SRCS_TEST != find '$(TEST_DIR)' -iname '*.c'
|
||||||
|
SRCS_EXAMPLE != find '$(EXAMPLE_DIR)' -iname '*.c'
|
||||||
|
|
||||||
|
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
|
||||||
|
OBJS_TEST := $(SRCS_TEST:%=$(BUILD_DIR)/%.o)
|
||||||
|
OBJS_EXAMPLE := $(SRCS_EXAMPLE:%=$(BUILD_DIR)/%.o)
|
||||||
|
DEPS := $(SRCS:%=$(BUILD_DIR)/%.d) $(SRCS_TEST:%=$(BUILD_DIR)/%.d)
|
||||||
|
|
||||||
|
BINS_TEST := $(OBJS_TEST:%.c.o=%)
|
||||||
|
BINS_EXAMPLE := $(OBJS_EXAMPLE:%.c.o=%)
|
||||||
|
|
||||||
|
TARGETS_TEST := $(BINS_TEST:%=test-%)
|
||||||
|
TARGETS_MEM_TEST := $(BINS_TEST:%=test-mem-%)
|
||||||
|
TARGETS_EXAMPLE := $(BINS_EXAMPLE:%=test-%)
|
||||||
|
|
||||||
|
_CFLAGS := $(addprefix -I,$(INC_DIRS)) $(CFLAGS) -Wall -Wextra
|
||||||
|
|
||||||
|
.PHONY: all
|
||||||
|
all: lib
|
||||||
|
|
||||||
|
|
||||||
|
# =====COMPILATION=====
|
||||||
|
# Utility used by the CI to lint
|
||||||
|
.PHONY: objs
|
||||||
|
objs: $(OBJS)
|
||||||
|
|
||||||
|
.PHONY: lib
|
||||||
|
lib: $(LIB)
|
||||||
|
$(LIB): $(OBJS)
|
||||||
|
ar -rcs $@ $(OBJS)
|
||||||
|
|
||||||
|
$(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c
|
||||||
|
mkdir -p $(dir $@)
|
||||||
|
$(CC) -c $(_CFLAGS) $< -o $@
|
||||||
|
|
||||||
|
# =====TESTING=====
|
||||||
|
.PHONY: test
|
||||||
|
test: $(TARGETS_TEST)
|
||||||
|
|
||||||
|
.PHONY: test-mem
|
||||||
|
test-mem: $(TARGETS_MEM_TEST)
|
||||||
|
|
||||||
|
.PHONY: $(TARGETS_TEST)
|
||||||
|
$(TARGETS_TEST): test-%: %
|
||||||
|
./$^
|
||||||
|
|
||||||
|
.PHONY: $(TARGETS_MEM_TEST)
|
||||||
|
$(TARGETS_MEM_TEST): test-mem-%: %
|
||||||
|
valgrind --tool=memcheck --error-exitcode=1 --track-origins=yes --leak-check=full ./$^
|
||||||
|
|
||||||
|
.PHONY: build-test
|
||||||
|
build-test: $(BINS_TEST)
|
||||||
|
|
||||||
|
$(BINS_TEST): %: %.c.o $(LIB)
|
||||||
|
$(CC) \
|
||||||
|
$^ -o $@
|
||||||
|
|
||||||
|
# Along with the include directory, each test includes $(TEST_DIR) (which
|
||||||
|
# contains the acutest.h header file), and the src directory of the module it's
|
||||||
|
# testing. This allows tests to access internal methods, which aren't publicly
|
||||||
|
# exposed.
|
||||||
|
$(BUILD_DIR)/$(TEST_DIR)/%.c.o: $(TEST_DIR)/%.c
|
||||||
|
mkdir -p $(dir $@)
|
||||||
|
$(CC) $(_CFLAGS) -I$(TEST_DIR) \
|
||||||
|
-I$(dir $(@:$(BUILD_DIR)/$(TEST_DIR)/%=$(SRC_DIR)/%)) \
|
||||||
|
-c $< -o $@
|
||||||
|
|
||||||
|
# =====EXAMPLES=====
|
||||||
|
.PHONY: build-example
|
||||||
|
build-example: $(BINS_EXAMPLE)
|
||||||
|
|
||||||
|
$(BINS_EXAMPLE): %: %.c.o $(LIB)
|
||||||
|
$(CC) \
|
||||||
|
$^ -o $@
|
||||||
|
|
||||||
|
# Example binaries link the resulting library
|
||||||
|
$(BUILD_DIR)/$(EXAMPLE_DIR)/%.c.o: $(EXAMPLE_DIR)/%.c
|
||||||
|
mkdir -p $(dir $@)
|
||||||
|
$(CC) $(_CFLAGS) -I$(PUB_INC_DIR) -c $< -o $@
|
||||||
|
|
||||||
|
# =====MAINTENANCE=====
|
||||||
|
.PHONY: lint
|
||||||
|
lint:
|
||||||
|
clang-format -n --Werror \
|
||||||
|
$(filter-out $(THIRDPARTY),$(SRCS)) \
|
||||||
|
$(filter-out $(THIRDPARTY),$(SRCS_H)) \
|
||||||
|
$(filter-out $(THIRDPARTY),$(SRCS_H_INTERNAL))
|
||||||
|
|
||||||
|
.PHONY: fmt
|
||||||
|
fmt:
|
||||||
|
clang-format -i \
|
||||||
|
$(filter-out $(THIRDPARTY),$(SRCS)) \
|
||||||
|
$(filter-out $(THIRDPARTY),$(SRCS_H)) \
|
||||||
|
$(filter-out $(THIRDPARTY),$(SRCS_H_INTERNAL))
|
||||||
|
|
||||||
|
.PHONY: check
|
||||||
|
check:
|
||||||
|
mkdir -p $(BUILD_DIR)/cppcheck
|
||||||
|
cppcheck \
|
||||||
|
$(addprefix -I,$(INC_DIRS)) \
|
||||||
|
--cppcheck-build-dir=$(BUILD_DIR)/cppcheck \
|
||||||
|
--error-exitcode=1 \
|
||||||
|
--enable=warning,style \
|
||||||
|
--inline-suppr \
|
||||||
|
--check-level=exhaustive \
|
||||||
|
--quiet \
|
||||||
|
-j$(shell nproc) \
|
||||||
|
$(filter-out $(THIRDPARTY),$(SRCS))
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf '$(BUILD_DIR)'
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: bear
|
||||||
|
bear: clean
|
||||||
|
bear -- make
|
||||||
|
bear --append -- make build-test
|
||||||
|
bear --append -- make build-example
|
||||||
|
|
||||||
|
|
||||||
|
# Make make aware of the .d files
|
||||||
|
-include $(DEPS)
|
|
@ -0,0 +1,32 @@
|
||||||
|
# LTM - Lander Template Module
|
||||||
|
|
||||||
|
This module provides an interface for generating `char` streams from a given
|
||||||
|
template.
|
||||||
|
|
||||||
|
```html
|
||||||
|
<body>
|
||||||
|
<h1>{{ title }}</h1>
|
||||||
|
<ul>
|
||||||
|
{{ item start }}
|
||||||
|
<li><a href="{{ url }}">{{ name }}</a></li>
|
||||||
|
{{ item end }}
|
||||||
|
</ul>
|
||||||
|
</body>
|
||||||
|
```
|
||||||
|
|
||||||
|
The above snippet shows everything the templating language currently offers;
|
||||||
|
either singular variables (here: `title`) or nested templates (here: `item`).
|
||||||
|
Note the absence of a loop construct. This is because each variable and nested
|
||||||
|
template is implicitely already a loop; a variable or a nested template can be
|
||||||
|
added as many times as needed, and they will appear in the order they were
|
||||||
|
added.
|
||||||
|
|
||||||
|
Template strings are compiled into templates. From a template, an instance can
|
||||||
|
be created to which the required data can be added to fill in the template.
|
||||||
|
Instantiating a template does not modify the original template, meaning
|
||||||
|
templates can be safely used concurrently.
|
||||||
|
|
||||||
|
Each inserted variable can get its data from a variety of sources, namely
|
||||||
|
`char` buffers, files or a custom reader function. The resulting `char` stream
|
||||||
|
is never fully loaded into memory; an instance can stream its content to a
|
||||||
|
provided buffer to prevent unnecessary memory usage.
|
|
@ -0,0 +1,17 @@
|
||||||
|
LIB_FILENAME = libltm.a
|
||||||
|
|
||||||
|
BUILD_DIR = build
|
||||||
|
SRC_DIR = src
|
||||||
|
TEST_DIR = test
|
||||||
|
EXAMPLE_DIR = example
|
||||||
|
THIRDPARTY =
|
||||||
|
|
||||||
|
PUB_INC_DIR = include
|
||||||
|
INC_DIRS = $(PUB_INC_DIR) src/_include
|
||||||
|
|
||||||
|
# -MMD: generate a .d file for every source file. This file can be imported by
|
||||||
|
# make and makes make aware that a header file has been changed, ensuring an
|
||||||
|
# object file is also recompiled if only a header is changed.
|
||||||
|
# -MP: generate a dummy target for every header file (according to the docs it
|
||||||
|
# prevents some errors when removing header files)
|
||||||
|
CFLAGS ?= -MMD -MP -g -Wall -Werror
|
|
@ -0,0 +1,48 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include "ltm/template.h"
|
||||||
|
|
||||||
|
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;
|
||||||
|
ltm_template_compile(&template, s);
|
||||||
|
ltm_instance *instance;
|
||||||
|
ltm_template_instantiate(&instance, template);
|
||||||
|
|
||||||
|
const char *filename = "src/ltm_instance.c";
|
||||||
|
|
||||||
|
struct stat sb;
|
||||||
|
stat(filename, &sb);
|
||||||
|
|
||||||
|
FILE *f = fopen(filename, "rb");
|
||||||
|
|
||||||
|
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[128];
|
||||||
|
size_t written = 0;
|
||||||
|
|
||||||
|
while (ltm_instance_write(&written, buf, 128, instance) != ltm_err_done) {
|
||||||
|
printf("%.*s", (int)written, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%.*s", (int)written, buf);
|
||||||
|
|
||||||
|
ltm_instance_free(instance);
|
||||||
|
ltm_template_free(template);
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
#ifndef LTM_COMMON
|
||||||
|
#define LTM_COMMON
|
||||||
|
|
||||||
|
#define LTM_RES(x) \
|
||||||
|
{ \
|
||||||
|
ltm_err res = x; \
|
||||||
|
if (res != ltm_err_ok) \
|
||||||
|
return res; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define LTM_RES2(x, e) \
|
||||||
|
{ \
|
||||||
|
ltm_err res = x; \
|
||||||
|
if (res != ltm_err_ok) { \
|
||||||
|
e; \
|
||||||
|
return res; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define LTM_MIN(x, y) ((x) < (y) ? (x) : (y))
|
||||||
|
#define LTM_MAX(x, y) ((x) > (y) ? (x) : (y))
|
||||||
|
|
||||||
|
typedef enum ltm_err {
|
||||||
|
ltm_err_ok = 0,
|
||||||
|
ltm_err_invalid_template,
|
||||||
|
ltm_err_failed_alloc,
|
||||||
|
ltm_err_failed_io,
|
||||||
|
ltm_err_not_found,
|
||||||
|
ltm_err_wrong_block_type,
|
||||||
|
ltm_err_done,
|
||||||
|
} ltm_err;
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,104 @@
|
||||||
|
#ifndef LTM_TEMPLATE
|
||||||
|
#define LTM_TEMPLATE
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "ltm/common.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a compiled template
|
||||||
|
*/
|
||||||
|
typedef struct ltm_template ltm_template;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile the given template.
|
||||||
|
*
|
||||||
|
* @param out where to store pointer to newly allocated `ltm_template`
|
||||||
|
* @param template nul-terminated string containing the template
|
||||||
|
*/
|
||||||
|
ltm_err ltm_template_compile(ltm_template **out, const char *template);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile the given template with a given length.
|
||||||
|
*
|
||||||
|
* @param out where to store pointer to newly allocated `ltm_template`
|
||||||
|
* @param template char buffer containing the template
|
||||||
|
* @param len length of the char buffer
|
||||||
|
*/
|
||||||
|
ltm_err ltm_template_compile_n(ltm_template **out, const char *template,
|
||||||
|
size_t len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Free the template instance. After freeing a template, no instances associated
|
||||||
|
* with it are safe to use.
|
||||||
|
*/
|
||||||
|
void ltm_template_free(ltm_template *template);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a specific instance of a template.
|
||||||
|
*/
|
||||||
|
typedef struct ltm_instance ltm_instance;
|
||||||
|
|
||||||
|
typedef ltm_err (*ltm_data_fn)(size_t *written, char *buf, size_t len,
|
||||||
|
void *data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new instance of the given template.
|
||||||
|
*/
|
||||||
|
ltm_err ltm_template_instantiate(ltm_instance **out,
|
||||||
|
const ltm_template *template);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Free the given instance, as well as all nested instances.
|
||||||
|
*/
|
||||||
|
void ltm_instance_free(ltm_instance *instance);
|
||||||
|
|
||||||
|
typedef enum ltm_instance_block_type {
|
||||||
|
ltm_instance_block_type_buf = 0,
|
||||||
|
ltm_instance_block_type_buf_owned,
|
||||||
|
ltm_instance_block_type_file,
|
||||||
|
ltm_instance_block_type_file_owned,
|
||||||
|
ltm_instance_block_type_nested,
|
||||||
|
ltm_instance_block_type_fn,
|
||||||
|
} ltm_instance_block_type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new variable to the template.
|
||||||
|
*/
|
||||||
|
ltm_err ltm_instance_block_add_var(ltm_instance *instance, const char *name,
|
||||||
|
ltm_instance_block_type type, void *data,
|
||||||
|
size_t len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new variable to the template whose data is provided by a data function.
|
||||||
|
*/
|
||||||
|
ltm_err ltm_instance_block_add_var_fn(ltm_instance *instance, const char *name,
|
||||||
|
ltm_data_fn fn, void *data, size_t len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new nested instance to the instance, returning a handle to the nested
|
||||||
|
* instance.
|
||||||
|
*/
|
||||||
|
ltm_err ltm_instance_block_add_nested(ltm_instance **out,
|
||||||
|
ltm_instance *instance, const char *name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the size of the resulting output of this instance.
|
||||||
|
*/
|
||||||
|
size_t ltm_instance_size(const ltm_instance *instance);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write at most `len` bytes to the given buffer.
|
||||||
|
*
|
||||||
|
* @param written outputs how many bytes were written to `buf`
|
||||||
|
* @param buf buffer to write to
|
||||||
|
* @param len length of the buffer
|
||||||
|
* @param instance instance to write
|
||||||
|
* @return `ltm_err_ok` if write was successful but there's more to be written,
|
||||||
|
* `ltm_err_done` if everything has been written successfully, or some other
|
||||||
|
* error code
|
||||||
|
*/
|
||||||
|
ltm_err ltm_instance_write(size_t *written, char *buf, size_t len,
|
||||||
|
ltm_instance *instance);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,119 @@
|
||||||
|
#ifndef LTM_TEMPLATE_INTERNAL
|
||||||
|
#define LTM_TEMPLATE_INTERNAL
|
||||||
|
|
||||||
|
#include "ltm/template.h"
|
||||||
|
|
||||||
|
typedef enum ltm_placeholder_type {
|
||||||
|
ltm_placeholder_type_invalid = 0,
|
||||||
|
ltm_placeholder_type_var,
|
||||||
|
ltm_placeholder_type_nested_start,
|
||||||
|
ltm_placeholder_type_nested_end,
|
||||||
|
} ltm_placeholder_type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal struct used when compiling templates
|
||||||
|
*/
|
||||||
|
typedef struct ltm_placeholder {
|
||||||
|
const char *start;
|
||||||
|
const char *end;
|
||||||
|
struct {
|
||||||
|
const char *s;
|
||||||
|
size_t len;
|
||||||
|
} name;
|
||||||
|
ltm_placeholder_type type;
|
||||||
|
} ltm_placeholder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type of a template block
|
||||||
|
*/
|
||||||
|
typedef enum ltm_template_block_type {
|
||||||
|
ltm_template_block_type_literal = 0,
|
||||||
|
ltm_template_block_type_var,
|
||||||
|
ltm_template_block_type_nested,
|
||||||
|
} ltm_template_block_type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A block in a template.
|
||||||
|
*/
|
||||||
|
typedef struct ltm_template_block {
|
||||||
|
ltm_template_block_type type;
|
||||||
|
struct {
|
||||||
|
void *ptr;
|
||||||
|
size_t len;
|
||||||
|
} data;
|
||||||
|
} ltm_template_block;
|
||||||
|
|
||||||
|
typedef struct ltm_template_block_name {
|
||||||
|
struct {
|
||||||
|
const char *s;
|
||||||
|
size_t len;
|
||||||
|
} name;
|
||||||
|
size_t index;
|
||||||
|
} ltm_template_block_name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a compiled template. A template consists of a list of blocks, of
|
||||||
|
* which some have names that can be indexed (e.g. variables, nested templates).
|
||||||
|
*/
|
||||||
|
struct ltm_template {
|
||||||
|
struct {
|
||||||
|
ltm_template_block *arr;
|
||||||
|
size_t len;
|
||||||
|
} blocks;
|
||||||
|
struct {
|
||||||
|
ltm_template_block_name *arr;
|
||||||
|
size_t len;
|
||||||
|
} names;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocate a new `ltm_template`.
|
||||||
|
*/
|
||||||
|
ltm_err ltm_template_init(ltm_template **out);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append a name to the template's list.
|
||||||
|
*/
|
||||||
|
ltm_err ltm_template_name_append(ltm_template *template, const char *s,
|
||||||
|
size_t len, size_t index);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append a block to the template's list.
|
||||||
|
*/
|
||||||
|
ltm_err ltm_template_block_append(ltm_template *template,
|
||||||
|
ltm_template_block_type type, void *data,
|
||||||
|
size_t len);
|
||||||
|
|
||||||
|
typedef struct ltm_instance_block {
|
||||||
|
ltm_instance_block_type type;
|
||||||
|
ltm_data_fn fn;
|
||||||
|
struct {
|
||||||
|
void *ptr;
|
||||||
|
size_t len;
|
||||||
|
} data;
|
||||||
|
struct ltm_instance_block *next;
|
||||||
|
} ltm_instance_block;
|
||||||
|
|
||||||
|
ltm_err ltm_instance_block_init(ltm_instance_block **out);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new block to the instance with the given type.
|
||||||
|
*/
|
||||||
|
ltm_err ltm_instance_block_add(ltm_instance_block **out, ltm_instance *instance,
|
||||||
|
const char *name, ltm_instance_block_type type);
|
||||||
|
|
||||||
|
struct ltm_instance {
|
||||||
|
const ltm_template *template;
|
||||||
|
struct {
|
||||||
|
ltm_instance_block *head;
|
||||||
|
ltm_instance_block *tail;
|
||||||
|
ltm_instance_block *current;
|
||||||
|
} blocks;
|
||||||
|
ltm_instance_block ***vars;
|
||||||
|
// How many bytes of the current block have been written
|
||||||
|
size_t written;
|
||||||
|
};
|
||||||
|
|
||||||
|
ltm_err ltm_instance_init(ltm_instance **out);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,350 @@
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "ltm/common.h"
|
||||||
|
#include "ltm/template.h"
|
||||||
|
#include "ltm/template_internal.h"
|
||||||
|
|
||||||
|
ltm_err ltm_instance_block_init(ltm_instance_block **out) {
|
||||||
|
ltm_instance_block *block = calloc(1, sizeof(ltm_instance_block));
|
||||||
|
|
||||||
|
if (block == NULL) {
|
||||||
|
return ltm_err_failed_alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = block;
|
||||||
|
|
||||||
|
return ltm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_err ltm_instance_init(ltm_instance **out) {
|
||||||
|
ltm_instance *instance = calloc(1, sizeof(ltm_instance));
|
||||||
|
|
||||||
|
if (instance == NULL) {
|
||||||
|
return ltm_err_failed_alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = instance;
|
||||||
|
|
||||||
|
return ltm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ltm_instance_free(ltm_instance *instance) {
|
||||||
|
if (instance->vars != NULL) {
|
||||||
|
free(instance->vars);
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_instance_block *block = instance->blocks.head;
|
||||||
|
|
||||||
|
while (block != NULL) {
|
||||||
|
switch (block->type) {
|
||||||
|
case ltm_instance_block_type_buf_owned:
|
||||||
|
free(block->data.ptr);
|
||||||
|
break;
|
||||||
|
case ltm_instance_block_type_file_owned:
|
||||||
|
fclose(block->data.ptr);
|
||||||
|
break;
|
||||||
|
case ltm_instance_block_type_nested:
|
||||||
|
ltm_instance_free(block->data.ptr);
|
||||||
|
break;
|
||||||
|
case ltm_instance_block_type_file:
|
||||||
|
case ltm_instance_block_type_fn:
|
||||||
|
case ltm_instance_block_type_buf:;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_instance_block *temp = block->next;
|
||||||
|
free(block);
|
||||||
|
block = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_err ltm_template_instantiate(ltm_instance **out,
|
||||||
|
const ltm_template *template) {
|
||||||
|
ltm_instance *instance;
|
||||||
|
LTM_RES(ltm_instance_init(&instance));
|
||||||
|
|
||||||
|
instance->template = template;
|
||||||
|
|
||||||
|
ltm_instance_block ***vars = NULL;
|
||||||
|
|
||||||
|
if (template->names.len > 0) {
|
||||||
|
vars = malloc(template->names.len * sizeof(ltm_instance_block **));
|
||||||
|
|
||||||
|
if (vars == NULL) {
|
||||||
|
ltm_instance_free(instance);
|
||||||
|
|
||||||
|
return ltm_err_failed_alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
instance->vars = vars;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t block_index = 0; block_index < template->blocks.len;
|
||||||
|
block_index++) {
|
||||||
|
ltm_template_block *template_block = &template->blocks.arr[block_index];
|
||||||
|
|
||||||
|
switch (template_block->type) {
|
||||||
|
case ltm_template_block_type_literal: {
|
||||||
|
ltm_instance_block *block;
|
||||||
|
LTM_RES2(ltm_instance_block_init(&block), ltm_instance_free(instance));
|
||||||
|
|
||||||
|
block->type = ltm_instance_block_type_buf;
|
||||||
|
block->data.ptr = template_block->data.ptr;
|
||||||
|
block->data.len = template_block->data.len;
|
||||||
|
|
||||||
|
if (instance->blocks.head == NULL) {
|
||||||
|
instance->blocks.head = block;
|
||||||
|
instance->blocks.tail = block;
|
||||||
|
instance->blocks.current = block;
|
||||||
|
} else {
|
||||||
|
instance->blocks.tail->next = block;
|
||||||
|
instance->blocks.tail = block;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case ltm_template_block_type_var:
|
||||||
|
case ltm_template_block_type_nested:
|
||||||
|
// Account for the possibility a template starts with a placeholder
|
||||||
|
*vars = instance->blocks.tail != NULL ? &instance->blocks.tail->next
|
||||||
|
: &instance->blocks.head;
|
||||||
|
vars++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = instance;
|
||||||
|
|
||||||
|
return ltm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_err ltm_instance_block_add(ltm_instance_block **out, ltm_instance *instance,
|
||||||
|
const char *name, ltm_instance_block_type type) {
|
||||||
|
const ltm_template *template = instance->template;
|
||||||
|
|
||||||
|
ltm_template_block_name *block_name = NULL;
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
while (i < template->names.len) {
|
||||||
|
block_name = &template->names.arr[i];
|
||||||
|
|
||||||
|
if (strncmp(name, block_name->name.s, block_name->name.len) == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == template->names.len) {
|
||||||
|
return ltm_err_not_found;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_template_block *template_block = &template->blocks.arr[block_name->index];
|
||||||
|
bool correct_type = false;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case ltm_instance_block_type_nested:
|
||||||
|
correct_type = template_block->type == ltm_template_block_type_nested;
|
||||||
|
break;
|
||||||
|
case ltm_instance_block_type_buf:
|
||||||
|
case ltm_instance_block_type_buf_owned:
|
||||||
|
case ltm_instance_block_type_file:
|
||||||
|
case ltm_instance_block_type_file_owned:
|
||||||
|
case ltm_instance_block_type_fn:
|
||||||
|
correct_type = template_block->type == ltm_template_block_type_var;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!correct_type) {
|
||||||
|
return ltm_err_wrong_block_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_instance_block *block;
|
||||||
|
LTM_RES(ltm_instance_block_init(&block));
|
||||||
|
|
||||||
|
block->type = type;
|
||||||
|
|
||||||
|
// Insert the block in the linked list
|
||||||
|
block->next = *instance->vars[i];
|
||||||
|
*instance->vars[i] = block;
|
||||||
|
instance->vars[i] = &block->next;
|
||||||
|
|
||||||
|
*out = block;
|
||||||
|
|
||||||
|
return ltm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_err ltm_instance_block_add_var(ltm_instance *instance, const char *name,
|
||||||
|
ltm_instance_block_type type, void *data,
|
||||||
|
size_t len) {
|
||||||
|
ltm_instance_block *block;
|
||||||
|
LTM_RES(ltm_instance_block_add(&block, instance, name, type));
|
||||||
|
|
||||||
|
block->data.ptr = data;
|
||||||
|
block->data.len = len;
|
||||||
|
|
||||||
|
return ltm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_err ltm_instance_block_add_var_fn(ltm_instance *instance, const char *name,
|
||||||
|
ltm_data_fn fn, void *data, size_t len) {
|
||||||
|
ltm_instance_block *block;
|
||||||
|
LTM_RES(ltm_instance_block_add(&block, instance, name,
|
||||||
|
ltm_instance_block_type_fn));
|
||||||
|
|
||||||
|
block->fn = fn;
|
||||||
|
block->data.ptr = data;
|
||||||
|
block->data.len = len;
|
||||||
|
|
||||||
|
return ltm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_err ltm_instance_block_add_nested(ltm_instance **out,
|
||||||
|
ltm_instance *instance,
|
||||||
|
const char *name) {
|
||||||
|
const ltm_template *template = instance->template;
|
||||||
|
|
||||||
|
ltm_template_block_name *block_name = NULL;
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
while (i < template->names.len) {
|
||||||
|
block_name = &template->names.arr[i];
|
||||||
|
|
||||||
|
if (strncmp(name, block_name->name.s, block_name->name.len) == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == template->names.len) {
|
||||||
|
return ltm_err_not_found;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_template_block *template_block = &template->blocks.arr[block_name->index];
|
||||||
|
|
||||||
|
if (template_block->type != ltm_template_block_type_nested) {
|
||||||
|
return ltm_err_wrong_block_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_instance_block *block;
|
||||||
|
LTM_RES(ltm_instance_block_init(&block));
|
||||||
|
|
||||||
|
ltm_instance *nested;
|
||||||
|
LTM_RES2(ltm_template_instantiate(&nested, template_block->data.ptr),
|
||||||
|
free(block));
|
||||||
|
|
||||||
|
block->type = ltm_instance_block_type_nested;
|
||||||
|
block->data.ptr = nested;
|
||||||
|
|
||||||
|
// We insert the block in the linked list and replace its next pointer as the
|
||||||
|
// new attachment point for this variable
|
||||||
|
block->next = *instance->vars[i];
|
||||||
|
*instance->vars[i] = block;
|
||||||
|
instance->vars[i] = &block->next;
|
||||||
|
|
||||||
|
*out = nested;
|
||||||
|
|
||||||
|
return ltm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ltm_instance_size(const ltm_instance *instance) {
|
||||||
|
size_t total = 0;
|
||||||
|
ltm_instance_block *block = instance->blocks.head;
|
||||||
|
|
||||||
|
while (block != NULL) {
|
||||||
|
switch (block->type) {
|
||||||
|
case ltm_instance_block_type_buf:
|
||||||
|
case ltm_instance_block_type_buf_owned:
|
||||||
|
case ltm_instance_block_type_file:
|
||||||
|
case ltm_instance_block_type_file_owned:
|
||||||
|
case ltm_instance_block_type_fn:
|
||||||
|
total += block->data.len;
|
||||||
|
break;
|
||||||
|
case ltm_instance_block_type_nested:
|
||||||
|
total += ltm_instance_size(block->data.ptr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
block = block->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_err ltm_instance_write(size_t *written, char *buf, size_t len,
|
||||||
|
ltm_instance *instance) {
|
||||||
|
*written = 0;
|
||||||
|
|
||||||
|
while ((*written < len) && (instance->blocks.current != NULL)) {
|
||||||
|
ltm_instance_block *current = instance->blocks.current;
|
||||||
|
|
||||||
|
switch (current->type) {
|
||||||
|
case ltm_instance_block_type_buf:
|
||||||
|
case ltm_instance_block_type_buf_owned: {
|
||||||
|
size_t cap =
|
||||||
|
LTM_MIN(current->data.len - instance->written, len - *written);
|
||||||
|
memcpy(&buf[*written], &((char *)current->data.ptr)[instance->written],
|
||||||
|
cap);
|
||||||
|
*written += cap;
|
||||||
|
instance->written += cap;
|
||||||
|
|
||||||
|
if (instance->written == current->data.len) {
|
||||||
|
instance->blocks.current = current->next;
|
||||||
|
instance->written = 0;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case ltm_instance_block_type_file:
|
||||||
|
case ltm_instance_block_type_file_owned: {
|
||||||
|
size_t cap =
|
||||||
|
LTM_MIN(current->data.len - instance->written, len - *written);
|
||||||
|
size_t read = fread(&buf[*written], 1, cap, current->data.ptr);
|
||||||
|
|
||||||
|
if ((read == 0) && (ferror(current->data.ptr) != 0)) {
|
||||||
|
return ltm_err_failed_io;
|
||||||
|
}
|
||||||
|
|
||||||
|
*written += read;
|
||||||
|
instance->written += read;
|
||||||
|
|
||||||
|
if (instance->written == current->data.len) {
|
||||||
|
instance->blocks.current = current->next;
|
||||||
|
instance->written = 0;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case ltm_instance_block_type_fn: {
|
||||||
|
size_t cap =
|
||||||
|
LTM_MIN(current->data.len - instance->written, len - *written);
|
||||||
|
size_t fn_written = 0;
|
||||||
|
LTM_RES(current->fn(&fn_written, &buf[*written], cap, current->data.ptr));
|
||||||
|
*written += fn_written;
|
||||||
|
instance->written += fn_written;
|
||||||
|
|
||||||
|
if (instance->written == current->data.len) {
|
||||||
|
instance->blocks.current = current->next;
|
||||||
|
instance->written = 0;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case ltm_instance_block_type_nested: {
|
||||||
|
size_t nested_written = 0;
|
||||||
|
ltm_err res = ltm_instance_write(&nested_written, &buf[*written],
|
||||||
|
len - *written, current->data.ptr);
|
||||||
|
*written += nested_written;
|
||||||
|
|
||||||
|
switch (res) {
|
||||||
|
case ltm_err_done:
|
||||||
|
instance->blocks.current = current->next;
|
||||||
|
instance->written = 0;
|
||||||
|
break;
|
||||||
|
case ltm_err_ok:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance->blocks.current == NULL ? ltm_err_done : ltm_err_ok;
|
||||||
|
}
|
|
@ -0,0 +1,93 @@
|
||||||
|
#include "ltm/template.h"
|
||||||
|
#include "ltm/template_internal.h"
|
||||||
|
|
||||||
|
ltm_err ltm_template_init(ltm_template **out) {
|
||||||
|
ltm_template *template = calloc(1, sizeof(ltm_template));
|
||||||
|
|
||||||
|
if (template == NULL) {
|
||||||
|
return ltm_err_failed_alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = template;
|
||||||
|
|
||||||
|
return ltm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ltm_template_free(ltm_template *template) {
|
||||||
|
if (template->blocks.len > 0) {
|
||||||
|
for (size_t i = 0; i < template->blocks.len; i++) {
|
||||||
|
ltm_template_block *block = &template->blocks.arr[i];
|
||||||
|
|
||||||
|
switch (block->type) {
|
||||||
|
case ltm_template_block_type_nested:
|
||||||
|
ltm_template_free(block->data.ptr);
|
||||||
|
break;
|
||||||
|
case ltm_template_block_type_var:
|
||||||
|
case ltm_template_block_type_literal:;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(template->blocks.arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (template->names.len > 0) {
|
||||||
|
free(template->names.arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(template);
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_err ltm_template_name_append(ltm_template *template, const char *s,
|
||||||
|
size_t len, size_t index) {
|
||||||
|
ltm_template_block_name *names =
|
||||||
|
realloc(template->names.arr,
|
||||||
|
(template->names.len + 1) * sizeof(ltm_template_block_name));
|
||||||
|
|
||||||
|
if (names == NULL) {
|
||||||
|
return ltm_err_failed_alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_template_block_name *name = &names[template->names.len];
|
||||||
|
|
||||||
|
name->name.s = s;
|
||||||
|
name->name.len = len;
|
||||||
|
name->index = index;
|
||||||
|
|
||||||
|
template->names.arr = names;
|
||||||
|
template->names.len++;
|
||||||
|
|
||||||
|
return ltm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_err ltm_template_block_append(ltm_template *template,
|
||||||
|
ltm_template_block_type type, void *data,
|
||||||
|
size_t len) {
|
||||||
|
ltm_template_block *blocks =
|
||||||
|
realloc(template->blocks.arr,
|
||||||
|
(template->blocks.len + 1) * sizeof(ltm_template_block));
|
||||||
|
|
||||||
|
if (blocks == NULL) {
|
||||||
|
return ltm_err_failed_alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_template_block *block = &blocks[template->blocks.len];
|
||||||
|
|
||||||
|
block->type = type;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case ltm_template_block_type_literal:
|
||||||
|
block->data.ptr = data;
|
||||||
|
block->data.len = len;
|
||||||
|
break;
|
||||||
|
case ltm_template_block_type_nested:
|
||||||
|
block->data.ptr = data;
|
||||||
|
break;
|
||||||
|
// For the other cases, we explicitely ignore the data and len arguments
|
||||||
|
default:;
|
||||||
|
}
|
||||||
|
|
||||||
|
template->blocks.arr = blocks;
|
||||||
|
template->blocks.len++;
|
||||||
|
|
||||||
|
return ltm_err_ok;
|
||||||
|
}
|
|
@ -0,0 +1,198 @@
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "ltm/common.h"
|
||||||
|
#include "ltm/template.h"
|
||||||
|
#include "ltm/template_internal.h"
|
||||||
|
|
||||||
|
static bool ltm_template_next_placeholder(ltm_placeholder *ph, const char *s,
|
||||||
|
size_t len) {
|
||||||
|
if (len == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ph->start = memchr(s, '{', len - 1);
|
||||||
|
|
||||||
|
if ((ph->start == NULL) || (ph->start[1] != '{')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t new_len = len - (ph->start - s);
|
||||||
|
|
||||||
|
// A template can never be valid without at least 5 characters
|
||||||
|
if (new_len < 5) {
|
||||||
|
ph->type = ltm_placeholder_type_invalid;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ph->end = memchr(ph->start + 2, '}', new_len - 3);
|
||||||
|
|
||||||
|
// Non-terminated placeholders aren't valid
|
||||||
|
if ((ph->end == NULL) || (ph->end[1] != '}')) {
|
||||||
|
ph->type = ltm_placeholder_type_invalid;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// End should point to final character
|
||||||
|
ph->end++;
|
||||||
|
|
||||||
|
// Parse the words
|
||||||
|
ph->name.s = ph->start + 2;
|
||||||
|
|
||||||
|
while ((*ph->name.s == ' ') && (ph->name.s != ph->end - 1)) {
|
||||||
|
ph->name.s++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Placeholder is empty
|
||||||
|
if (ph->name.s == ph->end - 1) {
|
||||||
|
ph->type = ltm_placeholder_type_invalid;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *ident = ph->name.s;
|
||||||
|
|
||||||
|
while ((*ident != ' ') && (ident != ph->end - 1)) {
|
||||||
|
ident++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ph->name.len = ident - ph->name.s;
|
||||||
|
|
||||||
|
// Skip whitespace over to next word
|
||||||
|
while ((*ident == ' ') && (ident != ph->end - 1)) {
|
||||||
|
ident++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ident == ph->end - 1) {
|
||||||
|
ph->type = ltm_placeholder_type_var;
|
||||||
|
} else {
|
||||||
|
// Further parse the identifier
|
||||||
|
const char *temp = ident;
|
||||||
|
|
||||||
|
while ((*temp != ' ') && (temp != ph->end - 1)) {
|
||||||
|
temp++;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ident_len = temp - ident;
|
||||||
|
|
||||||
|
if (strncmp("start", ident, ident_len) == 0) {
|
||||||
|
ph->type = ltm_placeholder_type_nested_start;
|
||||||
|
} else if (strncmp("end", ident, ident_len) == 0) {
|
||||||
|
ph->type = ltm_placeholder_type_nested_end;
|
||||||
|
} else {
|
||||||
|
ph->type = ltm_placeholder_type_invalid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_err ltm_template_compile_n(ltm_template **out, const char *s, size_t len) {
|
||||||
|
ltm_template *template;
|
||||||
|
LTM_RES(ltm_template_init(&template));
|
||||||
|
|
||||||
|
ltm_placeholder ph;
|
||||||
|
bool in_nested = false;
|
||||||
|
const char *nested_start = NULL, *nested_name = NULL;
|
||||||
|
size_t nested_depth = 0, nested_name_len = 0;
|
||||||
|
|
||||||
|
size_t cur_nested_depth = 0;
|
||||||
|
|
||||||
|
while (ltm_template_next_placeholder(&ph, s, len)) {
|
||||||
|
// Add part before placeholder as literal
|
||||||
|
if (!in_nested && (ph.start != s)) {
|
||||||
|
LTM_RES2(ltm_template_block_append(template,
|
||||||
|
ltm_template_block_type_literal,
|
||||||
|
(void *)s, ph.start - s),
|
||||||
|
ltm_template_free(template));
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (ph.type) {
|
||||||
|
// Invalid placeholders can be detected as early as possible
|
||||||
|
case ltm_placeholder_type_invalid:
|
||||||
|
ltm_template_free(template);
|
||||||
|
|
||||||
|
return ltm_err_invalid_template;
|
||||||
|
case ltm_placeholder_type_var:
|
||||||
|
if (!in_nested) {
|
||||||
|
LTM_RES2(ltm_template_block_append(
|
||||||
|
template, ltm_template_block_type_var, NULL, 0),
|
||||||
|
ltm_template_free(template));
|
||||||
|
LTM_RES2(ltm_template_name_append(template, ph.name.s, ph.name.len,
|
||||||
|
template->blocks.len - 1),
|
||||||
|
ltm_template_free(template));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ltm_placeholder_type_nested_start:
|
||||||
|
if (!in_nested) {
|
||||||
|
nested_start = ph.end + 1;
|
||||||
|
nested_depth = cur_nested_depth;
|
||||||
|
nested_name = ph.name.s;
|
||||||
|
nested_name_len = ph.name.len;
|
||||||
|
in_nested = true;
|
||||||
|
}
|
||||||
|
cur_nested_depth++;
|
||||||
|
break;
|
||||||
|
case ltm_placeholder_type_nested_end:
|
||||||
|
// This means there's more nested end placeholders than nested starts
|
||||||
|
if (cur_nested_depth == 0) {
|
||||||
|
ltm_template_free(template);
|
||||||
|
|
||||||
|
return ltm_err_invalid_template;
|
||||||
|
}
|
||||||
|
|
||||||
|
cur_nested_depth--;
|
||||||
|
|
||||||
|
if (in_nested && (cur_nested_depth == nested_depth)) {
|
||||||
|
size_t nested_len = ph.start - nested_start;
|
||||||
|
ltm_template *nested_template;
|
||||||
|
LTM_RES2(
|
||||||
|
ltm_template_compile_n(&nested_template, nested_start, nested_len),
|
||||||
|
ltm_template_free(template));
|
||||||
|
LTM_RES(ltm_template_block_append(
|
||||||
|
template, ltm_template_block_type_nested, nested_template, 0));
|
||||||
|
LTM_RES2(ltm_template_name_append(template, nested_name,
|
||||||
|
nested_name_len,
|
||||||
|
template->blocks.len - 1),
|
||||||
|
ltm_template_free(template));
|
||||||
|
|
||||||
|
in_nested = false;
|
||||||
|
}
|
||||||
|
// We encountered a nested end without a start
|
||||||
|
else if (!in_nested) {
|
||||||
|
ltm_template_free(template);
|
||||||
|
|
||||||
|
return ltm_err_invalid_template;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
len -= ph.end + 1 - s;
|
||||||
|
s = ph.end + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unfinished nested
|
||||||
|
if (in_nested) {
|
||||||
|
ltm_template_free(template);
|
||||||
|
|
||||||
|
return ltm_err_invalid_template;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add remaining trailing literal
|
||||||
|
if (len > 0) {
|
||||||
|
LTM_RES2(ltm_template_block_append(
|
||||||
|
template, ltm_template_block_type_literal, (void *)s, len),
|
||||||
|
ltm_template_free(template));
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = template;
|
||||||
|
|
||||||
|
return ltm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_err ltm_template_compile(ltm_template **out, const char *template) {
|
||||||
|
return ltm_template_compile_n(out, template, strlen(template));
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
|
#include "ltm/common.h"
|
||||||
|
#include "ltm/template.h"
|
||||||
|
#include "ltm/template_internal.h"
|
||||||
|
|
||||||
|
void test_single_placeholder() {
|
||||||
|
const char *s = "Hello, {{ world }}!";
|
||||||
|
|
||||||
|
ltm_template *template;
|
||||||
|
TEST_ASSERT(ltm_template_compile(&template, s) == ltm_err_ok);
|
||||||
|
|
||||||
|
ltm_instance *instance;
|
||||||
|
TEST_CHECK(ltm_template_instantiate(&instance, template) == ltm_err_ok);
|
||||||
|
|
||||||
|
TEST_CHECK(ltm_instance_block_add_var(instance, "world", ltm_instance_block_type_buf, "World", 5) == ltm_err_ok);
|
||||||
|
|
||||||
|
TEST_CHECK(ltm_instance_size(instance) == 13);
|
||||||
|
|
||||||
|
char buf[13];
|
||||||
|
|
||||||
|
size_t written = 0;
|
||||||
|
TEST_CHECK(ltm_instance_write(&written, buf, 5, instance) == ltm_err_ok);
|
||||||
|
TEST_CHECK(written == 5);
|
||||||
|
|
||||||
|
written = 0;
|
||||||
|
TEST_CHECK(ltm_instance_write(&written, buf + 5, 5, instance) == ltm_err_ok);
|
||||||
|
TEST_CHECK(written == 5);
|
||||||
|
|
||||||
|
written = 0;
|
||||||
|
TEST_CHECK(ltm_instance_write(&written, buf + 10, 5, instance) == ltm_err_done);
|
||||||
|
TEST_CHECK(written == 3);
|
||||||
|
|
||||||
|
TEST_CHECK(strncmp(buf, "Hello, World!", 13) == 0);
|
||||||
|
TEST_DUMP("buf", buf, 13);
|
||||||
|
|
||||||
|
ltm_instance_free(instance);
|
||||||
|
ltm_template_free(template);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_LIST = {
|
||||||
|
{ "instance single placeholder", test_single_placeholder },
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
|
@ -0,0 +1,118 @@
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
|
#include "ltm/common.h"
|
||||||
|
#include "ltm/template.h"
|
||||||
|
#include "ltm/template_internal.h"
|
||||||
|
|
||||||
|
void test_single_placeholder() {
|
||||||
|
const char *s = "Hello, {{ world }}";
|
||||||
|
|
||||||
|
ltm_template *template;
|
||||||
|
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_ok);
|
||||||
|
|
||||||
|
TEST_CHECK(template->blocks.len == 2);
|
||||||
|
|
||||||
|
TEST_CHECK(template->blocks.arr[0].type == ltm_template_block_type_literal);
|
||||||
|
TEST_CHECK(template->blocks.arr[0].data.ptr == s);
|
||||||
|
TEST_CHECK(template->blocks.arr[0].data.len == 7);
|
||||||
|
|
||||||
|
TEST_CHECK(template->blocks.arr[1].type == ltm_template_block_type_var);
|
||||||
|
|
||||||
|
TEST_CHECK(template->names.len == 1);
|
||||||
|
|
||||||
|
TEST_CHECK(template->names.arr[0].name.s == s + 10);
|
||||||
|
TEST_CHECK(template->names.arr[0].name.len == 5);
|
||||||
|
TEST_CHECK(template->names.arr[0].index == 1);
|
||||||
|
|
||||||
|
ltm_template_free(template);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_single_placeholder_trailing() {
|
||||||
|
const char *s = "Hello, {{ world }}!";
|
||||||
|
|
||||||
|
ltm_template *template;
|
||||||
|
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_ok);
|
||||||
|
|
||||||
|
TEST_CHECK(template->blocks.len == 3);
|
||||||
|
|
||||||
|
TEST_CHECK(template->blocks.arr[0].type == ltm_template_block_type_literal);
|
||||||
|
TEST_CHECK(template->blocks.arr[0].data.ptr == s);
|
||||||
|
TEST_CHECK(template->blocks.arr[0].data.len == 7);
|
||||||
|
|
||||||
|
TEST_CHECK(template->blocks.arr[1].type == ltm_template_block_type_var);
|
||||||
|
|
||||||
|
TEST_CHECK(template->blocks.arr[2].type == ltm_template_block_type_literal);
|
||||||
|
TEST_CHECK(template->blocks.arr[2].data.ptr == s + 18);
|
||||||
|
TEST_CHECK(template->blocks.arr[2].data.len == 1);
|
||||||
|
|
||||||
|
TEST_CHECK(template->names.len == 1);
|
||||||
|
|
||||||
|
TEST_CHECK(template->names.arr[0].name.s == s + 10);
|
||||||
|
TEST_CHECK(template->names.arr[0].name.len == 5);
|
||||||
|
TEST_CHECK(template->names.arr[0].index == 1);
|
||||||
|
|
||||||
|
ltm_template_free(template);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_single_nested() {
|
||||||
|
const char *s = "abc {{ l start }}some content {{ var }} {{ l end }}";
|
||||||
|
|
||||||
|
ltm_template *template;
|
||||||
|
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_ok);
|
||||||
|
|
||||||
|
TEST_CHECK(template->names.len == 1);
|
||||||
|
TEST_CHECK(template->names.arr[0].name.s == s + 7);
|
||||||
|
TEST_CHECK(template->names.arr[0].name.len == 1);
|
||||||
|
TEST_CHECK(template->names.arr[0].index == 1);
|
||||||
|
|
||||||
|
TEST_CHECK(template->blocks.len == 2);
|
||||||
|
|
||||||
|
TEST_CHECK(template->blocks.arr[0].type == ltm_template_block_type_literal);
|
||||||
|
TEST_CHECK(template->blocks.arr[0].data.ptr == s);
|
||||||
|
TEST_CHECK(template->blocks.arr[0].data.len == 4);
|
||||||
|
|
||||||
|
TEST_CHECK(template->blocks.arr[1].type == ltm_template_block_type_nested);
|
||||||
|
ltm_template *nested_template = template->blocks.arr[1].data.ptr;
|
||||||
|
|
||||||
|
TEST_CHECK(nested_template->blocks.len == 3);
|
||||||
|
TEST_CHECK(nested_template->blocks.arr[0].type == ltm_template_block_type_literal);
|
||||||
|
TEST_CHECK(nested_template->blocks.arr[0].data.ptr == s + 17);
|
||||||
|
TEST_CHECK(nested_template->blocks.arr[0].data.len == 13);
|
||||||
|
TEST_CHECK(nested_template->blocks.arr[1].type == ltm_template_block_type_var);
|
||||||
|
TEST_CHECK(nested_template->blocks.arr[2].type == ltm_template_block_type_literal);
|
||||||
|
TEST_CHECK(nested_template->blocks.arr[2].data.ptr == s + 39);
|
||||||
|
TEST_CHECK(nested_template->blocks.arr[2].data.len == 1);
|
||||||
|
|
||||||
|
TEST_CHECK(nested_template->names.len == 1);
|
||||||
|
TEST_CHECK(nested_template->names.arr[0].name.s == s + 33);
|
||||||
|
TEST_CHECK(nested_template->names.arr[0].name.len == 3);
|
||||||
|
TEST_CHECK(nested_template->names.arr[0].index == 1);
|
||||||
|
|
||||||
|
ltm_template_free(template);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_unclosed_placeholder() {
|
||||||
|
ltm_template *template;
|
||||||
|
|
||||||
|
const char *s = "abc {{ var }";
|
||||||
|
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_invalid_template);
|
||||||
|
|
||||||
|
s = "abc {{ var ";
|
||||||
|
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_invalid_template);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_unclosed_nested() {
|
||||||
|
ltm_template *template;
|
||||||
|
|
||||||
|
const char *s = "abc {{ var start }} {{ hello }}";
|
||||||
|
TEST_CHECK(ltm_template_compile(&template, s) == ltm_err_invalid_template);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_LIST = {
|
||||||
|
{ "template single placeholder", test_single_placeholder },
|
||||||
|
{ "template single placeholder trailing", test_single_placeholder_trailing },
|
||||||
|
{ "template single nested", test_single_nested },
|
||||||
|
{ "template unclosed placeholder", test_unclosed_placeholder },
|
||||||
|
{ "template unclosed nested", test_unclosed_nested },
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
File diff suppressed because it is too large
Load Diff
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include "lnm/common.h"
|
#include "lnm/common.h"
|
||||||
#include "lsm/store.h"
|
#include "lsm/store.h"
|
||||||
|
#include "ltm/template.h"
|
||||||
|
|
||||||
#include "lander.h"
|
#include "lander.h"
|
||||||
|
|
||||||
|
@ -29,6 +30,12 @@ void lander_ctx_reset(lander_ctx *ctx) {
|
||||||
|
|
||||||
ctx->entry = NULL;
|
ctx->entry = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ctx->instance != NULL) {
|
||||||
|
ltm_instance_free(ctx->instance);
|
||||||
|
|
||||||
|
ctx->instance = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void lander_ctx_free(lander_ctx *ctx) { free(ctx); }
|
void lander_ctx_free(lander_ctx *ctx) { free(ctx); }
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include "lsm/store.h"
|
#include "lsm/store.h"
|
||||||
|
|
||||||
#include "lander.h"
|
#include "lander.h"
|
||||||
|
#include "ltm/template.h"
|
||||||
|
|
||||||
static const char index_page[] =
|
static const char index_page[] =
|
||||||
"<!DOCTYPE html>\n"
|
"<!DOCTYPE html>\n"
|
||||||
|
@ -19,7 +20,7 @@ static const char index_page[] =
|
||||||
" </body>\n"
|
" </body>\n"
|
||||||
"</html>\n";
|
"</html>\n";
|
||||||
|
|
||||||
lnm_http_step_err lander_get_index(lnm_http_conn *conn) {
|
lnm_http_step_err lander_get_root(lnm_http_conn *conn) {
|
||||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||||
|
|
||||||
lnm_http_res_body_set_buf(&ctx->res, (char *)index_page,
|
lnm_http_res_body_set_buf(&ctx->res, (char *)index_page,
|
||||||
|
@ -72,13 +73,43 @@ lnm_err lander_entry_data_streamer(uint64_t *written, char *buf,
|
||||||
return lnm_err_ok;
|
return lnm_err_ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
lnm_http_step_err lander_get_paste(lnm_http_conn *conn) {
|
lnm_err lander_template_streamer(size_t *written, char *buf,
|
||||||
|
lnm_http_conn *conn, uint64_t offset,
|
||||||
|
uint64_t len) {
|
||||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||||
lander_ctx *c_ctx = ctx->c;
|
lander_ctx *c_ctx = ctx->c;
|
||||||
|
|
||||||
lnm_http_res_body_set_fn(&ctx->res, lander_entry_data_streamer,
|
// TODO respect offset variable
|
||||||
|
|
||||||
|
ltm_instance_write(written, buf, len, c_ctx->instance);
|
||||||
|
|
||||||
|
return lnm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
ltm_err lander_data_to_template(size_t *written, char *buf, size_t len,
|
||||||
|
void *data) {
|
||||||
|
lsm_entry_handle *entry = data;
|
||||||
|
|
||||||
|
lsm_entry_data_read(written, buf, entry, len);
|
||||||
|
|
||||||
|
return ltm_err_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
lnm_http_step_err lander_get_paste(lnm_http_conn *conn) {
|
||||||
|
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||||
|
lander_gctx *c_gctx = ctx->g->c;
|
||||||
|
lander_ctx *c_ctx = ctx->c;
|
||||||
|
|
||||||
|
ltm_template_instantiate(&c_ctx->instance, c_gctx->templates.paste);
|
||||||
|
/* ltm_instance_block_add_var(c_ctx->instance, ltm_instance_block_type_buf,
|
||||||
|
* lsm_str_ptr(c_ctx->entry)) */
|
||||||
|
ltm_instance_block_add_var_fn(c_ctx->instance, "paste",
|
||||||
|
lander_data_to_template, c_ctx->entry,
|
||||||
lsm_entry_data_len(c_ctx->entry));
|
lsm_entry_data_len(c_ctx->entry));
|
||||||
lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/plain",
|
|
||||||
|
lnm_http_res_body_set_fn(&ctx->res, lander_template_streamer,
|
||||||
|
ltm_instance_size(c_ctx->instance));
|
||||||
|
lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/html",
|
||||||
false);
|
false);
|
||||||
|
|
||||||
return lnm_http_step_err_done;
|
return lnm_http_step_err_done;
|
||||||
|
@ -112,6 +143,93 @@ lnm_http_step_err lander_get_file(lnm_http_conn *conn) {
|
||||||
return lnm_http_step_err_done;
|
return lnm_http_step_err_done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lnm_http_step_err lander_get_index(lnm_http_conn *conn) {
|
||||||
|
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||||
|
lander_gctx *c_gctx = ctx->g->c;
|
||||||
|
lander_ctx *c_ctx = ctx->c;
|
||||||
|
|
||||||
|
ltm_template_instantiate(&c_ctx->instance, c_gctx->templates.index);
|
||||||
|
|
||||||
|
const lsm_str *key;
|
||||||
|
lsm_entry_key(&key, c_ctx->entry);
|
||||||
|
|
||||||
|
ltm_instance_block_add_var(c_ctx->instance, "title_key",
|
||||||
|
ltm_instance_block_type_buf,
|
||||||
|
(void *)lsm_str_ptr(key), lsm_str_len(key));
|
||||||
|
|
||||||
|
lsm_str *prefix;
|
||||||
|
lsm_str_init_copy_n(&prefix, lsm_str_ptr(key), lsm_str_len(key));
|
||||||
|
lsm_str_append_c(prefix, "_", 1);
|
||||||
|
|
||||||
|
uint64_t prefix_len = lsm_str_len(prefix);
|
||||||
|
|
||||||
|
lsm_store_iterator *iter;
|
||||||
|
lsm_store_iter(&iter, c_gctx->store, prefix);
|
||||||
|
|
||||||
|
lsm_entry_handle *handle;
|
||||||
|
while (lsm_store_iter_next_read(&handle, iter) == lsm_error_ok) {
|
||||||
|
const lsm_str *entry_key;
|
||||||
|
lsm_entry_key(&entry_key, handle);
|
||||||
|
|
||||||
|
lander_entry_type t;
|
||||||
|
lsm_entry_attr_get_uint8_t((uint8_t *)&t, handle,
|
||||||
|
lander_attr_type_entry_type);
|
||||||
|
|
||||||
|
ltm_instance *nested;
|
||||||
|
|
||||||
|
switch (t) {
|
||||||
|
case lander_entry_type_redirect:
|
||||||
|
ltm_instance_block_add_nested(&nested, c_ctx->instance, "redirect");
|
||||||
|
|
||||||
|
ltm_instance_block_add_var(
|
||||||
|
nested, "full_key", ltm_instance_block_type_buf,
|
||||||
|
(void *)lsm_str_ptr(entry_key), lsm_str_len(entry_key));
|
||||||
|
ltm_instance_block_add_var(nested, "short_key",
|
||||||
|
ltm_instance_block_type_buf,
|
||||||
|
(void *)(lsm_str_ptr(entry_key) + prefix_len),
|
||||||
|
lsm_str_len(entry_key) - prefix_len);
|
||||||
|
break;
|
||||||
|
case lander_entry_type_file:
|
||||||
|
case lander_entry_type_paste:
|
||||||
|
ltm_instance_block_add_nested(&nested, c_ctx->instance,
|
||||||
|
t == lander_entry_type_file ? "file"
|
||||||
|
: "paste");
|
||||||
|
|
||||||
|
ltm_instance_block_add_var(
|
||||||
|
nested, "full_key", ltm_instance_block_type_buf,
|
||||||
|
(void *)lsm_str_ptr(entry_key), lsm_str_len(entry_key));
|
||||||
|
|
||||||
|
// Add the filename as the link name if known, or just the key otherwise
|
||||||
|
lsm_str *filename;
|
||||||
|
if (lsm_entry_attr_get(&filename, handle, lander_attr_type_file_name) ==
|
||||||
|
lsm_error_ok) {
|
||||||
|
ltm_instance_block_add_var(
|
||||||
|
nested, "filename", ltm_instance_block_type_buf,
|
||||||
|
(void *)lsm_str_ptr(filename), lsm_str_len(filename));
|
||||||
|
} else {
|
||||||
|
ltm_instance_block_add_var(
|
||||||
|
nested, "filename", ltm_instance_block_type_buf,
|
||||||
|
(void *)(lsm_str_ptr(entry_key) + prefix_len),
|
||||||
|
lsm_str_len(entry_key) - prefix_len);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO this should be done after writing the template
|
||||||
|
lsm_entry_close(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
lnm_http_res_body_set_fn(&ctx->res, lander_template_streamer,
|
||||||
|
ltm_instance_size(c_ctx->instance));
|
||||||
|
lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/html",
|
||||||
|
false);
|
||||||
|
|
||||||
|
lsm_store_iter_free(iter);
|
||||||
|
lsm_str_free(prefix);
|
||||||
|
|
||||||
|
return lnm_http_step_err_done;
|
||||||
|
}
|
||||||
|
|
||||||
lnm_http_step_err lander_get_entry(lnm_http_conn *conn) {
|
lnm_http_step_err lander_get_entry(lnm_http_conn *conn) {
|
||||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||||
lander_ctx *c_ctx = ctx->c;
|
lander_ctx *c_ctx = ctx->c;
|
||||||
|
@ -153,6 +271,9 @@ lnm_http_step_err lander_get_entry(lnm_http_conn *conn) {
|
||||||
case lander_entry_type_file:
|
case lander_entry_type_file:
|
||||||
res = lander_get_file(conn);
|
res = lander_get_file(conn);
|
||||||
break;
|
break;
|
||||||
|
case lander_entry_type_index:
|
||||||
|
res = lander_get_index(conn);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|
|
@ -124,3 +124,21 @@ lnm_http_step_err lander_post_file(lnm_http_conn *conn) {
|
||||||
|
|
||||||
return lnm_http_step_err_done;
|
return lnm_http_step_err_done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lnm_http_step_err lander_post_index(lnm_http_conn *conn) {
|
||||||
|
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||||
|
lander_ctx *c_ctx = ctx->c;
|
||||||
|
|
||||||
|
if (!lander_insert_entry(ctx)) {
|
||||||
|
return lnm_http_step_err_res;
|
||||||
|
}
|
||||||
|
|
||||||
|
lsm_entry_attr_insert_uint8_t(c_ctx->entry, lander_attr_type_entry_type,
|
||||||
|
lander_entry_type_index);
|
||||||
|
/* lander_header_to_attr(ctx, "X-Lander-Content-Type", */
|
||||||
|
/* lander_attr_type_content_type); */
|
||||||
|
/* lander_header_to_attr(ctx, "X-Lander-Filename",
|
||||||
|
* lander_attr_type_file_name); */
|
||||||
|
|
||||||
|
return lnm_http_step_err_done;
|
||||||
|
}
|
||||||
|
|
48
src/main.c
48
src/main.c
|
@ -4,11 +4,46 @@
|
||||||
|
|
||||||
#include "lnm/http/loop.h"
|
#include "lnm/http/loop.h"
|
||||||
#include "lnm/log.h"
|
#include "lnm/log.h"
|
||||||
|
#include "ltm/template.h"
|
||||||
|
|
||||||
#include "lander.h"
|
#include "lander.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
const char *lander_server = "lander/" LANDER_VERSION;
|
const char *lander_server = "lander/" LANDER_VERSION;
|
||||||
|
const char *paste_template =
|
||||||
|
"<!doctype html>\n"
|
||||||
|
"<head><link rel=\"stylesheet\" "
|
||||||
|
"href=\"https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/"
|
||||||
|
"styles/stackoverflow-dark.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><h1>{{ header }}</h1><pre><code>{{ paste }}</code></pre></body>";
|
||||||
|
|
||||||
|
const char *index_template =
|
||||||
|
"<!doctype html>\n"
|
||||||
|
"<body>\n"
|
||||||
|
"<h1>Index /{{ title_key }}</h1>\n"
|
||||||
|
"<h2>Redirects</h2>\n"
|
||||||
|
"<ul>\n"
|
||||||
|
"{{ redirect start }}\n"
|
||||||
|
"<li><a href=\"/{{ full_key }}\">{{ short_key }}</a></li>\n"
|
||||||
|
"{{ redirect end }}\n"
|
||||||
|
"</ul>\n"
|
||||||
|
"<h2>Files</h2>"
|
||||||
|
"<ul>\n"
|
||||||
|
"{{ file start }}\n"
|
||||||
|
"<li><a href=\"/{{ full_key }}\">{{ filename }}</a></li>\n"
|
||||||
|
"{{ file end }}\n"
|
||||||
|
"</ul>\n"
|
||||||
|
"<h2>Pastes</h2>\n"
|
||||||
|
"<ul>\n"
|
||||||
|
"{{ paste start }}\n"
|
||||||
|
"<li><a href=\"/{{ full_key }}\">{{ filename }}</a></li>\n"
|
||||||
|
"{{ paste end }}\n"
|
||||||
|
"</ul>\n"
|
||||||
|
"</body>\n";
|
||||||
|
|
||||||
lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) {
|
lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) {
|
||||||
lnm_http_loop *hl;
|
lnm_http_loop *hl;
|
||||||
|
@ -20,7 +55,7 @@ lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) {
|
||||||
lnm_http_loop_set_api_key(hl, api_key);
|
lnm_http_loop_set_api_key(hl, api_key);
|
||||||
lnm_http_loop_set_server(hl, lander_server);
|
lnm_http_loop_set_server(hl, lander_server);
|
||||||
|
|
||||||
lnm_http_step_init(&step, lander_get_index);
|
lnm_http_step_init(&step, lander_get_root);
|
||||||
lnm_http_route_init_literal(&route, lnm_http_method_get, "/", step);
|
lnm_http_route_init_literal(&route, lnm_http_method_get, "/", step);
|
||||||
lnm_http_loop_route_add(hl, route);
|
lnm_http_loop_route_add(hl, route);
|
||||||
|
|
||||||
|
@ -50,6 +85,14 @@ lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) {
|
||||||
lnm_http_step_append(&step, step, lander_stream_body_to_entry);
|
lnm_http_step_append(&step, step, lander_stream_body_to_entry);
|
||||||
lnm_http_loop_route_add(hl, route);
|
lnm_http_loop_route_add(hl, route);
|
||||||
|
|
||||||
|
lnm_http_step_init(&step, lnm_http_loop_step_auth);
|
||||||
|
lnm_http_route_init_regex(&route, lnm_http_method_post, "^/i(l?)/([^/]+)$", 2,
|
||||||
|
step);
|
||||||
|
lnm_http_step_append(&step, step, lander_post_index);
|
||||||
|
/* lnm_http_step_append(&step, step, lnm_http_loop_step_body_to_buf); */
|
||||||
|
/* lnm_http_step_append(&step, step, lander_post_redirect_body_to_attr); */
|
||||||
|
lnm_http_loop_route_add(hl, route);
|
||||||
|
|
||||||
lnm_http_step_init(&step, lnm_http_loop_step_auth);
|
lnm_http_step_init(&step, lnm_http_loop_step_auth);
|
||||||
lnm_http_route_init_regex(&route, lnm_http_method_delete, "^/([^/]+)$", 1,
|
lnm_http_route_init_regex(&route, lnm_http_method_delete, "^/([^/]+)$", 1,
|
||||||
step);
|
step);
|
||||||
|
@ -92,6 +135,9 @@ int main() {
|
||||||
lander_gctx *c_gctx = lander_gctx_init();
|
lander_gctx *c_gctx = lander_gctx_init();
|
||||||
c_gctx->data_dir = data_dir_s;
|
c_gctx->data_dir = data_dir_s;
|
||||||
|
|
||||||
|
ltm_template_compile(&c_gctx->templates.paste, paste_template);
|
||||||
|
ltm_template_compile(&c_gctx->templates.index, index_template);
|
||||||
|
|
||||||
lsm_str *data_dir;
|
lsm_str *data_dir;
|
||||||
lsm_str_init_copy(&data_dir, (char *)data_dir_s);
|
lsm_str_init_copy(&data_dir, (char *)data_dir_s);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue