Each datastore has an enumeration in the ncx_cfg_t
/* hardwire the 3 standard configs */ typedef enum ncx_cfg_t_ { NCX_CFGID_RUNNING, NCX_CFGID_CANDIDATE, NCX_CFGID_STARTUP } ncx_cfg_t;
There are APIs to access the datastores in ncx/cfg.h..
Use cfg_get_root to get the root data node for the configuration datastore.
The NULL pointer is returned if the datastore is not available (e.g. --with-startup=false so no NCX_CFGID_STARTUP datastore)
/******************************************************************** * FUNCTION cfg_get_root * * Get the config root for the specified config * * INPUT: * cfgid == config ID to get root from * * RETURNS: * config root or NULL if none or error *********************************************************************/ extern val_value_t * cfg_get_root (ncx_cfg_t cfgid);
There are APIs to write val_value_t structures to a file in either XML or JSON encoding.
The XML version (xml_wr_file) is shown below:
/******************************************************************** * FUNCTION xml_wr_file * * Write the specified value to a FILE in XML format * * INPUTS: * filespec == exact path of filename to open * val == value for output * attrs == top-level attributes to generate * docmode == TRUE if XML_DOC output mode should be used * == FALSE if XML output mode should be used * xmlhdr == TRUE if <?xml?> directive should be output * == FALSE if not * withns == TRUE if xmlns attributes should be used * FALSE to leave them out * expand_varexpr == TRUE if expanding variable expressions * == FALSE if not expanding variable expressions * with_owners == TRUE to generate ywx:owner attributes as needed * startindent == starting indent point * indent == indent amount (0..9 spaces) * * RETURNS: * status *********************************************************************/ extern status_t xml_wr_file (const xmlChar *filespec, val_value_t *val, xml_attrs_t *attrs, boolean docmode, boolean xmlhdr, boolean withns, boolean expand_varexpr, boolean with_owners, int32 startindent, int32 indent);
Usage Example
The following code snippet shows how to get the running datastore and save it in XML format to the file "/tmp/my-config.xml"
#include "procdefs.h" #include "val.h" #include "ncxtypes.h" #include "cfg.h" #include "xml_wr.h" static void dump_running (void) { val_value_t *val = cfg_get_root(NCX_CFGID_RUNNING); if (val) { status_t res = xml_wr_file((const xmlChar *)"/tmp/my-config.xml", val, // val node to write NULL, // attrs for first start-tag; use val instead false, // docmode; use plain XML true, // xmlhdr; print the preamble true, // withns; normal XML false, // expand_varexpr; not used 0, // startindent 2); // indent amount if (res != NO_ERR) { // handle error } } }