interpret/src/text.c

56 lines
1.8 KiB
C
Raw Normal View History

2023-03-22 08:11:56 +00:00
/** Dynamic contiguous string that is used to load files into a
`struct char_array`. */
2022-12-27 07:31:08 +00:00
#include "text.h"
#include <stdio.h>
2023-05-07 05:48:13 +00:00
#include <limits.h>
2023-05-07 06:00:49 +00:00
#include <stdint.h>
2022-12-27 07:31:08 +00:00
#define ARRAY_NAME char
#define ARRAY_TYPE char
#define ARRAY_BODY
2022-12-27 07:31:08 +00:00
#include "../src/array.h"
2023-03-22 08:11:56 +00:00
/** @return Idle. */
struct char_array text(void) { return char_array(); }
2023-03-22 08:11:56 +00:00
/** Destroys `text`. */
void text_(struct char_array *const text) { char_array_(text); }
2022-12-27 07:31:08 +00:00
2023-03-22 08:11:56 +00:00
/** Append a text file, `fn`, to `text`, and add a '\0'.
2022-12-27 07:31:08 +00:00
@return The start of the appended file or null on error. A partial read is a
2023-03-22 08:11:56 +00:00
failure. @throws[fopen, fread, realloc]
@throws[EISEQ] The text file has embedded nulls. */
char *text_append_file(struct char_array *text, const char *const fn) {
2022-12-27 07:31:08 +00:00
FILE *fp = 0;
const size_t granularity = 1024;
size_t nread, start;
char *cursor;
int success = 1;
assert(text && fn);
start = text->size;
if(!(fp = fopen(fn, "r"))) goto catch;
2023-05-07 05:48:13 +00:00
/* Read entire file in chunks. We don't allow any text file to go over
2023-05-07 06:00:49 +00:00
`INT_MAX` and `UINT32_MAX` because `printf("%.*s", int, char *)` is used
and we store it in an `uint32_t`. */
2022-12-27 07:31:08 +00:00
do if(!(cursor = char_array_buffer(text, granularity))
|| (nread = fread(cursor, 1, granularity, fp), ferror(fp))
2023-05-07 05:48:13 +00:00
|| !char_array_append(text, nread)
2023-05-07 06:00:49 +00:00
|| text->size - start >= (INT_MAX < UINT32_MAX ? INT_MAX : UINT32_MAX))
goto catch;
2022-12-27 07:31:08 +00:00
while(nread == granularity);
/* File to `C` string. */
if(!(cursor = char_array_new(text))) goto catch;
*cursor = '\0';
/* Binary files with embedded '\0' are not allowed; check just this read. */
if(strchr(text->data + start, '\0') != cursor)
{ errno = EILSEQ; goto catch; }
goto finally;
catch:
if(!errno) errno = EILSEQ; /* Will never be true on POSIX. */
success = 0;
finally:
if(fp) fclose(fp);
return success ? text->data + start : 0;
}