changeset 32:91bc2329ab0c

core: implement data directory access, closes #2454 @1h
author David Demelier <markand@malikania.fr>
date Mon, 13 Jan 2020 13:28:53 +0100
parents 80f008ce2079
children 7035c1eb44fb
files Makefile src/main.c src/sys.c src/sys.h
diffstat 4 files changed, 127 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sun Jan 12 21:05:42 2020 +0100
+++ b/Makefile	Mon Jan 13 13:28:53 2020 +0100
@@ -37,6 +37,10 @@
 OBJS=           ${SRCS:.c=.o}
 DEPS=           ${SRCS:.c=.d}
 
+PREFIX=         /usr/local
+BINDIR=         ${PREFIX}/bin
+SHAREDIR=       ${PREFIX}/share
+
 SDL_CFLAGS=     `pkg-config --cflags sdl2 SDL2_image SDL2_mixer SDL2_ttf`
 SDL_LDFLAGS=    `pkg-config --libs sdl2 SDL2_image SDL2_mixer SDL2_ttf`
 
@@ -61,7 +65,7 @@
 -include ${DEPS} ${TESTS_DEPS} ${TOOLS_DEPS}
 
 .c.o:
-	${CC} ${SDL_CFLAGS} ${CFLAGS} -c $< -o $@
+	${CC} -DPREFIX=\""${PREFIX}"\" -DBINDIR=\""${BINDIR}"\" -DSHAREDIR=\""${SHAREDIR}"\" ${SDL_CFLAGS} ${CFLAGS} -c $< -o $@
 
 .c:
 	${CC} ${TESTS_INCS} -o $@ ${CFLAGS} $< ${TESTS_LIBS}
@@ -85,6 +89,13 @@
 doxygen:
 	doxygen doxygen/Doxyfile
 
+install:
+	mkdir -p ${DESTDIR}${BINDIR}
+	cp molko ${DESTDIR}${BINDIR}
+	chmod 755 ${DESTDIR}${BINDIR}/molko
+	mkdir -p ${DESTDIR}${SHAREDIR}/molko
+	cp -R assets/* ${DESTDIR}${SHAREDIR}/molko
+
 clean:
 	rm -f ${PROG} src/main.o src/main.d
 	rm -f ${LIB} ${OBJS} ${DEPS}
--- a/src/main.c	Sun Jan 12 21:05:42 2020 +0100
+++ b/src/main.c	Mon Jan 13 13:28:53 2020 +0100
@@ -49,6 +49,9 @@
 		exit(1);
 	}
 
+	printf("----> [%s]\n", sys_datapath("default.map"));
+	exit(0);
+
 	if (!map_open(&map, "default.map"))
 		exit(1);
 
--- a/src/sys.c	Sun Jan 12 21:05:42 2020 +0100
+++ b/src/sys.c	Mon Jan 13 13:28:53 2020 +0100
@@ -16,13 +16,74 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <assert.h>
+
 #include <SDL.h>
 #include <SDL_image.h>
 #include <SDL_mixer.h>
 #include <SDL_ttf.h>
 
+#if defined(_WIN32)
+#	error "TODO"
+#else                           /* Assuming POSIX */
+#	include <sys/types.h>
+#	include <dirent.h>
+#endif
+
 #include "sys.h"
 
+#if defined(_WIN32)
+#	error "TODO"
+#else                           /* Assuming POSIX */
+
+static bool
+is_absolute(const char *path)
+{
+        assert(path);
+
+        return path[0] == '/';
+}
+
+static void
+determine(char path[], size_t pathlen)
+{
+        char localassets[FILENAME_MAX];
+        char *base = SDL_GetBasePath();
+        DIR *dp;
+
+        /* Try assets directory where executable lives. */
+        snprintf(localassets, sizeof (localassets), "%sassets", base);
+
+        if ((dp = opendir(localassets))) {
+                snprintf(path, pathlen, "%sassets", base);
+                closedir(dp);
+        } else {
+                /* We are not in the project source directory. */
+                if (is_absolute(SHAREDIR)) {
+                        /* SHAREDIR is absolute */
+                        snprintf(path, pathlen, "%s/molko", SHAREDIR);
+                } else if (is_absolute(BINDIR)) {
+                        /* SHAREDIR is relative but BINDIR is absolute */
+                        snprintf(path, pathlen, "%s/%s/molko", PREFIX, SHAREDIR);
+                } else {
+                        /* SHAREDIR, BINDIR are both relative */
+                        char *ptr = strstr(base, BINDIR);
+
+                        if (ptr) {
+                                *ptr = '\0';
+                                snprintf(path, pathlen, "%s%s/molko", base, SHAREDIR);
+                        } else {
+                                /* Unable to determine. */
+                                snprintf(path, pathlen, ".");
+                        }
+                }
+        }
+
+        SDL_free(base);
+}
+
+#endif
+
 bool
 sys_init(void)
 {
@@ -41,18 +102,37 @@
 const char *
 sys_datadir(void)
 {
-#if 0
-	static char path[1024] = { 0 };
+	static char path[1024];
 
-	if (path[0] == '\0') {
-		char *prefix = SDL_GetBasePath();
-		printf("%s\n", prefix);
-		SDL_free(prefix);
-	}
+	if (path[0] == '\0')
+		determine(path, sizeof (path));
 
 	return path;
-#endif
-	return NULL;
+}
+
+const char *
+sys_datapath(const char *fmt, ...)
+{
+        const char *ret;
+        va_list ap;
+
+        va_start(ap, fmt);
+        ret = sys_datapathv(fmt, ap);
+        va_end(ap);
+
+        return ret;
+}
+
+const char *
+sys_datapathv(const char *fmt, va_list ap)
+{
+        static char path[2048];
+        char filename[FILENAME_MAX];
+
+        vsnprintf(filename, sizeof (filename), fmt, ap);
+        snprintf(path, sizeof (path), "%s/%s", sys_datadir(), filename);
+
+        return path;
 }
 
 void
--- a/src/sys.h	Sun Jan 12 21:05:42 2020 +0100
+++ b/src/sys.h	Mon Jan 13 13:28:53 2020 +0100
@@ -24,6 +24,7 @@
  * \brief System routines.
  */
 
+#include <stdarg.h>
 #include <stdbool.h>
 
 /**
@@ -35,12 +36,33 @@
 /**
  * Get the base system directory path.
  *
- * \return the path
+ * \return the path where the executable lives
  */
 const char *
 sys_datadir(void);
 
 /**
+ * Construct path to assets directory using printf-like format.
+ *
+ * \param fmt the format string
+ * \return the path to the file
+ * \note This function returns pointer to static string.
+ */
+const char *
+sys_datapath(const char *fmt, ...);
+
+/**
+ * Similar to \a sys_datapath.
+ *
+ * \param fmt the format string
+ * \param ap the variadic arguments pointer
+ * \return the path to the file
+ * \note This function returns pointer to static string.
+ */
+const char *
+sys_datapathv(const char *fmt, va_list ap);
+
+/**
  * Close the system.
  */
 void