diff options
Diffstat (limited to 'src/serdi.c')
-rw-r--r-- | src/serdi.c | 97 |
1 files changed, 18 insertions, 79 deletions
diff --git a/src/serdi.c b/src/serdi.c index e7b8d7ba..6049c31b 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -14,10 +14,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#define _POSIX_C_SOURCE 200809L /* for fileno and posix_fadvise */ - #include "serd_config.h" #include "string_utils.h" +#include "system.h" #include "serd/serd.h" @@ -29,15 +28,8 @@ # include <io.h> #endif -#if USE_POSIX_FADVISE && USE_FILENO -# include <fcntl.h> -#endif - -#include <errno.h> #include <stdbool.h> -#include <stdint.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #define SERDI_ERROR(msg) fprintf(stderr, "serdi: " msg) @@ -135,38 +127,6 @@ quiet_error_sink(void* handle, const SerdError* e) return SERD_SUCCESS; } -static inline FILE* -serd_fopen(const char* path, const char* mode) -{ - FILE* fd = fopen(path, mode); - if (!fd) { - SERDI_ERRORF("failed to open file %s (%s)\n", path, strerror(errno)); - return NULL; - } - -#if USE_POSIX_FADVISE && USE_FILENO - posix_fadvise(fileno(fd), 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE); -#endif - - return fd; -} - -/** fread-like wrapper for getc (which is faster). */ -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; -} - static SerdWriterFlags choose_style(const SerdSyntax input_syntax, const SerdSyntax output_syntax, @@ -204,25 +164,23 @@ main(int argc, char** argv) return print_usage(argv[0], true); } - FILE* in_fd = NULL; SerdSyntax input_syntax = (SerdSyntax)0; SerdSyntax output_syntax = (SerdSyntax)0; - bool from_file = true; + bool from_string = false; + bool from_stdin = false; bool ascii = false; bool bulk_read = true; bool bulk_write = false; bool full_uris = false; bool lax = false; bool quiet = false; - const char* in_name = NULL; const char* add_prefix = NULL; const char* chop_prefix = NULL; const char* root_uri = NULL; int a = 1; for (; a < argc && argv[a][0] == '-'; ++a) { if (argv[a][1] == '\0') { - in_name = (const char*)"(stdin)"; - in_fd = stdin; + from_stdin = true; break; } @@ -243,8 +201,7 @@ main(int argc, char** argv) } else if (argv[a][1] == 'v') { return print_version(); } else if (argv[a][1] == 's') { - in_name = (const char*)"(string)"; - from_file = false; + from_string = true; ++a; break; } else if (argv[a][1] == 'i') { @@ -297,22 +254,9 @@ main(int argc, char** argv) _setmode(_fileno(stdout), _O_BINARY); #endif - char* input_path = NULL; - const char* input = (const char*)argv[a++]; - if (from_file) { - in_name = in_name ? in_name : input; - if (!in_fd) { - if (!strncmp(input, "file:", 5)) { - input_path = serd_parse_file_uri(input, NULL); - input = input_path; - } - if (!input || !(in_fd = serd_fopen(input, "rb"))) { - return 1; - } - } - } + const char* input = argv[a++]; - if (!input_syntax && !(input_syntax = guess_syntax(in_name))) { + if (!input_syntax && !(input_syntax = guess_syntax(input))) { input_syntax = SERD_TRIG; } @@ -329,7 +273,7 @@ main(int argc, char** argv) SerdNode* base = NULL; if (a < argc) { // Base URI given on command line base = serd_new_uri(SERD_MEASURE_STRING((const char*)argv[a])); - } else if (from_file && in_fd != stdin) { // Use input file URI + } else if (!from_string && !from_stdin) { // Use input file URI base = serd_new_file_uri(SERD_MEASURE_STRING(input), SERD_EMPTY_STRING()); } @@ -355,36 +299,31 @@ main(int argc, char** argv) serd_node_free(root); SerdStatus st = SERD_SUCCESS; - if (!from_file) { + if (from_string) { st = serd_reader_start_string(reader, input); - } else { + } else if (from_stdin) { st = serd_reader_start_stream(reader, - bulk_read ? (SerdReadFunc)fread - : serd_file_read_byte, + serd_file_read_byte, (SerdStreamErrorFunc)ferror, - in_fd, - in_name, - bulk_read ? 4096 : 1); + stdin, + "(stdin)", + 1); + } else { + st = serd_reader_start_file(reader, input, bulk_read); } if (!st) { st = serd_reader_read_document(reader); } - serd_reader_end_stream(reader); - + serd_reader_finish(reader); serd_reader_free(reader); serd_writer_finish(writer); serd_writer_free(writer); serd_env_free(env); serd_node_free(base); - free(input_path); - - if (from_file) { - fclose(in_fd); - } - if (fclose(out_fd)) { + if (fclose(stdout)) { perror("serdi: write error"); st = SERD_ERR_UNKNOWN; } |