cepheid

An Aurora 4X clone
Log | Files | Refs | README

commit e6f2e0819aa4045a81a2ee64ae29f71e11e9048a
parent e07bf3e21c611319129310df7f6d72ff39b01bb1
Author: hhvn <dev@hhvn.uk>
Date:   Thu, 17 Nov 2022 18:53:16 +0000

Rewrite dir2c.sh as dir2h.sh (generates data only)

Diffstat:
MMakefile | 2+-
Mdata/Makefile | 6+++---
Ddata/dir2c.sh | 48------------------------------------------------
Adata/dir2h.sh | 40++++++++++++++++++++++++++++++++++++++++
Adata/dirs.c | 33+++++++++++++++++++++++++++++++++
Msrc/main.h | 4++--
Msrc/save.c | 2+-
7 files changed, 80 insertions(+), 55 deletions(-)

diff --git a/Makefile b/Makefile @@ -3,7 +3,7 @@ DBDIR = db DBLIB = $(DBDIR)/db.o DBTOOL = $(DBDIR)/dbtool SRCDIR = src -SRC = $(shell find $(SRCDIR) -name "*.c") styles/$(STYLE).c data/sol.c +SRC = $(shell find $(SRCDIR) -name "*.c") styles/$(STYLE).c data/dirs.c OBJ = $(SRC:.c=.o) BIN = cepheid RAYLIB = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 diff --git a/data/Makefile b/data/Makefile @@ -2,7 +2,7 @@ FONTS = $(shell find . -name "*.ttf") ICONS = $(shell find icons -name "*.png") HEADERS = $(FONTS:.ttf=.h) $(ICONS:.png=.h) -all: $(HEADERS) sol.c +all: $(HEADERS) dirs.c sol: worlds.tsv worlds-parse.awk rm -rf sol @@ -11,8 +11,8 @@ sol: worlds.tsv worlds-parse.awk printf "x\t0\n" >> sol/index printf "y\t0\n" >> sol/index -sol.c: sol - ./dir2c.sh sol +dirs.c: sol + ./dir2h.sh sol # Thanks Jonathan worlds.tsv: diff --git a/data/dir2c.sh b/data/dir2c.sh @@ -1,48 +0,0 @@ -#!/bin/sh - -p="wdir_" -d="$1" -h="$d.h" -c="$d.c" - -rm -f $h $c - -cat > $c << EOF -#include <stdio.h> -#include <fcntl.h> -#include <unistd.h> -#include <limits.h> -#include <sys/stat.h> -#include "${h}" - -int -${p}${d}(char *dir) { - char path[PATH_MAX]; - int fd; - - if (mkdir(dir, 0755) == -1) - return -1; -EOF - -for f in $d/*; do - b=$(basename "$f") - v="${p}${d}_$(echo "$b" | tr " ()!'-" 'abcdef')" - - echo "unsigned char $v[] = {" >> $h - xxd -i < $f >> $h - echo "};" >> $h - - cat >> $c << EOF - snprintf(path, PATH_MAX, "%s/$b", dir); - fd = open(path, O_WRONLY|O_CREAT, 0644); - if (fd == -1) - return -1; - write(fd, $v, sizeof($v)); - close(fd); -EOF -done - -cat >> $c << EOF - return 0; -} -EOF diff --git a/data/dir2h.sh b/data/dir2h.sh @@ -0,0 +1,40 @@ +#!/bin/sh + +p="wdir_" +h="dirs.h" +c="dirs.c" + +cat > $h << EOF +struct WDir { + char *dir; + char *name; + unsigned char *data; + size_t size; +}; + +EOF + +for d in $*; do + for f in $d/*; do + b=$(basename "$f") + v="${p}${d}_$(echo "$b" | tr " ()!'-" 'abcdef')" + + echo "static unsigned char $v[] = {" >> $h + xxd -i < $f >> $h + echo "};" >> $h + done +done + +echo "static struct WDir wdirs[] = {" >> $h + +for d in $*; do + for f in $d/*; do + b=$(basename "$f") + v="${p}${d}_$(echo "$b" | tr " ()!'-" 'abcdef')" + + printf "\t{ \"%s\", \"%s\", %s, sizeof(%s) },\n" \ + "$d" "$b" "$v" "$v" >> $h + done +done + +echo "};" >> $h diff --git a/data/dirs.c b/data/dirs.c @@ -0,0 +1,33 @@ +#include <stdio.h> +#include <fcntl.h> +#include <unistd.h> +#include <limits.h> +#include <string.h> +#include <sys/stat.h> +#include "dirs.h" + +#define LEN(array) (sizeof(array)/sizeof(array[0])) + +int +dirs_write(char *dir, char *to) { + char path[PATH_MAX]; + int fd; + size_t i; + + if (mkdir(to, 0755) == -1) + return -1; + + for (i = 0; i < LEN(wdirs); i++) { + if (strcmp(wdirs[i].dir, dir) == 0) { + snprintf(path, PATH_MAX, "%s/%s", + to, wdirs[i].name); + fd = open(path, O_WRONLY|O_CREAT, 0644); + if (fd == -1) + return -1; + write(fd, wdirs[i].data, wdirs[i].size); + close(fd); + } + } + + return 0; +} diff --git a/src/main.h b/src/main.h @@ -176,8 +176,8 @@ void save_write(void); int save_exists(char *name); int save_create(char *name); -/* ../data/sol.c */ -int wdir_sol(char *dir); +/* ../data/dirs.c */ +int dirs_write(char *dir, char *to); /* data.c */ extern Font font; diff --git a/src/save.c b/src/save.c @@ -82,7 +82,7 @@ save_create(char *name) { return -1; snprintf(path, sizeof(path), "%s/%s/Systems/Sol", SAVEDIR, name); - if (wdir_sol(path) == -1) + if (dirs_write("sol", path) == -1) return -1; snprintf(path, sizeof(path), "%s/%s/index", SAVEDIR, name);