1 /* -*- Mode: c; c-basic-offset: 2 -*-
2  *
3  * raptor_locator.c - Raptor parsing locator functions
4  *
5  * Copyright (C) 2002-2006, David Beckett http://www.dajobe.org/
6  * Copyright (C) 2002-2006, 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 <raptor_config.h>
28 #endif
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <stdarg.h>
34 #ifdef HAVE_ERRNO_H
35 #include <errno.h>
36 #endif
37 #ifdef HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40 
41 /* Raptor includes */
42 #include "raptor2.h"
43 #include "raptor_internal.h"
44 
45 
46 /**
47  * raptor_locator_print:
48  * @locator: #raptor_locator to print
49  * @stream: stream to print to
50  *
51  * Print a raptor locator to a stream.
52  *
53  * Return value: non-0 on failure
54  **/
55 int
raptor_locator_print(raptor_locator * locator,FILE * stream)56 raptor_locator_print(raptor_locator* locator, FILE *stream)
57 {
58   if(!locator)
59     return 1;
60 
61   if(locator->uri)
62     fprintf(stream, "URI %s", raptor_uri_as_string(locator->uri));
63   else if(locator->file)
64     fprintf(stream, "file %s", locator->file);
65   else
66     return 0;
67   if(locator->line >= 0) {
68     fprintf(stream, ":%d", locator->line);
69     if(locator->column >= 0)
70       fprintf(stream, " column %d", locator->column);
71   }
72 
73   return 0;
74 }
75 
76 
77 /**
78  * raptor_locator_format:
79  * @buffer: buffer to store format
80  * @length: size of buffer (excluding NUL)
81  * @locator: #raptor_locator to format
82  *
83  * Format a raptor locator as a string.
84  *
85  * If buffer is NULL or @length is insufficient for the size of
86  * the locator, returns the number of additional bytes required
87  * in the buffer to write the locator.  Writes a terminating '\0'.
88  *
89  * Return value: 0 on success, >0 if additional bytes required in buffer, <0 on failure
90  **/
91 int
raptor_locator_format(char * buffer,size_t length,raptor_locator * locator)92 raptor_locator_format(char *buffer, size_t length, raptor_locator* locator)
93 {
94   size_t bufsize = 0;
95   const char* label_str;
96   size_t label_len = 0;
97   const char* value_str = NULL;
98   size_t value_len;
99 
100   if(!locator)
101     return -1;
102 
103   #define URI_STR "URI "
104   #define URI_STR_LEN 4  /* strlen(URI_STR) */
105   #define FILE_STR "file "
106   #define FILE_STR_LEN 5  /* strlen(FILE_STR) */
107   #define COLUMN_STR " column "
108   #define COLUMN_STR_LEN 8  /* strlen(COLUMN_STR) */
109 
110   if(locator->uri) {
111     label_str = URI_STR;
112     label_len = URI_STR_LEN;
113     value_str = (const char*)raptor_uri_as_counted_string(locator->uri,
114                                                           &value_len);
115   } else if(locator->file) {
116     label_str = FILE_STR;
117     label_len = FILE_STR_LEN;
118     value_str = locator->file;
119     value_len = strlen(value_str);
120   } else
121     return -1;
122 
123   bufsize = label_len + value_len;
124 
125   if(locator->line > 0) {
126     bufsize += 1 + raptor_format_integer(NULL, 0, locator->line, /* base */ 10,
127                                          -1, '\0');
128     if(locator->column >= 0)
129       bufsize += COLUMN_STR_LEN +
130         raptor_format_integer(NULL, 0, locator->column, /* base */ 10,
131                               -1, '\0');
132   }
133 
134   if(!buffer || !length || length < (bufsize + 1)) /* +1 for NUL */
135     return RAPTOR_BAD_CAST(int, bufsize);
136 
137 
138   memcpy(buffer, label_str, label_len);
139   buffer += label_len;
140   memcpy(buffer, value_str, value_len);
141   buffer += value_len;
142 
143   if(locator->line > 0) {
144     *buffer ++= ':';
145     buffer += raptor_format_integer(buffer, length,
146                                     locator->line, /* base */ 10,
147                                     -1, '\0');
148     if(locator->column >= 0) {
149       memcpy(buffer, COLUMN_STR, COLUMN_STR_LEN);
150       buffer += COLUMN_STR_LEN;
151       buffer += raptor_format_integer(buffer, length,
152                                       locator->column, /* base */ 10,
153                                       -1, '\0');
154     }
155   }
156   *buffer = '\0';
157 
158   return 0;
159 }
160 
161 
162 /**
163  * raptor_locator_line:
164  * @locator: locator
165  *
166  * Get line number from locator.
167  *
168  * Return value: integer line number, or -1 if there is no line number available
169  **/
170 int
raptor_locator_line(raptor_locator * locator)171 raptor_locator_line(raptor_locator *locator)
172 {
173   if(!locator)
174     return -1;
175   return locator->line;
176 }
177 
178 
179 /**
180  * raptor_locator_column:
181  * @locator: locator
182  *
183  * Get column number from locator.
184  *
185  * Return value: integer column number, or -1 if there is no column number available
186  **/
187 int
raptor_locator_column(raptor_locator * locator)188 raptor_locator_column(raptor_locator *locator)
189 {
190   if(!locator)
191     return -1;
192   return locator->column;
193 }
194 
195 
196 /**
197  * raptor_locator_byte:
198  * @locator: locator
199  *
200  * Get the locator byte offset from locator.
201  *
202  * Return value: integer byte number, or -1 if there is no byte offset available
203  **/
204 int
raptor_locator_byte(raptor_locator * locator)205 raptor_locator_byte(raptor_locator *locator)
206 {
207   if(!locator)
208     return -1;
209   return locator->byte;
210 }
211 
212 
213 /**
214  * raptor_locator_file:
215  * @locator: locator
216  *
217  * Get file name from locator.
218  *
219  * Return value: string file name, or NULL if there is no filename available
220  **/
221 const char *
raptor_locator_file(raptor_locator * locator)222 raptor_locator_file(raptor_locator *locator)
223 {
224   if(!locator)
225     return NULL;
226   return locator->file;
227 }
228 
229 
230 /**
231  * raptor_locator_uri:
232  * @locator: locator
233  *
234  * Get URI from locator.
235  *
236  * Returns a pointer to a shared string version of the URI in
237  * the locator.  This must be copied if it is needed.
238  *
239  * Return value: string URI, or NULL if there is no URI available
240  **/
241 const char *
raptor_locator_uri(raptor_locator * locator)242 raptor_locator_uri(raptor_locator *locator)
243 {
244   if(!locator)
245     return NULL;
246 
247   return (const char*)raptor_uri_as_string(locator->uri);
248 }
249