]> git.proxmox.com Git - libgit2.git/blob - src/buffer.h
remote: create FETCH_HEAD with a refspecless remote
[libgit2.git] / src / buffer.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #ifndef INCLUDE_buffer_h__
8 #define INCLUDE_buffer_h__
9
10 #include "common.h"
11 #include "git2/strarray.h"
12 #include "git2/buffer.h"
13 #include <stdarg.h>
14
15 /* typedef struct {
16 * char *ptr;
17 * size_t asize, size;
18 * } git_buf;
19 */
20
21 extern char git_buf__initbuf[];
22 extern char git_buf__oom[];
23
24 /* Use to initialize buffer structure when git_buf is on stack */
25 #define GIT_BUF_INIT { git_buf__initbuf, 0, 0 }
26
27 GIT_INLINE(bool) git_buf_is_allocated(const git_buf *buf)
28 {
29 return (buf->ptr != NULL && buf->asize > 0);
30 }
31
32 /**
33 * Initialize a git_buf structure.
34 *
35 * For the cases where GIT_BUF_INIT cannot be used to do static
36 * initialization.
37 */
38 extern void git_buf_init(git_buf *buf, size_t initial_size);
39
40 /**
41 * Attempt to grow the buffer to hold at least `target_size` bytes.
42 *
43 * If the allocation fails, this will return an error. If `mark_oom` is true,
44 * this will mark the buffer as invalid for future operations; if false,
45 * existing buffer content will be preserved, but calling code must handle
46 * that buffer was not expanded. If `preserve_external` is true, then any
47 * existing data pointed to be `ptr` even if `asize` is zero will be copied
48 * into the newly allocated buffer.
49 */
50 extern int git_buf_try_grow(
51 git_buf *buf, size_t target_size, bool mark_oom, bool preserve_external);
52
53 extern void git_buf_swap(git_buf *buf_a, git_buf *buf_b);
54 extern char *git_buf_detach(git_buf *buf);
55 extern void git_buf_attach(git_buf *buf, char *ptr, size_t asize);
56
57 /**
58 * Test if there have been any reallocation failures with this git_buf.
59 *
60 * Any function that writes to a git_buf can fail due to memory allocation
61 * issues. If one fails, the git_buf will be marked with an OOM error and
62 * further calls to modify the buffer will fail. Check git_buf_oom() at the
63 * end of your sequence and it will be true if you ran out of memory at any
64 * point with that buffer.
65 *
66 * @return false if no error, true if allocation error
67 */
68 GIT_INLINE(bool) git_buf_oom(const git_buf *buf)
69 {
70 return (buf->ptr == git_buf__oom);
71 }
72
73 /*
74 * Functions below that return int value error codes will return 0 on
75 * success or -1 on failure (which generally means an allocation failed).
76 * Using a git_buf where the allocation has failed with result in -1 from
77 * all further calls using that buffer. As a result, you can ignore the
78 * return code of these functions and call them in a series then just call
79 * git_buf_oom at the end.
80 */
81 int git_buf_sets(git_buf *buf, const char *string);
82 int git_buf_putc(git_buf *buf, char c);
83 int git_buf_put(git_buf *buf, const char *data, size_t len);
84 int git_buf_puts(git_buf *buf, const char *string);
85 int git_buf_printf(git_buf *buf, const char *format, ...) GIT_FORMAT_PRINTF(2, 3);
86 int git_buf_vprintf(git_buf *buf, const char *format, va_list ap);
87 void git_buf_clear(git_buf *buf);
88 void git_buf_consume(git_buf *buf, const char *end);
89 void git_buf_truncate(git_buf *buf, size_t len);
90 void git_buf_shorten(git_buf *buf, size_t amount);
91 void git_buf_rtruncate_at_char(git_buf *path, char separator);
92
93 int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...);
94 int git_buf_join(git_buf *buf, char separator, const char *str_a, const char *str_b);
95
96 /**
97 * Join two strings as paths, inserting a slash between as needed.
98 * @return 0 on success, -1 on failure
99 */
100 GIT_INLINE(int) git_buf_joinpath(git_buf *buf, const char *a, const char *b)
101 {
102 return git_buf_join(buf, '/', a, b);
103 }
104
105 GIT_INLINE(const char *) git_buf_cstr(const git_buf *buf)
106 {
107 return buf->ptr;
108 }
109
110 GIT_INLINE(size_t) git_buf_len(const git_buf *buf)
111 {
112 return buf->size;
113 }
114
115 void git_buf_copy_cstr(char *data, size_t datasize, const git_buf *buf);
116
117 #define git_buf_PUTS(buf, str) git_buf_put(buf, str, sizeof(str) - 1)
118
119 GIT_INLINE(ssize_t) git_buf_rfind_next(const git_buf *buf, char ch)
120 {
121 ssize_t idx = (ssize_t)buf->size - 1;
122 while (idx >= 0 && buf->ptr[idx] == ch) idx--;
123 while (idx >= 0 && buf->ptr[idx] != ch) idx--;
124 return idx;
125 }
126
127 GIT_INLINE(ssize_t) git_buf_rfind(const git_buf *buf, char ch)
128 {
129 ssize_t idx = (ssize_t)buf->size - 1;
130 while (idx >= 0 && buf->ptr[idx] != ch) idx--;
131 return idx;
132 }
133
134 GIT_INLINE(ssize_t) git_buf_find(const git_buf *buf, char ch)
135 {
136 void *found = memchr(buf->ptr, ch, buf->size);
137 return found ? (ssize_t)((const char *)found - buf->ptr) : -1;
138 }
139
140 /* Remove whitespace from the end of the buffer */
141 void git_buf_rtrim(git_buf *buf);
142
143 int git_buf_cmp(const git_buf *a, const git_buf *b);
144
145 /* Write data as base64 encoded in buffer */
146 int git_buf_put_base64(git_buf *buf, const char *data, size_t len);
147
148 /*
149 * Insert, remove or replace a portion of the buffer.
150 *
151 * @param buf The buffer to work with
152 *
153 * @param where The location in the buffer where the transformation
154 * should be applied.
155 *
156 * @param nb_to_remove The number of chars to be removed. 0 to not
157 * remove any character in the buffer.
158 *
159 * @param data A pointer to the data which should be inserted.
160 *
161 * @param nb_to_insert The number of chars to be inserted. 0 to not
162 * insert any character from the buffer.
163 *
164 * @return 0 or an error code.
165 */
166 int git_buf_splice(
167 git_buf *buf,
168 size_t where,
169 size_t nb_to_remove,
170 const char *data,
171 size_t nb_to_insert);
172
173 #endif