Skip to content

Commit a22eb92

Browse files
alexander-matzSven Verdoolaege
authored andcommitted
interface: use vsnprintf instead of open_memstream for macosx support
The function open_memstream is used in combination with fprintf in order to provide printf-like functionality on top of ostreams. open_memstream, however, is not available on macosx. The use of open_memstream and fprintf was therefore replaced by vsnprintf and a manually allocated buffer (the size is determined via dry-running vsnprintf). This modification does not change the external behaviour but restores macosx compatibility. Signed-off-by: Alexander Matz <a.matz.1988@gmail.com> Signed-off-by: Sven Verdoolaege <sven.verdoolaege@gmail.com>
1 parent aca4406 commit a22eb92

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

interface/cpp.cc

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,18 @@
5050
static void osprintf(ostream &os, const char *format, ...)
5151
{
5252
va_list arguments;
53-
FILE *string_stream;
5453
char *string_pointer;
5554
size_t size;
5655

5756
va_start(arguments, format);
58-
59-
string_stream = open_memstream(&string_pointer, &size);
60-
61-
if (!string_stream) {
62-
fprintf(stderr, "open_memstream failed -- aborting!\n");
63-
exit(1);
64-
}
65-
66-
vfprintf(string_stream, format, arguments);
67-
fclose(string_stream);
57+
size = vsnprintf(NULL, 0, format, arguments);
58+
string_pointer = new char[size + 1];
59+
va_end(arguments);
60+
va_start(arguments, format);
61+
vsnprintf(string_pointer, size + 1, format, arguments);
62+
va_end(arguments);
6863
os << string_pointer;
69-
free(string_pointer);
64+
delete[] string_pointer;
7065
}
7166

7267
/* Convert "l" to a string.

0 commit comments

Comments
 (0)