aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-08-02 11:32:31 -0400
committerDavid Robillard <d@drobilla.net>2022-01-28 21:57:07 -0500
commit2746098ad7d9f9d81e616a25e16aeae784f438a5 (patch)
treefa38401f32247bbd183fc5920e080c1674ef8b8a
parent7eafa3587c370a204e08afbc4013f748dd78feba (diff)
downloadserd-2746098ad7d9f9d81e616a25e16aeae784f438a5.tar.gz
serd-2746098ad7d9f9d81e616a25e16aeae784f438a5.tar.bz2
serd-2746098ad7d9f9d81e616a25e16aeae784f438a5.zip
Factor out opening input files
-rw-r--r--src/console.c27
-rw-r--r--src/console.h5
-rw-r--r--src/serdi.c14
3 files changed, 34 insertions, 12 deletions
diff --git a/src/console.c b/src/console.c
index 75de5f62..fd339526 100644
--- a/src/console.c
+++ b/src/console.c
@@ -15,6 +15,7 @@
*/
#include "console.h"
+#include "system.h"
#include "serd/serd.h"
@@ -26,6 +27,8 @@
# include <io.h>
#endif
+#include <string.h>
+
void
serd_set_stream_utf8_mode(FILE* const stream)
{
@@ -52,3 +55,27 @@ serd_print_version(const char* const program)
return 0;
}
+
+SerdByteSource*
+serd_open_input(const char* const filename, const size_t page_size)
+{
+ SerdByteSource* byte_source = NULL;
+ if (!strcmp(filename, "-")) {
+ serd_set_stream_utf8_mode(stdin);
+
+ SerdNode* name = serd_new_string(SERD_STRING("stdin"));
+
+ byte_source = serd_byte_source_new_function(serd_file_read_byte,
+ (SerdStreamErrorFunc)ferror,
+ NULL,
+ stdin,
+ name,
+ page_size);
+
+ serd_node_free(name);
+ } else {
+ byte_source = serd_byte_source_new_filename(filename, page_size);
+ }
+
+ return byte_source;
+}
diff --git a/src/console.h b/src/console.h
index 11b8e828..80c7590b 100644
--- a/src/console.h
+++ b/src/console.h
@@ -14,6 +14,8 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include "serd/serd.h"
+
#include <stdio.h>
void
@@ -21,3 +23,6 @@ serd_set_stream_utf8_mode(FILE* stream);
int
serd_print_version(const char* program);
+
+SerdByteSource*
+serd_open_input(const char* filename, size_t page_size);
diff --git a/src/serdi.c b/src/serdi.c
index 890408d2..56762b23 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -147,18 +147,8 @@ read_file(SerdWorld* const world,
syntax = syntax ? syntax : serd_guess_syntax(filename);
syntax = syntax ? syntax : SERD_TRIG;
- SerdByteSource* byte_source = NULL;
- if (!strcmp(filename, "-")) {
- SerdNode* name = serd_new_string(SERD_STRING("stdin"));
-
- byte_source = serd_byte_source_new_function(
- serd_file_read_byte, (SerdStreamErrorFunc)ferror, NULL, stdin, name, 1);
-
- serd_node_free(name);
- } else {
- byte_source =
- serd_byte_source_new_filename(filename, bulk_read ? SERD_PAGE_SIZE : 1u);
- }
+ SerdByteSource* byte_source =
+ serd_open_input(filename, bulk_read ? SERD_PAGE_SIZE : 1u);
if (!byte_source) {
SERDI_ERRORF(