X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=src%2Fcommon%2Freadwrite.c;h=43c83e9923710cd1400ce1fc10eae90ded692df0;hb=2b2a1e0169b3a863fd87b679d789a4e2b789eb67;hp=f05c1285a9d5cdda58ee909d4814aea019318b03;hpb=1cb57a35a3b3cc4ec8870531ca254a655c0bdda2;p=plomrogue diff --git a/src/common/readwrite.c b/src/common/readwrite.c index f05c128..43c83e9 100644 --- a/src/common/readwrite.c +++ b/src/common/readwrite.c @@ -1,36 +1,24 @@ -/* 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() */ #include /* access(), unlink() */ #include "rexit.h" /* exit_err(), exit_trouble() */ #include "try_malloc.h" /* try_malloc() */ -/* Return "path" + suffix "_tmp". Value is malloc'd, must be free externally. */ -static char * build_temp_path(char * path); - - - -static char * build_temp_path(char * path) -{ - char * suffix_tmp = "_tmp"; - uint16_t size = strlen(path) + strlen(suffix_tmp) + 1; - char * path_tmp = try_malloc(size, __func__); - int test = sprintf(path_tmp, "%s%s", path, suffix_tmp); - exit_trouble(test < 0, __func__, "sprintf"); - return path_tmp; -} - - - extern FILE * try_fopen(char * path, char * mode, const char * f) { char * msg1 = "Trouble in "; @@ -43,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; } @@ -85,12 +73,24 @@ 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; } +extern char * build_temp_path(char * path) +{ + char * suffix_tmp = "_tmp"; + uint16_t size = strlen(path) + strlen(suffix_tmp) + 1; + char * path_tmp = try_malloc(size, __func__); + int test = sprintf(path_tmp, "%s%s", path, suffix_tmp); + exit_trouble(test < 0, __func__, "sprintf"); + return path_tmp; +} + + + extern FILE * atomic_write_start(char * path, char ** path_tmp) { *path_tmp = build_temp_path(path); @@ -180,3 +180,80 @@ 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, + uint32_t * queue_size) +{ + int test = try_fgetc(file, __func__); + if (EOF != test) + { + do + { + char c = (char) test; + if ('\n' == c) + { + c = '\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; +} + + + +extern char * get_message_from_queue(char ** queue, uint32_t * queue_size) +{ + char * message = NULL; + if (*queue_size) + { + 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 message; +}