diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/linebuffer.c | 50 | ||||
| -rw-r--r-- | src/linebuffer.h | 2 | ||||
| -rw-r--r-- | src/main.c | 41 | ||||
| -rw-r--r-- | src/word.c | 13 |
4 files changed, 51 insertions, 55 deletions
diff --git a/src/linebuffer.c b/src/linebuffer.c index 1eb4390..7d00dc6 100644 --- a/src/linebuffer.c +++ b/src/linebuffer.c @@ -1,17 +1,16 @@ +#include "linebuffer.h" #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "linebuffer.h" - LineBuffer *linebuffer_create(size_t buffer_capacity) { LineBuffer *lb = malloc(sizeof(LineBuffer)); if (lb == NULL) { - return NULL; + return NULL; } lb->_buffer = malloc(sizeof(buffer_capacity) + 1); if (lb->_buffer == NULL) { - return NULL; + return NULL; } lb->_num_words = 0; lb->_buffer_length = 0; @@ -31,9 +30,9 @@ void linebuffer_clear(LineBuffer *lb) { 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++; + lb->_buffer[lb->_buffer_length] = ' '; + lb->_buffer[lb->_buffer_length + 1] = '\0'; + lb->_buffer_length++; } strcat(lb->_buffer, word); lb->_buffer_length += strlen(word); @@ -46,7 +45,7 @@ size_t linebuffer_space_remaining(LineBuffer *lb) { int linebuffer_write(LineBuffer *lb, FILE *fp) { if (lb->_buffer_length > 0) { - return fputs(lb->_buffer, fp); + return fputs(lb->_buffer, fp); } return 0; } @@ -58,27 +57,26 @@ int linebuffer_write_justified(LineBuffer *lb, FILE *fp) { size_t spaces_to_insert = 0; for (size_t i = 0; i < lb->_buffer_length; i++) { - if (lb->_buffer[i] != ' ') { - return_code = fputs(&lb->_buffer[i], fp); - if (return_code == EOF) { - return EOF; - } - } else { - spaces_to_insert = extra_spaces / (words_remaining - 1); - for (size_t j = 1; j <= spaces_to_insert + 1; j++) { - return_code = fputc(' ', fp); - if (return_code == EOF) { - return EOF; - } - extra_spaces -= spaces_to_insert; - words_remaining--; - } - } + if (lb->_buffer[i] != ' ') { + return_code = fputs(&lb->_buffer[i], fp); + if (return_code == EOF) { + return EOF; + } + } else { + spaces_to_insert = extra_spaces / (words_remaining - 1); + for (size_t j = 1; j <= spaces_to_insert + 1; j++) { + return_code = fputc(' ', fp); + if (return_code == EOF) { + return EOF; + } + extra_spaces -= spaces_to_insert; + words_remaining--; + } + } } return_code = fputc('\n', fp); if (return_code == EOF) { - return EOF; + return EOF; } return return_code; } - diff --git a/src/linebuffer.h b/src/linebuffer.h index b6f6e7e..00135f6 100644 --- a/src/linebuffer.h +++ b/src/linebuffer.h @@ -1,8 +1,8 @@ #ifndef LINEBUFFER_H #define LINEBUFFER_H -#include <stdio.h> #include <stddef.h> +#include <stdio.h> typedef struct { size_t _num_words; @@ -1,8 +1,8 @@ +#include "linebuffer.h" +#include "word.h" #include <stdbool.h> #include <stdio.h> #include <string.h> -#include "linebuffer.h" -#include "word.h" #define MAX_WORD_LENGTH 20 @@ -13,24 +13,23 @@ int main(void) { LineBuffer *lb = linebuffer_create(40); while (true) { - err = read_word(stdin, word, MAX_WORD_LENGTH + 1); - if (err == EOF) { - printf("Error reading word\n"); - return 1; - } - word_length = strlen(word); - if (word_length == 0) { - linebuffer_write(lb, stdout); - return 0; - } - if (word_length > MAX_WORD_LENGTH) { - word[MAX_WORD_LENGTH] = '*'; - } - if (word_length + 1 > linebuffer_space_remaining(lb)) { - linebuffer_write_justified(lb, stdout); - linebuffer_clear(lb); - } - linebuffer_append_word(lb, word); + err = read_word(stdin, word, MAX_WORD_LENGTH + 1); + if (err == EOF) { + printf("Error reading word\n"); + return 1; + } + word_length = strlen(word); + if (word_length == 0) { + linebuffer_write(lb, stdout); + return 0; + } + if (word_length > MAX_WORD_LENGTH) { + word[MAX_WORD_LENGTH] = '*'; + } + if (word_length + 1 > linebuffer_space_remaining(lb)) { + linebuffer_write_justified(lb, stdout); + linebuffer_clear(lb); + } + linebuffer_append_word(lb, word); } } - @@ -7,13 +7,13 @@ int read_word(FILE *fp, char *word, size_t length) { size_t position = 0; while ((ch = read_char(fp)) == ' ') - ; + ; while (ch != ' ' && ch != EOF) { - if (position < length) { - word[position++] = ch; - } - ch = read_char(fp); + if (position < length) { + word[position++] = ch; + } + ch = read_char(fp); } word[position] = '\0'; return ch; @@ -22,8 +22,7 @@ int read_word(FILE *fp, char *word, size_t length) { static inline int read_char(FILE *fp) { int ch = getc(fp); if (ch == '\n' || ch == '\t') { - return ' '; + return ' '; } return ch; } - |