diff options
author | David Robillard <d@drobilla.net> | 2020-06-28 17:13:23 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-10-27 13:13:59 +0100 |
commit | a749f75df13a56dc0649435a36ce2bc882cc134a (patch) | |
tree | 016b0b03abc2567460fe3a0ed461c02e5056269c | |
parent | b3d80af60ff695a9f6feb19d08bba0133aef3fdc (diff) | |
download | serd-a749f75df13a56dc0649435a36ce2bc882cc134a.tar.gz serd-a749f75df13a56dc0649435a36ce2bc882cc134a.tar.bz2 serd-a749f75df13a56dc0649435a36ce2bc882cc134a.zip |
WIP: Get base URI from single input path if possible
-rw-r--r-- | src/serdi.c | 45 |
1 files changed, 43 insertions, 2 deletions
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 <fcntl.h> #include <io.h> +#include <windows.h> #endif #include <limits.h> @@ -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) { |