changeset 309:0858e33a762d

misc: initial windows support
author David Demelier <markand@malikania.fr>
date Mon, 06 Sep 2021 09:53:41 +0200
parents 8523fb0c8540
children b271a0e8ce52
files .hgignore GNUmakefile libmlk-adventure/adventure/dialog/save.c libmlk-core/core/port.c libmlk-core/core/port.h libmlk-core/core/sys.c libmlk-core/core/zfile.c libmlk-rpg/rpg/map-file.c libmlk-rpg/rpg/tileset-file.c
diffstat 9 files changed, 202 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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$
--- 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}
 
--- 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 <core/painter.h>
 #include <core/sprite.h>
 #include <core/window.h>
+#include <core/port.h>
 
 #include <ui/align.h>
 #include <ui/label.h>
--- /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 <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 "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 <millert@openbsd.org>
+ *
+ * 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 <stdio.h>
+#include <windows.h>
+#include <share.h>
+#include <io.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+
+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 */
+
+/* }}} */
--- /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 <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 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 <stddef.h>
+
+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 <stdio.h>
+
+FILE *
+fmemopen(void *, size_t, const char *);
+
+#endif
+
+/* }}} */
+
+#endif /* !MOLKO_CORE_PORT_H */
--- 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 <SDL_ttf.h>
 
 #include "error.h"
+#include "port.h"
 #include "sound.h"
 #include "sys.h"
 
--- 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);
--- 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 <core/alloc.h>
 #include <core/error.h>
 #include <core/image.h>
+#include <core/port.h>
 #include <core/trace.h>
 #include <core/zfile.h>
 
@@ -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;
--- 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 <core/animation.h>
 #include <core/error.h>
 #include <core/image.h>
+#include <core/port.h>
 #include <core/util.h>
 #include <core/zfile.h>
 
@@ -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;