// Copyright 2011-2022 David Robillard // SPDX-License-Identifier: ISC #include "world.h" #include "serd_config.h" #include "system.h" #if defined(USE_POSIX_FADVISE) # include #endif #include #include #include #include FILE* serd_world_fopen(SerdWorld* world, const char* path, const char* mode) { FILE* fd = fopen(path, mode); if (!fd) { char message[1024] = {0}; serd_system_strerror(errno, message, sizeof(message)); serd_world_errorf( world, SERD_BAD_STREAM, "failed to open file %s (%s)\n", path, message); return NULL; } #if USE_POSIX_FADVISE && USE_FILENO (void)posix_fadvise(fileno(fd), 0, 0, POSIX_FADV_SEQUENTIAL); #endif return fd; } SerdStatus serd_world_error(const SerdWorld* const world, const SerdError* const e) { if (world->error_func) { world->error_func(world->error_handle, e); } else { if (e->filename) { fprintf(stderr, "error: %s:%u:%u: ", e->filename, e->line, e->col); } else { fprintf(stderr, "error: "); } vfprintf(stderr, e->fmt, *e->args); } return e->status; } SerdStatus serd_world_verrorf(const SerdWorld* const world, const SerdStatus st, const char* const fmt, va_list args) { va_list args_copy; va_copy(args_copy, args); const SerdError e = {st, NULL, 0, 0, fmt, &args_copy}; serd_world_error(world, &e); va_end(args_copy); return st; } SerdStatus serd_world_errorf(const SerdWorld* const world, const SerdStatus st, const char* const fmt, ...) { va_list args; // NOLINT(cppcoreguidelines-init-variables) va_start(args, fmt); const SerdError e = {st, NULL, 0, 0, fmt, &args}; serd_world_error(world, &e); va_end(args); return st; } SerdWorld* serd_world_new(void) { return (SerdWorld*)calloc(1, sizeof(SerdWorld)); } void serd_world_free(SerdWorld* const world) { free(world); } void serd_world_set_error_func(SerdWorld* world, SerdErrorFunc error_func, void* handle) { world->error_func = error_func; world->error_handle = handle; }