#include <string.h> /* for strlen(), strtok() */
#include "main.h" /* for World struct */
#include "rexit.h" /* for exit_err() */
-#include "misc.h" /* for textfile_sizes() */
+#include "readwrite.h" /* for textfile_sizes() */
#include "keybindings.h"
-#include <stdlib.h> /* for malloc(), free(), atoi() */
-#include <stdint.h> /* for uint16_t */
-#include <ncurses.h> /* for keycode defines in get_keyname() */
-#include <string.h> /* for strchr(), strlen(), strcmp(), memcpy()*/
-#include "windows.h" /* for draw_all_wins() and WinMeta struct */
-#include "misc.h" /* for texfile_sizes() */
-#include "main.h" /* for World struct */
-#include "rexit.h" /* for err_exit() */
+#include <stdlib.h> /* for malloc(), free(), atoi() */
+#include <stdint.h> /* for uint16_t */
+#include <ncurses.h> /* for keycode defines in get_keyname() */
+#include <string.h> /* for strchr(), strlen(), strcmp(), memcpy()*/
+#include "windows.h" /* for draw_all_wins() and WinMeta struct */
+#include "readwrite.h" /* for texfile_sizes() */
+#include "main.h" /* for World struct */
+#include "rexit.h" /* for err_exit() */
#include <stdint.h> /* for uint8_t */
#include <stdio.h> /* for FILE typedef */
#include <string.h> /* for strchr(), strlen(), memcpy(), strtok() */
-#include "readwrite.h" /* for [read/write]_uint[8/16/23][_bigendian]() */
-#include "misc.h" /* for textfile_sizes(), find_passable_pos() */
+#include "readwrite.h" /* for textfile_sizes(),
+ * [read/write]_uint[8/16/23][_bigendian]()
+ */
+#include "misc.h" /* for find_passable_pos() */
#include "main.h" /* for World struct */
#include "rexit.h" /* for err_exit() */
-extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
- uint16_t * n_lines_p)
-{
- int c = 0;
- uint16_t c_count = 0;
- uint16_t n_lines = 0;
- uint16_t linemax = 0;
- while (1)
- {
- c = getc(file);
- if (EOF == c)
- {
- break;
- }
- c_count++;
- if ('\n' == c)
- {
- if (c_count > linemax)
- {
- linemax = c_count;
- }
- c_count = 0;
- if (n_lines_p)
- {
- n_lines++;
- }
- }
- }
- if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
- { /* line / lack newline chars. */
- linemax = c_count;
- }
-
- if (-1 == fseek(file, 0, SEEK_SET))
- {
- return 1;
- }
- * linemax_p = linemax;
- if (n_lines_p)
- {
- * n_lines_p = n_lines;
- }
- return 0;
-}
-
-
-
extern void update_log(struct World * world, char * text)
{
static char * last_msg; /* TODO: valgrind is dissatisfied */
-#include <stdint.h> /* for uint8_t, uint16_t */
-#include <stdio.h> /* for FILE typedef */
+#include <stdint.h> /* for uint16_t */
#include "yx_uint16.h" /* for yx_uint16 coordinates */
struct World;
struct Map;
-/* 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);
-
-
-
/* Update game log by appending "text", or by appending a "." if "text" is the
* same as the last one passed.
*/
/* readwrite.c */
#include "readwrite.h"
-#include <stdio.h> /* for FILE typedef*/
+#include <stdio.h> /* for FILE typedef, fgetc(), fputc(), fseek() */
#include <stdint.h> /* for uint8_t, uint16_t, uint32_t */
static uint8_t write_uintX_bigendian(FILE * file, uint32_t x, uint8_t size);
+
+extern uint8_t textfile_sizes(FILE * file, uint16_t * linemax_p,
+ uint16_t * n_lines_p)
+{
+ int c = 0;
+ uint16_t c_count = 0;
+ uint16_t n_lines = 0;
+ uint16_t linemax = 0;
+ while (1)
+ {
+ c = fgetc(file);
+ if (EOF == c)
+ {
+ break;
+ }
+ c_count++;
+ if ('\n' == c)
+ {
+ if (c_count > linemax)
+ {
+ linemax = c_count;
+ }
+ c_count = 0;
+ if (n_lines_p)
+ {
+ n_lines++;
+ }
+ }
+ }
+ if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
+ { /* line / lack newline chars. */
+ linemax = c_count;
+ }
+
+ if (-1 == fseek(file, 0, SEEK_SET))
+ {
+ return 1;
+ }
+ * linemax_p = linemax;
+ if (n_lines_p)
+ {
+ * n_lines_p = n_lines;
+ }
+ return 0;
+}
+
+
+
static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size)
{
* x = 0;
/* readwrite.h:
*
- * Routines for reading/writing (multi-)byte data from/to files. They ensure a
- * defined endianness.
+ * Routines for reading and writing files.
*/
#ifndef READWRITE_H
-/* Each function returns 0 on success and 1 on failure. "x" is the value to be
- * written to "file" for write_* functions and for read_* functions the pointer
- * to where the value read from "file" is to be written.
+/* 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);
+
+
+
+/* These routines for reading values "x" from / writing values to "file" ensure a
+ * defined endianness and consistent error codes: return 0 on success and 1 on
+ * fgetc()/fputc() failure.
*/
extern uint8_t read_uint8(FILE * file, uint8_t * x);
extern uint8_t read_uint16_bigendian(FILE * file, uint16_t * x);
*/
#include "yx_uint16.h" /* for yx_uint16 struct */
#include "main.h" /* for Wins struct */
-#include "misc.h" /* for textfile_sizes() */
+#include "readwrite.h" /* for textfile_sizes() */
#include "rexit.h" /* for exit_err() */
#include "main.h" /* for World, Wins structs */
#include "draw_wins.h" /* for draw_keys_win(), draw_info_win(), draw_log_win(),