blob: be941ee481a735dcc73e3821c93e8ee682478d18 [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
Michael Wittene0390112011-02-15 23:12:04 +00004/* See Documentation/technical/api-strbuf.txt */
Pierre Habouzitb449f4c2007-09-06 13:20:05 +02005
Pierre Habouzitb315c5c2007-09-27 12:58:23 +02006extern char strbuf_slopbuf[];
Junio C Hamanod1df5742005-04-25 18:26:45 -07007struct strbuf {
Pierre Habouzitb449f4c2007-09-06 13:20:05 +02008 size_t alloc;
9 size_t len;
Brian Gerstbf0f9102005-05-18 08:14:09 -040010 char *buf;
Junio C Hamanod1df5742005-04-25 18:26:45 -070011};
12
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020013#define STRBUF_INIT { 0, 0, strbuf_slopbuf }
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020014
15/*----- strbuf life cycle -----*/
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020016extern void strbuf_init(struct strbuf *, size_t);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020017extern void strbuf_release(struct strbuf *);
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020018extern char *strbuf_detach(struct strbuf *, size_t *);
Pierre Habouzit917c9a72007-09-15 15:56:50 +020019extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
Pierre Habouzitc76689d2007-09-20 00:42:12 +020020static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
21 struct strbuf tmp = *a;
22 *a = *b;
23 *b = tmp;
24}
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020025
26/*----- strbuf size related -----*/
Lukas Sandström9b200fd2008-07-13 20:28:24 +020027static inline size_t strbuf_avail(const struct strbuf *sb) {
Pierre Habouzitc76689d2007-09-20 00:42:12 +020028 return sb->alloc ? sb->alloc - sb->len - 1 : 0;
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020029}
Junio C Hamanoa8f3e222007-09-26 02:26:06 -070030
31extern void strbuf_grow(struct strbuf *, size_t);
32
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020033static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
René Scharfe7141efa2011-04-27 19:24:50 +020034 if (len > (sb->alloc ? sb->alloc - 1 : 0))
35 die("BUG: strbuf_setlen() beyond buffer");
Pierre Habouzitc76689d2007-09-20 00:42:12 +020036 sb->len = len;
37 sb->buf[len] = '\0';
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020038}
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020039#define strbuf_reset(sb) strbuf_setlen(sb, 0)
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020040
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020041/*----- content related -----*/
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +020042extern void strbuf_trim(struct strbuf *);
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020043extern void strbuf_rtrim(struct strbuf *);
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +020044extern void strbuf_ltrim(struct strbuf *);
Lukas Sandström9b200fd2008-07-13 20:28:24 +020045extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +020046
Jeff King2f1d9e22011-06-09 11:54:58 -040047extern struct strbuf **strbuf_split_buf(const char *, size_t,
Jeff King28fc3a62011-06-09 11:51:22 -040048 int delim, int max);
Jeff King2f1d9e22011-06-09 11:54:58 -040049static inline struct strbuf **strbuf_split_str(const char *str,
50 int delim, int max)
51{
52 return strbuf_split_buf(str, strlen(str), delim, max);
53}
54static inline struct strbuf **strbuf_split_max(const struct strbuf *sb,
55 int delim, int max)
56{
57 return strbuf_split_buf(sb->buf, sb->len, delim, max);
58}
Jeff King28fc3a62011-06-09 11:51:22 -040059static inline struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
60{
61 return strbuf_split_max(sb, delim, 0);
62}
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +020063extern void strbuf_list_free(struct strbuf **);
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020064
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020065/*----- add data in your buffer -----*/
66static inline void strbuf_addch(struct strbuf *sb, int c) {
67 strbuf_grow(sb, 1);
68 sb->buf[sb->len++] = c;
69 sb->buf[sb->len] = '\0';
70}
71
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020072extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
Pierre Habouzitc76689d2007-09-20 00:42:12 +020073extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020074
Pierre Habouzit917c9a72007-09-15 15:56:50 +020075/* splice pos..pos+len with given data */
76extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
Pierre Habouzitc76689d2007-09-20 00:42:12 +020077 const void *, size_t);
Pierre Habouzit917c9a72007-09-15 15:56:50 +020078
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020079extern void strbuf_add(struct strbuf *, const void *, size_t);
80static inline void strbuf_addstr(struct strbuf *sb, const char *s) {
81 strbuf_add(sb, s, strlen(s));
82}
Lukas Sandström9b200fd2008-07-13 20:28:24 +020083static inline void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2) {
Junio C Hamano81d2cae2010-01-12 12:09:54 -080084 strbuf_grow(sb, sb2->len);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020085 strbuf_add(sb, sb2->buf, sb2->len);
86}
René Scharfe91db2672007-11-10 12:16:05 +010087extern void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020088
Marco Costalbac3a670d2008-02-09 15:40:19 +010089typedef size_t (*expand_fn_t) (struct strbuf *sb, const char *placeholder, void *context);
90extern void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, void *context);
René Scharfe9b864e72008-11-23 00:09:30 +010091struct strbuf_expand_dict_entry {
92 const char *placeholder;
93 const char *value;
94};
95extern size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, void *context);
Jeff King361df5d2010-01-13 12:36:42 -050096extern void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
René Scharfecde75e52007-11-09 01:49:42 +010097
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -080098__attribute__((format (printf,2,3)))
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020099extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
Jeff Kingebeb6092011-02-25 23:08:53 -0600100__attribute__((format (printf,2,0)))
101extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200102
Junio C Hamano895680f2011-11-04 21:06:30 -0700103extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *buf, size_t size);
104
105static inline void strbuf_complete_line(struct strbuf *sb)
106{
107 if (sb->len && sb->buf[sb->len - 1] != '\n')
108 strbuf_addch(sb, '\n');
109}
110
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200111extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
112/* XXX: if read fails, any partial read is undone */
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200113extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
Pierre Habouzit387e7e12007-09-27 15:25:55 +0200114extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
Linus Torvaldsb11b7e12008-12-17 09:36:40 -0800115extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200116
Brandon Caseyc7e4f0d2009-08-04 22:31:56 -0500117extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200118extern int strbuf_getline(struct strbuf *, FILE *, int);
Thomas Rast5e8617f2012-02-22 20:34:22 +0100119extern int strbuf_getwholeline_fd(struct strbuf *, int, int);
Junio C Hamanod1df5742005-04-25 18:26:45 -0700120
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -0400121extern void stripspace(struct strbuf *buf, int skip_comments);
Stephan Beyer71982032008-07-25 18:28:42 +0200122extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
Kristian Høgsberg6d69b6f2007-09-17 20:06:45 -0400123
Junio C Hamanoa552de72009-03-21 13:17:30 -0700124extern int strbuf_branchname(struct strbuf *sb, const char *name);
Junio C Hamanoa2fab532009-03-21 14:35:51 -0700125extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
Junio C Hamanoa552de72009-03-21 13:17:30 -0700126
Jeff Kingc5051162011-12-10 05:34:20 -0500127extern void strbuf_addstr_urlencode(struct strbuf *, const char *,
128 int reserved);
129
Nguyễn Thái Ngọc Duy9a0a30a2012-04-23 19:30:22 +0700130__attribute__((format (printf,1,2)))
131extern int printf_ln(const char *fmt, ...);
132__attribute__((format (printf,2,3)))
133extern int fprintf_ln(FILE *fp, const char *fmt, ...);
134
Junio C Hamanod1df5742005-04-25 18:26:45 -0700135#endif /* STRBUF_H */