1 /* -*- Mode: c; c-basic-offset: 2 -*-
2 *
3 * rdf_statement_common.c - RDF Triple (Statement) interface
4 *
5 * Copyright (C) 2000-2008, David Beckett http://www.dajobe.org/
6 * Copyright (C) 2000-2005, University of Bristol, UK http://www.bristol.ac.uk/
7 *
8 * This package is Free Software and part of Redland http://librdf.org/
9 *
10 * It is licensed under the following three licenses as alternatives:
11 * 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
12 * 2. GNU General Public License (GPL) V2 or any newer version
13 * 3. Apache License, V2.0 or any newer version
14 *
15 * You may not use this file except in compliance with at least one of
16 * the above three licenses.
17 *
18 * See LICENSE.html or LICENSE.txt at the top of this package for the
19 * complete terms and further detail along with the license texts for
20 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
21 *
22 *
23 */
24
25
26 #ifdef HAVE_CONFIG_H
27 #include <rdf_config.h>
28 #endif
29
30 #ifdef WIN32
31 #include <win32_rdf_config.h>
32 #endif
33
34 #include <stdio.h>
35 #include <string.h>
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39
40 #include <redland.h>
41
42
43 #ifndef STANDALONE
44
45 /* class methods */
46
47 /**
48 * librdf_init_statement:
49 * @world: redland world object
50 *
51 * INTERNAL - Initialise the statement module.
52 *
53 **/
54 void
librdf_init_statement(librdf_world * world)55 librdf_init_statement(librdf_world *world)
56 {
57 }
58
59
60 /**
61 * librdf_finish_statement:
62 * @world: redland world object
63 *
64 * INTERNAL - Terminate the statement module.
65 *
66 **/
67 void
librdf_finish_statement(librdf_world * world)68 librdf_finish_statement(librdf_world *world)
69 {
70 }
71
72
73
74 /**
75 * librdf_statement_encode_parts2:
76 * @world: redland world object
77 * @statement: statement to serialise
78 * @context_node: #librdf_node context node (can be NULL)
79 * @buffer: the buffer to use
80 * @length: buffer size
81 * @fields: fields to encode
82 *
83 * Serialise parts of a statement into a buffer.
84 *
85 * Encodes the given statement in the buffer, which must be of sufficient
86 * size. If buffer is NULL, no work is done but the size of buffer
87 * required is returned.
88 *
89 * The fields values are or-ed combinations of:
90 * #LIBRDF_STATEMENT_SUBJECT #LIBRDF_STATEMENT_PREDICATE
91 * #LIBRDF_STATEMENT_OBJECT
92 * or #LIBRDF_STATEMENT_ALL for subject,prdicate,object fields
93 *
94 * If context_node is given, it is encoded also
95 *
96 * Return value: the number of bytes written or 0 on failure.
97 **/
98 size_t
librdf_statement_encode_parts2(librdf_world * world,librdf_statement * statement,librdf_node * context_node,unsigned char * buffer,size_t length,librdf_statement_part fields)99 librdf_statement_encode_parts2(librdf_world* world,
100 librdf_statement* statement,
101 librdf_node* context_node,
102 unsigned char *buffer, size_t length,
103 librdf_statement_part fields)
104 {
105 size_t total_length=0;
106 size_t node_len;
107 unsigned char *p;
108
109 LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, 0);
110
111 /* min size */
112 if(buffer && length < 1)
113 return 0;
114
115 p=buffer;
116 /* magic number 'x' */
117 if(p) {
118 *p++='x';
119 length--;
120 }
121 total_length++;
122
123 if((fields & LIBRDF_STATEMENT_SUBJECT) && statement->subject) {
124 /* 's' + subject */
125 if(p) {
126 if(length < 1)
127 return 0;
128 *p++='s';
129 length--;
130 }
131 total_length++;
132
133 node_len=librdf_node_encode(statement->subject, p, length);
134 if(!node_len)
135 return 0;
136 if(p) {
137 p += node_len;
138 length -= node_len;
139 }
140
141
142 total_length += node_len;
143 }
144
145
146 if((fields & LIBRDF_STATEMENT_PREDICATE) && statement->predicate) {
147 /* 'p' + predicate */
148 if(p) {
149 if(length < 1)
150 return 0;
151
152 *p++='p';
153 length--;
154 }
155 total_length++;
156
157 node_len=librdf_node_encode(statement->predicate, p, length);
158 if(!node_len)
159 return 0;
160 if(p) {
161 p += node_len;
162 length -= node_len;
163 }
164
165 total_length += node_len;
166 }
167
168 if((fields & LIBRDF_STATEMENT_OBJECT) && statement->object) {
169 /* 'o' object */
170 if(p) {
171 if(length < 1)
172 return 0;
173
174 *p++='o';
175 length--;
176 }
177 total_length++;
178
179 node_len= librdf_node_encode(statement->object, p, length);
180 if(!node_len)
181 return 0;
182 if(p) {
183 p += node_len;
184 length -= node_len;
185 }
186
187 total_length += node_len;
188 }
189
190 if(context_node) {
191 /* 'o' object */
192 if(p) {
193 *p++='c';
194 length--;
195 }
196 total_length++;
197
198 node_len= librdf_node_encode(context_node, p, length);
199 if(!node_len)
200 return 0;
201
202 /* p and length changes never needed to be calculated [clang] */
203 /*
204 if(p) {
205 p += node_len;
206 length -= node_len;
207 }
208 */
209
210 total_length += node_len;
211 }
212
213 return total_length;
214 }
215
216 #endif
217