aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 9d8e5b45fe32bb95ddd00f60e4e23c7f5a2b6a99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "linebuffer.h"
#include "word.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#define MAX_WORD_LENGTH 20

int main(void) {
    char word[MAX_WORD_LENGTH + 2];
    size_t word_length;
    int err;
    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);
    }
}