michaelrsweet / mxml

Tiny XML library.
https://www.msweet.org/mxml
Apache License 2.0
426 stars 157 forks source link

Add mxmlOptions APIs #312

Closed michaelrsweet closed 1 month ago

michaelrsweet commented 2 months ago

The current 4.0 API can be further improved by moving all of the thread-global options/callbacks and call-time load/save callbacks into a common mxml_options_t object that can be passed to the mxmlLoadXxx and mxmlSaveXxx functions. Proposed API is as follows:

typedef struct _mxml_options_s mxml_options_t; // Load/save options
typedef size_t (*mxml_io_cb_t)(void *cbdata, void *buffer, size_t bytes); // IO callback function

extern void mxmlOptionsDelete(mxml_options_t *options);
extern mxml_options_t *mxmlOptionsNew(void);
extern void mxmlOptionsSetCustomCallbacks(mxml_options_t *options, mxml_custom_load_cb_t load_cb, mxml_custom_save_cb_t save_cb, void *cbdata);
extern void mxmlOptionsSetEntityCallback(mxml_options_t *options, mxml_entity_cb_t cb, void *cbdata);
extern void mxmlOptionsSetErrorCallback(mxml_options_t *options, mxml_error_cb_t cb, void *cbdata);
extern void mxmlOptionsSetSAXCallback(mxml_options_t *options, mxml_sax_cb_t cb, void *cbdata);
extern void mxmlOptionsSetTypeCallback(mxml_options_t *options, mxml_type_cb_t cb, void *cbdata);
extern void mxmlOptionsSetTypeValue(mxml_options_t *options, mxml_type_t type);
extern void mxmlOptionsSetWhitespaceCallback(mxml_options_t *options, mxml_ws_cb_t cb, void *cbdata);
extern void mxmlOptionsSetWrapMargin(mxml_options_t *options, int col);

extern mxml_node_t  *mxmlLoadFd(mxml_node_t *top, mxml_options_t *options, int fd);
extern mxml_node_t  *mxmlLoadFile(mxml_node_t *top, mxml_options_t *options, FILE *fp);
extern mxml_node_t  *mxmlLoadFilename(mxml_node_t *top, mxml_options_t *options, const char *filename);
extern mxml_node_t  *mxmlLoadIO(mxml_node_t *top, mxml_options_t *options, mxml_io_cb_t cb, void *cbdata);
extern mxml_node_t  *mxmlLoadString(mxml_node_t *top, mxml_options_t *options, const char *s);

extern char     *mxmlSaveAllocString(mxml_node_t *node, mxml_options_t *options);
extern bool     mxmlSaveFd(mxml_node_t *node, mxml_options_t *options, int fd);
extern bool     mxmlSaveFile(mxml_node_t *node, mxml_options_t *options, FILE *fp);
extern bool     mxmlSaveFilename(mxml_node_t *node, mxml_options_t *options, const char *filename);
extern bool     mxmlSaveIO(mxml_node_t *node, mxml_options_t *options, mxml_io_cb_t cb, void *cbdata);
extern size_t       mxmlSaveString(mxml_node_t *node, mxml_options_t *options, char *buffer, size_t bufsize);

The string callback functions would remain thread-global since otherwise we'd need to pass the options everywhere or maintain extra pointers in the mxml_node_t structure.

michaelrsweet commented 2 months ago

Still need to finish documentation, but the core implementation is now done:

[master e676eb3] Implement mxmlOptions APIs to normalize all of the load/save option stuff (Issue #312)

michaelrsweet commented 1 month ago

[master db39074] Finalize documentation changes (Issue #312)