Volksdata 1.0b7
RDF library
Loading...
Searching...
No Matches
codec_nt.c
Go to the documentation of this file.
2
3
5typedef struct nt_codec_iterator {
6 const VOLK_Codec * codec;
7 VOLK_GraphIterator *gr_it;
8 const VOLK_Term * gr_uri;
9 size_t cur;
11 char * str_s,
15
16
17
18/* * * Codec functions. * * */
19
20static VOLK_rc
21term_to_nt (const VOLK_Term *term, char **out_p)
22{
23 VOLK_rc rc;
24 char *out = NULL, *escaped;
25 const char *metadata = NULL;
26 size_t buf_len;
27
28 char *data = term->data;
29 switch (term->type) {
31 out = realloc (*out_p, strlen (data) + 3);
32 if (UNLIKELY (!out)) return VOLK_MEM_ERR;
33
34 sprintf (out, "<%s>", data);
35
36 if (data != term->data) free (data);
37 rc = VOLK_OK;
38 break;
39
41 // Calculate string length.
42 if (escape_lit (term->data, &escaped) != VOLK_OK)
43 return VOLK_ERROR;
44 buf_len = strlen (escaped) + 3; // Room for "" and terminator
45
46 if (
47 term->datatype != 0
49 ) {
50 metadata = term->datatype->data;
51 buf_len += strlen (metadata) + 4; // Room for ^^<>
52 }
53
54 out = realloc (*out_p, buf_len);
55 if (UNLIKELY (!out)) return VOLK_MEM_ERR;
56
57 sprintf (out, "\"%s\"", escaped);
58 free (escaped);
59
60 // Add datatype.
61 if (metadata)
62 out = strcat (strcat (strcat (out, "^^<"), metadata), ">");
63
64 rc = VOLK_OK;
65
66 break;
67
69 // Calculate string length.
70 if (escape_lit (term->data, &escaped) != VOLK_OK)
71 return VOLK_ERROR;
72 buf_len = strlen (escaped) + 3; // Room for "" and terminator
73
74 if (term->lang[0] != '\0') {
75 metadata = term->lang;
76 buf_len += strlen (metadata) + 1; // Room for @
77 }
78
79 out = realloc (*out_p, buf_len);
80 if (UNLIKELY (!out)) return VOLK_MEM_ERR;
81
82 sprintf (out, "\"%s\"", escaped);
83 free (escaped);
84
85 // Add lang.
86 if (metadata) out = strcat (strcat (out, "@"), metadata);
87
88 rc = VOLK_OK;
89
90 break;
91
92 case VOLK_TERM_BNODE:
93 out = realloc (*out_p, strlen (term->data) + 3);
94 if (UNLIKELY (!out)) return VOLK_MEM_ERR;
95
96 sprintf (out, "_:%s", term->data);
97 rc = VOLK_OK;
98
99 break;
100
101 default:
102 out = *out_p; // This is considered garbage.
103 rc = VOLK_PARSE_ERR;
104 }
105
106 *out_p = out;
107 return rc;
108}
109
110
111static void *
112gr_to_nt_init (const VOLK_Graph *gr)
113{
115 CALLOC_GUARD (it, NULL);
116
117 it->codec = &nt_codec;
118 it->gr_it = VOLK_graph_lookup(gr, NULL, NULL, NULL, &it->cur);
119 it->gr_uri = VOLK_graph_uri (gr);
120
121 return it;
122}
123
124
125static VOLK_rc
126gr_to_nt_iter (void *h, char **res) {
127 VOLK_NTCodecIterator *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 term_to_nt (abs_s, &it->str_s);
143 term_to_nt (spo->p, &it->str_p);
144 term_to_nt (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
170static void
171gr_to_nt_done (void *h)
172{
173 VOLK_NTCodecIterator *it = h;
175 free (it->str_s);
176 free (it->str_p);
177 free (it->str_o);
178 free (it);
179}
180
181
182const VOLK_Codec nt_codec = {
183 .name = "N-Triples",
184 .mimetype = "application/n-triples",
185 .extension = "nt",
186
187 .encode_term = term_to_nt,
188
189 .encode_graph_init = gr_to_nt_init,
190 .encode_graph_iter = gr_to_nt_iter,
191 .encode_graph_done = gr_to_nt_done,
192
193 .decode_term = VOLK_nt_parse_term,
194 .decode_graph = VOLK_nt_parse_doc,
195};
const VOLK_Codec nt_codec
N-Triples codec.
Definition codec_nt.c:182
#define UNLIKELY(x)
Definition core.h:39
VOLK_rc escape_lit(const char *in, char **out_p)
Add escape character (backslash) to illegal literal characters.
Definition codec.c:81
#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:568
void VOLK_graph_iter_free(VOLK_GraphIterator *it)
Free a graph iterator.
Definition graph.c:598
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:381
#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
VOLK_rc VOLK_nt_parse_doc(FILE *fh, const char *sh, VOLK_Graph **gr_p, size_t *ct, char **err_p)
Parse an RDF document in N-Triples format.
Definition parser_nt.c:1769
VOLK_rc VOLK_nt_parse_term(const char *rep, VOLK_Term **term)
Parse a single term.
Definition parser_nt.c:1749
NT codec iterator type.
Definition codec_nt.c:5
const VOLK_Term * gr_uri
Graph URI.
Definition codec_nt.c:8
char * str_o
Temporary string.
Definition codec_nt.c:13
char * str_s
Temporary string.
Definition codec_nt.c:11
char * str_p
Temporary string.
Definition codec_nt.c:12
VOLK_GraphIterator * gr_it
Graph iterator.
Definition codec_nt.c:7
const VOLK_Codec * codec
Codec that generated this iterator.
Definition codec_nt.c:6
VOLK_rc rc
Internal return code.
Definition codec_nt.c:10
size_t cur
Internal cursor.
Definition codec_nt.c:9
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