From: Christian Heller Date: Fri, 14 Nov 2014 06:02:36 +0000 (+0100) Subject: Simplify queue reading/writing. X-Git-Tag: tce~599 X-Git-Url: https://plomlompom.com/repos/%27%29;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20chunks.push%28escapeHTML%28span%5B2%5D%29%29;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20chunks.push%28%27?a=commitdiff_plain;h=eec39e9d6f991c90c8555859b5f10eb30c0370c1;p=plomrogue Simplify queue reading/writing. --- diff --git a/src/client/io.c b/src/client/io.c index 299df7f..69a0865 100644 --- a/src/client/io.c +++ b/src/client/io.c @@ -231,8 +231,7 @@ static void ping_pong_test(time_t last_server_answer_time) static void try_growing_queue(time_t * last_server_answer_time) { - if (read_file_into_queue(world.file_server_out, &world.queue, - &world.queue_size)) + if (read_file_into_queue(world.file_server_out, &world.queue)) { * last_server_answer_time = time(0); } @@ -244,7 +243,7 @@ static uint8_t read_outfile() { uint8_t ret = 0; char * msg; - while (NULL != (msg=get_message_from_queue(&world.queue,&world.queue_size))) + while (NULL != (msg = get_message_from_queue(&world.queue))) { char * log_prefix = "LOG "; if (!strncmp(msg, log_prefix, strlen(log_prefix))) diff --git a/src/client/world.h b/src/client/world.h index 047a13d..745100a 100644 --- a/src/client/world.h +++ b/src/client/world.h @@ -38,7 +38,6 @@ struct World char * player_inventory; /* one-item-per-line string list of owned items */ char * mem_map; /* map cells of player's map memory */ char * queue; /* Stores un-processed messages read from the input file. */ - uint32_t queue_size;/* Length of .queue sequence of \0-terminated strings.*/ struct yx_uint8 player_pos; /* coordinates of player on map */ uint16_t turn; /* world/game turn */ uint8_t halfdelay; /* how long to wait for getch() input in io_loop() */ diff --git a/src/common/readwrite.c b/src/common/readwrite.c index 77d83d9..94150f1 100644 --- a/src/common/readwrite.c +++ b/src/common/readwrite.c @@ -12,7 +12,7 @@ * fputc(), fwrite(), fclose(), fopen(), clearerr() */ #include /* free() */ -#include /* strlen(), memcpy() */ +#include /* strlen(), memcpy(), strchr() */ #include /* access(), unlink() */ #include "rexit.h" /* exit_err(), exit_trouble() */ #include "try_malloc.h" /* try_malloc() */ @@ -183,80 +183,54 @@ extern uint32_t textfile_width(FILE * file) -extern uint8_t read_file_into_queue(FILE * file, char ** queue, - uint32_t * queue_size) +extern uint8_t read_file_into_queue(FILE * file, char ** queue) { - int test = try_fgetc(file, __func__); - if (EOF != test) + uint8_t ret = 0; + int test; + while (EOF != (test = try_fgetc(file, __func__))) { - char * err_size = "Queue growing too large."; - do + ret = 1; + if ('\0' != test) { - char c = (char) test; - if ('\n' == c) + if (*queue) { - c = '\0'; + char * new_queue = try_malloc(strlen(*queue) + 1 + 1, __func__); + memcpy(new_queue, *queue, strlen(*queue)); + new_queue[strlen(*queue)] = (char) test; + new_queue[strlen(*queue) + 1] = '\0'; + free(*queue); + *queue = new_queue; + } + else + { + *queue = try_malloc(1 + 1, __func__); + (*queue)[0] = (char) test; + (*queue)[1] = '\0'; } - char * new_queue = try_malloc(*queue_size + 1, __func__); - memcpy(new_queue, *queue, *queue_size); - char * new_pos = new_queue + *queue_size; - * new_pos = c; - exit_err(*queue_size == UINT32_MAX, err_size); - *queue_size = *queue_size + 1; - free(*queue); - *queue = new_queue; - } - while (EOF != (test = try_fgetc(file, __func__))); - if (*queue_size && '\0' != (*queue)[*queue_size - 1]) - { - char * new_queue = try_malloc(*queue_size + 1, __func__); - memcpy(new_queue, *queue, *queue_size); - new_queue[*queue_size] = '\0'; - exit_err(*queue_size == UINT32_MAX, err_size); - *queue_size = *queue_size + 1; - free(*queue); - *queue = new_queue; } - return 1; } - return 0; + return ret; } -extern char * get_message_from_queue(char ** queue, uint32_t * queue_size) +extern char * get_message_from_queue(char ** queue) { - char * message = NULL; - if (*queue_size) + if (!(*queue)) { - size_t cutout_len = strlen(*queue); - uint8_t is_nullbyte_chunk = !cutout_len; - if (0 < cutout_len) - { - cutout_len++; - message = try_malloc(cutout_len, __func__); - memcpy(message, *queue, cutout_len); - } - for (; - cutout_len != *queue_size && '\0' == (*queue)[cutout_len]; - cutout_len++); - *queue_size = *queue_size - cutout_len; - if (0 == *queue_size) - { - free(*queue); /* NULL so read_file_into_queue() and */ - *queue = NULL; /* cleanup() may free() this every time, */ - } /* even when it's un-allocated. */ - else - { - char * new_queue = try_malloc(*queue_size, __func__); - memcpy(new_queue, &((*queue)[cutout_len]), *queue_size); - free(*queue); - *queue = new_queue; - if (is_nullbyte_chunk) - { - return get_message_from_queue(queue, queue_size); - } - } + return NULL; + } + char * first_nl = strchr(*queue, '\n'); + if (!first_nl) + { + return NULL; } - return message; + char * msg = try_malloc(first_nl - (*queue) + 1, __func__); + memcpy(msg, *queue, first_nl - (*queue)); + msg[first_nl - (*queue)] = '\0'; + char * new_queue = try_malloc(strlen(first_nl + 1) + 1, __func__); + memcpy(new_queue, first_nl + 1, strlen(first_nl + 1) + 1); + free(*queue); + *queue = new_queue; + return msg; } diff --git a/src/common/readwrite.h b/src/common/readwrite.h index c151f13..ce7b749 100644 --- a/src/common/readwrite.h +++ b/src/common/readwrite.h @@ -53,20 +53,13 @@ extern void detect_atomic_leftover(char * path); /* Return largest line length from "file" (including newline chars). */ extern uint32_t textfile_width(FILE * file); -/* Read "file" for load of bytes to put onto "queue" of "queue_size" (growing - * that size). May put many \0-terminated strings on the queue at once. \n chars - * are replaced with \0 chars. If the queue doesn't end in \0, a \o byte is - * appended to it. Returns 1 if reading succeeds, 0 if nothing is read. - */ -extern uint8_t read_file_into_queue(FILE * file, char ** queue, - uint32_t * queue_size); +/* Read "file" for load of non-zero bytes to put onto "queue" string. */ +extern uint8_t read_file_into_queue(FILE * file, char ** queue); -/* Cut out and return first \0-terminated string from "queue" and appropriately - * reduce "queue_size". Return NULL if queue is empty. Superfluous \0 bytes - * after the string are also cut out. Should the queue start with \0 bytes, - * those are cut out before returning anything after them. +/* Return message from "queue" (identified as \n-delimited string, with \n as + * \0 in the returned message; return nothing if no \n delimiter found). */ -extern char * get_message_from_queue(char ** queue, uint32_t * queue_size); +extern char * get_message_from_queue(char ** queue); diff --git a/src/server/io.c b/src/server/io.c index 0cf2c05..d4c9faf 100644 --- a/src/server/io.c +++ b/src/server/io.c @@ -190,7 +190,7 @@ static void try_growing_queue() dur.tv_nsec = 33333333; while (1) { - if (read_file_into_queue(world.file_in, &world.queue,&world.queue_size)) + if (read_file_into_queue(world.file_in, &world.queue)) { return; } @@ -349,9 +349,9 @@ static void write_map(struct Thing * player, FILE * file) extern char * io_round() { - if (0 < world.queue_size) + if (world.queue && strlen(world.queue)) { - return get_message_from_queue(&world.queue, &world.queue_size); + return get_message_from_queue(&world.queue); } if (world.do_update) { @@ -359,7 +359,7 @@ extern char * io_round() world.do_update = 0; } try_growing_queue(); - return get_message_from_queue(&world.queue, &world.queue_size); + return get_message_from_queue(&world.queue); } diff --git a/src/server/world.h b/src/server/world.h index eedb2e3..f6deb10 100644 --- a/src/server/world.h +++ b/src/server/world.h @@ -29,7 +29,6 @@ struct World struct Thing * things; /* All physical things of the game world. */ char * server_test; /* String uniquely identifying server process. */ char * queue; /* Stores un-processed messages read from the input file. */ - uint32_t queue_size;/* Length of .queue sequence of \0-terminated strings.*/ uint32_t seed; /* Randomness seed. */ uint32_t seed_map; /* Map seed. */ uint16_t replay; /* Turn up to which to replay game. No replay if zero. */