From: Christian Heller Date: Wed, 4 Sep 2013 01:38:13 +0000 (+0200) Subject: Moved textfile_sizes() to readwrite library. X-Git-Tag: tce~976 X-Git-Url: https://plomlompom.com/repos/te"st.html?a=commitdiff_plain;h=0e6c855b67e0cd3def7196396f3d1f45ff85b692;p=plomrogue Moved textfile_sizes() to readwrite library. --- diff --git a/src/command_db.c b/src/command_db.c index bdcdcd1..61ae4b1 100644 --- a/src/command_db.c +++ b/src/command_db.c @@ -7,7 +7,7 @@ #include /* 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() */ diff --git a/src/keybindings.c b/src/keybindings.c index 8b01958..75fe728 100644 --- a/src/keybindings.c +++ b/src/keybindings.c @@ -3,14 +3,14 @@ #include "keybindings.h" -#include /* for malloc(), free(), atoi() */ -#include /* for uint16_t */ -#include /* for keycode defines in get_keyname() */ -#include /* 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 /* for malloc(), free(), atoi() */ +#include /* for uint16_t */ +#include /* for keycode defines in get_keyname() */ +#include /* 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() */ diff --git a/src/map_objects.c b/src/map_objects.c index f623801..2edb31d 100644 --- a/src/map_objects.c +++ b/src/map_objects.c @@ -5,8 +5,10 @@ #include /* for uint8_t */ #include /* for FILE typedef */ #include /* 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() */ diff --git a/src/misc.c b/src/misc.c index 4b25f65..4632a0b 100644 --- a/src/misc.c +++ b/src/misc.c @@ -17,53 +17,6 @@ -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 */ diff --git a/src/misc.h b/src/misc.h index 568c56d..c7b8855 100644 --- a/src/misc.h +++ b/src/misc.h @@ -9,26 +9,13 @@ -#include /* for uint8_t, uint16_t */ -#include /* for FILE typedef */ +#include /* 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. */ diff --git a/src/readwrite.c b/src/readwrite.c index 5b31852..96505ad 100644 --- a/src/readwrite.c +++ b/src/readwrite.c @@ -1,7 +1,7 @@ /* readwrite.c */ #include "readwrite.h" -#include /* for FILE typedef*/ +#include /* for FILE typedef, fgetc(), fputc(), fseek() */ #include /* for uint8_t, uint16_t, uint32_t */ @@ -25,6 +25,54 @@ static uint8_t read_uintX_bigendian(FILE * file, uint32_t * x, uint8_t size); 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; diff --git a/src/readwrite.h b/src/readwrite.h index cf4de14..128e44a 100644 --- a/src/readwrite.h +++ b/src/readwrite.h @@ -1,7 +1,6 @@ /* 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 @@ -14,9 +13,21 @@ -/* 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); diff --git a/src/wincontrol.c b/src/wincontrol.c index 10a9382..aeae7b2 100644 --- a/src/wincontrol.c +++ b/src/wincontrol.c @@ -12,7 +12,7 @@ */ #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(),