File: srxwrite.c

package info (click to toggle)
rasqal 0.9.33-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,452 kB
  • sloc: ansic: 42,463; sh: 12,077; yacc: 4,986; makefile: 2,443; perl: 2,063; lex: 1,948; xml: 555
file content (197 lines) | stat: -rw-r--r-- 4,962 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* -*- Mode: c; c-basic-offset: 2 -*-
 *
 * srxwrite.c - SPARQL Results Format writing test program
 *
 * Copyright (C) 2010, David Beckett http://www.dajobe.org/
 * 
 * This package is Free Software and part of Redland http://librdf.org/
 * 
 * It is licensed under the following three licenses as alternatives:
 *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
 *   2. GNU General Public License (GPL) V2 or any newer version
 *   3. Apache License, V2.0 or any newer version
 * 
 * You may not use this file except in compliance with at least one of
 * the above three licenses.
 * 
 * See LICENSE.html or LICENSE.txt at the top of this package for the
 * complete terms and further detail along with the license texts for
 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
 * 
 * 
 */


#ifdef HAVE_CONFIG_H
#include <rasqal_config.h>
#endif

#ifdef WIN32
#include <win32_rasqal_config.h>
#endif

#include <stdio.h>
#include <string.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif


#include <raptor.h>

/* Rasqal includes */
#include <rasqal.h>

#ifdef RAPTOR_V2_AVAILABLE
#else
#define raptor_new_iostream_to_file_handle(world, fh) raptor_new_iostream_to_file_handle(fh)
#endif

static char *program = NULL;

int main(int argc, char *argv[]);

int
main(int argc, char *argv[]) 
{ 
  int rc = 0;
  raptor_iostream* iostr = NULL;
  char* p;
  raptor_uri* base_uri = NULL;
  rasqal_query_results* results = NULL;
  const char* write_formatter_name = NULL;
  rasqal_query_results_formatter* write_formatter = NULL;
  rasqal_world *world;
  rasqal_variables_table* vars_table = NULL;
  rasqal_row* row = NULL;
  rasqal_literal* l = NULL;
#ifdef RAPTOR_V2_AVAILABLE
  raptor_world *raptor_world_ptr;
#endif
  
  program = argv[0];
  if((p=strrchr(program, '/')))
    program = p+1;
  else if((p=strrchr(program, '\\')))
    program = p+1;
  argv[0] = program;
  
  world = rasqal_new_world();
  if(!world || rasqal_world_open(world)) {
    fprintf(stderr, "%s: rasqal_world init failed\n", program);
    return(1);
  }

  if(argc < 2 || argc > 2) {
    fprintf(stderr, "USAGE: %s [<write formatter>]\n",
            program);

    rc = 1;
    goto tidy;
  }

  if(argc > 1) {
    if(strcmp(argv[1], "-"))
      write_formatter_name = argv[1];
  }

#ifdef RAPTOR_V2_AVAILABLE
  raptor_world_ptr = rasqal_world_get_raptor(world);
#endif
  
  vars_table = rasqal_new_variables_table(world);
  if(!vars_table) {
    fprintf(stderr, "%s: Failed to create variables table\n", program);
    rc = 1;
    goto tidy;
  }

#define NUMBER_VARIABLES 2
  rasqal_variables_table_add2(vars_table, RASQAL_VARIABLE_TYPE_NORMAL,
                              (const unsigned char*)"a", 1, NULL);
  rasqal_variables_table_add2(vars_table, RASQAL_VARIABLE_TYPE_NORMAL,
                              (const unsigned char*)"b", 1, NULL);

  results = rasqal_new_query_results2(world, NULL,
                                      RASQAL_QUERY_RESULTS_BINDINGS);
  if(!results) {
    fprintf(stderr, "%s: Failed to create query results\n", program);
    rc = 1;
    goto tidy;
  }

  row = rasqal_new_row_for_size(world, NUMBER_VARIABLES);

  l = rasqal_new_boolean_literal(world, 1);
  if(!l) {
    fprintf(stderr, "%s: Failed to create boolean literal\n", program);
    rc = 1;
    goto tidy;
  }
  rasqal_row_set_value_at(row, 0, l);
  rasqal_free_literal(l); l = NULL;
  
  l = rasqal_new_integer_literal(world, RASQAL_LITERAL_INTEGER, 42);
  if(!l) {
    fprintf(stderr, "%s: Failed to integer boolean literal\n", program);
    rc = 1;
    goto tidy;
  }
  rasqal_row_set_value_at(row, 1, l);
  rasqal_free_literal(l); l = NULL;

  rasqal_query_results_add_row(results, row);
  row = NULL; /* now owned by results */


  write_formatter = rasqal_new_query_results_formatter(world, 
                                                       write_formatter_name,
                                                       NULL, NULL);
  if(!write_formatter) {
    fprintf(stderr, "%s: Failed to create query results write formatter '%s'\n",
            program, write_formatter_name);
    rc = 1;
    goto tidy;
  }
  
  iostr = raptor_new_iostream_to_file_handle(raptor_world_ptr, stdout);
  if(!iostr) {
    fprintf(stderr, "%s: Creating output iostream failed\n", program);
  } else {
    rasqal_query_results_formatter_write(iostr, write_formatter,
                                         results, base_uri);
    raptor_free_iostream(iostr);
  }

  iostr = NULL;
  

  tidy:
  if(l)
    rasqal_free_literal(l);

  if(row)
    rasqal_free_row(row);

  if(write_formatter)
    rasqal_free_query_results_formatter(write_formatter);

  if(iostr)
    raptor_free_iostream(iostr);
  
  if(results)
    rasqal_free_query_results(results);

  if(vars_table)
    rasqal_free_variables_table(vars_table);

  if(base_uri)
    raptor_free_uri(base_uri);

  rasqal_free_world(world);

  return (rc);
}