# HG changeset patch # User David Demelier # Date 1665847125 -7200 # Node ID 1645433e008db48f232e2a7ccaa0d2116c4dc7f7 # Parent 67b52de5d3c066d9ef849e55e5671465d4326ec3 core: rename ERR_INTERNAL to ERR_SDL diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/err.c --- a/src/libmlk-core/core/err.c Sat Oct 15 15:56:09 2022 +0200 +++ b/src/libmlk-core/core/err.c Sat Oct 15 17:18:45 2022 +0200 @@ -24,7 +24,7 @@ err_string(int e) { switch (e) { - case ERR_INTERNAL: + case ERR_SDL: return SDL_GetError(); case ERR_NO_MEM: return "out of memory"; diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/err.h --- a/src/libmlk-core/core/err.h Sat Oct 15 15:56:09 2022 +0200 +++ b/src/libmlk-core/core/err.h Sat Oct 15 17:18:45 2022 +0200 @@ -20,7 +20,7 @@ #define MLK_ERR_H #define ERR_NONE 0 -#define ERR_INTERNAL -1 +#define ERR_SDL -1 #define ERR_NO_MEM -2 #define ERR_NO_SUPPORT -3 diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/font.c --- a/src/libmlk-core/core/font.c Sat Oct 15 15:56:09 2022 +0200 +++ b/src/libmlk-core/core/font.c Sat Oct 15 17:18:45 2022 +0200 @@ -35,7 +35,7 @@ assert(path); if (!(font->handle = TTF_OpenFont(path, size))) - return ERR_INTERNAL; + return ERR_SDL; return 0; } @@ -50,7 +50,7 @@ if (!(ops = SDL_RWFromConstMem(buffer, buflen)) || (!(font->handle = TTF_OpenFontRW(ops, 1, size)))) - return ERR_INTERNAL; + return ERR_SDL; return 0; } @@ -86,7 +86,7 @@ } if (!(surface = func(font->handle, text, fg))) - return ERR_INTERNAL; + return ERR_SDL; return texture_from_surface(tex, surface); } @@ -111,7 +111,7 @@ *h = 0; if (TTF_SizeUTF8(font->handle, text, (int *)w, (int *)h) != 0) - return ERR_INTERNAL; + return ERR_SDL; return 0; } diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/image.c --- a/src/libmlk-core/core/image.c Sat Oct 15 15:56:09 2022 +0200 +++ b/src/libmlk-core/core/image.c Sat Oct 15 17:18:45 2022 +0200 @@ -45,7 +45,7 @@ assert(path); if (!(tex->handle = IMG_LoadTexture(RENDERER(), path))) - return ERR_INTERNAL; + return ERR_SDL; dimensions(tex); @@ -60,7 +60,7 @@ SDL_RWops *ops = SDL_RWFromConstMem(buffer, size); if (!ops || !(tex->handle = IMG_LoadTexture_RW(RENDERER(), ops, 1))) - return ERR_INTERNAL; + return ERR_SDL; dimensions(tex); diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/texture.c --- a/src/libmlk-core/core/texture.c Sat Oct 15 15:56:09 2022 +0200 +++ b/src/libmlk-core/core/texture.c Sat Oct 15 17:18:45 2022 +0200 @@ -39,7 +39,7 @@ if (!tex->handle) { tex->w = tex->h = 0; - return ERR_INTERNAL; + return ERR_SDL; } tex->w = w; @@ -166,7 +166,7 @@ if (!(tex->handle = SDL_CreateTextureFromSurface(RENDERER(), surface))) { tex->w = tex->h = 0; - return ERR_INTERNAL; + return ERR_SDL; } tex->w = surface->w; diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/vfs-directory.c --- a/src/libmlk-core/core/vfs-directory.c Sat Oct 15 15:56:09 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,115 +0,0 @@ -/* - * vfs-directory.c -- VFS subsystem for directories - * - * Copyright (c) 2020-2022 David Demelier - * - * 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 -#include -#include -#include -#include -#include - -#include - -#include "alloc.h" -#include "error.h" -#include "vfs.h" - -struct self { - char base[PATH_MAX]; -}; - -static inline void -normalize(char *path) -{ - size_t len = strlen(path); - - for (char *p = path; *p; ++p) - if (*p == '\\') - *p = '/'; - - while (len && path[len - 1] == '/') - path[--len] = 0; -} - -static size_t -file_read(struct vfs_file *file, void *buf, size_t bufsz) -{ - return fread(buf, 1, bufsz, file->data); -} - -static size_t -file_write(struct vfs_file *file, const void *buf, size_t bufsz) -{ - return fwrite(buf, 1, bufsz, file->data); -} - -static int -file_flush(struct vfs_file *file) -{ - return fflush(file->data) == EOF ? -1 : 0; -} - -static void -file_finish(struct vfs_file *file) -{ - fclose(file->data); -} - -static int -dir_open(struct vfs *vfs, struct vfs_file *file, const char *entry, const char *mode) -{ - struct self *self = vfs->data; - char path[PATH_MAX]; - - snprintf(path, sizeof (path), "%s/%s", self->base, entry); - - if (!(file->data = fopen(path, mode))) - return errorf("%s: %s", path, strerror(errno)); - - file->read = file_read; - file->write = file_write; - file->flush = file_flush; - file->finish = file_finish; - - return 0; -} - -static void -dir_finish(struct vfs *vfs) -{ - free(vfs->data); -} - -void -vfs_directory(struct vfs *vfs, const char *path) -{ - assert(vfs); - assert(path); - - struct self *self; - - self = alloc_new(sizeof (*self)); - util_strlcpy(self->base, path, sizeof (self->base)); - - /* Remove terminator and switch to UNIX paths. */ - normalize(self->base); - - vfs->data = self; - vfs->open = dir_open; - vfs->finish = dir_finish; -} diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/vfs-directory.h --- a/src/libmlk-core/core/vfs-directory.h Sat Oct 15 15:56:09 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,33 +0,0 @@ -/* - * vfs-directory.h -- VFS subsystem for directories - * - * Copyright (c) 2020-2022 David Demelier - * - * 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_VFS_DIRECTORY_H -#define MLK_CORE_VFS_DIRECTORY_H - -#include "core.h" - -struct vfs; - -CORE_BEGIN_DECLS - -void -vfs_directory(struct vfs *, const char *); - -CORE_END_DECLS - -#endif /* !MLK_CORE_VFS_DIRECTORY_H */ diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/vfs-zip.c --- a/src/libmlk-core/core/vfs-zip.c Sat Oct 15 15:56:09 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,129 +0,0 @@ -/* - * vfs-zip.c -- VFS subsystem for zip archives - * - * Copyright (c) 2020-2022 David Demelier - * - * 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" - -#if defined(MLK_WITH_ZIP) - -#include -#include - -#include - -#include "error.h" -#include "vfs-zip.h" -#include "vfs.h" - -static inline int -mkflags(const char *mode) -{ - /* TODO: this should check for mutual exclusions. */ - int flags = 0; - - for (; *mode; ++mode) { - switch (*mode) { - case 'w': - flags |= ZIP_CREATE; - break; - case 'x': - flags |= ZIP_EXCL; - break; - case '+': - flags |= ZIP_TRUNCATE; - break; - case 'r': - flags |= ZIP_RDONLY; - break; - default: - break; - } - } - - return flags; -} - -static size_t -file_read(struct vfs_file *file, void *buf, size_t bufsz) -{ - return zip_fread(file->data, buf, bufsz); -} - -static size_t -file_write(struct vfs_file *file, const void *buf, size_t bufsz) -{ - (void)file; - (void)buf; - (void)bufsz; - - return errorf("operation not supported"); -} - -static int -file_flush(struct vfs_file *file) -{ - (void)file; - - return 0; -} - -static void -file_finish(struct vfs_file *file) -{ - zip_fclose(file->data); -} - -static int -impl_open(struct vfs *vfs, struct vfs_file *file, const char *entry, const char *mode) -{ - (void)mode; - - if (!(file->data = zip_fopen(vfs->data, entry, 0))) - return errorf("unable to open file in archive"); - - file->read = file_read; - file->write = file_write; - file->flush = file_flush; - file->finish = file_finish; - - return 0; -} - -static void -impl_finish(struct vfs *vfs) -{ - zip_close(vfs->data); -} - -int -vfs_zip(struct vfs *vfs, const char *file, const char *mode) -{ - assert(vfs); - assert(file); - - memset(vfs, 0, sizeof (*vfs)); - - if (!(vfs->data = zip_open(file, mkflags(mode), NULL))) - return errorf("%s: unable to open zip file", file); - - vfs->open = impl_open; - vfs->finish = impl_finish; - - return 0; -} - -#endif /* !MLK_WITH_ZIP */ diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/vfs-zip.h --- a/src/libmlk-core/core/vfs-zip.h Sat Oct 15 15:56:09 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ -/* - * vfs-zip.h -- VFS subsystem for zip archives - * - * Copyright (c) 2020-2022 David Demelier - * - * 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_VFS_ZIP_H -#define MLK_CORE_VFS_ZIP_H - -#include "config.h" - -#if defined(MLK_WITH_ZIP) - -#include "core.h" - -struct vfs; - -CORE_BEGIN_DECLS - -int -vfs_zip(struct vfs *, const char *, const char *); - -CORE_END_DECLS - -#endif /* !MLK_WITH_ZIP */ - -#endif /* !MLK_CORE_VFS_ZIP_H */ diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/vfs.c --- a/src/libmlk-core/core/vfs.c Sat Oct 15 15:56:09 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,145 +0,0 @@ -/* - * vfs.c -- virtual file system abstraction - * - * Copyright (c) 2020-2022 David Demelier - * - * 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 -#include -#include -#include -#include - -#include "buf.h" -#include "error.h" -#include "vfs.h" -#include "vfs_p.h" - -int -vfs_open(struct vfs *vfs, struct vfs_file *file, const char *entry, const char *mode) -{ - assert(vfs); - assert(entry); - - memset(file, 0, sizeof (*file)); - - return vfs->open(vfs, file, entry, mode); -} - -int -vfs_ok(struct vfs *vfs) -{ - return vfs && vfs->open && vfs->finish; -} - -void -vfs_finish(struct vfs *vfs) -{ - assert(vfs); - - vfs->finish(vfs); - memset(vfs, 0, sizeof (*vfs)); -} - -int -vfs_file_ok(struct vfs_file *file) -{ - return file && file->read && file->write && file->flush && file->finish; -} - -size_t -vfs_file_read(struct vfs_file *file, void *buf, size_t bufsz) -{ - assert(file); - assert(buf); - - return file->read(file, buf, bufsz); -} - -char * -vfs_file_aread(struct vfs_file *file, size_t *outlen) -{ - struct buf buf = {0}; - char data[BUFSIZ]; - size_t nr; - - while ((nr = vfs_file_read(file, data, sizeof (data))) > 0) { - if (buf_printf(&buf, "%.*s", (int)nr, data) < 0) { - errorf("%s", strerror(errno)); - buf_finish(&buf); - return NULL; - } - } - - if (outlen) - *outlen = buf.length; - - return buf.data; -} - -size_t -vfs_file_write(struct vfs_file *file, void *buf, size_t bufsz) -{ - assert(file); - assert(buf); - - return file->write(file, buf, bufsz); -} - -int -vfs_file_flush(struct vfs_file *file) -{ - assert(file); - - return file->flush(file); -} - -void -vfs_file_finish(struct vfs_file *file) -{ - assert(file); - - file->finish(file); -} - -/* private */ - -static int -rw_vfs_file_close(SDL_RWops *context) -{ - free(context->hidden.mem.base); - free(context); - - return 0; -} - -SDL_RWops * -vfs_to_rw(struct vfs_file *file) -{ - SDL_RWops *ops; - char *data; - size_t datasz; - - if (!(data = vfs_file_aread(file, &datasz))) - return NULL; - if (!(ops = SDL_RWFromConstMem(data, datasz))) { - free(data); - return errorf("%s", SDL_GetError()), NULL; - } - - ops->close = rw_vfs_file_close; - - return ops; -} diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/vfs.h --- a/src/libmlk-core/core/vfs.h Sat Oct 15 15:56:09 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,77 +0,0 @@ -/* - * vfs.h -- virtual file system abstraction - * - * Copyright (c) 2020-2022 David Demelier - * - * 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_VFS_H -#define MLK_CORE_VFS_H - -#include - -#include "core.h" - -struct vfs_file; - -struct vfs { - void *data; - int (*open)(struct vfs *, struct vfs_file *, const char *, const char *); - void (*finish)(struct vfs *); -}; - -struct vfs_file { - void *data; - size_t (*read)(struct vfs_file *, void *, size_t); - size_t (*write)(struct vfs_file *, const void *, size_t); - int (*flush)(struct vfs_file *); - void (*finish)(struct vfs_file *); -}; - -CORE_BEGIN_DECLS - -/* vfs */ - -int -vfs_open(struct vfs *, struct vfs_file *, const char *, const char *); - -int -vfs_ok(struct vfs *); - -void -vfs_finish(struct vfs *); - -/* vfs_file */ - -int -vfs_file_ok(struct vfs_file *); - -size_t -vfs_file_read(struct vfs_file *, void *, size_t); - -char * -vfs_file_aread(struct vfs_file *, size_t *); - -size_t -vfs_file_write(struct vfs_file *, void *, size_t); - -int -vfs_file_flush(struct vfs_file *); - -void -vfs_file_finish(struct vfs_file *); - -CORE_END_DECLS - -#endif /* MLK_CORE_VFS_H */ diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/vfs_p.h --- a/src/libmlk-core/core/vfs_p.h Sat Oct 15 15:56:09 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,27 +0,0 @@ -/* - * vfs.h -- (PRIVATE) virtual file system abstraction - * - * Copyright (c) 2020-2022 David Demelier - * - * 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_VFS_P_H -#define MLK_CORE_VFS_P_H - -#include - -SDL_RWops * -vfs_to_rw(struct vfs_file *); - -#endif /* !MLK_CORE_VFS_P_H */ diff -r 67b52de5d3c0 -r 1645433e008d src/libmlk-core/core/window.c --- a/src/libmlk-core/core/window.c Sat Oct 15 15:56:09 2022 +0200 +++ b/src/libmlk-core/core/window.c Sat Oct 15 17:18:45 2022 +0200 @@ -21,7 +21,7 @@ #include -#include "error.h" +#include "err.h" #include "util.h" #include "window.h" #include "window_p.h" @@ -84,7 +84,7 @@ assert(title); if (!load_window(title, w, h) || !load_renderer()) - return errorf("%s", SDL_GetError()); + return ERR_SDL; window.w = w; window.h = h;