# HG changeset patch # User David Demelier # Date 1630914821 -7200 # Node ID 0858e33a762d1281b9609e135a7e8738d201f8ef # Parent 8523fb0c85407000a3ce11aef2f2d3ea3cac8654 misc: initial windows support diff -r 8523fb0c8540 -r 0858e33a762d .hgignore --- a/.hgignore Fri Jul 09 11:29:36 2021 +0200 +++ b/.hgignore Mon Sep 06 09:53:41 2021 +0200 @@ -3,6 +3,7 @@ \.d$ \.o$ \.mo$ +\.exe$ # generated files. assets/.*\.h$ diff -r 8523fb0c8540 -r 0858e33a762d GNUmakefile --- a/GNUmakefile Fri Jul 09 11:29:36 2021 +0200 +++ b/GNUmakefile Mon Sep 06 09:53:41 2021 +0200 @@ -57,6 +57,7 @@ libmlk-core/core/music.c \ libmlk-core/core/painter.c \ libmlk-core/core/panic.c \ + libmlk-core/core/port.c \ libmlk-core/core/script.c \ libmlk-core/core/sound.c \ libmlk-core/core/sprite.c \ @@ -264,10 +265,7 @@ -Ilibmlk-core \ -Ilibmlk-ui \ -Ilibmlk-rpg \ - -Ilibmlk-adventure \ - ${SDL2_INCS} \ - ${JANSSON_INCS} \ - ${ZSTD_INCS} + -Ilibmlk-adventure DEFS= -DMOLKO_PREFIX=\"${PREFIX}\" \ -DMOLKO_BINDIR=\"${BINDIR}\" \ -DMOLKO_DATADIR=\"${DATADIR}\" \ @@ -386,6 +384,7 @@ ${LIBMLK_CORE_OBJS}: config.h +${LIBMLK_CORE}: INCS += ${SDL2_INCS} ${LIBMLK_CORE}: ${LIBMLK_CORE_OBJS} ${LIBMLK_CORE_MO} ${CMD.ar} diff -r 8523fb0c8540 -r 0858e33a762d libmlk-adventure/adventure/dialog/save.c --- a/libmlk-adventure/adventure/dialog/save.c Fri Jul 09 11:29:36 2021 +0200 +++ b/libmlk-adventure/adventure/dialog/save.c Mon Sep 06 09:53:41 2021 +0200 @@ -26,6 +26,7 @@ #include #include #include +#include #include #include diff -r 8523fb0c8540 -r 0858e33a762d libmlk-core/core/port.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmlk-core/core/port.c Mon Sep 06 09:53:41 2021 +0200 @@ -0,0 +1,127 @@ +/* + * port.c -- portability bits + * + * Copyright (c) 2020 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 "port.h" + +/* {{{ strlcpy (BSD extension, incoming in next POSIX). */ + +#if defined(MOLKO_PORT_NEED_STRLCPY) + +/* $OpenBSD: strlcpy.c,v 1.16 2019/01/25 00:19:25 millert Exp $ */ + +/* + * Copyright (c) 1998, 2015 Todd C. Miller + * + * Permission to use, copy, modify, and 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. + */ + +/* + * Copy string src to buffer dst of size dsize. At most dsize-1 + * chars will be copied. Always NUL terminates (unless dsize == 0). + * Returns strlen(src); if retval >= dsize, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t dsize) +{ + const char *osrc = src; + size_t nleft = dsize; + + /* Copy as many bytes as will fit. */ + if (nleft != 0) { + while (--nleft != 0) { + if ((*dst++ = *src++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src. */ + if (nleft == 0) { + if (dsize != 0) + *dst = '\0'; /* NUL-terminate dst */ + while (*src++) + ; + } + + return(src - osrc - 1); /* count does not include NUL */ +} + +#endif /* !MOLKO_PORT_NEED_STRLCPY */ + +/* }}} */ + +/* {{{ fmemopen (POSIX). */ + +/* https://github.com/Arryboom/fmemopen_windows */ + +#if defined(MOLKO_PORT_NEED_FMEMOPEN) + +#include +#include +#include +#include +#include +#include + +FILE * +fmemopen(void *buf, size_t len, const char *type) +{ + int fd; + FILE *fp; + char tp[MAX_PATH - 13]; + char fn[MAX_PATH + 1]; + int * pfd = &fd; + int retner = -1; + char tfname[] = "MemTF_"; + if (!GetTempPathA(sizeof(tp), tp)) + return NULL; + if (!GetTempFileNameA(tp, tfname, 0, fn)) + return NULL; + retner = _sopen_s(pfd, fn, _O_CREAT | _O_SHORT_LIVED | _O_TEMPORARY | _O_RDWR | _O_BINARY | _O_NOINHERIT, _SH_DENYRW, _S_IREAD | _S_IWRITE); + if (retner != 0) + return NULL; + if (fd == -1) + return NULL; + fp = _fdopen(fd, "wb+"); + if (!fp) { + _close(fd); + return NULL; + } + /* + * File descriptors passed into _fdopen are owned by the returned FILE + * stream.If _fdopen is successful, do not call _close on the file + * descriptor. Calling fclose on the returned FILE * also closes the + * file descriptor. + */ + fwrite(buf, len, 1, fp); + rewind(fp); + return fp; +} + +#endif /* !MOLKO_PORT_NEED_FMEMOPEN */ + +/* }}} */ diff -r 8523fb0c8540 -r 0858e33a762d libmlk-core/core/port.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmlk-core/core/port.h Mon Sep 06 09:53:41 2021 +0200 @@ -0,0 +1,56 @@ +/* + * port.h -- portability bits + * + * Copyright (c) 2020 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 MOLKO_CORE_PORT_H +#define MOLKO_CORE_PORT_H + +/* {{{ strlcpy (BSD extension, incoming in next POSIX). */ + +#if defined(_WIN32) +# define MOLKO_PORT_NEED_STRLCPY +#endif + +#if defined(MOLKO_PORT_NEED_STRLCPY) + +#include + +size_t +strlcpy(char *, const char *, size_t); + +#endif + +/* }}} */ + +/* {{{ fmemopen (POSIX). */ + +#if defined(_WIN32) +# define MOLKO_PORT_NEED_FMEMOPEN +#endif + +#if defined(MOLKO_PORT_NEED_FMEMOPEN) + +#include + +FILE * +fmemopen(void *, size_t, const char *); + +#endif + +/* }}} */ + +#endif /* !MOLKO_CORE_PORT_H */ diff -r 8523fb0c8540 -r 0858e33a762d libmlk-core/core/sys.c --- a/libmlk-core/core/sys.c Fri Jul 09 11:29:36 2021 +0200 +++ b/libmlk-core/core/sys.c Mon Sep 06 09:53:41 2021 +0200 @@ -38,6 +38,7 @@ #include #include "error.h" +#include "port.h" #include "sound.h" #include "sys.h" diff -r 8523fb0c8540 -r 0858e33a762d libmlk-core/core/zfile.c --- a/libmlk-core/core/zfile.c Fri Jul 09 11:29:36 2021 +0200 +++ b/libmlk-core/core/zfile.c Mon Sep 06 09:53:41 2021 +0200 @@ -37,6 +37,12 @@ # define ZSTD_MAGICNUMBER 0xFD2FB528 #endif +/* Windows thing. */ +#if !defined(O_BINARY) +# define O_BINARY +#endif + +#include "port.h" #include "zfile.h" static int @@ -61,11 +67,12 @@ 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)) || read(fd, in, st.st_size) != st.st_size) + 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))) { @@ -129,7 +136,7 @@ memset(zf, 0, sizeof (*zf)); - if ((fd = open(path, O_RDONLY)) < 0) + if ((fd = open(path, O_RDONLY | O_BINARY)) < 0) return -1; return is_zstd(fd) ? decompress(fd, zf) : reopen(fd, zf); diff -r 8523fb0c8540 -r 0858e33a762d libmlk-rpg/rpg/map-file.c --- a/libmlk-rpg/rpg/map-file.c Fri Jul 09 11:29:36 2021 +0200 +++ b/libmlk-rpg/rpg/map-file.c Mon Sep 06 09:53:41 2021 +0200 @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -229,7 +230,7 @@ while (fgets(line, sizeof (line), ctx->fp)) { /* Remove \n if any */ - line[strcspn(line, "\n")] = '\0'; + line[strcspn(line, "\r\n")] = '\0'; if (parse_line(ctx, line) < 0) return -1; diff -r 8523fb0c8540 -r 0858e33a762d libmlk-rpg/rpg/tileset-file.c --- a/libmlk-rpg/rpg/tileset-file.c Fri Jul 09 11:29:36 2021 +0200 +++ b/libmlk-rpg/rpg/tileset-file.c Mon Sep 06 09:53:41 2021 +0200 @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -282,7 +283,7 @@ while (fgets(line, sizeof (line), ctx->fp)) { /* Remove \n if any */ - line[strcspn(line, "\n")] = '\0'; + line[strcspn(line, "\r\n")] = '\0'; if (parse_line(ctx, line) < 0) return -1;