]>
git.proxmox.com Git - libgit2.git/blob - src/buffer.h
2 * Copyright (C) 2009-2012 the libgit2 contributors
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.
7 #ifndef INCLUDE_buffer_h__
8 #define INCLUDE_buffer_h__
11 #include "git2/strarray.h"
19 extern char git_buf__initbuf
[];
20 extern char git_buf__oom
[];
22 #define GIT_BUF_INIT { git_buf__initbuf, 0, 0 }
25 * Initialize a git_buf structure.
27 * For the cases where GIT_BUF_INIT cannot be used to do static
30 extern void git_buf_init(git_buf
*buf
, size_t initial_size
);
33 * Attempt to grow the buffer to hold at least `target_size` bytes.
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.
40 extern int git_buf_try_grow(git_buf
*buf
, size_t target_size
, bool mark_oom
);
43 * Grow the buffer to hold at least `target_size` bytes.
45 * If the allocation fails, this will return an error and the buffer will be
46 * marked as invalid for future operations, invaliding contents.
48 * @return 0 on success or -1 on failure
50 GIT_INLINE(int) git_buf_grow(git_buf
*buf
, size_t target_size
)
52 return git_buf_try_grow(buf
, target_size
, true);
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
);
61 * Test if there have been any reallocation failures with this git_buf.
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
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.
69 * @return false if no error, true if allocation error
71 GIT_INLINE(bool) git_buf_oom(const git_buf
*buf
)
73 return (buf
->ptr
== git_buf__oom
);
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.
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);
90 int git_buf_vprintf(git_buf
*buf
, const char *format
, va_list ap
);
91 void git_buf_clear(git_buf
*buf
);
92 void git_buf_consume(git_buf
*buf
, const char *end
);
93 void git_buf_truncate(git_buf
*buf
, size_t len
);
94 void git_buf_rtruncate_at_char(git_buf
*path
, char separator
);
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
);
100 * Join two strings as paths, inserting a slash between as needed.
101 * @return 0 on success, -1 on failure
103 GIT_INLINE(int) git_buf_joinpath(git_buf
*buf
, const char *a
, const char *b
)
105 return git_buf_join(buf
, '/', a
, b
);
108 GIT_INLINE(const char *) git_buf_cstr(const git_buf
*buf
)
113 GIT_INLINE(size_t) git_buf_len(const git_buf
*buf
)
118 void git_buf_copy_cstr(char *data
, size_t datasize
, const git_buf
*buf
);
120 #define git_buf_PUTS(buf, str) git_buf_put(buf, str, sizeof(str) - 1)
122 GIT_INLINE(ssize_t
) git_buf_rfind_next(git_buf
*buf
, char ch
)
124 ssize_t idx
= (ssize_t
)buf
->size
- 1;
125 while (idx
>= 0 && buf
->ptr
[idx
] == ch
) idx
--;
126 while (idx
>= 0 && buf
->ptr
[idx
] != ch
) idx
--;
130 GIT_INLINE(ssize_t
) git_buf_rfind(git_buf
*buf
, char ch
)
132 ssize_t idx
= (ssize_t
)buf
->size
- 1;
133 while (idx
>= 0 && buf
->ptr
[idx
] != ch
) idx
--;
137 /* Remove whitespace from the end of the buffer */
138 void git_buf_rtrim(git_buf
*buf
);
140 int git_buf_cmp(const git_buf
*a
, const git_buf
*b
);
142 /* Write data as base64 encoded in buffer */
143 int git_buf_put_base64(git_buf
*buf
, const char *data
, size_t len
);
146 * Insert, remove or replace a portion of the buffer.
148 * @param buf The buffer to work with
150 * @param where The location in the buffer where the transformation
153 * @param nb_to_remove The number of chars to be removed. 0 to not
154 * remove any character in the buffer.
156 * @param data A pointer to the data which should be inserted.
158 * @param nb_to_insert The number of chars to be inserted. 0 to not
159 * insert any character from the buffer.
161 * @return 0 or an error code.
168 size_t nb_to_insert
);