From: Christian Heller Date: Tue, 25 Mar 2014 15:45:30 +0000 (+0100) Subject: Move tests for value size < UINT8_MAX into err_try_fgets() (flag: "8"). X-Git-Tag: tce~806 X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bprefix%7D%7D/day?a=commitdiff_plain;h=1d09b2399527569b3a8cea18abf44cad33248719;p=plomrogue Move tests for value size < UINT8_MAX into err_try_fgets() (flag: "8"). --- diff --git a/src/common/err_try_fgets.c b/src/common/err_try_fgets.c index 7f399a9..9912f11 100644 --- a/src/common/err_try_fgets.c +++ b/src/common/err_try_fgets.c @@ -1,7 +1,8 @@ /* err_try_fgets.c */ -#include /* uint8_t, uint32_t */ +#include /* uint8_t, uint32_t, UINT8_MAX */ #include /* FILE, sprintf() */ +#include /* atoi() */ #include /* strlen(), strchr(), strcmp() */ #include "../common/readwrite.h" /* try_fgets() */ #include "../common/rexit.h" /* exit_err() */ @@ -57,6 +58,7 @@ extern void err_try_fgets(char * line, uint32_t linemax, FILE * file, char * err_int = "Expected valid positive or negative integer number."; char * err_full = "Hit non-empty line where empty line was expected."; char * err_delim = "Expected proper delimiter, found something else."; + char * err_uint8 = "Value is too large. Must be 255 or less."; char * f_name = "err_try_fgets()"; line[0] = '\0'; try_fgets(line, linemax + 1, file, f_name); @@ -83,4 +85,6 @@ extern void err_try_fgets(char * line, uint32_t linemax, FILE * file, err_line(strlen(line) < 2 && ('-' == line[i] || '+' == line[i]), line, context, err_int); } + err_line(strchr(test, '8') && atoi(line) > UINT8_MAX, line, context, + err_uint8); } diff --git a/src/common/err_try_fgets.h b/src/common/err_try_fgets.h index 8ea6368..6bb07ee 100644 --- a/src/common/err_try_fgets.h +++ b/src/common/err_try_fgets.h @@ -43,7 +43,8 @@ extern void err_line(uint8_t test, char * line, char * intro, char * msg); * 'i': check for "line" describing an integer in all its chars before end or \n * (i.e. all other chars must be digits, except the first char, which may * be '+' or '-'; a '+' or '-' without digits following is invalid) - */ + * '8': check for "line" describing an integer smaller than or equal UINT8_MAX +*/ extern void err_try_fgets(char * line, uint32_t linemax, FILE * file, char * context, char * test); diff --git a/src/server/map_object_actions.c b/src/server/map_object_actions.c index 9feab55..1c49108 100644 --- a/src/server/map_object_actions.c +++ b/src/server/map_object_actions.c @@ -2,7 +2,7 @@ #include "map_object_actions.h" #include /* NULL */ -#include /* uint8_t, uint16_t, UINT8_MAx */ +#include /* uint8_t, uint16_t */ #include /* sprintf(), ungetc() */ #include /* free(), atoi() */ #include /* strlen(), strcmp(), memcpy(), strncmp() */ @@ -235,8 +235,7 @@ extern void init_map_object_actions() char line[linemax + 1]; struct MapObjAct ** moa_ptr_ptr = &world.map_obj_acts; char * context = "Failed reading map object actions config file. "; - char * err_toolarge = "Value is too large."; - char * err_uniq = "Declaration of ID already used."; + char * err_uniq = "Declaration of ID already used."; reset_err_try_fgets_counter(); while (1) { @@ -247,16 +246,14 @@ extern void init_map_object_actions() } exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()"); struct MapObjAct * moa = try_malloc(sizeof(struct MapObjAct), f_name); - err_try_fgets(line, linemax, file, context, "nfi"); - err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge); + err_try_fgets(line, linemax, file, context, "nfi8"); moa->id = atoi(line); struct MapObjAct * moa_test = world.map_obj_acts; for (; NULL != moa_test; moa_test = moa_test->next) { err_line(moa->id == moa_test->id, line, context, err_uniq); } - err_try_fgets(line, linemax, file, context, "0nfi"); - err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge); + err_try_fgets(line, linemax, file, context, "0nfi8"); moa->effort = atoi(line); err_try_fgets(line, linemax, file, context, "0nf"); line[strlen(line) - 1] = '\0'; diff --git a/src/server/map_objects.c b/src/server/map_objects.c index f60cefe..50f372f 100644 --- a/src/server/map_objects.c +++ b/src/server/map_objects.c @@ -3,7 +3,7 @@ #include "map_objects.h" #include /* NULL */ #include /* FILE typedef */ -#include /* uint8_t, uint16_t, UINT8_MAX */ +#include /* uint8_t, uint16_t */ #include /* free(), atoi() */ #include /* strlen(), memcpy(), memset() */ #include "../common/err_try_fgets.h" /* err_try_fgets(), err_line(), @@ -105,8 +105,7 @@ extern void init_map_object_defs() { char * f_name = "init_map_object_defs()"; char * context = "Failed reading map object definitions file. "; - char * err_toolarge = "Value is too large."; - char * err_uniq = "Declaration of ID already used."; + char * err_uniq = "Declaration of ID already used."; FILE * file = try_fopen(world.path_map_obj_defs, "r", f_name); uint32_t linemax = textfile_width(file); struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs; @@ -122,28 +121,24 @@ extern void init_map_object_defs() exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()"); struct MapObjDef * mod = try_malloc(sizeof(struct MapObjDef), f_name); mod->next = NULL; - err_try_fgets(line, linemax, file, context, "nfi"); - err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge); + err_try_fgets(line, linemax, file, context, "nfi8"); mod->id = atoi(line); struct MapObjDef * mod_test = world.map_obj_defs; for (; NULL != mod_test; mod_test = mod_test->next) { err_line(mod->id == mod_test->id, line, context, err_uniq); } - err_try_fgets(line, linemax, file, context, "0nfi"); - err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge); + err_try_fgets(line, linemax, file, context, "0nfi8"); mod->corpse_id = atoi(line); err_try_fgets(line, linemax, file, context, "0nfs"); mod->char_on_map = line[0]; - err_try_fgets(line, linemax, file, context, "0nfi"); - err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge); + err_try_fgets(line, linemax, file, context, "0nfi8"); mod->lifepoints = atoi(line); err_try_fgets(line, linemax, file, context, "0nf"); line[strlen(line) - 1] = '\0'; mod->name = try_malloc(strlen(line) + 1, f_name); memcpy(mod->name, line, strlen(line) + 1); - err_try_fgets(line, linemax, file, context, "0nfi"); - err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge); + err_try_fgets(line, linemax, file, context, "0nfi8"); mod->consumable = atoi(line); * last_mod_ptr_ptr = mod; last_mod_ptr_ptr = &mod->next;