aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2017-09-24 16:35:44 +0200
committerDavid Robillard <d@drobilla.net>2017-09-24 19:02:59 +0200
commitf1d036ccba93c9e9d89dcbdae720887a630a35e5 (patch)
treecc19501cdb1aaa59c17bc2ccef4495c528c2cfc6
parent8d954ab071e286f0b2bdfc542bb3725eb5a2ab0e (diff)
downloadserd-f1d036ccba93c9e9d89dcbdae720887a630a35e5.tar.gz
serd-f1d036ccba93c9e9d89dcbdae720887a630a35e5.tar.bz2
serd-f1d036ccba93c9e9d89dcbdae720887a630a35e5.zip
Make serdi syntax options case-insensitive
-rw-r--r--NEWS3
-rw-r--r--doc/serdi.16
-rw-r--r--src/reader.c9
-rw-r--r--src/serd_internal.h12
-rw-r--r--src/serdi.c48
5 files changed, 47 insertions, 31 deletions
diff --git a/NEWS b/NEWS
index ed213940..08fb2a3b 100644
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,6 @@
serd (0.29.3) unstable;
+ * Make serdi syntax options case-insensitive
* Add basic error tolerant parsing for line-based formats
* Support strict parsing of prefixed names
* Report errors for all failing test cases
@@ -7,7 +8,7 @@ serd (0.29.3) unstable;
* Add NTriples test suite
* Fix building with MSVC
- -- David Robillard <d@drobilla.net> Sat, 23 Sep 2017 20:33:19 +0200
+ -- David Robillard <d@drobilla.net> Sun, 24 Sep 2017 16:34:20 +0200
serd (0.28.0) stable;
diff --git a/doc/serdi.1 b/doc/serdi.1
index ad1772fc..c11d2b3b 100644
--- a/doc/serdi.1
+++ b/doc/serdi.1
@@ -34,7 +34,8 @@ Print the command line options.
.TP
\fB\-i SYNTAX\fR
-Read input in SYNTAX (turtle/ntriples/trig/nquads).
+Read input as SYNTAX.
+Valid values (case-insensitive): turtle, ntriples, trig, nquads.
.TP
\fB\-l\fR
@@ -42,7 +43,8 @@ Lax (non-strict) parsing.
.TP
\fB\-o SYNTAX\fR
-Write output in SYNTAX (turtle/ntriples/nquads).
+Write output as SYNTAX.
+Valid values (case-insensitive): turtle, ntriples, trig, nquads.
.TP
\fB\-p PREFIX\fR
diff --git a/src/reader.c b/src/reader.c
index 9e30f9c1..788d0a74 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -1593,14 +1593,7 @@ tokcmp(SerdReader* reader, Ref ref, const char* tok, size_t n)
if (!node || node->n_bytes != n) {
return -1;
}
- const char* s1 = (const char*)node->buf;
- const char* s2 = tok;
- for (; n > 0 && *s2; s1++, s2++, --n) {
- if (toupper(*s1) != toupper(*s2)) {
- return ((*(uint8_t*)s1 < *(uint8_t*)s2) ? -1 : +1);
- }
- }
- return 0;
+ return serd_strncasecmp((const char*)node->buf, tok, n);
}
static bool
diff --git a/src/serd_internal.h b/src/serd_internal.h
index 6a2893fb..9af5195b 100644
--- a/src/serd_internal.h
+++ b/src/serd_internal.h
@@ -20,6 +20,7 @@
#define _POSIX_C_SOURCE 200809L /* for posix_memalign and posix_fadvise */
#include <assert.h>
+#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@@ -335,6 +336,17 @@ serd_substrlen(const uint8_t* str,
size_t* n_bytes,
SerdNodeFlags* flags);
+static inline int
+serd_strncasecmp(const char* s1, const char* s2, size_t n)
+{
+ for (; n > 0 && *s2; s1++, s2++, --n) {
+ if (toupper(*s1) != toupper(*s2)) {
+ return ((*(uint8_t*)s1 < *(uint8_t*)s2) ? -1 : +1);
+ }
+ }
+ return 0;
+}
+
static inline uint32_t
utf8_num_bytes(const uint8_t c)
{
diff --git a/src/serdi.c b/src/serdi.c
index 75a978b9..d2af3e05 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -24,6 +24,32 @@
#define SERDI_ERROR(msg) fprintf(stderr, "serdi: " msg);
#define SERDI_ERRORF(fmt, ...) fprintf(stderr, "serdi: " fmt, __VA_ARGS__);
+typedef struct {
+ SerdSyntax syntax;
+ const char* name;
+ const char* extension;
+} Syntax;
+
+static const Syntax syntaxes[] = {
+ {SERD_TURTLE, "turtle", ".ttl"},
+ {SERD_NTRIPLES, "ntriples", ".nt"},
+ {SERD_NQUADS, "nquads", ".nq"},
+ {SERD_TRIG, "trig", ".trig"},
+ {(SerdSyntax)0, NULL, NULL}
+};
+
+static SerdSyntax
+get_syntax(const char* name)
+{
+ for (const Syntax* s = syntaxes; s->name; ++s) {
+ if (!serd_strncasecmp(s->name, name, strlen(name))) {
+ return s->syntax;
+ }
+ }
+ SERDI_ERRORF("unknown syntax `%s'\n", name);
+ return (SerdSyntax)0;
+}
+
static int
print_version(void)
{
@@ -59,24 +85,6 @@ print_usage(const char* name, bool error)
return error ? 1 : 0;
}
-static bool
-set_syntax(SerdSyntax* syntax, const char* name)
-{
- if (!strcmp(name, "turtle")) {
- *syntax = SERD_TURTLE;
- } else if (!strcmp(name, "ntriples")) {
- *syntax = SERD_NTRIPLES;
- } else if (!strcmp(name, "nquads")) {
- *syntax = SERD_NQUADS;
- } else if (!strcmp(name, "trig")) {
- *syntax = SERD_TRIG;
- } else {
- SERDI_ERRORF("unknown syntax `%s'\n", name);
- return false;
- }
- return true;
-}
-
static int
missing_arg(const char* name, char opt)
{
@@ -138,13 +146,13 @@ main(int argc, char** argv)
} else if (argv[a][1] == 'i') {
if (++a == argc) {
return missing_arg(argv[0], 'i');
- } else if (!set_syntax(&input_syntax, argv[a])) {
+ } else if (!(input_syntax = get_syntax(argv[a]))) {
return print_usage(argv[0], true);
}
} else if (argv[a][1] == 'o') {
if (++a == argc) {
return missing_arg(argv[0], 'o');
- } else if (!set_syntax(&output_syntax, argv[a])) {
+ } else if (!(output_syntax = get_syntax(argv[a]))) {
return print_usage(argv[0], true);
}
} else if (argv[a][1] == 'p') {