diff options
| -rw-r--r-- | .gitignore | 5 | ||||
| -rw-r--r-- | Dockerfile | 18 | ||||
| -rw-r--r-- | LICENSE | 21 | ||||
| -rw-r--r-- | Makefile | 22 | ||||
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | docker-compose.yml | 10 | ||||
| -rw-r--r-- | src/linebuffer.c | 84 | ||||
| -rw-r--r-- | src/linebuffer.h | 86 | ||||
| -rw-r--r-- | src/main.c | 36 | ||||
| -rw-r--r-- | src/word.c | 29 | ||||
| -rw-r--r-- | src/word.h | 8 |
11 files changed, 323 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9360af8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# macOS +.DS_Store + +# Build +/justify diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7b54f5a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM alpine:3.20.1 + +RUN apk update && \ + apk upgrade && \ + apk add --no-cache \ + build-base \ + gdb \ + gcc \ + make \ + valgrind \ + bash + +WORKDIR /app +COPY . /app + +RUN make + +ENTRYPOINT ["/bin/bash"] @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Eugene Triguba + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7a1ef44 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +CC = gcc +OPTIMIZATION_FLAG = -O0 +CC_FLAGS = -Wall -Wextra -pedantic $(OPTIMIZATION_FLAG) -std=c11 + +SRC_ENTRY_FILE = src/main.c +SRC_FILES = src/linebuffer.c src/word.c + +BUILD_OUTFILE = justify + +build: $(SRC_ENTRY_FILE) $(SRC_FILES) + $(CC) $(SRC_ENTRY_FILE) $(SRC_FILES) -o $(BUILD_OUTFILE) $(CC_FLAGS) + +build_optimized: $(SRC_ENTRY_FILE) $(SRC_FILES) + $(MAKE) OPTIMIZATION_FLAG=-O3 build + +memcheck: + $(MAKE) build + valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose ./$(BUILD_OUTFILE) + +clean: + rm -f $(BUILD_OUTFILE) $(TEST_OUTFILE) + diff --git a/README.md b/README.md new file mode 100644 index 0000000..859e18a --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Justify + +`justify` is a utility for justifying text. + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7679ddf --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +services: + justify: + build: + context: . + dockerfile: Dockerfile + container_name: justify + volumes: + - .:/app + tty: true + 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 |