X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=src%2Fcommon%2Freadwrite.c;h=94150f17b515b723fe70d43adfafae8be3455775;hb=d88ddfea1633a4d5220afa8b5ceefdf924ee06ce;hp=43c83e9923710cd1400ce1fc10eae90ded692df0;hpb=2b2a1e0169b3a863fd87b679d789a4e2b789eb67;p=plomrogue
diff --git a/src/common/readwrite.c b/src/common/readwrite.c
index 43c83e9..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,77 +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__)))
{
- 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;
- *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';
- *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;
}