sml-c is WIP, expect bugs and unexpected behavior.
sml-c is an implementation of SimpleML by Stefan John / Stenway.
just like any single-header library:
// in ONE .c file
#define SML_IMPL
#include "sml.h"
// everywhere else
#include "sml.h"
sml-c provides a simple api for loading sml files. the basic program structure looks like this:
#define SML_IMPL
#include "sml.h"
int main() {
sml_document_t doc;
// load file from memory into the sml_document_t data structure
sml_load(&doc, "example.sml");
// do whatever you want with doc
// free all memory associated with data structure
sml_unload(&doc);
return 0;
}
sml-c expects you to traverse the document data structure it provides yourself. the sml_print
functions are short and clean examples for writing code to traverse the document and parse data, see sml.h
.
populate an sml_document_t with the sml file at filename.
frees all memory associated with the sml_document_t.
prints SML tree as valid SML to stdout.
prints SML tree as valid SML to a FILE *
.
data structures are generally linked list based, this macro just removes the linked list boilerplate. if you're not a fan of metaprogramming, it's unnecessary.
examples of usage:
// assume this exists and is valid for the sake of the example
sml_element_t *root = ...;
// traversing sub elements of an element
sml_element_t *element;
SML_FOREACH(element, root->elements)
/* do stuff */;
// traversing attributes of an element
sml_attribute_t *attrib;
SML_FOREACH(attrib, root->attributes)
/* do stuff */;
// traversing values of an attribute
sml_attribute_t *attrib = ...; // assuming this is valid
sml_value_t *value;
SML_FOREACH(value, attrib->values)
/* do stuff */;
for memory and stack size concerns, sml-c provides macros to change certain settings.
// configure before #including your SML implementation
#define SML_IMPL
#define SML_MALLOC(size) my_custom_malloc(size)
#include "sml.h"
4096 by default. when reading in a file with sml_load
, this is the size of the char array buffer.
4096 by default. when allocating a new page for the sml_document allocator, this is the number of bytes allocated. if memory usage is higher than expected, lowering this value will typically result in less maximum memory usage but more SML_MALLOC
calls.
256 by default. this is the number of tokens sml_parse stores per line. if one of your attributes has more than 255 values, increase this value to avoid an invalid write.
256 by default. this is the number of nested elements sml_parse stores. if you have more than 256 levels of nesting, increase this value to avoid an invalid write.
malloc
, realloc
, and free
by default. sml-c will still use its page allocation model if these are redefined, but if you have your own custom allocator you would like to use below that, go ahead and redefine these.