txtbuf.h (3527B)
1 /* 2 * Copyright (c) 2022-2024 lumidify <nobody@lumidify.org> 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #ifndef LTK_TXTBUF_H 18 #define LTK_TXTBUF_H 19 20 #include <stddef.h> 21 22 /* 23 * txtbuf is really just a string data type that is badly named. 24 * The stored text is always nul-terminated. 25 * FIXME: this data type is abused in some places and manually 26 * created so it isn't nul-terminated 27 */ 28 29 typedef struct { 30 size_t len, cap; 31 char *text; 32 } txtbuf; 33 34 /* 35 * Create an empty txtbuf. 36 */ 37 txtbuf *txtbuf_new(void); 38 39 /* 40 * Create a new txtbuf, initializing it with the nul-terminated 41 * string 'str'. The input string is copied. 42 */ 43 txtbuf *txtbuf_new_from_char(const char *str); 44 45 /* 46 * Create a new txtbuf, initializing it with the string 'str' 47 * of length 'len'. The input string is copied. 48 */ 49 txtbuf *txtbuf_new_from_char_len(const char *str, size_t len); 50 51 /* 52 * Replace the stored text in 'buf' with the text generated by 53 * 'snprintf' when called with the given format string and args. 54 */ 55 void txtbuf_fmt(txtbuf *buf, const char *fmt, ...); 56 57 /* 58 * Replace the stored text in 'buf' with 'text'. 59 */ 60 void txtbuf_set_text(txtbuf *buf, const char *text); 61 62 /* 63 * Same as txtbuf_set_text, but with explicit length for 'text'. 64 */ 65 void txtbuf_set_textn(txtbuf *buf, const char *text, size_t len); 66 67 /* 68 * Append 'text' to the text stored in 'buf'. 69 */ 70 void txtbuf_append(txtbuf *buf, const char *text); 71 72 /* 73 * Same as txtbuf_append, but with explicit length for 'text'. 74 */ 75 void txtbuf_appendn(txtbuf *buf, const char *text, size_t len); 76 77 /* 78 * Compare the text of two txtbuf's like 'strcmp'. 79 */ 80 int txtbuf_cmp(txtbuf *buf1, txtbuf *buf2); 81 82 /* 83 * Convenience function for calling 'txtbuf_cmp' and checking if the 84 * return value is 0, i.e. the strings are equal. 85 */ 86 int txtbuf_eql(txtbuf *buf1, txtbuf *buf2); 87 88 /* 89 * Make sure the txtbuf has space for at least the given size, 90 * plus '\0' at the end. 91 */ 92 void txtbuf_resize(txtbuf *buf, size_t sz); 93 94 /* 95 * Destroy a txtbuf. 96 */ 97 void txtbuf_destroy(txtbuf *buf); 98 99 /* 100 * Copy txtbuf 'src' to txtbuf 'dst'. 101 */ 102 void txtbuf_copy(txtbuf *dst, txtbuf *src); 103 104 /* 105 * Duplicate txtbuf 'src'. 106 */ 107 txtbuf *txtbuf_dup(txtbuf *src); 108 109 /* 110 * Get copy of text stored in 'buf'. 111 * The returned text belongs to the caller and needs to be freed. 112 */ 113 char *txtbuf_get_textcopy(txtbuf *buf); 114 115 /* 116 * Get text stored in 'buf'. 117 * The returned text belongs to the txtbuf and must not be changed. 118 * The returned text may be invalidated as soon as any other 119 * functions are called on the txtbuf. 120 */ 121 const char *txtbuf_get_text(txtbuf *buf); 122 123 /* 124 * Get the length of the text stored in 'buf'. 125 */ 126 size_t txtbuf_len(txtbuf *buf); 127 128 /* 129 * Clear the text, but do not reduce the internal capacity 130 * (for efficiency if it will be filled up again anyways). 131 */ 132 void txtbuf_clear(txtbuf *buf); 133 134 #endif /* LTK_TXTBUF_H */