home · contact · privacy
Server: Remove log_help(), this should be serverd by the client.
[plomrogue] / src / common / readwrite.c
index a206c9c2f2e7eb61ca215d1392ab015b580d7f3b..94150f17b515b723fe70d43adfafae8be3455775 100644 (file)
@@ -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 <stddef.h> /* size_t */
+#include <stddef.h> /* NULL, size_t */
 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
 #include <stdio.h> /* FILE, fseek(), sprintf(), fgets(), fgetc(), ferror(),
                     * fputc(), fwrite(), fclose(), fopen(), clearerr()
                     */
 #include <stdlib.h> /* free() */
-#include <string.h> /* strlen() */
+#include <string.h> /* strlen(), memcpy(), strchr() */
 #include <unistd.h> /* access(), unlink() */
 #include "rexit.h" /* exit_err(), exit_trouble() */
 #include "try_malloc.h" /* try_malloc() */
@@ -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;
+}