diff --git a/v0/buffer.c b/v0/buffer.c deleted file mode 100644 index f7ccf23..0000000 --- a/v0/buffer.c +++ /dev/null @@ -1,63 +0,0 @@ -#include "buffer.h" - -#include -#include - -typedef enum { - BUFFER_FILE, - BUFFER_STRING -} BufferType; - -struct Buffer { - BufferType type; - union { - FILE* file; - struct { - const char* data; - size_t pos; - } string; - } source; -}; - -Buffer* buffer_open_file(const char* path) { - FILE* f = fopen(path, "r"); - if (f == NULL) - return NULL; - Buffer* buf = malloc(sizeof(Buffer)); - if (buf == NULL) { - fclose(f); - return NULL; - } - buf->type = BUFFER_FILE; - buf->source.file = f; - return buf; -} - -Buffer* buffer_open_string(const char* string) { - Buffer* buf = malloc(sizeof(Buffer)); - if (buf == NULL) - return NULL; - buf->type = BUFFER_STRING; - buf->source.string.data = string; - buf->source.string.pos = 0; - return buf; -} - -void buffer_close(Buffer* buffer) { - if (buffer->type == BUFFER_FILE) - fclose(buffer->source.file); - free(buffer); -} - -char buffer_read(Buffer* buffer) { - if (buffer->type == BUFFER_FILE) { - int c = fgetc(buffer->source.file); - return c == EOF ? (char)-1 : (char)c; - } else { - char c = buffer->source.string.data[buffer->source.string.pos]; - if (c == '\0') - return (char)-1; - buffer->source.string.pos++; - return c; - } -} diff --git a/v0/buffer.h b/v0/buffer.h deleted file mode 100644 index 9b5c8e3..0000000 --- a/v0/buffer.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * An interface that wraps files and strings. - * Allows stream–like reading from it. - */ -#ifndef BUFFER_H -#define BUFFER_H - -/** - * An interface to a source of textual data. - */ -typedef struct Buffer Buffer; - -/** - * Opens a file. - * - * @param path The path to the file. - * @returns The newly–opened buffer. - */ -Buffer* buffer_open_file(const char* path); - -/** - * Opens a string. - * - * The string is not copied, and must not be free'd until the - * buffer itself has been closed. - * - * @param string The contents stored in the buffer. - * @returns A newly–opened buffer that reads from the string. - */ -Buffer* buffer_open_string(const char* string); - -/** - * Closes the buffer. - * - * @param buffer The buffer to close. - */ -void buffer_close(Buffer* buffer); - -/** - * Reads a single character from the buffer. - * - * If there are no more characters in the buffer, - * this returns `-1`. - * - * @param buffer The buffer to read from. - * @returns the next character in the buffer. - */ -char buffer_read(Buffer* buffer); - -#endif