* fputc(), fwrite(), fclose(), fopen(), clearerr()
*/
#include <stdlib.h> /* free() */
-#include <string.h> /* strlen(), memcpy() */
+#include <string.h> /* strlen(), memcpy(), strchr() */
#include <unistd.h> /* access(), unlink() */
#include "rexit.h" /* exit_err(), exit_trouble() */
#include "try_malloc.h" /* try_malloc() */
-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;
}
/* 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);