From a749f75df13a56dc0649435a36ce2bc882cc134a Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 28 Jun 2020 17:13:23 +0200 Subject: WIP: Get base URI from single input path if possible --- src/serdi.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'src/serdi.c') diff --git a/src/serdi.c b/src/serdi.c index de422092..1e9ceb88 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -14,13 +14,17 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#define _XOPEN_SOURCE 600 /* for realpath */ + #include "system.h" #include "serd/serd.h" #ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN 1 #include #include +#include #endif #include @@ -39,6 +43,32 @@ typedef struct { SerdNode* g; } FilterPattern; +static char* +serd_realpath(const char* const path) +{ + if (!path) { + return NULL; + } + +#ifdef _WIN32 + const DWORD size = GetFullPathName(path, 0, NULL, NULL); + if (size == 0) { + return NULL; + } + + char* const out = (char*)calloc(size, 1); + const DWORD ret = GetFullPathName(path, MAX_PATH, out, NULL); + if (ret == 0 || ret >= size) { + free(out); + return NULL; + } + + return out; +#else + return realpath(path, NULL); +#endif +} + static int print_version(void) { @@ -311,6 +341,9 @@ main(int argc, char** argv) _setmode(_fileno(stdout), _O_BINARY); #endif + char** inputs = argv + a; + int n_inputs = argc - a; + bool input_has_graphs = serd_syntax_has_graphs(input_syntax); for (int i = a; i < argc; ++i) { if (serd_syntax_has_graphs(serd_guess_syntax(argv[i]))) { @@ -323,6 +356,16 @@ main(int argc, char** argv) output_syntax = input_has_graphs ? SERD_NQUADS : SERD_NTRIPLES; } + if (!base && n_inputs == 1 && + (output_syntax == SERD_NQUADS || output_syntax == SERD_NTRIPLES)) { + // Choose base URI from the single input path + char* const abs_path = serd_realpath(inputs[0]); + if (abs_path) { + base = serd_new_file_uri(abs_path, NULL); + free(abs_path); + } + } + FILE* out_fd = stdout; SerdWorld* world = serd_world_new(); SerdEnv* env = serd_env_new(base); @@ -397,8 +440,6 @@ main(int argc, char** argv) serd_reader_free(reader); } - char** inputs = argv + a; - int n_inputs = argc - a; size_t prefix_len = 0; char* prefix = NULL; if (n_inputs > 1) { -- cgit v1.2.1