// Copyright 2011-2021 David Robillard // SPDX-License-Identifier: ISC #include "console.h" #include "serd/serd.h" #include "zix/allocator.h" #include "zix/filesystem.h" #ifdef _WIN32 # ifdef _MSC_VER # define WIN32_LEAN_AND_MEAN 1 # endif # include # include #endif #include #include void serd_set_stream_utf8_mode(FILE* const stream) { #ifdef _WIN32 _setmode(_fileno(stream), _O_BINARY); #else (void)stream; #endif } int serd_print_version(const char* const program) { printf("%s %d.%d.%d \n", program, SERD_MAJOR_VERSION, SERD_MINOR_VERSION, SERD_MICRO_VERSION); printf("Copyright 2011-2023 David Robillard .\n" "License: \n" "This is free software; you are free to change and redistribute it.\n" "There is NO WARRANTY, to the extent permitted by law.\n"); return 0; } SerdStatus serd_set_base_uri_from_path(SerdEnv* const env, const char* const path) { char* const input_path = zix_canonical_path(NULL, path); if (!input_path) { return SERD_BAD_ARG; } SerdNode* const file_uri = serd_node_new( NULL, serd_a_file_uri(serd_string(input_path), serd_empty_string())); serd_env_set_base_uri(env, serd_node_string_view(file_uri)); serd_node_free(NULL, file_uri); zix_free(NULL, input_path); return SERD_SUCCESS; } SerdSyntax serd_choose_syntax(SerdWorld* const world, const SerdSyntax requested, const char* const filename) { if (requested) { return requested; } const SerdSyntax guessed = serd_guess_syntax(filename); if (guessed != SERD_SYNTAX_EMPTY) { return guessed; } serd_logf(world, SERD_LOG_LEVEL_WARNING, "unable to determine syntax of \"%s\", trying TriG", filename); return SERD_TRIG; } SerdStatus serd_set_input_option(const SerdStringView name, SerdSyntax* const syntax, SerdReaderFlags* const flags) { typedef struct { const char* name; SerdReaderFlag flag; } InputOption; static const InputOption input_options[] = { {"lax", SERD_READ_LAX}, {"variables", SERD_READ_VARIABLES}, {"relative", SERD_READ_RELATIVE}, {"global", SERD_READ_GLOBAL}, {"generated", SERD_READ_GENERATED}, {NULL, SERD_READ_LAX}, }; const SerdSyntax named_syntax = serd_syntax_by_name(name.data); if (!serd_strncasecmp(name.data, "empty", name.length) || named_syntax != SERD_SYNTAX_EMPTY) { *syntax = named_syntax; return SERD_SUCCESS; } for (const InputOption* o = input_options; o->name; ++o) { if (!serd_strncasecmp(o->name, name.data, name.length)) { *flags |= o->flag; return SERD_SUCCESS; } } return SERD_FAILURE; } SerdStatus serd_set_output_option(const SerdStringView name, SerdSyntax* const syntax, SerdWriterFlags* const flags) { typedef struct { const char* name; SerdWriterFlag flag; } OutputOption; static const OutputOption output_options[] = { {"ascii", SERD_WRITE_ASCII}, {"expanded", SERD_WRITE_EXPANDED}, {"lax", SERD_WRITE_LAX}, {"terse", SERD_WRITE_TERSE}, {"verbatim", SERD_WRITE_VERBATIM}, {NULL, SERD_WRITE_ASCII}, }; const SerdSyntax named_syntax = serd_syntax_by_name(name.data); if (!serd_strncasecmp(name.data, "empty", name.length) || named_syntax != SERD_SYNTAX_EMPTY) { *syntax = named_syntax; return SERD_SUCCESS; } for (const OutputOption* o = output_options; o->name; ++o) { if (!serd_strncasecmp(o->name, name.data, name.length)) { *flags |= o->flag; return SERD_SUCCESS; } } return SERD_FAILURE; } /// Wrapper for getc that is compatible with SerdReadFunc but faster than fread static size_t serd_file_read_byte(void* buf, size_t size, size_t nmemb, void* stream) { (void)size; (void)nmemb; const int c = getc((FILE*)stream); if (c == EOF) { *((uint8_t*)buf) = 0; return 0; } *((uint8_t*)buf) = (uint8_t)c; return 1; } SerdInputStream serd_open_tool_input(const char* const filename) { if (!strcmp(filename, "-")) { const SerdInputStream in = serd_open_input_stream( serd_file_read_byte, (SerdErrorFunc)ferror, NULL, stdin); serd_set_stream_utf8_mode(stdin); return in; } return serd_open_input_file(filename); } SerdOutputStream serd_open_tool_output(const char* const filename) { if (!filename || !strcmp(filename, "-")) { serd_set_stream_utf8_mode(stdout); return serd_open_output_stream((SerdWriteFunc)fwrite, (SerdErrorFunc)ferror, (SerdCloseFunc)fclose, stdout); } return serd_open_output_file(filename); }