home · contact · privacy
Simplified textfile_sizes() and replaced all get_linemax() calls with it.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 26 Nov 2013 16:20:13 +0000 (17:20 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 26 Nov 2013 16:20:13 +0000 (17:20 +0100)
README
src/command_db.c
src/keybindings.c
src/map_object_actions.c
src/map_objects.c
src/misc.c
src/readwrite.c
src/readwrite.h
src/wincontrol.c

diff --git a/README b/README
index 33a231fc8c84e09c3b92313852468c34e3c34c5a..5ce32d5241d46d372a22b27388d7cb0d1aa5f43d 100644 (file)
--- a/README
+++ b/README
@@ -12,9 +12,8 @@ skeletons or "magic meat"--such objects can be collected, and "magic meat" can
 be consumed to gain hitpoints. Note that different kinds of moves take different
 numbers of turns to finish.
 
 be consumed to gain hitpoints. Note that different kinds of moves take different
 numbers of turns to finish.
 
-Enemies' AI is very dumb so far: Each turn, they
-try to move in the (beeline) direction of the nearest enemy, so they often bump
-into obstacles.
+Enemies' AI is very dumb so far: Each turn, they try to move in the (beeline)
+direction of the nearest enemy, so they often bump into obstacles.
 
 There is only one save file (named "savefile"), and it gets overwritten each new
 turn. To start over with a new world, delete it.
 
 There is only one save file (named "savefile"), and it gets overwritten each new
 turn. To start over with a new world, delete it.
index fd7f4dc7a2f78ae431ebd0bf37ee52bfda574da1..110f91f4eb2ddcf9c1fade659375e35b493526b0 100644 (file)
@@ -82,9 +82,8 @@ extern void init_command_db()
 
     char * path = "config/commands";
     FILE * file = try_fopen(path, "r", f_name);
 
     char * path = "config/commands";
     FILE * file = try_fopen(path, "r", f_name);
-    uint16_t lines, linemax;
-    uint8_t test = textfile_sizes(file, &linemax, &lines);
-    exit_trouble(test, f_name, "textfile_sizes()");
+    uint16_t lines;
+    uint16_t linemax = textfile_sizes(file, &lines);
     char line[linemax + 1];
 
     struct Command * cmds = try_malloc(lines * sizeof(struct Command), f_name);
     char line[linemax + 1];
 
     struct Command * cmds = try_malloc(lines * sizeof(struct Command), f_name);
index 29afb78483eaf8c35c76e4d4941427cf5287225a..0fd2cdcd56d77d47f831145ced0eb09a2c0a7559 100644 (file)
@@ -146,9 +146,8 @@ extern void init_keybindings(char * path, struct KeyBiData * kbd)
     char * f_name = "init_keybindings()";
 
     FILE * file = try_fopen(path, "r", f_name);
     char * f_name = "init_keybindings()";
 
     FILE * file = try_fopen(path, "r", f_name);
-    uint16_t lines, linemax;
-    char * err = "textfile_sizes() in init_keybindings() returns error.";
-    exit_err(textfile_sizes(file, &linemax, &lines), err);
+    uint16_t lines;
+    uint16_t linemax = textfile_sizes(file, &lines);
 
     char command[linemax + 1];
     char * cmdptr;
 
     char command[linemax + 1];
     char * cmdptr;
index cdd1e97e322eeaa0b07958a71657710d59eeb527..9541214d1fadd8d71f6004556c5b09616da27940 100644 (file)
@@ -12,7 +12,7 @@
 #include "misc.h" /* for update_log(), try_malloc() */
 #include "map.h" /* for is_passable() */
 #include "main.h" /* for world global */
 #include "misc.h" /* for update_log(), try_malloc() */
 #include "map.h" /* for is_passable() */
 #include "main.h" /* for world global */
-#include "readwrite.h" /* for try_fopen(), try_fclose(), get_linemax() */
+#include "readwrite.h" /* for try_fopen(), try_fclose(), textfile_sizes() */
 #include "rexit.h" /* for exit_err() */
 
 
 #include "rexit.h" /* for exit_err() */
 
 
@@ -186,7 +186,7 @@ extern void init_map_object_actions()
 
     char * path = "config/map_object_actions";
     FILE * file = try_fopen(path, "r", f_name);
 
     char * path = "config/map_object_actions";
     FILE * file = try_fopen(path, "r", f_name);
-    uint16_t linemax = get_linemax(file, f_name);
+    uint16_t linemax = textfile_sizes(file, NULL);
     char line[linemax + 1];
 
     struct MapObjAct ** moa_ptr_ptr = &world.map_obj_acts;
     char line[linemax + 1];
 
     struct MapObjAct ** moa_ptr_ptr = &world.map_obj_acts;
index 1a598cba7e8ba302c7cbb4b2ae0e0f53a40d2564..0407352e8e494ebf95078eeac1401cb971e0b964 100644 (file)
@@ -5,8 +5,8 @@
 #include <stdint.h> /* for uint8_t */
 #include <stdio.h> /* for FILE typedef */
 #include <string.h> /* for strchr(), strlen(), memcpy(), strtok() */
 #include <stdint.h> /* for uint8_t */
 #include <stdio.h> /* for FILE typedef */
 #include <string.h> /* for strchr(), strlen(), memcpy(), strtok() */
-#include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose()
-                        * [read/write]_uint[8/16/23][_bigendian]()
+#include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose(),
+                        * try_fgets()
                         */
 #include "misc.h" /* for try_malloc(), find_passable_pos() */
 #include "main.h" /* for world global */
                         */
 #include "misc.h" /* for try_malloc(), find_passable_pos() */
 #include "main.h" /* for world global */
@@ -72,7 +72,7 @@ extern void init_map_object_defs(char * filename)
 {
     char * f_name = "init_map_object_defs()";
     FILE * file = try_fopen(filename, "r", f_name);
 {
     char * f_name = "init_map_object_defs()";
     FILE * file = try_fopen(filename, "r", f_name);
-    uint16_t linemax = get_linemax(file, f_name);
+    uint16_t linemax = textfile_sizes(file, NULL);
     struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs;
     char * delim = " ";
     char line[linemax + 1];
     struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs;
     char * delim = " ";
     char line[linemax + 1];
index 1b8fe63ee789aa85427134a120ad4d405c71cbbb..97c9fa69a96d0a2445eb54965c2fa6b5fdab3b47 100644 (file)
@@ -6,7 +6,7 @@
 #include <stdlib.h> /* for size_t, calloc(), free() */
 #include <string.h> /* for strlen(), strcmp(), memcpy() */
 #include <stdint.h> /* for uint8_t, uint16_t */
 #include <stdlib.h> /* for size_t, calloc(), free() */
 #include <string.h> /* for strlen(), strcmp(), memcpy() */
 #include <stdint.h> /* for uint8_t, uint16_t */
-#include "readwrite.h" /* for try_fopen(), try_fclose(), get_linemax(),
+#include "readwrite.h" /* for try_fopen(), try_fclose(), textfile_sizes(),
                         * try_fputc(), try_fgetc()
                         */
 #include "map_objects.h" /* for struct MapObj, get_player(), read_map_objects(),
                         * try_fputc(), try_fgetc()
                         */
 #include "map_objects.h" /* for struct MapObj, get_player(), read_map_objects(),
@@ -283,7 +283,7 @@ extern void load_game()
     char * f_name = "load_game2()";
     char * filename = "savefile";
     FILE * file = try_fopen(filename, "r", f_name);
     char * f_name = "load_game2()";
     char * filename = "savefile";
     FILE * file = try_fopen(filename, "r", f_name);
-    uint16_t linemax = get_linemax(file, f_name);
+    uint16_t linemax = textfile_sizes(file, NULL);
     char line[linemax + 1];
     try_fgets(line, linemax + 1, file, f_name);
     world.mapseed = atoi(line);
     char line[linemax + 1];
     try_fgets(line, linemax + 1, file, f_name);
     world.mapseed = atoi(line);
index 78f1bffb73d4b4472c5daa699ba4220f6c797456..06a63448cc9d157974e07140a5d8ad2f2710215a 100644 (file)
@@ -105,17 +105,7 @@ extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
 
 
 
 
 
 
-extern uint16_t get_linemax(FILE * file, char * f)
-{
-    uint16_t linemax;
-    exit_trouble(textfile_sizes(file, &linemax, NULL), f, "textfile_sizes()");
-    return linemax;
-}
-
-
-
-extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
-                              uint16_t * n_lines_p)
+extern uint16_t textfile_sizes(FILE * file, uint16_t * n_lines_p)
 {
     char * f_name = "textfile_sizes()";
     int c = 0;
 {
     char * f_name = "textfile_sizes()";
     int c = 0;
@@ -147,17 +137,12 @@ extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
     {                                /* line / lack newline chars.            */
         linemax = c_count;
     }
     {                                /* line / lack newline chars.            */
         linemax = c_count;
     }
-
-    if (-1 == fseek(file, 0, SEEK_SET))
-    {
-        return 1;
-    }
-    * linemax_p = linemax;
+    exit_trouble(-1 == fseek(file, 0, SEEK_SET), f_name, "fseek()");
     if (n_lines_p)
     {
         * n_lines_p = n_lines;
     }
     if (n_lines_p)
     {
         * n_lines_p = n_lines;
     }
-    return 0;
+    return linemax;
 }
 
 
 }
 
 
index 2e0ca9f47e858a57241c9b4595dffffadd8fe661..0dc47da9eb4a7ee404bf48892e49868d76475caf 100644 (file)
@@ -11,8 +11,8 @@
 
 
 
 
 
 
-/* Wrappers to calling from function called "f" of fopen(), fclose(), fgets()
- * and fwrite() and calling exit_err() with appropriate error messages.
+/* Wrappers to fopen(), fclose(), fgets() and fwrite() from function called "f",
+ * calling exit_err() upon error with appropriate error messages.
  */
 extern FILE * try_fopen(char * path, char * mode, char * f);
 extern void try_fclose(FILE * file, char * f);
  */
 extern FILE * try_fopen(char * path, char * mode, char * f);
 extern void try_fclose(FILE * file, char * f);
@@ -26,34 +26,25 @@ extern void try_fputc(uint8_t c, FILE * file, char * f);
  */
 extern char * try_fgets(char * line, int size, FILE * file, char * f);
 
  */
 extern char * try_fgets(char * line, int size, FILE * file, char * f);
 
-/* Wrappers to calling fgetc() and (try_fgetc_noeof()) treating all EOFs returns
- * as errors to call exit_trouble(), or (try_fgetc()) only if ferror() says so.
+/* Wrapper to calling fgetc() from function "f", treating either
+ * (try_fgetc_noeof()) all EOF returns as errors triggering exit_trouble(), or
+ * (try_fgetc()) only those accompanied by a positive ferror() result.
  */
 extern int try_fgetc(FILE * file, char * f);
 extern uint8_t try_fgetc_noeof(FILE * file, char * f);
 
 /* Wrapper to successive call of fclose() from function called "f" on "file",
  */
 extern int try_fgetc(FILE * file, char * f);
 extern uint8_t try_fgetc_noeof(FILE * file, char * f);
 
 /* Wrapper to successive call of fclose() from function called "f" on "file",
- * then unlink() on file at "p2" if it exists, then rename() on "p1" to "p2".
- * Used for handling atomic saving of files via temp files.
+ * then unlink() on file at path "p2" if it exists, then rename() from path "p1"
+ * to "p2". Used for handling atomic saving of files via temp files.
  */
 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
                                      char * f);
 
  */
 extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
                                      char * f);
 
-/* Wrapper: Call textfile_sizes() from function called "f" to get max line
- * length (includes newline char) of "file", exit via exit_err() with
- * exit_trouble() on failure.
+/* Return largest line length from "file" the largest line length (including
+ * newline chars) and write the number of newline chars in "file" to the memory
+ * pointed to by "n_lines_p" if it is not passed as NULL.
  */
  */
-extern uint16_t get_linemax(FILE * file, char * f);
-
-/* Learn from "file" the largest line length (pointed to by "linemax_p"; length
- * includes newline chars) and (pointed to by "n_lines_p" if it is not set to
- * NULL) the number of lines (= number of newline chars).
- *
- * Returns 0 on success, 1 on error of fseek() (called to return to initial file
- * reading position).
- */
-extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
-                              uint16_t * n_lines_p);
+extern uint16_t textfile_sizes(FILE * file, uint16_t * n_lines_p);
 
 /* Read/write via try_(fgetc|fputc)() uint32 values with defined endianness. */
 extern uint32_t read_uint32_bigendian(FILE * file);
 
 /* Read/write via try_(fgetc|fputc)() uint32 values with defined endianness. */
 extern uint32_t read_uint32_bigendian(FILE * file);
index 6371efa919c65b111cc6d2c0d2c5a994c7401b28..74bd4a202b5a96f73973e4f399985d5b37ec75ca 100644 (file)
@@ -9,7 +9,7 @@
                       */
 #include "yx_uint16.h" /* for yx_uint16 struct */
 #include "main.h" /* for world global */
                       */
 #include "yx_uint16.h" /* for yx_uint16 struct */
 #include "main.h" /* for world global */
-#include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose(),
+#include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose(),
                         * try_fgets(), try_fclose_unlink_rename(), try_fwrite()
                         * try_fgetc_noeof()
                         */
                         * try_fgets(), try_fclose_unlink_rename(), try_fwrite()
                         * try_fgetc_noeof()
                         */
@@ -83,7 +83,7 @@ static void init_winconf_from_file(char id, struct WinConf * winconf)
     /* Prepare reading in file line by line into "line" array. */
     FILE * file = try_fopen(path, "r", context);
     free(path);
     /* Prepare reading in file line by line into "line" array. */
     FILE * file = try_fopen(path, "r", context);
     free(path);
-    uint16_t linemax = get_linemax(file, context);
+    uint16_t linemax = textfile_sizes(file, NULL/*, context*/);
     char line[linemax + 1];
 
     /* Read/determine winconf->title, ->draw, ->height(_type),->width(_type). */
     char line[linemax + 1];
 
     /* Read/determine winconf->title, ->draw, ->height(_type),->width(_type). */
@@ -396,7 +396,7 @@ extern void sorted_wintoggle_and_activate()
     /* Read from file order of windows to be toggled + active win selection. */
     char * path = "config/windows/toggle_order_and_active";
     FILE * file = try_fopen(path, "r", f_name);
     /* Read from file order of windows to be toggled + active win selection. */
     char * path = "config/windows/toggle_order_and_active";
     FILE * file = try_fopen(path, "r", f_name);
-    uint16_t linemax = get_linemax(file, f_name);
+    uint16_t linemax = textfile_sizes(file, NULL);
     char win_order[linemax + 1];
     try_fgets(win_order, linemax + 1, file, f_name);
     uint8_t a = try_fgetc_noeof(file, f_name);
     char win_order[linemax + 1];
     try_fgets(win_order, linemax + 1, file, f_name);
     uint8_t a = try_fgetc_noeof(file, f_name);