<libroxml  version="3.0.0" />
contact: tristan.lelong@libroxml.net
Functions
roxml_commit.c File Reference

XML serialize to file or buffer module. More...

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include "roxml_core.h"

Go to the source code of this file.

Functions

ROXML_STATIC ROXML_INT void roxml_realloc_buf (char **buf, int *len, int min_len)
 
ROXML_STATIC ROXML_INT void roxml_print_spaces (FILE *f, char **buf, int *offset, int *len, int lvl)
 space printing function More...
 
ROXML_STATIC ROXML_INT void roxml_write_string (FILE *f, char **buf, int *offset, int *len, char *str,...)
 string writter function More...
 
ROXML_STATIC ROXML_INT void roxml_write_other_node (node_t *n, FILE *f, char **buf, int *offset, int *len, char *name)
 
ROXML_STATIC ROXML_INT void roxml_write_elm_name_open (node_t *n, FILE *f, char **buf, int *offset, int *len, char *name, char *ns)
 
ROXML_STATIC ROXML_INT void roxml_write_elm_name_close (node_t *n, FILE *f, char **buf, int *offset, int *len, char *name, char *ns)
 
ROXML_STATIC ROXML_INT void roxml_write_elm_attr (node_t *n, FILE *f, char **buf, int *offset, int *len)
 
ROXML_STATIC ROXML_INT void roxml_write_node (node_t *n, FILE *f, char **buf, int human, int lvl, int *offset, int *len)
 tree write function More...
 
ROXML_STATIC ROXML_INT void roxml_commit_nodes (node_t *n, FILE *f, char **buf, int human, int *size, int *len)
 
ROXML_API int roxml_commit_file (node_t *n, char *dest, int human)
 sync to named file function More...
 
ROXML_API int roxml_commit_buffer (node_t *n, char **buffer, int human)
 sync to a memory buffer function More...
 
ROXML_API int roxml_commit_fd (node_t *n, int fd, int human)
 sync to file descriptor function More...
 
ROXML_API int roxml_commit_changes (node_t *n, char *dest, char **buffer, int human)
 sync function More...
 

Detailed Description

XML serialize to file or buffer module.

(C) Copyright 2014 Tristan Lelong trist.nosp@m.an.l.nosp@m.elong.nosp@m.@lib.nosp@m.roxml.nosp@m..net

SPDX-Licence-Identifier: LGPL-2.1+ The author added a static linking exception, see License.txt.

Definition in file roxml_commit.c.

Function Documentation

◆ roxml_commit_buffer()

roxml_commit_buffer ( node_t n,
char **  buffer,
int  human 
)

sync to a memory buffer function

this function syncs changes from the RAM tree to the given buffer in human or one-line format The tree will be processed starting with the root node 'n' and following with all its children or if n is the root, all its siblings and children. The tree will be dumped to a buffer if 'buffer' is not null. the buffer is allocated by the library and a pointer to it will be stored into 'buffer'. The allocated buffer can be freed using free()

Parameters
nthe root node of the tree to write
bufferthe address of a buffer where the tree will be written. This buffer have to be freed after use
human0 for one-line tree, or 1 for human format (using indentation, newlines...)
Returns
the number of bytes written to file or buffer

One should do:

#include <roxml.h>
int main(void)
{
char *buffer = NULL;
node_t *root = roxml_add_node(NULL, 0, ROXML_ELM_NODE, "xml", NULL);
node_t *tmp = roxml_add_node(root, 0, ROXML_CMT_NODE, NULL, "sample XML file");
tmp = roxml_add_node(root, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(tmp, 0, ROXML_ATTR_NODE, "id", "42");
tmp = roxml_add_node(tmp, 0, ROXML_ELM_NODE, "price", "24");
roxml_commit_changes(root, &buffer, 1);
roxml_close(root);
return 0;
}

to generate the following xml bloc:

<root>
 <!-- sample XML file -->
 <item id="42">
  <price>
   24
  </price>
 </item>
</root>

or also

#include <roxml.h>
int main(void)
{
char *buffer = NULL;
node_t *root = roxml_add_node(NULL, 0, ROXML_ELM_NODE, "xml", NULL);
node_t *tmp = roxml_add_node(root, 0, ROXML_CMT_NODE, NULL, "sample XML file");
tmp = roxml_add_node(root, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(tmp, 0, ROXML_ATTR_NODE, "id", "42");
tmp = roxml_add_node(tmp, 0, ROXML_ELM_NODE, "price", "24");
roxml_commit_buffer(root, &buffer, 0);
roxml_close(root);
return 0;
}

to generate the following xml bloc:

<root><!-- sample XML file --><item id="42"><price>24</price></item></root>
See also
roxml_commit_changes
roxml_commit_file
roxml_commit_fd

Definition at line 296 of file roxml_commit.c.

◆ roxml_commit_changes()

roxml_commit_changes ( node_t n,
char *  dest,
char **  buffer,
int  human 
)

sync function

Deprecated:
this function sync changes from the RAM tree to the given buffer or file in human or one-line format The tree will be processed starting with the root node 'n' and following with all its children or if n is the root, all its siblings and children. The tree will be dumped to a file if 'dest' is not null and contains a valid path. The tree will be dumped to a buffer if 'buffer' is not null. the buffer is allocated by the library and a pointer to it will be stored into 'buffer'. The allocated buffer can be freed usinf free()
Warning
in the case of a tree loaded using roxml_load_doc, the roxml_commit_changes cannot be done to that same file since it may override datas it need. This usually result in a new file filled with garbages. The solution is to write it to a temporary file and rename it after roxml_close the current tree.

This function is now deprecated and one should use roxml_commit_buffer or roxml_commit_file that achieves the exact same goal.

Parameters
nthe root node of the tree to write
destthe path to a file to write tree to
bufferthe address of a buffer where the tree will be written. This buffer have to be freed after use
human0 for one-line tree, or 1 for human format (using tabs, newlines...)
Returns
the number of bytes written to file or buffer
See also
roxml_commit_buffer
roxml_commit_file

This is a legacy function that proposes the same functionnalities as both roxml_commit_file and roxml_commit_buffer. New code should use the latter functions when needed.

One should do:

#include <roxml.h>
int main(void)
{
node_t *root = roxml_add_node(NULL, 0, ROXML_ELM_NODE, "xml", NULL);
node_t *tmp = roxml_add_node(root, 0, ROXML_CMT_NODE, NULL, "sample XML file");
tmp = roxml_add_node(root, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(tmp, 0, ROXML_ATTR_NODE, "id", "42");
tmp = roxml_add_node(tmp, 0, ROXML_ELM_NODE, "price", "24");
roxml_commit_changes(root, "/tmp/test.xml", NULL, 1);
return 0;
}

to generate the following xml bloc:

<root>
 <!-- sample XML file -->
 <item id="42">
  <price>
   24
  </price>
 </item>
</root>

or also

#include <roxml.h>
int main(void)
{
node_t *root = roxml_add_node(NULL, 0, ROXML_ELM_NODE, "xml", NULL);
node_t *tmp = roxml_add_node(root, 0, ROXML_CMT_NODE, NULL, "sample XML file");
tmp = roxml_add_node(root, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(tmp, 0, ROXML_ATTR_NODE, "id", "42");
tmp = roxml_add_node(tmp, 0, ROXML_ELM_NODE, "price", "24");
roxml_commit_changes(root, "/tmp/test.xml", NULL, 0);
roxml_close(root);
return 0;
}

to generate the following xml bloc:

<root><!-- sample XML file --><item id="42"><price>24</price></item></root>

the buffer variant works more or less the same way

#include <stdio.h>
#include <roxml.h>
int main(void)
{
int len = 0;
char * buffer = NULL;
FILE * file_out;
node_t *root = roxml_add_node(NULL, 0, ROXML_ELM_NODE, "xml", NULL);
node_t *tmp = roxml_add_node(root, 0, ROXML_CMT_NODE, NULL, "sample XML file");
tmp = roxml_add_node(root, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(tmp, 0, ROXML_ATTR_NODE, "id", "42");
tmp = roxml_add_node(tmp, 0, ROXML_ELM_NODE, "price", "24");
len = roxml_commit_changes(root, NULL, &buffer, 0);
file_out = fopen("/tmp/test.xml", "w");
fwrite(buffer, 1, len, file_out);
fclose(file_out);
roxml_close(root);
return 0;
}

to generate the following xml bloc:

<root><!-- sample XML file --><item id="42"><price>24</price></item></root>
See also
roxml_commit_file
roxml_commit_buffer
roxml_commit_fd

Definition at line 357 of file roxml_commit.c.

◆ roxml_commit_fd()

roxml_commit_fd ( node_t n,
int  fd,
int  human 
)

sync to file descriptor function

this function synchronizes changes from the RAM tree to the given file in human or one-line format. The tree will be processed starting with the root node 'n' and following with all its children or if n is the root, all its siblings and children. The tree will be dumped to a file if fd is a valid file descriptor.

Warning
in the case of a tree loaded using roxml_load_doc, the roxml_commit_fd cannot be done to that same file since it may override datas it needs. This usually results in a new file filled with garbage. The solution is to write it to a temporary file and rename it after roxml_close the current tree.
Parameters
nthe root node of the tree to write
fdthe file descriptor to write tree to
human0 for one-line tree, or 1 for human format (using tabs, newlines...)
Returns
the number of bytes written to file

The file described by fd is not truncated so this function allows one to append an XML subtree to an existing file.

One should do:

#include <roxml.h>
int main(void)
{
int fd = open("/tmp/test.xml", O_TRUNC|O_WRONLY, 0666);
node_t *root = roxml_add_node(NULL, 0, ROXML_ELM_NODE, "xml", NULL);
node_t *tmp = roxml_add_node(root, 0, ROXML_CMT_NODE, NULL, "sample XML file");
tmp = roxml_add_node(root, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(tmp, 0, ROXML_ATTR_NODE, "id", "42");
tmp = roxml_add_node(tmp, 0, ROXML_ELM_NODE, "price", "24");
roxml_commit_fd(root, fd, 1);
roxml_close(root);
close(fd);
return 0;
}

to generate the following xml bloc:

<root>
 <!-- sample XML file -->
 <item id="42">
  <price>
   24
  </price>
 </item>
</root>

or also

#include <roxml.h>
int main(void)
{
int fd = open("/tmp/test.xml", O_TRUNC|O_WRONLY, 0666);
node_t *root = roxml_add_node(NULL, 0, ROXML_ELM_NODE, "xml", NULL);
node_t *tmp = roxml_add_node(root, 0, ROXML_CMT_NODE, NULL, "sample XML file");
tmp = roxml_add_node(root, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(tmp, 0, ROXML_ATTR_NODE, "id", "42");
tmp = roxml_add_node(tmp, 0, ROXML_ELM_NODE, "price", "24");
roxml_commit_fd(root, fd, 0);
roxml_close(root);
close(fd);
return 0;
}

to generate the following xml bloc:

<root><!-- sample XML file --><item id="42"><price>24</price></item></root>
See also
roxml_commit_changes
roxml_commit_file
roxml_commit_buffer

Definition at line 317 of file roxml_commit.c.

◆ roxml_commit_file()

roxml_commit_file ( node_t n,
char *  dest,
int  human 
)

sync to named file function

this function sync changes from the RAM tree to the given file in human or one-line format The tree will be processed starting with the root node 'n' and following with all its children or if n is the root, all its siblings and children. The tree will be dumped to a file if 'dest' is not null and contains a valid path.

Warning
in the case of a tree loaded using roxml_load_doc, the roxml_commit_file cannot be done to that same file since it may override datas it needs. This usually resultis in a new file filled with garbage. The solution is to write it to a temporary file and rename it after roxml_close the current tree.
Parameters
nthe root node of the tree to write
destthe path to a file to write tree to
human0 for one-line tree, or 1 for human format (using tabs, newlines...)
Returns
the number of bytes written to file or buffer

One should do:

#include <roxml.h>
int main(void)
{
node_t *root = roxml_add_node(NULL, 0, ROXML_ELM_NODE, "xml", NULL);
node_t *tmp = roxml_add_node(root, 0, ROXML_CMT_NODE, NULL, "sample XML file");
tmp = roxml_add_node(root, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(tmp, 0, ROXML_ATTR_NODE, "id", "42");
tmp = roxml_add_node(tmp, 0, ROXML_ELM_NODE, "price", "24");
roxml_commit_file(root, "/tmp/test.xml", 1);
roxml_close(root);
return 0;
}

to generate the following xml bloc:

<root>
 <!-- sample XML file -->
 <item id="42">
  <price>
   24
  </price>
 </item>
</root>

or also

#include <roxml.h>
int main(void)
{
node_t *root = roxml_add_node(NULL, 0, ROXML_ELM_NODE, "xml", NULL);
node_t *tmp = roxml_add_node(root, 0, ROXML_CMT_NODE, NULL, "sample XML file");
tmp = roxml_add_node(root, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(tmp, 0, ROXML_ATTR_NODE, "id", "42");
tmp = roxml_add_node(tmp, 0, ROXML_ELM_NODE, "price", "24");
roxml_commit_file(root, "/tmp/test.xml", 0);
roxml_close(root);
return 0;
}

to generate the following xml bloc:

<root><!-- sample XML file --><item id="42"><price>24</price></item></root>
See also
roxml_commit_changes
roxml_commit_buffer
roxml_commit_fd

Definition at line 272 of file roxml_commit.c.

◆ roxml_print_spaces()

roxml_print_spaces ( FILE *  f,
char **  buf,
int *  offset,
int *  len,
int  lvl 
)

space printing function

this function add some space to output when committing change in human format

Parameters
fthe file pointer if any
bufthe pointer to string if any
offsetthe current offset in stream
lenthe total len of buffer if any
lvlthe level in the tree
Returns

Definition at line 37 of file roxml_commit.c.

◆ roxml_write_node()

roxml_write_node ( node_t n,
FILE *  f,
char **  buf,
int  human,
int  lvl,
int *  offset,
int *  len 
)

tree write function

this function write each node of the tree to output

Parameters
nthe node to write
fthe file pointer if any
bufthe pointer to the buffer string if any
human1 to use the human format else 0
lvlthe current level in tree
offsetthe current offset in stream
lenthe total len of buffer if any
Returns

Definition at line 203 of file roxml_commit.c.

◆ roxml_write_string()

void roxml_write_string ( FILE *  f,
char **  buf,
int *  offset,
int *  len,
char *  str,
  ... 
)

string writter function

this function write a string to output when committing change

Parameters
fthe file pointer if any
bufthe pointer to string if any
strthe string to write
offsetthe current offset in stream
lenthe total len of buffer if any
Returns

Definition at line 67 of file roxml_commit.c.