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