-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxml_inline.c
More file actions
87 lines (69 loc) · 2.12 KB
/
Copy pathxml_inline.c
File metadata and controls
87 lines (69 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"
typedef struct {
uint8_t *file_str;
size_t file_len;
size_t offset;
} xml_inline_ocb_t; // open context block
/**
* This call is generated by the c-atom's tool xml2c_inliner.py
* simply run 'make prebuild'
* @param path
* @param size
* @return
*/
const char *xml_inline_find_file(const char *path, size_t *size);
static int xml_inline_open_callback(FILE *file, const char *path) {
size_t filesize;
if (file->private_data == NULL) {
file->private_data = calloc(1, sizeof(xml_inline_ocb_t));
}
xml_inline_ocb_t *ocb = (xml_inline_ocb_t *)file->private_data;
const char *file_string = xml_inline_find_file(path, &filesize);
if (file_string == NULL) {
errno = ENOENT;
return -1;
}
// check size
#define MAXLEN (filesize + 100)
ocb->file_len = strnlen(file_string, MAXLEN);
if (ocb->file_len >= MAXLEN) {
errno = EFBIG;
return -1;
}
if (ocb->file_len != filesize) {
errno = EIO;
return -1;
}
ocb->file_str = (uint8_t *)file_string;
ocb->offset = 0;
return 0;
}
static ssize_t xml_inline_read_callback(FILE *file, char *buf, size_t count) {
xml_inline_ocb_t *ocb = (xml_inline_ocb_t *)file->private_data;
size_t bytes_left = ocb->file_len - ocb->offset;
ssize_t br = count > bytes_left ? bytes_left : count;
if (br > 0) {
memcpy(buf, ocb->file_str + ocb->offset, br);
ocb->offset += br;
}
errno = 0;
return br;
}
static int xml_inline_close_callback(FILE *file) {
xml_inline_ocb_t *ocb = (xml_inline_ocb_t *)file->private_data;
// TODO
return 0;
}
int xml_inline_mount(const char *mount_to) {
struct file_operations f_op = {.open = xml_inline_open_callback,
.write = NULL,
.read = xml_inline_read_callback,
.close = xml_inline_close_callback,
.dev = NULL};
struct node *node = node_mount(mount_to, &f_op);
return 0;
}