diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/linebuffer.c | 139 | ||||
| -rw-r--r-- | src/linebuffer.h | 75 | ||||
| -rw-r--r-- | src/main.c | 49 | ||||
| -rw-r--r-- | src/strlcat.h | 15 | ||||
| -rw-r--r-- | src/word.h | 28 |
5 files changed, 120 insertions, 186 deletions
diff --git a/src/linebuffer.c b/src/linebuffer.c index 6dfb6c3..772ba77 100644 --- a/src/linebuffer.c +++ b/src/linebuffer.c @@ -1,89 +1,110 @@ #include "linebuffer.h" - +#include "strlcat.h" #include <ctype.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "strlcat.h" +#define NULL_TERMINATOR_LEN 1 LineBuffer *linebuffer_create(size_t buffer_capacity) { - LineBuffer *lb = malloc(sizeof(LineBuffer)); - if (lb == NULL) { - return NULL; - } - lb->_buffer = malloc(buffer_capacity + 1); - if (lb->_buffer == NULL) { - free(lb); - return NULL; - } - memset(lb->_buffer, 0, buffer_capacity + 1); - lb->_num_words = 0; - lb->_buffer_length = 0; - lb->_buffer_capacity = buffer_capacity; - return lb; + LineBuffer *lb = malloc(sizeof(LineBuffer)); + if (lb == NULL) { + return NULL; + } + lb->_buffer = malloc(buffer_capacity + NULL_TERMINATOR_LEN); + if (lb->_buffer == NULL) { + free(lb); + return NULL; + } + memset(lb->_buffer, 0, buffer_capacity + NULL_TERMINATOR_LEN); + lb->_num_words = 0; + lb->_buffer_length = 0; + lb->_buffer_capacity = buffer_capacity; + return lb; } void linebuffer_destroy(LineBuffer *lb) { - free(lb->_buffer); - free(lb); + free(lb->_buffer); + free(lb); } void linebuffer_clear(LineBuffer *lb) { - memset(lb->_buffer, 0, lb->_buffer_capacity + 1); - lb->_num_words = 0; - lb->_buffer_length = 0; + memset(lb->_buffer, 0, lb->_buffer_capacity + NULL_TERMINATOR_LEN); + lb->_num_words = 0; + lb->_buffer_length = 0; } -void linebuffer_append_word(LineBuffer *lb, const char *word) { - if (lb->_num_words > 0) { - lb->_buffer[lb->_buffer_length] = ' '; - lb->_buffer[lb->_buffer_length + 1] = '\0'; - lb->_buffer_length++; - } - strlcat(lb->_buffer, word, (lb->_buffer_capacity + 1) - lb->_buffer_length); - lb->_buffer_length += strlen(word); - lb->_num_words++; +bool linebuffer_append_word(LineBuffer *lb, const char *word) { + if (!linebuffer_can_fit(lb, word)) { + return false; + } + if (lb->_num_words > 0) { + lb->_buffer[lb->_buffer_length] = ' '; + lb->_buffer_length++; + } + strlcat(lb->_buffer, word, lb->_buffer_capacity + NULL_TERMINATOR_LEN); + lb->_buffer_length += strlen(word); + lb->_num_words++; + return true; +} + +bool linebuffer_can_fit(LineBuffer *lb, const char *word) { + size_t append_len = strlen(word); + if (lb->_num_words > 0) { + // Account for space before the word + append_len++; + } + return append_len <= linebuffer_space_remaining(lb); } size_t linebuffer_space_remaining(LineBuffer *lb) { - return lb->_buffer_capacity - lb->_buffer_length; + return lb->_buffer_capacity - lb->_buffer_length; } int linebuffer_write(LineBuffer *lb, FILE *fp) { - if (lb->_buffer_length > 0) { - return fputs(lb->_buffer, fp); + int return_code = 0; + if (lb->_buffer_length > 0) { + return_code = fputs(lb->_buffer, fp); + if (return_code == EOF) { + return EOF; } - return 0; + return_code = fputc('\n', fp); + if (return_code == EOF) { + return EOF; + } + } + return return_code; } int linebuffer_write_justified(LineBuffer *lb, FILE *fp) { - int return_code = 0; - size_t words_remaining = lb->_num_words; - size_t extra_spaces = linebuffer_space_remaining(lb); - size_t spaces_to_insert = 0; + int return_code = 0; + size_t words_remaining = lb->_num_words; + size_t extra_spaces = linebuffer_space_remaining(lb); + size_t spaces_to_insert = 0; - for (size_t i = 0; i < lb->_buffer_length; i++) { - if (!isspace(lb->_buffer[i])) { - return_code = fputc(lb->_buffer[i], fp); - if (return_code == EOF) { - return EOF; - } - } else { - spaces_to_insert = extra_spaces / (words_remaining - 1); - extra_spaces -= spaces_to_insert; - for (size_t j = 1; j <= spaces_to_insert + 1; j++) { - return_code = fputc(' ', fp); - if (return_code == EOF) { - return EOF; - } - } - words_remaining--; - } - } - return_code = fputc('\n', fp); - if (return_code == EOF) { + for (size_t i = 0; i < lb->_buffer_length; i++) { + if (!isspace(lb->_buffer[i])) { + return_code = fputc(lb->_buffer[i], fp); + if (return_code == EOF) { return EOF; + } + } else { + spaces_to_insert = extra_spaces / (words_remaining - 1); + extra_spaces -= spaces_to_insert; + for (size_t j = 1; j <= spaces_to_insert + 1; j++) { + return_code = fputc(' ', fp); + if (return_code == EOF) { + return EOF; + } + } + words_remaining--; } - return return_code; + } + return_code = fputc('\n', fp); + if (return_code == EOF) { + return EOF; + } + return return_code; } diff --git a/src/linebuffer.h b/src/linebuffer.h deleted file mode 100644 index 173bad3..0000000 --- a/src/linebuffer.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef LINEBUFFER_H -#define LINEBUFFER_H - -#include <stddef.h> -#include <stdio.h> - -typedef struct { - size_t _num_words; - size_t _buffer_length; - size_t _buffer_capacity; - char *_buffer; -} LineBuffer; - -/** - * @brief Create a new LineBuffer - * - * @param line_length The length of the buffer to use. - * - * @returns The newly allocated LineBuffer; otherwise NULL - * if the allocation fails. - */ -LineBuffer *linebuffer_create(size_t line_length); - -/** - * @brief Release any allocated memory for the LineBuffer - * - * @param lb The LineBuffer to destroy. - */ -void linebuffer_destroy(LineBuffer *lb); - -/** - * @brief Clear the current buffer - * - * @param lb The LineBuffer to clear. - */ -void linebuffer_clear(LineBuffer *lb); - -/** - * @brief Append a word to the end of the buffer. - * - * @param lb The line buffer to append to. - * @param word The word to append. - */ -void linebuffer_append_word(LineBuffer *lb, const char *word); - -/** - * @brief Check how many characters can be added. - * - * @param lb The line buffer to check the space of. - * - * @returns The number of characters left in the line. - */ -size_t linebuffer_space_remaining(LineBuffer *lb); - -/** - * @brief Write the current line. - * - * @param lb The line buffer to read. - * @param fp The file to write out the line buffer to. - * - * @returns >= 0 on success; EOF on error. - */ -int linebuffer_write(LineBuffer *lb, FILE *fp); - -/** - * @brief Write the current line as justified text. - * - * @param lb The line buffer to read. - * @param fp The file to write out the line buffer to. - * - * @returns >= 0 on success; EOF on error. - */ -int linebuffer_write_justified(LineBuffer *lb, FILE *fp); - -#endif @@ -1,18 +1,43 @@ #include <stdbool.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> +#include <getopt.h> #include "linebuffer.h" #include "word.h" -#define MAX_WORD_LENGTH 20 -#define LINEBUFFER_CAPACITY 60 +#define DEFAULT_LINEBUFFER_CAPACITY 72 -int main(void) { +void print_usage(const char *prog) { + fprintf(stderr, "Usage: %s [-w WIDTH]\n", prog); +} + +int main(int argc, char *argv[]) { + int opt; + size_t linebuffer_capacity = DEFAULT_LINEBUFFER_CAPACITY; + + while ((opt = getopt(argc, argv, "w:h")) != -1) { + switch (opt) { + case 'w': + linebuffer_capacity = (size_t)atoi(optarg); + if (linebuffer_capacity == 0) { + print_usage(argv[0]); + return EXIT_FAILURE; + } + break; + case 'h': + default: + print_usage(argv[0]); + return EXIT_FAILURE; + } + } + + const size_t MAX_WORD_LENGTH = linebuffer_capacity; char word[MAX_WORD_LENGTH + 2]; size_t word_length; int read_word_result; - LineBuffer *lb = linebuffer_create(LINEBUFFER_CAPACITY); + LineBuffer *lb = linebuffer_create(linebuffer_capacity); while ((read_word_result = read_word(stdin, word, MAX_WORD_LENGTH + 1)) == READ_WORD_SUCCESS) { @@ -20,21 +45,27 @@ int main(void) { if (word_length == 0) { linebuffer_write(lb, stdout); linebuffer_destroy(lb); - return 0; + return EXIT_SUCCESS; } if (word_length > MAX_WORD_LENGTH) { + // TODO: If the word is too long for a given line, + // we should wrap it to the next line. word[MAX_WORD_LENGTH] = '*'; } - if (word_length >= linebuffer_space_remaining(lb)) { + if (!linebuffer_append_word(lb, word)) { linebuffer_write_justified(lb, stdout); linebuffer_clear(lb); + linebuffer_append_word(lb, word); } - linebuffer_append_word(lb, word); memset(word, 0, MAX_WORD_LENGTH + 2); } if (read_word_result == READ_WORD_ERR_READ) { fprintf(stderr, "Error reading word\n"); - return 1; + return EXIT_FAILURE; + } + if (lb->_num_words > 0) { + linebuffer_write(lb, stdout); } - return 0; + linebuffer_destroy(lb); + return EXIT_SUCCESS; } diff --git a/src/strlcat.h b/src/strlcat.h deleted file mode 100644 index 0470b83..0000000 --- a/src/strlcat.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef HAVE_STRLCAT -#define HAVE_STRLCAT - -#include <stddef.h> - -/* - * Appends src to string dst of size siz (unlike strncat, siz is the - * full size of dst, not space left). At most siz-1 characters - * will be copied. Always NUL terminates (unless siz <= strlen(dst)). - * Returns strlen(src) + MIN(siz, strlen(initial dst)). - * If retval >= siz, truncation occurred. - */ -size_t strlcat(char *dst, const char *src, size_t dstsize); - -#endif diff --git a/src/word.h b/src/word.h deleted file mode 100644 index b5d94b5..0000000 --- a/src/word.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef WORD_H -#define WORD_H - -#include <stdio.h> - -#define READ_WORD_ERR_READ -1 -#define READ_WORD_ERR_EOF 0 -#define READ_WORD_SUCCESS 1 - -/** - * Read in a word from a file up to size length. - * - * If there is any leading whitespace, it will be discarded - * before starting to read in a word. - * - * @param[in] fp The file to read a word from. - * @param[out] word The word that was read as a null-terminated string. - * @param[in] length The length of the word to read in. - * - * @note `length` should be the number of characters you'd - * like to read in plus one to take into account the - * null terminator. - * - * @returns < 0 on read error; 0 on EOF; > 0 on success. - */ -int read_word(FILE *fp, char *word, size_t length); - -#endif |