changeset 557:944798a59b8a

util: introduce mlk_util_die
author David Demelier <markand@malikania.fr>
date Wed, 08 Mar 2023 13:17:38 +0100
parents 7eebac09fcb7
children a2443afe8a1f
files libmlk-util/CMakeLists.txt libmlk-util/mlk/util/util.h mlk-map/mlk-map.c mlk-tileset/mlk-tileset.c
diffstat 4 files changed, 40 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/libmlk-util/CMakeLists.txt	Wed Mar 08 12:51:18 2023 +0100
+++ b/libmlk-util/CMakeLists.txt	Wed Mar 08 13:17:38 2023 +0100
@@ -26,6 +26,7 @@
 	${libmlk-util_SOURCE_DIR}/mlk/util/openbsd/getopt.c
 	${libmlk-util_SOURCE_DIR}/mlk/util/openbsd/strlcat.c
 	${libmlk-util_SOURCE_DIR}/mlk/util/openbsd/strlcpy.c
+	${libmlk-util_SOURCE_DIR}/mlk/util/util.c
 	${libmlk-util_SOURCE_DIR}/mlk/util/util.h
 )
 
--- a/libmlk-util/mlk/util/util.h	Wed Mar 08 12:51:18 2023 +0100
+++ b/libmlk-util/mlk/util/util.h	Wed Mar 08 13:17:38 2023 +0100
@@ -86,6 +86,15 @@
 #endif
 
 /**
+ * Print a fatal message and exit with code 1.
+ *
+ * \pre fmt != NULL
+ * \param fmt the printf format string
+ */
+void
+mlk_util_die(const char *fmt, ...);
+
+/**
  * Compatibility version of OpenBSD [strlcpy].
  *
  * [strlcpy]: http://man.openbsd.org/strlcpy
--- a/mlk-map/mlk-map.c	Wed Mar 08 12:51:18 2023 +0100
+++ b/mlk-map/mlk-map.c	Wed Mar 08 13:17:38 2023 +0100
@@ -1,5 +1,5 @@
 /*
- * main.c -- convert tiled tiled JSON files into custom files
+ * mlk-map.c -- convert tiled tiled JSON files into custom files
  *
  * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
  *
@@ -18,29 +18,14 @@
 
 #include <assert.h>
 #include <limits.h>
-#include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
 #include <jansson.h>
 
 #include <mlk/util/util.h>
 
-static void
-die(const char *fmt, ...)
-{
-	assert(fmt);
-
-	va_list ap;
-
-	va_start(ap, fmt);
-	vfprintf(stderr, fmt, ap);
-	va_end(ap);
-	exit(1);
-}
-
 static bool
 is_layer(const char *name)
 {
@@ -108,9 +93,9 @@
 	json_t *height = json_object_get(document, "height");
 
 	if (!width || !json_is_integer(width))
-		die("missing 'width' property\n");
+		mlk_util_die("missing 'width' property\n");
 	if (!height || !json_is_integer(height))
-		die("missing 'height' property\n");
+		mlk_util_die("missing 'height' property\n");
 
 	printf("columns|%d\n", (int)json_integer_value(width));
 	printf("rows|%d\n", (int)json_integer_value(height));
@@ -129,13 +114,13 @@
 	const json_t *exec, *block;
 
 	if (!x || !json_is_number(x))
-		die("invalid 'x' property in object\n");
+		mlk_util_die("invalid 'x' property in object\n");
 	if (!y || !json_is_number(y))
-		die("invalid 'y' property in object\n");
+		mlk_util_die("invalid 'y' property in object\n");
 	if (!width || !json_is_number(width))
-		die("invalid 'width' property in object\n");
+		mlk_util_die("invalid 'width' property in object\n");
 	if (!height || !json_is_number(height))
-		die("invalid 'height' property in object\n");
+		mlk_util_die("invalid 'height' property in object\n");
 
 	/* This is optional and set to 0 if not present. */
 	block = find_property(props, "block");
@@ -165,9 +150,9 @@
 	size_t index;
 
 	if (!name || !json_is_string(name))
-		die("invalid 'name' property in layer");
+		mlk_util_die("invalid 'name' property in layer");
 	if (!is_layer(json_string_value(name)))
-		die("invalid 'name' layer: %s\n", json_string_value(name));
+		mlk_util_die("invalid 'name' layer: %s\n", json_string_value(name));
 
 	printf("layer|%s\n", json_string_value(name));
 
@@ -175,7 +160,7 @@
 	if (json_is_array(data)) {
 		json_array_foreach(data, index, tile) {
 			if (!json_is_integer(tile))
-				die("invalid 'data' property in layer\n");
+				mlk_util_die("invalid 'data' property in layer\n");
 
 			printf("%d\n", (int)json_integer_value(tile));
 		}
@@ -185,7 +170,7 @@
 	if (json_is_array(objects)) {
 		json_array_foreach(objects, index, object) {
 			if (!json_is_object(object))
-				die("invalid 'objects' property in layer\n");
+				mlk_util_die("invalid 'objects' property in layer\n");
 
 			write_object(object);
 		}
@@ -203,7 +188,7 @@
 
 	json_array_foreach(layers, index, layer) {
 		if (!json_is_object(layer))
-			die("layer is not an object\n");
+			mlk_util_die("layer is not an object\n");
 
 		write_layer(layer);
 	}
@@ -216,19 +201,19 @@
 	const json_t *tileset, *source;
 
 	if (json_array_size(tilesets) != 1)
-		die("map must contain exactly one tileset");
+		mlk_util_die("map must contain exactly one tileset");
 
 	tileset = json_array_get(tilesets, 0);
 	source = json_object_get(tileset, "source");
 
 	if (!json_is_string(source))
-		die("invalid 'source' property in tileset\n");
+		mlk_util_die("invalid 'source' property in tileset\n");
 
 	/* We need to replace the .json extension to .tileset. */
 	snprintf(path, sizeof (path), "%s", json_string_value(source));
 
 	if (!(ext = strstr(path, ".json")))
-		die("could not determine tileset extension");
+		mlk_util_die("could not determine tileset extension");
 
 	*ext = '\0';
 
@@ -244,7 +229,7 @@
 	document = json_loadf(stdin, 0, &error);
 
 	if (!document)
-		die("%d:%d: %s\n", error.line, error.column, error.text);
+		mlk_util_die("%d:%d: %s\n", error.line, error.column, error.text);
 
 	write_properties(json_object_get(document, "properties"));
 	write_dimensions(document);
--- a/mlk-tileset/mlk-tileset.c	Wed Mar 08 12:51:18 2023 +0100
+++ b/mlk-tileset/mlk-tileset.c	Wed Mar 08 13:17:38 2023 +0100
@@ -1,5 +1,5 @@
 /*
- * main.c -- convert tiled tilesets JSON files into custom files
+ * mlk-tileset.c -- convert tiled tilesets JSON files into custom files
  *
  * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
  *
@@ -17,24 +17,12 @@
  */
 
 #include <assert.h>
-#include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
 
 #include <jansson.h>
 
-static void
-die(const char *fmt, ...)
-{
-	assert(fmt);
-
-	va_list ap;
-
-	va_start(ap, fmt);
-	vfprintf(stderr, fmt, ap);
-	va_end(ap);
-	exit(1);
-}
+#include <mlk/util/util.h>
 
 static void
 write_dimensions(const json_t *document)
@@ -43,9 +31,9 @@
 	const json_t *tileheight = json_object_get(document, "tileheight");
 
 	if (!json_is_integer(tilewidth))
-		die("invalid 'tilewidth' property\n");
+		mlk_util_die("invalid 'tilewidth' property\n");
 	if (!json_is_integer(tileheight))
-		die("invalid 'tileheight' property\n");
+		mlk_util_die("invalid 'tileheight' property\n");
 
 	printf("tilewidth|%u\n", (unsigned int)json_integer_value(tilewidth));
 	printf("tileheight|%u\n", (unsigned int)json_integer_value(tileheight));
@@ -57,7 +45,7 @@
 	const json_t *image = json_object_get(document, "image");
 
 	if (!json_is_string(image))
-		die("invalid 'image' property\n");
+		mlk_util_die("invalid 'image' property\n");
 
 	printf("image|%s\n", json_string_value(image));
 }
@@ -72,7 +60,7 @@
 		const json_t *name = json_object_get(obj, "name");
 
 		if (!name || !json_is_string(name))
-			die("invalid property object\n");
+			mlk_util_die("invalid property object\n");
 
 		if (strcmp(json_string_value(name), prop) == 0)
 			return json_object_get(obj, "value");
@@ -94,7 +82,7 @@
 		return;
 
 	if (!json_is_integer(id))
-		die("invalid 'id' property in tile\n");
+		mlk_util_die("invalid 'id' property in tile\n");
 
 	if (json_is_string(file)) {
 		printf("%d|%s|", (int)json_integer_value(id), json_string_value(file));
@@ -120,9 +108,9 @@
 		return;
 
 	if (!json_is_integer(id))
-		die("invalid 'id' property in tile\n");
+		mlk_util_die("invalid 'id' property in tile\n");
 	if (!json_is_array(objects))
-		die("invalid 'objects' property in tile\n");
+		mlk_util_die("invalid 'objects' property in tile\n");
 
 	x = json_object_get(first, "x");
 	y = json_object_get(first, "y");
@@ -131,7 +119,7 @@
 
 	if (!json_is_integer(x) || !json_is_integer(y) ||
 	    !json_is_integer(w) || !json_is_integer(h))
-		die("invalid collide object in tile description\n");
+		mlk_util_die("invalid collide object in tile description\n");
 
 	printf("%d|%d|%d|%d|%d\n",
 	    (int)json_integer_value(id),
@@ -154,7 +142,7 @@
 
 	json_array_foreach(tiles, index, object) {
 		if (!json_is_object(object))
-			die("tile is not an object\n");
+			mlk_util_die("tile is not an object\n");
 
 		write_collision(object);
 	}
@@ -173,7 +161,7 @@
 
 	json_array_foreach(tiles, index, object) {
 		if (!json_is_object(object))
-			die("tile is not an object\n");
+			mlk_util_die("tile is not an object\n");
 
 		write_animation(object);
 	}
@@ -191,9 +179,9 @@
 	document = json_loadf(stdin, 0, &error);
 
 	if (!document)
-		die("%d:%d: %s\n", error.line, error.column, error.text);
+		mlk_util_die("%d:%d: %s\n", error.line, error.column, error.text);
 	if (!json_is_object(document))
-		die("root value isn't an object\n");
+		mlk_util_die("root value isn't an object\n");
 
 	write_dimensions(document);
 	write_image(document);