Commit 6a0f6df9 authored by vasil's avatar vasil

Add ut_snprintf() function. On Windows this needs to be implemented

using auxiliary functions because there is no snprintf-variant on
Windows that behaves exactly as specified in the standard:

* Always return the number of characters that would have been printed
  if the size were unlimited (not including the final `\0').
* Always '\0'-terminate the result
* Do not touch the buffer if size=0, only return the number of characters
  that would have been printed. Can be used to estimate the size needed
  and to allocate it dynamically.

See http://www.freebsd.org/cgi/query-pr.cgi?pr=87260 for the reason why
2 ap variables are used.

Approved by:	Heikki
parent 8785af49
...@@ -290,6 +290,24 @@ ut_copy_file( ...@@ -290,6 +290,24 @@ ut_copy_file(
FILE* dest, /* in: output file */ FILE* dest, /* in: output file */
FILE* src); /* in: input file to be appended to output */ FILE* src); /* in: input file to be appended to output */
/**************************************************************************
snprintf(). */
#ifdef __WIN__
int
ut_snprintf(
/* out: number of characters that would
have been printed if the size were
unlimited, not including the terminating
'\0'. */
char* str, /* out: string */
size_t size, /* in: str size */
const char* fmt, /* in: format */
...); /* in: format values */
#else
#define ut_snprintf snprintf
#endif /* __WIN__ */
#ifndef UNIV_NONINL #ifndef UNIV_NONINL
#include "ut0ut.ic" #include "ut0ut.ic"
#endif #endif
......
...@@ -528,3 +528,47 @@ ut_copy_file( ...@@ -528,3 +528,47 @@ ut_copy_file(
} }
} while (len > 0); } while (len > 0);
} }
/**************************************************************************
snprintf(). */
#ifdef __WIN__
#include <stdarg.h>
int
ut_snprintf(
/* out: number of characters that would
have been printed if the size were
unlimited, not including the terminating
'\0'. */
char* str, /* out: string */
size_t size, /* in: str size */
const char* fmt, /* in: format */
...) /* in: format values */
{
int res;
va_list ap1;
va_list ap2;
va_start(ap1, fmt);
va_start(ap2, fmt);
res = _vscprintf(fmt, ap1);
if (res == -1) {
return(-1);
}
if (size > 0) {
_vsnprintf(str, size, fmt, ap2);
if ((size_t)res >= size) {
str[size - 1] = '\0';
}
}
va_end(ap1);
va_end(ap2);
return(res);
}
#endif /* __WIN__ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment