aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2017-09-24 16:36:56 +0200
committerDavid Robillard <d@drobilla.net>2017-09-24 19:02:59 +0200
commitf34171637b94c204971a2256849491f025862373 (patch)
treeb6d2a1a89b013534ea5b44cdbb5a983fe98f3a01
parentf1d036ccba93c9e9d89dcbdae720887a630a35e5 (diff)
downloadserd-f34171637b94c204971a2256849491f025862373.tar.gz
serd-f34171637b94c204971a2256849491f025862373.tar.bz2
serd-f34171637b94c204971a2256849491f025862373.zip
Make serdi guess input syntax from extension if unspecified
-rw-r--r--NEWS3
-rw-r--r--src/serdi.c29
2 files changed, 29 insertions, 3 deletions
diff --git a/NEWS b/NEWS
index 08fb2a3b..04684ba3 100644
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,6 @@
serd (0.29.3) unstable;
+ * Make serdi guess input syntax from extension if unspecified
* Make serdi syntax options case-insensitive
* Add basic error tolerant parsing for line-based formats
* Support strict parsing of prefixed names
@@ -8,7 +9,7 @@ serd (0.29.3) unstable;
* Add NTriples test suite
* Fix building with MSVC
- -- David Robillard <d@drobilla.net> Sun, 24 Sep 2017 16:34:20 +0200
+ -- David Robillard <d@drobilla.net> Sun, 24 Sep 2017 16:36:37 +0200
serd (0.28.0) stable;
diff --git a/src/serdi.c b/src/serdi.c
index d2af3e05..59dbaf35 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -50,6 +50,20 @@ get_syntax(const char* name)
return (SerdSyntax)0;
}
+static SerdSyntax
+guess_syntax(const char* filename)
+{
+ const char* ext = strrchr(filename, '.');
+ if (ext) {
+ for (const Syntax* s = syntaxes; s->name; ++s) {
+ if (!serd_strncasecmp(s->extension, ext, strlen(ext))) {
+ return s->syntax;
+ }
+ }
+ }
+ return (SerdSyntax)0;
+}
+
static int
print_version(void)
{
@@ -106,8 +120,8 @@ main(int argc, char** argv)
}
FILE* in_fd = NULL;
- SerdSyntax input_syntax = SERD_TURTLE;
- SerdSyntax output_syntax = SERD_NTRIPLES;
+ SerdSyntax input_syntax = (SerdSyntax)0;
+ SerdSyntax output_syntax = (SerdSyntax)0;
bool from_file = true;
bool bulk_read = true;
bool bulk_write = false;
@@ -192,6 +206,17 @@ main(int argc, char** argv)
}
}
+ if (!input_syntax && !(input_syntax = guess_syntax((const char*)in_name))) {
+ input_syntax = SERD_TRIG;
+ }
+
+ if (!output_syntax) {
+ output_syntax = (
+ (input_syntax == SERD_TURTLE || input_syntax == SERD_NTRIPLES)
+ ? SERD_NTRIPLES
+ : SERD_NQUADS);
+ }
+
SerdURI base_uri = SERD_URI_NULL;
SerdNode base = SERD_NODE_NULL;
if (a < argc) { // Base URI given on command line