1 /* -*- Mode: c; c-basic-offset: 2 -*-
2 *
3 * raptor_fake.c - Fake Raptor V2 - just enough for flickrdf.c
4 *
5 * Copyright (C) 2011-2012, David Beckett http://www.dajobe.org/
6 *
7 * This file is licensed under the following three licenses as alternatives:
8 * 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
9 * 2. GNU General Public License (GPL) V2 or any newer version
10 * 3. Apache License, V2.0 or any newer version
11 *
12 * You may not use this file except in compliance with at least one of
13 * the above three licenses.
14 *
15 * See LICENSE.html or LICENSE.txt at the top of this package for the
16 * complete terms and further detail along with the license texts for
17 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
18 *
19 */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdarg.h>
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #ifdef HAVE_ERRNO_H
36 #include <errno.h>
37 #endif
38
39
40 #include <raptor_fake.h>
41
raptor_new_world(void)42 raptor_world* raptor_new_world(void) { return NULL; }
raptor_world_open(raptor_world * world)43 int raptor_world_open(raptor_world* world) { return 0; }
raptor_free_world(raptor_world * world)44 void raptor_free_world(raptor_world* world) { }
45
46 raptor_uri*
raptor_new_uri(raptor_world * world,const unsigned char * uri_string)47 raptor_new_uri(raptor_world* world, const unsigned char *uri_string)
48 {
49 size_t len;
50 raptor_uri* u;
51
52 if(!uri_string)
53 return NULL;
54
55 len = strlen((const char*)uri_string);
56 u = (raptor_uri*)malloc(len + 1);
57 memcpy(u, uri_string, len + 1);
58
59 return u;
60 }
61
62
63 raptor_uri*
raptor_new_uri_from_uri_local_name(raptor_world * world,raptor_uri * uri,const unsigned char * local_name)64 raptor_new_uri_from_uri_local_name(raptor_world* world,
65 raptor_uri *uri,
66 const unsigned char *local_name)
67 {
68 size_t len1;
69 size_t len2;
70 raptor_uri* newu;
71
72 if(!uri || !local_name)
73 return NULL;
74
75 len1 = strlen((const char*)uri);
76 len2 = strlen((const char*)local_name);
77
78 newu = (raptor_uri*)malloc(len1 + len2 + 1);
79 memcpy(newu, uri, len1);
80 memcpy(newu + len1, local_name, len2 + 1);
81
82 return newu;
83 }
84
85
86 raptor_uri*
raptor_new_uri_from_uri(raptor_uri * uri)87 raptor_new_uri_from_uri(raptor_uri *uri)
88 {
89 return raptor_new_uri(NULL, (const unsigned char *)uri);
90 }
91
92 void
raptor_free_uri(raptor_uri * u)93 raptor_free_uri(raptor_uri* u)
94 {
95 free(u);
96 }
97
98
99 raptor_term*
raptor_new_term_from_blank(raptor_world * world,const unsigned char * blank)100 raptor_new_term_from_blank(raptor_world* world, const unsigned char* blank)
101 {
102 raptor_term* newt;
103 size_t blank_len;
104
105 if(!blank)
106 return NULL;
107
108 blank_len = strlen((const char*)blank);
109
110 newt = (raptor_term*)calloc(sizeof(*newt), 1);
111 newt->type = RAPTOR_TERM_TYPE_BLANK;
112 newt->value.blank.string = malloc(blank_len + 1);
113
114 memcpy(newt->value.blank.string, blank, blank_len + 1);
115
116 return newt;
117 }
118
119 raptor_term*
raptor_new_term_from_uri_string(raptor_world * world,const unsigned char * uri_string)120 raptor_new_term_from_uri_string(raptor_world* world,
121 const unsigned char *uri_string)
122 {
123 raptor_term* newt;
124
125 if(!uri_string)
126 return NULL;
127
128 newt = (raptor_term*)calloc(sizeof(*newt), 1);
129 newt->type = RAPTOR_TERM_TYPE_URI;
130 newt->value.uri = raptor_new_uri(world, uri_string);
131
132 return newt;
133 }
134
135
136 raptor_term*
raptor_new_term_from_uri(raptor_world * world,raptor_uri * uri)137 raptor_new_term_from_uri(raptor_world* world, raptor_uri* uri)
138 {
139 return raptor_new_term_from_uri_string(world, (const unsigned char *)uri);
140 }
141
142
143 raptor_term*
raptor_new_term_from_literal(raptor_world * world,const unsigned char * literal,raptor_uri * datatype,const unsigned char * language)144 raptor_new_term_from_literal(raptor_world* world, const unsigned char* literal, raptor_uri* datatype, const unsigned char* language)
145 {
146 raptor_term* newt;
147 size_t literal_len;
148
149 if(!literal)
150 return NULL;
151
152 literal_len = strlen((const char*)literal);
153
154 newt = (raptor_term*)calloc(sizeof(*newt), 1);
155 newt->type = RAPTOR_TERM_TYPE_LITERAL;
156
157 newt->value.literal.string = (unsigned char*)malloc(literal_len + 1);
158 memcpy(newt->value.literal.string, literal, literal_len + 1);
159
160 if(datatype)
161 newt->value.literal.datatype = raptor_new_uri_from_uri(datatype);
162
163 return newt;
164 }
165
166 void
raptor_free_term(raptor_term * term)167 raptor_free_term(raptor_term* term)
168 {
169 if(term->type == RAPTOR_TERM_TYPE_URI)
170 raptor_free_uri(term->value.uri);
171
172 if(term->type == RAPTOR_TERM_TYPE_BLANK)
173 free(term->value.blank.string);
174 if(term->type == RAPTOR_TERM_TYPE_LITERAL) {
175 if(term->value.literal.datatype)
176 raptor_free_uri(term->value.literal.datatype);
177 free(term->value.literal.string);
178 }
179 }
180
181
182 #define NSERIALIZERS 2
183 raptor_syntax_description serializers[NSERIALIZERS]= {
184 { { "ntriples", NULL}, "N-Triples" },
185 { { "turtle", NULL}, "Turtle", },
186 };
187
188
189 int
raptor_world_is_serializer_name(raptor_world * world,const char * name)190 raptor_world_is_serializer_name(raptor_world* world, const char* name)
191 {
192 int i;
193
194 for(i = 0; i < NSERIALIZERS; i++) {
195 if(strcmp(serializers[i].names[0], name))
196 return 1;
197 }
198 return 0;
199 }
200
201
202 const raptor_syntax_description*
raptor_world_get_serializer_description(raptor_world * world,unsigned int counter)203 raptor_world_get_serializer_description(raptor_world* world,
204 unsigned int counter)
205 {
206 if(counter > (NSERIALIZERS-1))
207 return NULL;
208
209 return &serializers[counter];
210 }
211
212
213 raptor_serializer*
raptor_new_serializer(raptor_world * world,const char * serializer_name)214 raptor_new_serializer(raptor_world* world, const char* serializer_name)
215 {
216 raptor_serializer* s;
217 s = (raptor_serializer*)calloc(sizeof(raptor_serializer), 1);
218 s->output_turtle = !strcmp((const char*)serializer_name, "turtle");
219 return s;
220 }
221
222
223 void
raptor_free_serializer(raptor_serializer * s)224 raptor_free_serializer(raptor_serializer* s)
225 {
226 free(s);
227 }
228
229
230 void
raptor_serializer_set_namespace(raptor_serializer * serializer,raptor_uri * uri,const unsigned char * prefix)231 raptor_serializer_set_namespace(raptor_serializer* serializer,
232 raptor_uri* uri, const unsigned char* prefix)
233 {
234 FILE* fh = serializer->fh;
235 if(serializer->output_turtle)
236 fprintf(fh, "@prefix %s: <%s> .\n", (const char*)prefix, (const char*)uri);
237 }
238
239
240 void
raptor_serializer_start_to_file_handle(raptor_serializer * serializer,raptor_uri * base_uri,FILE * fh)241 raptor_serializer_start_to_file_handle(raptor_serializer* serializer,
242 raptor_uri* base_uri, FILE* fh)
243 {
244 serializer->fh = fh;
245 if(base_uri && serializer->output_turtle)
246 fprintf(fh, "@base <%s>\n", (char*)base_uri);
247 }
248
249
250 void
raptor_serializer_serialize_statement(raptor_serializer * serializer,raptor_statement * s)251 raptor_serializer_serialize_statement(raptor_serializer* serializer,
252 raptor_statement* s)
253 {
254 FILE *fh = serializer->fh;
255
256 if(s->subject->type == RAPTOR_TERM_TYPE_URI)
257 fprintf(fh, "<%s>", (const char*)s->subject->value.uri);
258 else /* blank node */
259 fprintf(fh, "_:%s", (const char*)s->subject->value.blank.string);
260
261 fprintf(fh, " <%s> ", (const char*)s->predicate->value.uri);
262
263 if(s->object->type == RAPTOR_TERM_TYPE_LITERAL) {
264 const char *p;
265 char c;
266
267 fputc('"', fh);
268 for(p = (const char*)s->object->value.literal.string; (c = *p); p++) {
269 if(c == '\n') {
270 fputs("\\\n", fh);
271 continue;
272 } else if(c == '\t') {
273 fputs("\\\t", fh);
274 continue;
275 } if(c == '\r') {
276 fputs("\\\r", fh);
277 continue;
278 } else if(c == '"' || c == '\\')
279 fputc('\\', fh);
280 fputc(c, fh);
281 }
282 fputc('"', fh);
283 if(s->object->value.literal.datatype) {
284 fputs("^^<", fh);
285 fputs((const char*)s->object->value.literal.datatype, fh);
286 fputc('>', fh);
287 }
288 } else if(s->object->type == RAPTOR_TERM_TYPE_URI)
289 fprintf(fh, "<%s>", (const char*)s->object->value.uri);
290 else /* blank node */
291 fprintf(fh, "_:%s", (const char*)s->object->value.blank.string);
292
293 fputs(" . \n", fh);
294 }
295
296
297 void
raptor_serializer_serialize_end(raptor_serializer * serializer)298 raptor_serializer_serialize_end(raptor_serializer* serializer)
299 {
300 fflush(serializer->fh);
301 }
302
303 void
raptor_statement_init(raptor_statement * statement,raptor_world * world)304 raptor_statement_init(raptor_statement *statement, raptor_world *world)
305 {
306 memset(statement, '\0', sizeof(*statement));
307 return;
308 }
309
310 void
raptor_statement_clear(raptor_statement * statement)311 raptor_statement_clear(raptor_statement *statement)
312 {
313 raptor_free_term(statement->subject);
314 raptor_free_term(statement->predicate);
315 raptor_free_term(statement->object);
316 }
317