aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2025-06-29 20:48:41 -0400
committerEugene Triguba <eugenetriguba@gmail.com>2025-06-29 20:48:41 -0400
commit129a10d1ee57a047ae9a3084e175de1e7daf6ffe (patch)
tree079c753b5a9e4856c121a1a2eb59dc6ceba9396f /src
parentIgnore .cache (diff)
downloadcli-129a10d1ee57a047ae9a3084e175de1e7daf6ffe.zip
Fix justify behavior
Diffstat (limited to '')
-rw-r--r--include/linebuffer.h (renamed from src/linebuffer.h)15
-rw-r--r--include/strlcat.h (renamed from src/strlcat.h)0
-rw-r--r--include/word.h (renamed from src/word.h)0
-rw-r--r--src/linebuffer.c139
-rw-r--r--src/main.c49
5 files changed, 134 insertions, 69 deletions
diff --git a/src/linebuffer.h b/include/linebuffer.h
index 173bad3..a3d9e4c 100644
--- a/src/linebuffer.h
+++ b/include/linebuffer.h
@@ -2,6 +2,7 @@
#define LINEBUFFER_H
#include <stddef.h>
+#include <stdbool.h>
#include <stdio.h>
typedef struct {
@@ -40,8 +41,20 @@ void linebuffer_clear(LineBuffer *lb);
*
* @param lb The line buffer to append to.
* @param word The word to append.
+ *
+ * @retursns true if the word was appended; false if it could not fit.
+ */
+bool linebuffer_append_word(LineBuffer *lb, const char *word);
+
+/**
+ * @brief Check if a word can fit in the current line buffer.
+ *
+ * @param lb The line buffer to check.
+ * @param word The word to check.
+ *
+ * @returns true if the word can fit; false otherwise.
*/
-void linebuffer_append_word(LineBuffer *lb, const char *word);
+bool linebuffer_can_fit(LineBuffer *lb, const char *word);
/**
* @brief Check how many characters can be added.
diff --git a/src/strlcat.h b/include/strlcat.h
index 0470b83..0470b83 100644
--- a/src/strlcat.h
+++ b/include/strlcat.h
diff --git a/src/word.h b/include/word.h
index b5d94b5..b5d94b5 100644
--- a/src/word.h
+++ b/include/word.h
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/main.c b/src/main.c
index 2228279..1dbfe4c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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;
}