]>
git.proxmox.com Git - libgit2.git/blob - src/buffer.h
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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"
12 #include "git2/buffer.h"
21 extern char git_buf__initbuf
[];
22 extern char git_buf__oom
[];
24 /* Use to initialize buffer structure when git_buf is on stack */
25 #define GIT_BUF_INIT { git_buf__initbuf, 0, 0 }
27 GIT_INLINE(bool) git_buf_is_allocated(const git_buf
*buf
)
29 return (buf
->ptr
!= NULL
&& buf
->asize
> 0);
33 * Initialize a git_buf structure.
35 * For the cases where GIT_BUF_INIT cannot be used to do static
38 extern void git_buf_init(git_buf
*buf
, size_t initial_size
);
41 * Attempt to grow the buffer to hold at least `target_size` bytes.
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.
50 extern int git_buf_try_grow(
51 git_buf
*buf
, size_t target_size
, bool mark_oom
, bool preserve_external
);
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
);
58 * Test if there have been any reallocation failures with this git_buf.
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.
66 * @return false if no error, true if allocation error
68 GIT_INLINE(bool) git_buf_oom(const git_buf
*buf
)
70 return (buf
->ptr
== git_buf__oom
);
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.
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
);
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
);
97 * Join two strings as paths, inserting a slash between as needed.
98 * @return 0 on success, -1 on failure
100 GIT_INLINE(int) git_buf_joinpath(git_buf
*buf
, const char *a
, const char *b
)
102 return git_buf_join(buf
, '/', a
, b
);
105 GIT_INLINE(const char *) git_buf_cstr(const git_buf
*buf
)
110 GIT_INLINE(size_t) git_buf_len(const git_buf
*buf
)
115 void git_buf_copy_cstr(char *data
, size_t datasize
, const git_buf
*buf
);
117 #define git_buf_PUTS(buf, str) git_buf_put(buf, str, sizeof(str) - 1)
119 GIT_INLINE(ssize_t
) git_buf_rfind_next(const git_buf
*buf
, char ch
)
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
--;
127 GIT_INLINE(ssize_t
) git_buf_rfind(const git_buf
*buf
, char ch
)
129 ssize_t idx
= (ssize_t
)buf
->size
- 1;
130 while (idx
>= 0 && buf
->ptr
[idx
] != ch
) idx
--;
134 GIT_INLINE(ssize_t
) git_buf_find(const git_buf
*buf
, char ch
)
136 void *found
= memchr(buf
->ptr
, ch
, buf
->size
);
137 return found
? (ssize_t
)((const char *)found
- buf
->ptr
) : -1;
140 /* Remove whitespace from the end of the buffer */
141 void git_buf_rtrim(git_buf
*buf
);
143 int git_buf_cmp(const git_buf
*a
, const git_buf
*b
);
145 /* Write data as base64 encoded in buffer */
146 int git_buf_put_base64(git_buf
*buf
, const char *data
, size_t len
);
149 * Insert, remove or replace a portion of the buffer.
151 * @param buf The buffer to work with
153 * @param where The location in the buffer where the transformation
156 * @param nb_to_remove The number of chars to be removed. 0 to not
157 * remove any character in the buffer.
159 * @param data A pointer to the data which should be inserted.
161 * @param nb_to_insert The number of chars to be inserted. 0 to not
162 * insert any character from the buffer.
164 * @return 0 or an error code.
171 size_t nb_to_insert
);