diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2025-06-29 20:48:41 -0400 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2025-06-29 20:48:41 -0400 |
| commit | 129a10d1ee57a047ae9a3084e175de1e7daf6ffe (patch) | |
| tree | 079c753b5a9e4856c121a1a2eb59dc6ceba9396f /include | |
| parent | Ignore .cache (diff) | |
| download | cli-129a10d1ee57a047ae9a3084e175de1e7daf6ffe.zip | |
Fix justify behavior
Diffstat (limited to 'include')
| -rw-r--r-- | include/linebuffer.h | 88 | ||||
| -rw-r--r-- | include/strlcat.h | 15 | ||||
| -rw-r--r-- | include/word.h | 28 |
3 files changed, 131 insertions, 0 deletions
diff --git a/include/linebuffer.h b/include/linebuffer.h new file mode 100644 index 0000000..a3d9e4c --- /dev/null +++ b/include/linebuffer.h @@ -0,0 +1,88 @@ +#ifndef LINEBUFFER_H +#define LINEBUFFER_H + +#include <stddef.h> +#include <stdbool.h> +#include <stdio.h> + +typedef struct { + size_t _num_words; + size_t _buffer_length; + size_t _buffer_capacity; + char *_buffer; +} LineBuffer; + +/** + * @brief Create a new LineBuffer + * + * @param 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); + +/** + * @brief Release any allocated memory for the LineBuffer + * + * @param lb The LineBuffer to destroy. + */ +void linebuffer_destroy(LineBuffer *lb); + +/** + * @brief Clear the current buffer + * + * @param lb The LineBuffer to clear. + */ +void linebuffer_clear(LineBuffer *lb); + +/** + * @brief Append a word to the end of the buffer. + * + * @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. + */ +bool linebuffer_can_fit(LineBuffer *lb, const char *word); + +/** + * @brief Check how many characters can be added. + * + * @param 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); + +/** + * @brief Write the current line. + * + * @param lb The line buffer to read. + * @param fp The file to write out the line buffer to. + * + * @returns >= 0 on success; EOF on error. + */ +int linebuffer_write(LineBuffer *lb, FILE *fp); + +/** + * @brief Write the current line as justified text. + * + * @param lb The line buffer to read. + * @param 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/include/strlcat.h b/include/strlcat.h new file mode 100644 index 0000000..0470b83 --- /dev/null +++ b/include/strlcat.h @@ -0,0 +1,15 @@ +#ifndef HAVE_STRLCAT +#define HAVE_STRLCAT + +#include <stddef.h> + +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. + */ +size_t strlcat(char *dst, const char *src, size_t dstsize); + +#endif diff --git a/include/word.h b/include/word.h new file mode 100644 index 0000000..b5d94b5 --- /dev/null +++ b/include/word.h @@ -0,0 +1,28 @@ +#ifndef WORD_H +#define WORD_H + +#include <stdio.h> + +#define READ_WORD_ERR_READ -1 +#define READ_WORD_ERR_EOF 0 +#define READ_WORD_SUCCESS 1 + +/** + * Read in a word from a file up to size length. + * + * If there is any leading whitespace, it will be discarded + * before starting to read in a word. + * + * @param[in] fp The file to read a word from. + * @param[out] word The word that was read as a null-terminated string. + * @param[in] length The length of the word to read in. + * + * @note `length` should be the number of characters you'd + * like to read in plus one to take into account the + * null terminator. + * + * @returns < 0 on read error; 0 on EOF; > 0 on success. + */ +int read_word(FILE *fp, char *word, size_t length); + +#endif |