From f6aaf0667ee0f1ab62342ff7becca2beb6cabc9c Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Fri, 30 Aug 2013 00:55:38 +0200
Subject: [PATCH] Read in initial window sizes from files below config/.

---
 config/Win_Info  |  2 ++
 config/Win_Keys  |  2 ++
 config/Win_Log   |  2 ++
 config/Win_Map   |  2 ++
 src/main.c       | 16 ++++++----------
 src/wincontrol.c | 34 +++++++++++++++++++++++++++++++++-
 src/wincontrol.h | 10 ++++++++++
 7 files changed, 57 insertions(+), 11 deletions(-)
 create mode 100644 config/Win_Info
 create mode 100644 config/Win_Keys
 create mode 100644 config/Win_Log
 create mode 100644 config/Win_Map

diff --git a/config/Win_Info b/config/Win_Info
new file mode 100644
index 0000000..5ebcc55
--- /dev/null
+++ b/config/Win_Info
@@ -0,0 +1,2 @@
+3
+20
diff --git a/config/Win_Keys b/config/Win_Keys
new file mode 100644
index 0000000..8defa01
--- /dev/null
+++ b/config/Win_Keys
@@ -0,0 +1,2 @@
+0
+29
diff --git a/config/Win_Log b/config/Win_Log
new file mode 100644
index 0000000..245603a
--- /dev/null
+++ b/config/Win_Log
@@ -0,0 +1,2 @@
+-4
+20
diff --git a/config/Win_Map b/config/Win_Map
new file mode 100644
index 0000000..7ed456a
--- /dev/null
+++ b/config/Win_Map
@@ -0,0 +1,2 @@
+0
+-51
diff --git a/src/main.c b/src/main.c
index f637aea..bd25c4f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -204,18 +204,14 @@ int main(int argc, char *argv[])
                         "main().";
     exit_err(init_win_meta(screen, &win_meta), &world, err_winmem);
     world.wins.meta = &win_meta;
-    struct Win win_keys = init_win(&win_meta,
-                                   "Keys", 0,  29, &world, draw_keys_win);
-    struct Win win_info = init_win(&win_meta,
-                                   "Info", 3,  20, &world, draw_info_win);
-    struct Win win_log  = init_win(&win_meta,
-                                  "Log",  -4,  20, &world, draw_log_win);
-    struct Win win_map  = init_win(&win_meta,
-                                  "Map",   0, -51, &world, draw_map_win);
+    struct Win win_keys = init_win_from_file(&world, "Keys", draw_keys_win);
+    struct Win win_info = init_win_from_file(&world, "Info", draw_info_win);
+    struct Win win_log  = init_win_from_file(&world, "Log",  draw_log_win);
+    struct Win win_map  = init_win_from_file(&world, "Map",  draw_map_win);
     world.wins.keys = &win_keys;
-    world.wins.log = &win_log;
+    world.wins.log  = &win_log;
     world.wins.info = &win_info;
-    world.wins.map = &win_map;
+    world.wins.map  = &win_map;
     sorted_wintoggle(&world);
 
     /* Replay mode. */
diff --git a/src/wincontrol.c b/src/wincontrol.c
index acb6c56..4ebf9ef 100644
--- a/src/wincontrol.c
+++ b/src/wincontrol.c
@@ -1,10 +1,11 @@
 /* wincontrol.c */
 
 #include "wincontrol.h"
+#include <stdlib.h> /* for malloc(), free() */
 #include <string.h> /* for strlen() */
 #include <stdint.h> /* for uint8_t, uint16_t */
 #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(),
-                      * resize_active_win(), struct Win, struct WinMeta
+                      * resize_active_win(), init_win(), structs Win, WinMeta
                       */
 #include "yx_uint16.h" /* for yx_uint16 struct */
 #include "main.h" /* for Wins struct */
@@ -14,6 +15,37 @@
 
 
 
+extern struct Win init_win_from_file(struct World * world, char * w_name,
+                                     void (* f) (struct Win *))
+{
+    char * err = "Trouble in init_win_from_file() with malloc().";
+    char * prefix = "config/Win_";
+    uint8_t size = strlen(prefix) + strlen(w_name) + 1;
+    char * path = malloc(size);
+    exit_err(NULL == path, world, err);
+    sprintf(path, "%s%s", prefix, w_name);
+
+    err = "Trouble in init_win_from_file() with fopen().";
+    FILE * file = fopen(path, "r");
+    free(path);
+    exit_err(NULL == file, world, err);
+    uint16_t linemax;
+    textfile_sizes(file, &linemax, NULL);
+    char * line = malloc(linemax);
+    err = "Trouble in init_win_from_file() with fgets().";
+    exit_err(NULL == fgets(line, linemax, file), world, err);
+    int16_t height = atoi(line);
+    exit_err(NULL == fgets(line, linemax, file), world, err);
+    int16_t width = atoi(line);
+    free(line);
+    err = "Trouble in init_win_from_file() with fclose().";
+    exit_err(fclose(file), world, err);
+
+    struct WinMeta * wmeta = world->wins.meta;
+    return init_win(wmeta, w_name, height, width, world, f);
+}
+
+
 
 extern void sorted_wintoggle(struct World * world)
 {
diff --git a/src/wincontrol.h b/src/wincontrol.h
index 017c18b..33c3b76 100644
--- a/src/wincontrol.h
+++ b/src/wincontrol.h
@@ -16,6 +16,16 @@ struct World;
 
 
 
+/* Wrapper around init_win() that reads the desired window size from a file
+ * at the path prefixing the provided win name "w_name" with "config/Win_".
+ * "f" is the window drawing function (Win._draw()).
+ */
+extern struct Win init_win_from_file(struct World * world, char * w_name,
+                                     void (* f) (struct Win *));
+
+
+
+
 /* Toggle windows in world->wins in the order desribed by the first line of
  * config/toggled_win_order, wherein each character may correspond to one
  * hardcoded window.
-- 
2.30.2