]> git.proxmox.com Git - libgit2.git/commitdiff
Add printf method to the File Buffer
authorVicent Marti <tanoku@gmail.com>
Tue, 22 Feb 2011 12:58:54 +0000 (14:58 +0200)
committerVicent Marti <tanoku@gmail.com>
Tue, 22 Feb 2011 13:19:23 +0000 (15:19 +0200)
Signed-off-by: Vicent Marti <tanoku@gmail.com>
src/filebuf.c
src/filebuf.h

index 58ff0b648a13d61ad18b3e12e9633b2c7ed3f8c4..ace7428e09d4673b312d161eea0686ede3e4e6ff 100644 (file)
@@ -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;
+
+}
+
index f2e4dba5128f8833526735e77c5561260301405f..b2b5eaf4b4bd3a4b5ea6cfca1415e324c8cecb25 100644 (file)
@@ -31,6 +31,7 @@ typedef struct git_filebuf git_filebuf;
 
 int git_filebuf_write(git_filebuf *lock, void *buff, size_t len);
 int git_filebuf_reserve(git_filebuf *file, void **buff, size_t len);
+int git_filebuf_printf(git_filebuf *file, const char *format, ...);
 
 int git_filebuf_open(git_filebuf *lock, const char *path, int flags);
 int git_filebuf_commit(git_filebuf *lock);