Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sordi.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. Copyright 2011-2013 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #define _BSD_SOURCE // for realpath
  15. #include <assert.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #ifdef _WIN32
  19. # include <windows.h>
  20. #endif
  21. #include "serd/serd.h"
  22. #include "sord/sord.h"
  23. #include "sord_config.h"
  24. #define SORDI_ERROR(msg) fprintf(stderr, "sordi: " msg);
  25. #define SORDI_ERRORF(fmt, ...) fprintf(stderr, "sordi: " fmt, __VA_ARGS__);
  26. typedef struct {
  27. SerdWriter* writer;
  28. SerdEnv* env;
  29. SerdNode base_uri_node;
  30. SerdURI base_uri;
  31. SordModel* sord;
  32. } State;
  33. static int
  34. print_version(void)
  35. {
  36. printf("sordi " SORD_VERSION " <http://drobilla.net/software/sord>\n");
  37. printf("Copyright 2011-2013 David Robillard <http://drobilla.net>.\n"
  38. "License: <http://www.opensource.org/licenses/isc>\n"
  39. "This is free software; you are free to change and redistribute it."
  40. "\nThere is NO WARRANTY, to the extent permitted by law.\n");
  41. return 0;
  42. }
  43. static int
  44. print_usage(const char* name, bool error)
  45. {
  46. FILE* const os = error ? stderr : stdout;
  47. fprintf(os, "%s", error ? "\n" : "");
  48. fprintf(os, "Usage: %s [OPTION]... INPUT [BASE_URI]\n", name);
  49. fprintf(os, "Load and re-serialise RDF data.\n");
  50. fprintf(os, "Use - for INPUT to read from standard input.\n\n");
  51. fprintf(os, " -h Display this help and exit\n");
  52. fprintf(os, " -i SYNTAX Input syntax (`turtle' or `ntriples')\n");
  53. fprintf(os, " -o SYNTAX Output syntax (`turtle' or `ntriples')\n");
  54. fprintf(os, " -s INPUT Parse INPUT as string (terminates options)\n");
  55. fprintf(os, " -v Display version information and exit\n");
  56. return error ? 1 : 0;
  57. }
  58. static bool
  59. set_syntax(SerdSyntax* syntax, const char* name)
  60. {
  61. if (!strcmp(name, "turtle")) {
  62. *syntax = SERD_TURTLE;
  63. } else if (!strcmp(name, "ntriples")) {
  64. *syntax = SERD_NTRIPLES;
  65. } else {
  66. SORDI_ERRORF("unknown syntax `%s'\n", name);
  67. return false;
  68. }
  69. return true;
  70. }
  71. static uint8_t*
  72. absolute_path(const uint8_t* path)
  73. {
  74. #ifdef _WIN32
  75. char* out = (char*)malloc(MAX_PATH);
  76. GetFullPathName((const char*)path, MAX_PATH, out, NULL);
  77. return (uint8_t*)out;
  78. #else
  79. return (uint8_t*)realpath((const char*)path, NULL);
  80. #endif
  81. }
  82. int
  83. main(int argc, char** argv)
  84. {
  85. if (argc < 2) {
  86. return print_usage(argv[0], true);
  87. }
  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);
  112. }
  113. if (!set_syntax(&input_syntax, argv[a])) {
  114. return print_usage(argv[0], true);
  115. }
  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);
  120. }
  121. if (!set_syntax(&output_syntax, argv[a])) {
  122. return print_usage(argv[0], true);
  123. }
  124. } else {
  125. SORDI_ERRORF("invalid option -- '%s'\n", argv[a] + 1);
  126. return print_usage(argv[0], true);
  127. }
  128. }
  129. if (a == argc) {
  130. SORDI_ERROR("missing input\n");
  131. return print_usage(argv[0], true);
  132. }
  133. const uint8_t* input = (const uint8_t*)argv[a++];
  134. uint8_t* in_path = NULL;
  135. if (from_file) {
  136. in_name = in_name ? in_name : input;
  137. if (!in_fd) {
  138. in_path = absolute_path(serd_uri_to_path(in_name));
  139. if (!in_path || !(in_fd = fopen((const char*)in_path, "rb"))) {
  140. return 1;
  141. }
  142. }
  143. }
  144. SerdURI base_uri = SERD_URI_NULL;
  145. SerdNode base_uri_node = SERD_NODE_NULL;
  146. if (a < argc) { // Base URI given on command line
  147. base_uri_node = serd_node_new_uri_from_string(
  148. (const uint8_t*)argv[a], NULL, &base_uri);
  149. } else if (in_fd != stdin && from_file) { // Use input file URI
  150. base_uri_node = serd_node_new_file_uri(in_path, NULL, &base_uri, false);
  151. }
  152. free(in_path);
  153. if (!base_uri_node.buf) {
  154. SORDI_ERROR("missing base URI\n");
  155. return print_usage(argv[0], true);
  156. }
  157. SordWorld* world = sord_world_new();
  158. SordModel* sord = sord_new(world, SORD_SPO|SORD_OPS, false);
  159. SerdEnv* env = serd_env_new(&base_uri_node);
  160. SerdReader* reader = sord_new_reader(sord, env, input_syntax, NULL);
  161. const SerdStatus status = (from_file)
  162. ? serd_reader_read_file_handle(reader, in_fd, in_name)
  163. : serd_reader_read_string(reader, input);
  164. serd_reader_free(reader);
  165. SerdEnv* write_env = serd_env_new(&base_uri_node);
  166. int output_style = SERD_STYLE_RESOLVED;
  167. if (output_syntax == SERD_NTRIPLES) {
  168. output_style |= SERD_STYLE_ASCII;
  169. } else {
  170. output_style |= SERD_STYLE_CURIED | SERD_STYLE_ABBREVIATED;
  171. }
  172. SerdWriter* writer = serd_writer_new(
  173. output_syntax,
  174. (SerdStyle)output_style,
  175. write_env, &base_uri, serd_file_sink, stdout);
  176. // Write @prefix directives
  177. serd_env_foreach(env,
  178. (SerdPrefixSink)serd_writer_set_prefix,
  179. writer);
  180. // Write statements
  181. sord_write(sord, writer, NULL);
  182. serd_writer_finish(writer);
  183. serd_writer_free(writer);
  184. serd_env_free(env);
  185. serd_env_free(write_env);
  186. serd_node_free(&base_uri_node);
  187. sord_free(sord);
  188. sord_world_free(world);
  189. return (status > SERD_FAILURE) ? 1 : 0;
  190. }