aboutsummaryrefslogtreecommitdiffstats
path: root/include/linebuffer.h
blob: b11cb8f9fd0c51fa55df8ee526c2948cb14d6e2f (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef LINEBUFFER_H
#define LINEBUFFER_H

#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>

typedef struct {
    size_t _num_words;         // Number of words in the buffer
    size_t _line_width;        // Max display width (e.g., 72 columns)
    size_t _buffer_length;     // Current bytes used
    size_t _display_width;     // Current display width
    size_t _buffer_capacity;   // Byte capacity (line_width * 4 for UTF-8)
    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