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.
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 * 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;
#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() */
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;
#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 */
{
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];
#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(),
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);
-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;
{ /* 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;
}
- return 0;
+ return linemax;
}
-/* 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 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",
- * 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);
-/* 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);
*/
#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()
*/
/* 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). */
/* 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);