aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-05-27 15:32:12 +0200
committerDavid Robillard <d@drobilla.net>2018-05-27 21:10:21 +0200
commit7a4582a570657cee6e62842a98a0c7be3da7cb03 (patch)
tree23dbc4451c0764caf387675495f6ea67438b95f4
parentc03c2f77ac51ac12b46408aa6a63a06dc7eefdb8 (diff)
downloadserd-7a4582a570657cee6e62842a98a0c7be3da7cb03.tar.gz
serd-7a4582a570657cee6e62842a98a0c7be3da7cb03.tar.bz2
serd-7a4582a570657cee6e62842a98a0c7be3da7cb03.zip
Move syntax from string/filename utilities to public API
-rw-r--r--serd/serd.h26
-rw-r--r--src/serdi.c47
-rw-r--r--src/syntax.c59
-rw-r--r--wscript1
4 files changed, 89 insertions, 44 deletions
diff --git a/serd/serd.h b/serd/serd.h
index 32dfa128..32b53b8b 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -399,6 +399,32 @@ serd_base64_decode(const char* str, size_t len, size_t* size);
/**
@}
+ @name Syntax Utilities
+ @{
+*/
+
+/**
+ Get a syntax by name.
+
+ Case-insensitive, supports "Turtle", "NTriples", "NQuads", and "TriG". Zero
+ is returned if the name is not recognized.
+*/
+SERD_API
+SerdSyntax
+serd_syntax_by_name(const char* name);
+
+/**
+ Guess a syntax from a filename.
+
+ This uses the file extension to guess the syntax of a file. Zero is
+ returned if the extension is not recognized.
+*/
+SERD_API
+SerdSyntax
+serd_guess_syntax(const char* filename);
+
+/**
+ @}
@name URI
@{
*/
diff --git a/src/serdi.c b/src/serdi.c
index 70061b68..eddc315b 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -15,7 +15,6 @@
*/
#include "serd_config.h"
-#include "string_utils.h"
#include "system.h"
#include "serd/serd.h"
@@ -29,46 +28,6 @@
#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 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)
{
@@ -171,7 +130,7 @@ main(int argc, char** argv)
} else if (argv[a][1] == 'i') {
if (++a == argc) {
return missing_arg(argv[0], 'i');
- } else if (!(input_syntax = get_syntax(argv[a]))) {
+ } else if (!(input_syntax = serd_syntax_by_name(argv[a]))) {
return print_usage(argv[0], true);
}
} else if (argv[a][1] == 'k') {
@@ -186,7 +145,7 @@ main(int argc, char** argv)
} else if (argv[a][1] == 'o') {
if (++a == argc) {
return missing_arg(argv[0], 'o');
- } else if (!(output_syntax = get_syntax(argv[a]))) {
+ } else if (!(output_syntax = serd_syntax_by_name(argv[a]))) {
return print_usage(argv[0], true);
}
} else if (argv[a][1] == 'p') {
@@ -217,7 +176,7 @@ main(int argc, char** argv)
const char* input = (const char*)argv[a++];
- if (!input_syntax && !(input_syntax = guess_syntax(input))) {
+ if (!input_syntax && !(input_syntax = serd_guess_syntax(input))) {
input_syntax = SERD_TRIG;
}
diff --git a/src/syntax.c b/src/syntax.c
new file mode 100644
index 00000000..f049c417
--- /dev/null
+++ b/src/syntax.c
@@ -0,0 +1,59 @@
+/*
+ Copyright 2011-2018 David Robillard <http://drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include "string_utils.h"
+
+#include "serd/serd.h"
+
+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}
+};
+
+SerdSyntax
+serd_syntax_by_name(const char* const name)
+{
+ for (const Syntax* s = syntaxes; s->name; ++s) {
+ if (!serd_strncasecmp(s->name, name, strlen(name))) {
+ return s->syntax;
+ }
+ }
+ return (SerdSyntax)0;
+}
+
+SerdSyntax
+serd_guess_syntax(const char* const 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;
+}
+
diff --git a/wscript b/wscript
index 6471a1a9..fedc2bc4 100644
--- a/wscript
+++ b/wscript
@@ -99,6 +99,7 @@ lib_source = ['src/base64.c',
'src/sink.c',
'src/statement.c',
'src/string.c',
+ 'src/syntax.c',
'src/system.c',
'src/uri.c',
'src/world.c',