aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/linebuffer.c84
-rw-r--r--src/linebuffer.h86
-rw-r--r--src/main.c36
-rw-r--r--src/word.c29
-rw-r--r--src/word.h8
5 files changed, 243 insertions, 0 deletions
diff --git a/src/linebuffer.c b/src/linebuffer.c
new file mode 100644
index 0000000..1eb4390
--- /dev/null
+++ b/src/linebuffer.c
@@ -0,0 +1,84 @@
+#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;
+ }
+ lb->_buffer = malloc(sizeof(buffer_capacity) + 1);
+ if (lb->_buffer == NULL) {
+ return NULL;
+ }
+ 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);
+}
+
+void linebuffer_clear(LineBuffer *lb) {
+ 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++;
+ }
+ strcat(lb->_buffer, word);
+ lb->_buffer_length += strlen(word);
+ lb->_num_words++;
+}
+
+size_t linebuffer_space_remaining(LineBuffer *lb) {
+ return lb->_buffer_capacity - lb->_buffer_length;
+}
+
+int linebuffer_write(LineBuffer *lb, FILE *fp) {
+ if (lb->_buffer_length > 0) {
+ return fputs(lb->_buffer, fp);
+ }
+ return 0;
+}
+
+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;
+
+ 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--;
+ }
+ }
+ }
+ return_code = fputc('\n', fp);
+ if (return_code == EOF) {
+ return EOF;
+ }
+ return return_code;
+}
+
diff --git a/src/linebuffer.h b/src/linebuffer.h
new file mode 100644
index 0000000..b6f6e7e
--- /dev/null
+++ b/src/linebuffer.h
@@ -0,0 +1,86 @@
+#ifndef LINEBUFFER_H
+#define LINEBUFFER_H
+
+#include <stdio.h>
+#include <stddef.h>
+
+typedef struct {
+ size_t _num_words;
+ size_t _buffer_length;
+ size_t _buffer_capacity;
+ char *_buffer;
+} LineBuffer;
+
+/**
+ * Create a new LineBuffer
+ *
+ * Args:
+ * 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);
+
+/**
+ * Release any allocated memory for the LineBuffer
+ *
+ * Args:
+ * lb: The LineBuffer to destroy.
+ */
+void linebuffer_destroy(LineBuffer *lb);
+
+/**
+ * Clear the current buffer
+ *
+ * Args:
+ * lb: The LineBuffer to clear.
+ */
+void linebuffer_clear(LineBuffer *lb);
+
+/**
+ * Append a word to the end of the buffer.
+ *
+ * Args:
+ * lb: The line buffer to append to.
+ * word: The word to append.
+ */
+void linebuffer_append_word(LineBuffer *lb, const char *word);
+
+/**
+ * Check how many characters can be added.
+ *
+ * Args:
+ * 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);
+
+/**
+ * Write the current line.
+ *
+ * Args:
+ * lb: The line buffer to read.
+ * fp: The file to write out the line buffer to.
+ *
+ * Returns:
+ * >= 0 on success; EOF on error.
+ */
+int linebuffer_write(LineBuffer *lb, FILE *fp);
+
+/**
+ * Write the current line as justified text.
+ *
+ * Args:
+ * lb: The line buffer to read.
+ * 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
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..6623f18
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,36 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include "linebuffer.h"
+#include "word.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);
+ }
+}
+
diff --git a/src/word.c b/src/word.c
new file mode 100644
index 0000000..f10a648
--- /dev/null
+++ b/src/word.c
@@ -0,0 +1,29 @@
+#include "word.h"
+
+static int read_char(FILE *fp);
+
+int read_word(FILE *fp, char *word, size_t length) {
+ int ch;
+ size_t position = 0;
+
+ while ((ch = read_char(fp)) == ' ')
+ ;
+
+ while (ch != ' ' && ch != EOF) {
+ if (position < length) {
+ word[position++] = ch;
+ }
+ ch = read_char(fp);
+ }
+ word[position] = '\0';
+ return ch;
+}
+
+static inline int read_char(FILE *fp) {
+ int ch = getc(fp);
+ if (ch == '\n' || ch == '\t') {
+ return ' ';
+ }
+ return ch;
+}
+
diff --git a/src/word.h b/src/word.h
new file mode 100644
index 0000000..77afbdc
--- /dev/null
+++ b/src/word.h
@@ -0,0 +1,8 @@
+#ifndef WORD_H
+#define WORD_H
+
+#include <stdio.h>
+
+int read_word(FILE *fp, char *word, size_t length);
+
+#endif