X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=src%2Fcommon%2Freadwrite.c;h=94150f17b515b723fe70d43adfafae8be3455775;hb=a5b35a816a4a52235aa5c56386193ebdf64619db;hp=ac7b56acad990ac6763d0022fd4e0c37cd99c34a;hpb=c31d3a8d3f993a2cfcc8c5e438283e3a693d5b27;p=plomrogue
diff --git a/src/common/readwrite.c b/src/common/readwrite.c
index ac7b56a..94150f1 100644
--- a/src/common/readwrite.c
+++ b/src/common/readwrite.c
@@ -1,13 +1,18 @@
-/* src/common/readwrite.c */
+/* src/common/readwrite.c
+ *
+ * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
+ * or any later version. For details on its copyright, license, and warranties,
+ * see the file NOTICE in the root directory of the PlomRogue source package.
+ */
#include "readwrite.h"
-#include /* size_t */
+#include /* NULL, size_t */
#include /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
#include /* FILE, fseek(), sprintf(), fgets(), fgetc(), ferror(),
* fputc(), fwrite(), fclose(), fopen(), clearerr()
*/
#include /* free() */
-#include /* strlen() */
+#include /* strlen(), memcpy(), strchr() */
#include /* access(), unlink() */
#include "rexit.h" /* exit_err(), exit_trouble() */
#include "try_malloc.h" /* try_malloc() */
@@ -26,7 +31,7 @@ extern FILE * try_fopen(char * path, char * mode, const char * f)
int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,mode,msg3,path,msg4);
exit_trouble(test < 0, __func__, "sprintf");
FILE * file_p = fopen(path, mode);
- exit_err(NULL == file_p, msg);
+ exit_err(!file_p, msg);
free(msg);
return file_p;
}
@@ -68,7 +73,7 @@ extern int try_fgetc(FILE * file, const char * f)
extern char * try_fgets(char * line, int linemax, FILE * file, const char * f)
{
char * test = fgets(line, linemax, file);
- exit_trouble(NULL == test && ferror(file), f, "fgets");
+ exit_trouble(!test && ferror(file), f, "fgets");
return test;
}
@@ -175,3 +180,57 @@ extern uint32_t textfile_width(FILE * file)
exit_trouble(-1 == fseek(file, 0, SEEK_SET), __func__, "fseek");
return linemax;
}
+
+
+
+extern uint8_t read_file_into_queue(FILE * file, char ** queue)
+{
+ uint8_t ret = 0;
+ int test;
+ while (EOF != (test = try_fgetc(file, __func__)))
+ {
+ ret = 1;
+ if ('\0' != test)
+ {
+ if (*queue)
+ {
+ 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';
+ }
+ }
+ }
+ return ret;
+}
+
+
+
+extern char * get_message_from_queue(char ** queue)
+{
+ if (!(*queue))
+ {
+ return NULL;
+ }
+ char * first_nl = strchr(*queue, '\n');
+ if (!first_nl)
+ {
+ return NULL;
+ }
+ 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;
+}