blob: eba7ba423a2d3a383ef93f663c95695438269edf [file] [log] [blame]
Junio C Hamanod1df5742005-04-25 18:26:45 -07001#ifndef STRBUF_H
2#define STRBUF_H
Pierre Habouzitb449f4c2007-09-06 13:20:05 +02003
4/*
5 * Strbuf's can be use in many ways: as a byte array, or to store arbitrary
6 * long, overflow safe strings.
7 *
8 * Strbufs has some invariants that are very important to keep in mind:
9 *
10 * 1. the ->buf member is always malloc-ed, hence strbuf's can be used to
11 * build complex strings/buffers whose final size isn't easily known.
12 *
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020013 * It is NOT legal to copy the ->buf pointer away.
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020014 * `strbuf_detach' is the operation that detachs a buffer from its shell
15 * while keeping the shell valid wrt its invariants.
16 *
17 * 2. the ->buf member is a byte array that has at least ->len + 1 bytes
18 * allocated. The extra byte is used to store a '\0', allowing the ->buf
19 * member to be a valid C-string. Every strbuf function ensure this
20 * invariant is preserved.
21 *
22 * Note that it is OK to "play" with the buffer directly if you work it
23 * that way:
24 *
25 * strbuf_grow(sb, SOME_SIZE);
Steffen Prohaskaa1f611d2007-11-04 09:02:21 +010026 * ... Here, the memory array starting at sb->buf, and of length
27 * ... strbuf_avail(sb) is all yours, and you are sure that
28 * ... strbuf_avail(sb) is at least SOME_SIZE.
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020029 * strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
30 *
Steffen Prohaskaa1f611d2007-11-04 09:02:21 +010031 * Of course, SOME_OTHER_SIZE must be smaller or equal to strbuf_avail(sb).
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020032 *
33 * Doing so is safe, though if it has to be done in many places, adding the
34 * missing API to the strbuf module is the way to go.
35 *
36 * XXX: do _not_ assume that the area that is yours is of size ->alloc - 1
37 * even if it's true in the current implementation. Alloc is somehow a
38 * "private" member that should not be messed with.
39 */
40
41#include <assert.h>
42
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020043extern char strbuf_slopbuf[];
Junio C Hamanod1df5742005-04-25 18:26:45 -070044struct strbuf {
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020045 size_t alloc;
46 size_t len;
Brian Gerstbf0f9102005-05-18 08:14:09 -040047 char *buf;
Junio C Hamanod1df5742005-04-25 18:26:45 -070048};
49
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020050#define STRBUF_INIT { 0, 0, strbuf_slopbuf }
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020051
52/*----- strbuf life cycle -----*/
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020053extern void strbuf_init(struct strbuf *, size_t);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020054extern void strbuf_release(struct strbuf *);
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020055extern char *strbuf_detach(struct strbuf *, size_t *);
Pierre Habouzit917c9a72007-09-15 15:56:50 +020056extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
Pierre Habouzitc76689d2007-09-20 00:42:12 +020057static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
58 struct strbuf tmp = *a;
59 *a = *b;
60 *b = tmp;
61}
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020062
63/*----- strbuf size related -----*/
Lukas Sandström9b200fd2008-07-13 20:28:24 +020064static inline size_t strbuf_avail(const struct strbuf *sb) {
Pierre Habouzitc76689d2007-09-20 00:42:12 +020065 return sb->alloc ? sb->alloc - sb->len - 1 : 0;
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020066}
Junio C Hamanoa8f3e222007-09-26 02:26:06 -070067
68extern void strbuf_grow(struct strbuf *, size_t);
69
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020070static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
Junio C Hamanoa8f3e222007-09-26 02:26:06 -070071 if (!sb->alloc)
72 strbuf_grow(sb, 0);
73 assert(len < sb->alloc);
Pierre Habouzitc76689d2007-09-20 00:42:12 +020074 sb->len = len;
75 sb->buf[len] = '\0';
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020076}
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020077#define strbuf_reset(sb) strbuf_setlen(sb, 0)
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020078
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020079/*----- content related -----*/
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +020080extern void strbuf_trim(struct strbuf *);
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020081extern void strbuf_rtrim(struct strbuf *);
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +020082extern void strbuf_ltrim(struct strbuf *);
Lukas Sandström9b200fd2008-07-13 20:28:24 +020083extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +020084extern void strbuf_tolower(struct strbuf *);
85
86extern struct strbuf **strbuf_split(const struct strbuf *, int delim);
87extern void strbuf_list_free(struct strbuf **);
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020088
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020089/*----- add data in your buffer -----*/
90static inline void strbuf_addch(struct strbuf *sb, int c) {
91 strbuf_grow(sb, 1);
92 sb->buf[sb->len++] = c;
93 sb->buf[sb->len] = '\0';
94}
95
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020096extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
Pierre Habouzitc76689d2007-09-20 00:42:12 +020097extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020098
Pierre Habouzit917c9a72007-09-15 15:56:50 +020099/* splice pos..pos+len with given data */
100extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
Pierre Habouzitc76689d2007-09-20 00:42:12 +0200101 const void *, size_t);
Pierre Habouzit917c9a72007-09-15 15:56:50 +0200102
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200103extern void strbuf_add(struct strbuf *, const void *, size_t);
104static inline void strbuf_addstr(struct strbuf *sb, const char *s) {
105 strbuf_add(sb, s, strlen(s));
106}
Lukas Sandström9b200fd2008-07-13 20:28:24 +0200107static inline void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2) {
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200108 strbuf_add(sb, sb2->buf, sb2->len);
109}
René Scharfe91db2672007-11-10 12:16:05 +0100110extern void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200111
Marco Costalbac3a670d2008-02-09 15:40:19 +0100112typedef size_t (*expand_fn_t) (struct strbuf *sb, const char *placeholder, void *context);
113extern void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, void *context);
René Scharfecde75e52007-11-09 01:49:42 +0100114
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200115__attribute__((format(printf,2,3)))
116extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
117
118extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
119/* XXX: if read fails, any partial read is undone */
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200120extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
Pierre Habouzit387e7e12007-09-27 15:25:55 +0200121extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200122
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200123extern int strbuf_getline(struct strbuf *, FILE *, int);
Junio C Hamanod1df5742005-04-25 18:26:45 -0700124
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -0400125extern void stripspace(struct strbuf *buf, int skip_comments);
Stephan Beyer71982032008-07-25 18:28:42 +0200126extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -0400127
Junio C Hamanod1df5742005-04-25 18:26:45 -0700128#endif /* STRBUF_H */