Add commercialcredit command
[shigofumi.git] / src / io.h
blob2cf5b6aec6e1c84cc1080a970a1ec82ae330c5ba
1 #ifndef __IO_H__
2 #define __IO_H__
3 #include <libxml/tree.h> /* For xmlDoc and xmlNodePtr */
4 #include <libxml/xpath.h>
6 /* Open file and return descriptor. Return -1 in case of error. */
7 int open_file_for_writing(const char *file, _Bool truncate, _Bool overwrite);
9 /* Create new file.
10 * @file is static template for file name. See mkstemps(3). It will contain
11 * new file name on return.
12 * @suffix_length is number of bytes of immutable file name suffix.
13 * @return descriptor of opened file, or -1 in case of error. */
14 int create_new_file(char *file, int suffix_length);
16 /* Return 0, -1 in case of error */
17 int mmap_file(const char *file, int *fd, void **buffer, size_t *length);
19 /* Return 0, -1 in case of error */
20 int munmap_file(int fd, void *buffer, size_t length);
22 /* Return 0, -1 in case of error.
23 * @length and @mime_type are optional. */
24 int load_data_from_file(const char *file, void **data, size_t *length,
25 char **mime_type);
27 /* Save @data to file specified by descriptor @fd. If @fd is negative, @file
28 * file will be opened first. Descriptor is closed at the end of this
29 * function. Supply @file name even if @fd is positive, the name could be used
30 * in messages. */
31 int save_data_to_file(const char *file, int fd, const void *data,
32 const size_t length, const char *mime_type, _Bool overwrite);
34 /* Parse @buffer as XML document and return @node_list specified by
35 * @xpath_expression.
36 * @node_list is weak copy that must be non-recursively freed by caller. Caller
37 * must free XML document (accessible through @node_list member) on its own.
38 * In case of error @node_list value will be invalid.
39 * If @node_list is returned empty, function freed parsed document already.
40 * @xpat_expr is UTF-8 encoded XPath expression */
41 int load_xml_subtree_from_memory(const void *buffer, size_t length,
42 xmlNodePtr *node_list, const char *xpath_expr);
44 /* Deallocate struct isds_document with embedded XML recursively and NULL it */
45 void free_document_with_xml_node_list(struct isds_document **document);
47 /* Serialize XML @node_list to automatically rellaocated libxml @buffer */
48 int serialize_xml_to_buffer(xmlBufferPtr *buffer, const xmlNodePtr node_list);
50 /* Save @node_list to file specified by descriptor @fd. If @fd is negative, @file
51 * file will be opened first. Descriptor is closed at the end of this
52 * function. Supply @file name even if @fd is positive, the name could be used
53 * in messages. */
54 int save_xml_to_file(const char *file, int fd, const xmlNodePtr node_list,
55 const char *mime_type, _Bool overwrite);
57 /* Return 0 if @path is directory, 1 if not, -1 if error occurred */
58 int is_directory(const char *path);
60 /* Remove a file.
61 * @file to remove
62 * @return 0 on success, non-zero otherwise. */
63 int unlink_file(const char *file);
64 #endif