From f1d036ccba93c9e9d89dcbdae720887a630a35e5 Mon Sep 17 00:00:00 2001
From: David Robillard <d@drobilla.net>
Date: Sun, 24 Sep 2017 16:35:44 +0200
Subject: Make serdi syntax options case-insensitive

---
 src/reader.c        |  9 +--------
 src/serd_internal.h | 12 ++++++++++++
 src/serdi.c         | 48 ++++++++++++++++++++++++++++--------------------
 3 files changed, 41 insertions(+), 28 deletions(-)

(limited to 'src')

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') {
-- 
cgit v1.2.1