| 1 | /** @file stringbuffer.h |
| 2 | * |
| 3 | * Implements a general string buffer: it builds a string by concatenating. It |
| 4 | * allocates enough memory to hold the whole string; there is no upper limit |
| 5 | * for the total string length. |
| 6 | * |
| 7 | * Usage is: |
| 8 | * <code> |
| 9 | * StringBuffer *sb = stringbuffer_new(); |
| 10 | * stringbuffer_append_string(sb, "abc"); |
| 11 | * stringbuffer_append_string(sb, "def"); |
| 12 | * ... more calls to stringbuffer_append_xxx() |
| 13 | * char *str = stringbuffer_finish(sb) |
| 14 | * ... use str |
| 15 | * free(str); |
| 16 | * </code> |
| 17 | * |
| 18 | * No function ever fails. In case not enough memory is availabl, {@link |
| 19 | * fatal()} is called. |
| 20 | */ |
| 21 | |
| 22 | #ifndef STRING_BUFFER_H |
| 23 | #define STRING_BUFFER_H |
| 24 | |
| 25 | #include <ctype.h> |
| 26 | |
| 27 | #include "global.h" |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * The string buffer state. |
| 32 | */ |
| 33 | typedef struct StringBuffer StringBuffer; |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Create a new string buffer. |
| 38 | * |
| 39 | * @return The newly allocated string buffer. |
| 40 | */ |
| 41 | StringBuffer *stringbuffer_new(void); |
| 42 | |
| 43 | /** |
| 44 | * Totally delete a string buffer. |
| 45 | * @param sb String to delete, pointer becomes invalid after the call. |
| 46 | */ |
| 47 | void stringbuffer_delete(StringBuffer *sb); |
| 48 | |
| 49 | /** |
| 50 | * Deallocate the string buffer instance and return the string. |
| 51 | * |
| 52 | * The passed string buffer must not be accessed afterwards. |
| 53 | * |
| 54 | * @param sb The string buffer to deallocate. |
| 55 | * |
| 56 | * @return The result string; to free it, call <code>free()</code> on it. |
| 57 | */ |
| 58 | char *stringbuffer_finish(StringBuffer *sb); |
| 59 | |
| 60 | /** |
| 61 | * Deallocate the string buffer instance and return the string as a shared |
| 62 | * string. |
| 63 | * |
| 64 | * The passed string buffer must not be accessed afterwards. |
| 65 | * |
| 66 | * @param sb The string buffer to deallocate. |
| 67 | * |
| 68 | * @return The result shared string; to free it, call |
| 69 | * <code>free_string()</code> on it. |
| 70 | */ |
| 71 | sstring stringbuffer_finish_shared(StringBuffer *sb); |
| 72 | |
| 73 | /** |
| 74 | * Append a string to a string buffer instance. |
| 75 | * |
| 76 | * @param sb The string buffer to modify. |
| 77 | * |
| 78 | * @param str The string to append. |
| 79 | */ |
| 80 | void stringbuffer_append_string(StringBuffer *sb, const char *str); |
| 81 | |
| 82 | /** |
| 83 | * Append a formatted string to a string buffer instance. |
| 84 | * |
| 85 | * @param sb The string buffer to modify. |
| 86 | * |
| 87 | * @param format The format string to append. |
| 88 | */ |
| 89 | void stringbuffer_append_printf(StringBuffer *sb, const char *format, ...); |
| 90 | |
| 91 | /** |
| 92 | * Append the contents of a string buffer instance to another string buffer |
| 93 | * instance. |
| 94 | * |
| 95 | * @param sb The string buffer to modify. |
| 96 | * |
| 97 | * @param sb2 The string buffer to append; it must be different from sb. |
| 98 | */ |
| 99 | void stringbuffer_append_stringbuffer(StringBuffer *sb, const StringBuffer *sb2); |
| 100 | |
| 101 | /** |
| 102 | * Return the current length of the buffer. |
| 103 | * @param sb The string buffer to check. |
| 104 | * |
| 105 | * @return current length of sb. |
| 106 | */ |
| 107 | size_t stringbuffer_length(StringBuffer *sb); |
| 108 | |
| 109 | /** |
| 110 | * Trim trailing whitespace from a stringbuffer. |
| 111 | * |
| 112 | * @param sb |
| 113 | * The stringbuffer. |
| 114 | */ |
| 115 | void stringbuffer_trim_whitespace(StringBuffer *sb); |
| 116 | |
| 117 | #endif |