Mercurial > molko
changeset 428:26d23999a54b
core: get rid of zfile
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 15 Oct 2022 14:38:15 +0200 |
parents | b12e13e7a385 |
children | 67b52de5d3c0 |
files | GNUmakefile src/libmlk-core/core/sys.c src/libmlk-core/core/zfile.c src/libmlk-core/core/zfile.h src/libmlk-rpg/rpg/map-file.c src/libmlk-rpg/rpg/tileset-file.c |
diffstat | 6 files changed, 9 insertions(+), 252 deletions(-) [+] |
line wrap: on
line diff
--- a/GNUmakefile Sat Oct 15 14:19:58 2022 +0200 +++ b/GNUmakefile Sat Oct 15 14:38:15 2022 +0200 @@ -10,7 +10,6 @@ INCDIR ?= $(PREFIX)/include # User options. -WITH_ZSTD ?= yes WITH_DEBUG ?= no OS := $(shell uname -s) @@ -30,11 +29,6 @@ JANSSON_INCS ?= $(shell pkg-config --cflags jansson) JANSSON_LIBS ?= $(shell pkg-config --libs jansson) -ifeq ($(WITH_ZSTD),yes) -ZSTD_INCS ?= $(shell pkg-config --cflags libzstd) -ZSTD_LIBS ?= $(shell pkg-config --libs libzstd) -endif - ifeq ($(OS),Darwin) OPENAL_LIBS ?= -framework OpenAL else @@ -58,10 +52,6 @@ $(OPENAL_INCS) \ $(SNDFILE_INCS) -ifeq ($(WITH_ZSTD),yes) -INCS += $(ZSTD_INCS) -endif - OPTS := -Wall -Wextra -pipe ifeq ($(OS),Darwin) @@ -90,10 +80,6 @@ $(OPENAL_LIBS) \ $(SNDFILE_LIBS) -ifeq ($(WITH_ZSTD),yes) -LIBMLK += $(ZSTD_LIBS) -endif - .DEFAULT_GOAL := all .SUFFIXES: @@ -111,17 +97,6 @@ %.a: $(AR) -rc $@ $(OBJS) -# {{{ config.h - -config.h: - rm -f config.h - touch config.h -ifeq ($(WITH_ZSTD),yes) - echo "#define MLK_WITH_ZSTD" >> config.h -endif - -# }}} - # {{{ libmlk-util LIBMLK_UTIL := libmlk-util.a @@ -224,14 +199,12 @@ src/libmlk-core/core/texture.c \ src/libmlk-core/core/trace.c \ src/libmlk-core/core/util.c \ - src/libmlk-core/core/window.c \ - src/libmlk-core/core/zfile.c + src/libmlk-core/core/window.c LIBMLK_CORE_OBJS := $(LIBMLK_CORE_SRCS:.c=.o) LIBMLK_CORE_DEPS := $(LIBMLK_CORE_SRCS:.c=.d) -include $(LIBMLK_CORE_DEPS) -$(LIBMLK_CORE_OBJS): config.h $(LIBMLK_CORE): private OBJS := $(LIBMLK_CORE_OBJS) $(LIBMLK_CORE): $(LIBMLK_CORE_OBJS) @@ -449,7 +422,6 @@ cp -R src/libmlk-rpg/rpg/*.h $(DESTDIR)$(INCDIR)/mlk/rpg clean: - rm -f config.h rm -f $(MLK_BCC) $(MLK_MAP) $(MLK_TILESET) rm -f $(LIBMLK_SQLITE) $(LIBMLK_SQLITE_DEPS) $(LIBMLK_SQLITE_OBJS) rm -f $(LIBMLK_UTIL) $(LIBMLK_UTIL_DEPS) $(LIBMLK_UTIL_OBJS)
--- a/src/libmlk-core/core/sys.c Sat Oct 15 14:19:58 2022 +0200 +++ b/src/libmlk-core/core/sys.c Sat Oct 15 14:38:15 2022 +0200 @@ -16,8 +16,6 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "config.h" - #include <sys/stat.h> #include <assert.h> #include <errno.h>
--- a/src/libmlk-core/core/zfile.c Sat Oct 15 14:19:58 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,162 +0,0 @@ -/* - * zfile.c -- load potentially ZSTD compressed file - * - * Copyright (c) 2020-2022 David Demelier <markand@malikania.fr> - * - * 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. - * - * THE 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 "config.h" - -#include <sys/stat.h> -#include <assert.h> -#include <errno.h> -#include <fcntl.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - -#if defined(_WIN32) -# include <io.h> -#else -# include <unistd.h> -#endif - -/* - * If not enabled, we still need to check if a file is in zstandard so we use - * the magic number defined in the zstd.h file or copy it if zstd is disabled. - */ -#if defined(MLK_WITH_ZSTD) -# include <zstd.h> -#else -# define ZSTD_MAGICNUMBER 0xFD2FB528 -#endif - -/* Windows thing. */ -#if !defined(O_BINARY) -# define O_BINARY 0 -#endif - -#include <util/util.h> - -#include "zfile.h" - -static int -is_zstd(int fd) -{ - uint32_t magic = 0; - - read(fd, &magic, sizeof (magic)); - lseek(fd, 0, SEEK_SET); - -#if SDL_BYTEORDER != SDL_LIL_ENDIAN - magic = SDL_Swap32(magic); -#endif - - return magic == ZSTD_MAGICNUMBER; -} - -static int -decompress(int fd, struct zfile *zf) -{ -#if defined(MLK_WITH_ZSTD) - char *in = NULL; - unsigned long long datasz; - struct stat st; - ssize_t nr; - - /* Load uncompressed data. */ - if (fstat(fd, &st) < 0) - goto fail; - if (!(in = calloc(1, st.st_size)) || (nr = read(fd, in, st.st_size)) != st.st_size) - goto fail; - - switch ((datasz = ZSTD_getFrameContentSize(in, st.st_size))) { - case ZSTD_CONTENTSIZE_ERROR: - errno = EINVAL; - goto fail; - case ZSTD_CONTENTSIZE_UNKNOWN: - errno = ENOTSUP; - goto fail; - default: - break; - } - - /* Finally decompress. */ - if (!(zf->data = calloc(1, datasz + 1))) - goto fail; - if (ZSTD_isError(ZSTD_decompress(zf->data, datasz, in, st.st_size))) { - errno = EINVAL; - goto fail; - } - if (!(zf->fp = util_fmemopen(zf->data, datasz, "r"))) - goto fail; - - close(fd); - free(in); - - return 0; - -fail: - close(fd); - free(zf->data); - free(in); - - return -1; -#else - (void)fd; - (void)zf; - - errno = ENOTSUP; - - return -1; -#endif -} - -static int -reopen(int fd, struct zfile *zf) -{ - if (!(zf->fp = fdopen(fd, "r"))) - return close(fd), -1; - - return 0; -} - -int -zfile_open(struct zfile *zf, const char *path) -{ - assert(zf); - assert(path); - - int fd; - - memset(zf, 0, sizeof (*zf)); - - if ((fd = open(path, O_RDONLY | O_BINARY)) < 0) - return -1; - - return is_zstd(fd) ? decompress(fd, zf) : reopen(fd, zf); -} - -void -zfile_close(struct zfile *zf) -{ - assert(zf); - - free(zf->data); - - if (zf->fp) - fclose(zf->fp); - - memset(zf, 0, sizeof (*zf)); -}
--- a/src/libmlk-core/core/zfile.h Sat Oct 15 14:19:58 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ -/* - * zfile.h -- load potentially ZSTD compressed file - * - * Copyright (c) 2020-2022 David Demelier <markand@malikania.fr> - * - * 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. - * - * THE 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. - */ - -#ifndef MLK_CORE_ZFILE_H -#define MLK_CORE_ZFILE_H - -#include <stdio.h> - -#include "core.h" - -struct zfile { - FILE *fp; - char *data; -}; - -CORE_BEGIN_DECLS - -int -zfile_open(struct zfile *, const char *); - -void -zfile_close(struct zfile *); - -CORE_END_DECLS - -#endif /* !MLK_CORE_ZFILE_H */
--- a/src/libmlk-rpg/rpg/map-file.c Sat Oct 15 14:19:58 2022 +0200 +++ b/src/libmlk-rpg/rpg/map-file.c Sat Oct 15 14:38:15 2022 +0200 @@ -30,7 +30,6 @@ #include <core/error.h> #include <core/image.h> #include <core/trace.h> -#include <core/zfile.h> #include "map-file.h" @@ -272,30 +271,26 @@ .mf = file, .map = map, }; - struct zfile zf; int ret = 0; memset(map, 0, sizeof (*map)); - alloc_pool_init(&file->blocks, sizeof (*map->blocks), NULL); - if (zfile_open(&zf, path) < 0) + if (!(ctx.fp = fopen(path, "r"))) goto fail; - - ctx.fp = zf.fp; - if ((ret = parse(&ctx, path)) < 0 || (ret = check(map)) < 0) goto fail; - zfile_close(&zf); + fclose(ctx.fp); return 0; fail: - errorf("%s: %s", path, strerror(errno)); map_finish(map); map_file_finish(file); - zfile_close(&zf); + + if (ctx.fp) + fclose(ctx.fp); return -1; }
--- a/src/libmlk-rpg/rpg/tileset-file.c Sat Oct 15 14:19:58 2022 +0200 +++ b/src/libmlk-rpg/rpg/tileset-file.c Sat Oct 15 14:38:15 2022 +0200 @@ -31,7 +31,6 @@ #include <core/error.h> #include <core/image.h> #include <core/util.h> -#include <core/zfile.h> #include "tileset-file.h" #include "tileset.h" @@ -306,20 +305,16 @@ .tf = tf, .tileset = tileset }; - struct zfile zf; int ret = 0; memset(tileset, 0, sizeof (*tileset)); - if (zfile_open(&zf, path) < 0) - return errorf("%s: %s", path, strerror(errno)); - - ctx.fp = zf.fp; - + if (!(ctx.fp = fopen(path, "r"))) + return -1; if ((ret = parse(&ctx, path)) < 0 || (ret = check(tileset)) < 0) tileset_file_finish(tf); - zfile_close(&zf); + fclose(ctx.fp); return ret; }