Skip to content

Commit

Permalink
Merge pull request #211 from tlsa/tlsa/copy
Browse files Browse the repository at this point in the history
Add a copy API
  • Loading branch information
tlsa authored May 28, 2023
2 parents e285d74 + 5286029 commit 9b6409c
Show file tree
Hide file tree
Showing 11 changed files with 10,002 additions and 75 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ endif
BUILDDIR_SHARED = $(BUILDDIR)/shared
BUILDDIR_STATIC = $(BUILDDIR)/static

LIB_SRC_FILES = mem.c free.c load.c save.c util.c utf8.c
LIB_SRC_FILES = mem.c free.c load.c save.c copy.c util.c utf8.c
LIB_SRC := $(addprefix src/,$(LIB_SRC_FILES))
LIB_OBJ = $(patsubst %.c,%.o, $(addprefix $(BUILDDIR)/,$(LIB_SRC)))
LIB_DEP = $(patsubst %.c,%.d, $(addprefix $(BUILDDIR)/,$(LIB_SRC)))
Expand All @@ -116,7 +116,8 @@ ifeq ($(UNAME_S),Darwin)
endif

TEST_SRC_FILES = units/free.c units/load.c units/test.c units/util.c \
units/errs.c units/file.c units/save.c units/utf8.c
units/errs.c units/file.c units/save.c units/copy.c \
units/utf8.c
TEST_SRC := $(addprefix test/,$(TEST_SRC_FILES))
TEST_OBJ = $(patsubst %.c,%.o, $(addprefix $(BUILDDIR)/,$(TEST_SRC)))
TEST_DEP = $(patsubst %.c,%.d, $(addprefix $(BUILDDIR)/,$(TEST_SRC)))
Expand Down
31 changes: 31 additions & 0 deletions include/cyaml/cyaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ typedef enum cyaml_err {
CYAML_ERR_BAD_CONFIG_NULL_MEMFN, /**< Client gave NULL mem function. */
CYAML_ERR_BAD_PARAM_NULL_CONFIG, /**< Client gave NULL config arg. */
CYAML_ERR_BAD_PARAM_NULL_SCHEMA, /**< Client gave NULL schema arg. */
CYAML_ERR_DATA_TARGET_NON_NULL, /**< Data target must be NULL ptr */
CYAML_ERR_LIBYAML_EMITTER_INIT, /**< Failed to initialise libyaml. */
CYAML_ERR_LIBYAML_PARSER_INIT, /**< Failed to initialise libyaml. */
CYAML_ERR_LIBYAML_EVENT_INIT, /**< Failed to initialise libyaml. */
Expand Down Expand Up @@ -1760,6 +1761,36 @@ extern cyaml_err_t cyaml_save_data(
const cyaml_data_t *data,
unsigned seq_count);

/**
* Copy a loaded document.
*
* This performs a deep-clone, creating a new copy of everything in the
* document, including allocations.
*
* This is a convenience function, which is exposed here just in case it
* is useful to clients. Clients would be better off writing their own copy
* function for the specific data once loaded.
*
* \note The input `data` parameter may be NULL if it is allowed by the schema.
* For example, if there is a top level mapping, containing only optional
* fields, and none of them are set, the provided data may be NULL.
*
* \param[in] config Client's CYAML configuration structure.
* \param[in] schema CYAML schema for the YAML to be copied.
* \param[in] data The caller-owned data to be copied.
* \param[in] seq_count If top level type is sequence, this should be the
* entry count, otherwise it is ignored.
* \param[out] data_out Returns the caller-owned loaded data on success.
* Untouched on failure.
* \return \ref CYAML_OK on success, or appropriate error code otherwise.
*/
extern cyaml_err_t cyaml_copy(
const cyaml_config_t *config,
const cyaml_schema_value_t *schema,
const cyaml_data_t *data,
unsigned seq_count,
cyaml_data_t **data_out);

/**
* Free data returned by a CYAML load function.
*
Expand Down
Loading

0 comments on commit 9b6409c

Please sign in to comment.