<libroxml  version="3.0.2" />
contact: tristan.lelong@libroxml.net
roxml_file.c
Go to the documentation of this file.
1
12#include <stdlib.h>
13#include <string.h>
14#include "roxml_core.h"
15#include "roxml_parser.h"
16#include "roxml_internal.h"
17
18ROXML_INT int roxml_read_file(int pos, int size, char *buffer, node_t *node)
19{
20 int ret_len = 0;
21
22 if (fseek(node->src.fil, pos, SEEK_SET) == 0)
23 ret_len = fread(buffer, 1, size, node->src.fil);
24
25 return ret_len;
26}
27
28ROXML_INT void roxml_close_file(node_t *root)
29{
30 fclose(root->src.fil);
31}
32
33ROXML_INT int roxml_parse_file(roxml_load_ctx_t *context, roxml_parser_item_t *parser, FILE *file)
34{
35 char *int_buffer;
36 int error = 0;
37 int circle = 0;
38 int int_len = 0;
39
40 context->type = ROXML_FILE;
41 context->src = (void *)file;
42 context->pos = 0;
43
44 int_buffer = malloc(ROXML_BULK_READ + 1);
45
46 do {
47 int ret = 0;
48 int chunk_len = 0;
49 int_len = fread(int_buffer + circle, 1, ROXML_BULK_READ - circle, file) + circle;
50 int_buffer[int_len] = '\0';
51
52 if (int_len == ROXML_BULK_READ)
53 chunk_len = int_len - ROXML_BASE_LEN;
54 else
55 chunk_len = int_len;
56
57 ret = roxml_parse_line(parser, int_buffer, chunk_len, context);
58 circle = int_len - ret;
59 if ((ret < 0) || (circle < 0)) {
60 error = 1;
61 break;
62 }
63 memmove(int_buffer, int_buffer + ret, circle);
64 } while (int_len == ROXML_BULK_READ);
65
66 free(int_buffer);
67
68 if (error == 0 && context->lvl != 0)
69 ROXML_PARSE_ERROR("mismatch in open/close nodes");
70 return error;
71}
72
74{
75 FILE *file = NULL;
76 node_t *current_node = NULL;
77
78 if (fd < 0)
79 return NULL;
80
81 file = fdopen(fd, "r");
82 if (file == NULL)
83 return ROXML_INVALID_DOC;
84
85 current_node = roxml_create_node(0, file, ROXML_ELM_NODE | ROXML_FILE);
86
87 return roxml_load(current_node, file, NULL);
88}
89
91{
92 node_t *current_node = NULL;
93 FILE *file = fopen(filename, "rb");
94
95 if (file == NULL)
96 return ROXML_INVALID_DOC;
97
98 current_node = roxml_create_node(0, file, ROXML_ELM_NODE | ROXML_FILE);
99
100 return roxml_load(current_node, file, NULL);
101}
#define ROXML_INVALID_DOC
Definition roxml.h:235
#define ROXML_ELM_NODE
Definition roxml.h:70
#define ROXML_API
Definition roxml.h:24
ROXML_INT node_t * roxml_create_node(int pos, void *src, int type)
internal function
Definition roxml_core.c:46
ROXML_INT node_t * roxml_load(node_t *current_node, FILE *file, char *buffer)
generic load function
Definition roxml_core.c:201
XML parsing core module.
#define ROXML_BULK_READ
#define ROXML_BASE_LEN
#define ROXML_FILE
ROXML_INT int roxml_parse_file(roxml_load_ctx_t *context, roxml_parser_item_t *parser, FILE *file)
generic load function
Definition roxml_file.c:33
ROXML_INT int roxml_read_file(int pos, int size, char *buffer, node_t *node)
read xml doc function
Definition roxml_file.c:18
ROXML_INT void roxml_close_file(node_t *root)
unload function
Definition roxml_file.c:28
ROXML_API node_t * roxml_load_doc(char *filename)
load function for files
Definition roxml_file.c:90
ROXML_API node_t * roxml_load_fd(int fd)
load function for file descriptors
Definition roxml_file.c:73
internal header required by all libroxml sources
ROXML_INT int roxml_parse_line(roxml_parser_item_t *parser, char *line, int len, void *ctx)
line parsing function
Parsing engine.
node_t structure
void * src
xml parsing context
the parser item struct