Volksdata 1.0b7
RDF library
Loading...
Searching...
No Matches
writer_nt.c
Go to the documentation of this file.
2
3
5 VOLK_GraphIterator *gr_it;
6 const VOLK_Term * gr_uri;
7 size_t cur;
9 char * str_s,
12};
13
14
15
16/* * * Codec functions. * * */
17
19nt_encode_term (const VOLK_Term *term, char **out_p)
20{
21 VOLK_rc rc;
22 char *out = NULL, *escaped;
23 const char *metadata = NULL;
24 size_t buf_len;
25
26 char *data = term->data;
27 switch (term->type) {
29 out = realloc (*out_p, strlen (data) + 3);
30 if (UNLIKELY (!out)) return VOLK_MEM_ERR;
31
32 sprintf (out, "<%s>", data);
33
34 if (data != term->data) free (data);
35 rc = VOLK_OK;
36 break;
37
39 // Calculate string length.
40 if (escape_lit (term->data, &escaped) != VOLK_OK)
41 return VOLK_ERROR;
42 buf_len = strlen (escaped) + 3; // Room for "" and terminator
43
44 if (
45 term->datatype != 0
47 ) {
48 metadata = term->datatype->data;
49 buf_len += strlen (metadata) + 4; // Room for ^^<>
50 }
51
52 out = realloc (*out_p, buf_len);
53 if (UNLIKELY (!out)) return VOLK_MEM_ERR;
54
55 sprintf (out, "\"%s\"", escaped);
56 free (escaped);
57
58 // Add datatype.
59 if (metadata)
60 out = strcat (strcat (strcat (out, "^^<"), metadata), ">");
61
62 rc = VOLK_OK;
63
64 break;
65
67 // Calculate string length.
68 if (escape_lit (term->data, &escaped) != VOLK_OK)
69 return VOLK_ERROR;
70 buf_len = strlen (escaped) + 3; // Room for "" and terminator
71
72 if (term->lang[0] != '\0') {
73 metadata = term->lang;
74 buf_len += strlen (metadata) + 1; // Room for @
75 }
76
77 out = realloc (*out_p, buf_len);
78 if (UNLIKELY (!out)) return VOLK_MEM_ERR;
79
80 sprintf (out, "\"%s\"", escaped);
81 free (escaped);
82
83 // Add lang.
84 if (metadata) out = strcat (strcat (out, "@"), metadata);
85
86 rc = VOLK_OK;
87
88 break;
89
90 case VOLK_TERM_BNODE:
91 out = realloc (*out_p, strlen (term->data) + 3);
92 if (UNLIKELY (!out)) return VOLK_MEM_ERR;
93
94 sprintf (out, "_:%s", term->data);
95 rc = VOLK_OK;
96
97 break;
98
99 default:
100 out = *out_p; // This is considered garbage.
101 rc = VOLK_PARSE_ERR;
102 }
103
104 *out_p = out;
105 return rc;
106}
107
108
109void *
110nt_encode_graph_init (const VOLK_Graph *gr, VOLK_CodecFlags _unused)
111{
112 (void) _unused;
113
114 NTEncodeIterator *it;
115 CALLOC_GUARD (it, NULL);
116
117 it->gr_it = VOLK_graph_lookup(gr, NULL, NULL, NULL, &it->cur);
118 it->gr_uri = VOLK_graph_uri (gr);
119
120 return it;
121}
122
123
125nt_encode_graph_iter (void *h, char **res)
126{
127 NTEncodeIterator *it = h;
128 VOLK_Triple *spo = NULL;
129
130 VOLK_rc rc = VOLK_graph_iter_next (it->gr_it, &spo);
131 if (rc != VOLK_OK) goto finally;
132
133 // If IRIRefs are relative to the graph URI, make them absolute.
134 VOLK_Term *abs_s, *abs_o;
135 if (spo->s->type == VOLK_TERM_IRIREF)
136 abs_s = VOLK_iriref_new_abs (it->gr_uri, spo->s);
137 else abs_s = spo->s;
138 if (spo->o->type == VOLK_TERM_IRIREF)
139 abs_o = VOLK_iriref_new_abs (it->gr_uri, spo->o);
140 else abs_o = spo->o;
141
142 nt_encode_term (abs_s, &it->str_s);
143 nt_encode_term (spo->p, &it->str_p);
144 nt_encode_term (abs_o, &it->str_o);
145
146 if (abs_s != spo->s) VOLK_term_free (abs_s);
147 if (abs_o != spo->o) VOLK_term_free (abs_o);
148 VOLK_triple_free (spo);
149
150 // 3 term separators + dot + newline + terminal = 6
151 char *tmp = realloc (
152 *res, strlen (it->str_s) + strlen (it->str_p)
153 + strlen (it->str_o) + 6);
154 if (UNLIKELY (!tmp)) {
155 *res = NULL;
156 rc = VOLK_MEM_ERR;
157 goto finally;
158 }
159
160 sprintf (tmp, "%s %s %s .\n", it->str_s, it->str_p, it->str_o);
161 *res = tmp;
162
163 it->cur++;
164
165finally:
166 return rc;
167}
168
169
170void
172{
173 NTEncodeIterator *it = h;
174 VOLK_graph_iter_free (it->gr_it);
175 free (it->str_s);
176 free (it->str_p);
177 free (it->str_o);
178 free (it);
179}
#define UNLIKELY(x)
Definition core.h:39
VOLK_CodecFlags
Parse error information.
VOLK_rc escape_lit(const char *in, char **out_p)
Add escape character (backslash) to illegal literal characters.
#define VOLK_graph_lookup(...)
Non-transactional version of VOLK_graph_lookup_txn.
Definition graph.h:364
VOLK_rc VOLK_graph_iter_next(VOLK_GraphIterator *it, VOLK_Triple **spo_p)
Advance a cursor obtained by a lookup and return a matching triple.
Definition graph.c:575
void VOLK_graph_iter_free(VOLK_GraphIterator *it)
Free a graph iterator.
Definition graph.c:605
const VOLK_Term * VOLK_graph_uri(const VOLK_Graph *gr)
Read-only graph URI.
Definition graph.c:263
#define CALLOC_GUARD(var, rc)
Allocate one pointer with calloc and return rc if it fails.
Definition core.h:388
#define VOLK_ERROR
Generic error return code.
Definition core.h:123
#define VOLK_MEM_ERR
Memory allocation error.
Definition core.h:144
#define VOLK_OK
Generic success return code.
Definition core.h:83
#define VOLK_PARSE_ERR
Codec parser error.
Definition core.h:126
int VOLK_rc
Definition core.h:79
VOLK_Term * VOLK_iriref_new_abs(const VOLK_Term *root, const VOLK_Term *iri)
Create a new absolute IRI from a path relative to a root IRI.
Definition term.c:231
VOLK_Term * VOLK_default_datatype
Default literal data type URI.
Definition term.c:60
void VOLK_term_free(VOLK_Term *term)
Definition term.c:387
void VOLK_triple_free(VOLK_Triple *spo)
Free a triple and all its internal pointers.
Definition term.c:532
@ VOLK_TERM_IRIREF
IRI reference.
Definition term.h:35
@ VOLK_TERM_LT_LITERAL
Language-tagged string literal.
Definition term.h:37
@ VOLK_TERM_LITERAL
Literal without language tag.
Definition term.h:36
@ VOLK_TERM_BNODE
Blank node.
Definition term.h:38
RDF term.
Definition term.h:62
char * data
URI, literal value, or BNode label.
Definition term.h:63
struct term_t * datatype
Data type IRI for VOLK_TERM_LITERAL.
Definition term.h:65
VOLK_TermType type
Term type.
Definition term.h:70
VOLK_LangTag lang
Lang tag for VOLK_TERM_LT_LITERAL.
Definition term.h:66
RDF triple.
Definition term.h:86
VOLK_Term * p
Predicate.
Definition term.h:88
VOLK_Term * s
Subject.
Definition term.h:87
VOLK_Term * o
Object.
Definition term.h:89
NT encode iterator type.
Definition writer_nt.c:4
char * str_p
Temporary string.
Definition writer_nt.c:10
char * str_o
Temporary string.
Definition writer_nt.c:11
VOLK_GraphIterator * gr_it
Graph iterator.
Definition writer_nt.c:5
size_t cur
Internal cursor.
Definition writer_nt.c:7
char * str_s
Temporary string.
Definition writer_nt.c:9
VOLK_rc rc
Internal return code.
Definition writer_nt.c:8
const VOLK_Term * gr_uri
Graph URI.
Definition writer_nt.c:6
void nt_encode_graph_done(void *h)
Definition writer_nt.c:171
VOLK_rc nt_encode_graph_iter(void *h, char **res)
Definition writer_nt.c:125
VOLK_rc nt_encode_term(const VOLK_Term *term, char **out_p)
Definition writer_nt.c:19
void * nt_encode_graph_init(const VOLK_Graph *gr, VOLK_CodecFlags _unused)
Definition writer_nt.c:110