summaryrefslogtreecommitdiff
path: root/source/fitz/buffer.c
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2014-03-18 16:37:19 +0100
committerTor Andersson <tor.andersson@artifex.com>2014-03-19 12:52:05 +0100
commit30bcbe60b93a0e4a697871f69e8367476796f27c (patch)
tree39c2fa6d826a866fe6a5692b6e3575956b41c44c /source/fitz/buffer.c
parent4c2715a0bcecfed6ebdfee901920631b09364d7e (diff)
downloadmupdf-30bcbe60b93a0e4a697871f69e8367476796f27c.tar.xz
Implement our own vsnprintf variant.
The primary motivator for this is so that we can print floating point values and get the full accuracy out, without having to print 1.5 as 1.5000000, and without getting 23e24 etc. We only support %c, %f, %d, %o, %x and %s currently. We only support the zero padding qualifier, for integers. We do support some extensions: %C turns values >=128 into UTF-8. %M prints a fz_matrix. %R prints a fz_rect. %P prints a fz_point. We also implement a fprintf variant on top of this to allow for consistent results when using fz_output. a
Diffstat (limited to 'source/fitz/buffer.c')
-rw-r--r--source/fitz/buffer.c44
1 files changed, 23 insertions, 21 deletions
diff --git a/source/fitz/buffer.c b/source/fitz/buffer.c
index 07ce9361..e9295a26 100644
--- a/source/fitz/buffer.c
+++ b/source/fitz/buffer.c
@@ -224,45 +224,47 @@ fz_buffer_printf(fz_context *ctx, fz_buffer *buffer, const char *fmt, ...)
int ret;
va_list args;
va_start(args, fmt);
-
ret = fz_buffer_vprintf(ctx, buffer, fmt, args);
-
va_end(args);
-
return ret;
}
int
fz_buffer_vprintf(fz_context *ctx, fz_buffer *buffer, const char *fmt, va_list old_args)
{
+ int slack;
int len;
+ va_list args;
+
+ slack = buffer->cap - buffer->len;
+#ifdef _MSC_VER /* Microsoft Visual C */
+ args = old_args;
+#else
+ va_copy(args, old_args);
+#endif
+ len = fz_vsnprintf((char *)buffer->data + buffer->len, slack, fmt, args);
+#ifndef _MSC_VER
+ va_end(args);
+#endif
- do
+ /* len = number of chars written, not including the terminating
+ * NULL, so len+1 > slack means "truncated". */
+ if (len+1 > slack)
{
- int slack = buffer->cap - buffer->len;
+ /* Grow the buffer and retry */
+ fz_ensure_buffer(ctx, buffer, buffer->len + len);
+ slack = buffer->cap - buffer->len;
- if (slack > 0)
- {
- va_list args;
#ifdef _MSC_VER /* Microsoft Visual C */
- args = old_args;
+ args = old_args;
#else
- va_copy(args, old_args);
+ va_copy(args, old_args);
#endif
- len = vsnprintf((char *)buffer->data + buffer->len, slack, fmt, args);
+ len = fz_vsnprintf((char *)buffer->data + buffer->len, slack, fmt, args);
#ifndef _MSC_VER
- va_end(args);
+ va_end(args);
#endif
- /* len = number of chars written, not including the terminating
- * NULL, so len+1 > slack means "truncated". MSVC differs here
- * and returns -1 for truncated. */
- if (len >= 0 && len+1 <= slack)
- break;
- }
- /* Grow the buffer and retry */
- fz_grow_buffer(ctx, buffer);
}
- while (1);
buffer->len += len;