diff options
author | Vicent Marti <tanoku@gmail.com> | 2011-02-22 14:58:54 +0200 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2011-02-22 15:19:23 +0200 |
commit | 5591ea15a50d3214830a3d152a78a34bcb5f1966 (patch) | |
tree | 5c57db7a617331bb4717b20664cca088b7bbdf8e /src/filebuf.c | |
parent | af774b012cba233a577a95ee9b15944df6f3bbbf (diff) | |
download | libgit2-5591ea15a50d3214830a3d152a78a34bcb5f1966.tar.gz |
Add printf method to the File Buffer
Signed-off-by: Vicent Marti <tanoku@gmail.com>
Diffstat (limited to 'src/filebuf.c')
-rw-r--r-- | src/filebuf.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/filebuf.c b/src/filebuf.c index 58ff0b648..ace7428e0 100644 --- a/src/filebuf.c +++ b/src/filebuf.c @@ -22,6 +22,7 @@ * the Free Software Foundation, 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ +#include <stdarg.h> #include "common.h" #include "filebuf.h" @@ -259,3 +260,27 @@ int git_filebuf_reserve(git_filebuf *file, void **buffer, size_t len) return GIT_SUCCESS; } +int git_filebuf_printf(git_filebuf *file, const char *format, ...) +{ + va_list arglist; + size_t space_left = file->buf_size - file->buf_pos; + int len, error; + + va_start(arglist, format); + + len = vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist); + + if (len < 0 || (size_t)len >= space_left) { + if ((error = flush_buffer(file)) < GIT_SUCCESS) + return error; + + len = vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist); + if (len < 0 || (size_t)len > file->buf_size) + return GIT_ENOMEM; + } + + file->buf_pos += len; + return GIT_SUCCESS; + +} + |