Add initial bits for Qt6 support
[carla.git] / source / modules / lilv / sord-0.16.0 / src / sordi.c
blob8ddbfb408f59fc2223961edc8c90f8e631a7c812
1 /*
2 Copyright 2011-2016 David Robillard <http://drobilla.net>
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <assert.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #ifdef _WIN32
22 # include <windows.h>
23 #endif
25 #include "serd/serd.h"
26 #include "sord/sord.h"
27 #include "sord_config.h"
29 #define SORDI_ERROR(msg) fprintf(stderr, "sordi: " msg);
30 #define SORDI_ERRORF(fmt, ...) fprintf(stderr, "sordi: " fmt, __VA_ARGS__);
32 typedef struct {
33 SerdWriter* writer;
34 SerdEnv* env;
35 SerdNode base_uri_node;
36 SerdURI base_uri;
37 SordModel* sord;
38 } State;
40 static int
41 print_version(void)
43 printf("sordi " SORD_VERSION " <http://drobilla.net/software/sord>\n");
44 printf("Copyright 2011-2016 David Robillard <http://drobilla.net>.\n"
45 "License: <http://www.opensource.org/licenses/isc>\n"
46 "This is free software; you are free to change and redistribute it."
47 "\nThere is NO WARRANTY, to the extent permitted by law.\n");
48 return 0;
51 static int
52 print_usage(const char* name, bool error)
54 FILE* const os = error ? stderr : stdout;
55 fprintf(os, "%s", error ? "\n" : "");
56 fprintf(os, "Usage: %s [OPTION]... INPUT [BASE_URI]\n", name);
57 fprintf(os, "Load and re-serialise RDF data.\n");
58 fprintf(os, "Use - for INPUT to read from standard input.\n\n");
59 fprintf(os, " -h Display this help and exit\n");
60 fprintf(os, " -i SYNTAX Input syntax (`turtle' or `ntriples')\n");
61 fprintf(os, " -o SYNTAX Output syntax (`turtle' or `ntriples')\n");
62 fprintf(os, " -s INPUT Parse INPUT as string (terminates options)\n");
63 fprintf(os, " -v Display version information and exit\n");
64 return error ? 1 : 0;
67 static bool
68 set_syntax(SerdSyntax* syntax, const char* name)
70 if (!strcmp(name, "turtle")) {
71 *syntax = SERD_TURTLE;
72 } else if (!strcmp(name, "ntriples")) {
73 *syntax = SERD_NTRIPLES;
74 } else {
75 SORDI_ERRORF("unknown syntax `%s'\n", name);
76 return false;
78 return true;
81 int
82 main(int argc, char** argv)
84 if (argc < 2) {
85 return print_usage(argv[0], true);
88 FILE* in_fd = NULL;
89 SerdSyntax input_syntax = SERD_TURTLE;
90 SerdSyntax output_syntax = SERD_NTRIPLES;
91 bool from_file = true;
92 const uint8_t* in_name = NULL;
93 int a = 1;
94 for (; a < argc && argv[a][0] == '-'; ++a) {
95 if (argv[a][1] == '\0') {
96 in_name = (const uint8_t*)"(stdin)";
97 in_fd = stdin;
98 break;
99 } else if (argv[a][1] == 'h') {
100 return print_usage(argv[0], false);
101 } else if (argv[a][1] == 'v') {
102 return print_version();
103 } else if (argv[a][1] == 's') {
104 in_name = (const uint8_t*)"(string)";
105 from_file = false;
106 ++a;
107 break;
108 } else if (argv[a][1] == 'i') {
109 if (++a == argc) {
110 SORDI_ERROR("option requires an argument -- 'i'\n\n");
111 return print_usage(argv[0], true);
113 if (!set_syntax(&input_syntax, argv[a])) {
114 return print_usage(argv[0], true);
116 } else if (argv[a][1] == 'o') {
117 if (++a == argc) {
118 SORDI_ERROR("option requires an argument -- 'o'\n\n");
119 return print_usage(argv[0], true);
121 if (!set_syntax(&output_syntax, argv[a])) {
122 return print_usage(argv[0], true);
124 } else {
125 SORDI_ERRORF("invalid option -- '%s'\n", argv[a] + 1);
126 return print_usage(argv[0], true);
130 if (a == argc) {
131 SORDI_ERROR("missing input\n");
132 return print_usage(argv[0], true);
135 const uint8_t* input = (const uint8_t*)argv[a++];
136 if (from_file) {
137 in_name = in_name ? in_name : input;
138 if (!in_fd) {
139 input = serd_uri_to_path(in_name);
140 if (!input || !(in_fd = fopen((const char*)input, "rb"))) {
141 return 1;
146 SerdURI base_uri = SERD_URI_NULL;
147 SerdNode base = SERD_NODE_NULL;
148 if (a < argc) { // Base URI given on command line
149 base = serd_node_new_uri_from_string(
150 (const uint8_t*)argv[a], NULL, &base_uri);
151 } else if (from_file && in_fd != stdin) { // Use input file URI
152 base = serd_node_new_file_uri(input, NULL, &base_uri, false);
155 SordWorld* world = sord_world_new();
156 SordModel* sord = sord_new(world, SORD_SPO|SORD_OPS, false);
157 SerdEnv* env = serd_env_new(&base);
158 SerdReader* reader = sord_new_reader(sord, env, input_syntax, NULL);
160 SerdStatus status = (from_file)
161 ? serd_reader_read_file_handle(reader, in_fd, in_name)
162 : serd_reader_read_string(reader, input);
164 serd_reader_free(reader);
166 FILE* out_fd = stdout;
167 SerdEnv* write_env = serd_env_new(&base);
169 int output_style = SERD_STYLE_RESOLVED;
170 if (output_syntax == SERD_NTRIPLES) {
171 output_style |= SERD_STYLE_ASCII;
172 } else {
173 output_style |= SERD_STYLE_CURIED | SERD_STYLE_ABBREVIATED;
176 SerdWriter* writer = serd_writer_new(
177 output_syntax,
178 (SerdStyle)output_style,
179 write_env, &base_uri, serd_file_sink, stdout);
181 // Write @prefix directives
182 serd_env_foreach(env,
183 (SerdPrefixSink)serd_writer_set_prefix,
184 writer);
186 // Write statements
187 sord_write(sord, writer, NULL);
189 serd_writer_finish(writer);
190 serd_writer_free(writer);
192 serd_env_free(env);
193 serd_env_free(write_env);
194 serd_node_free(&base);
196 sord_free(sord);
197 sord_world_free(world);
199 if (from_file) {
200 fclose(in_fd);
203 if (fclose(out_fd)) {
204 perror("sordi: write error");
205 status = SERD_ERR_UNKNOWN;
208 return (status > SERD_FAILURE) ? 1 : 0;