Docs: Review of programming-work.itexi.
[lilypond/mpolesky.git] / flower / file-cookie.cc
blobbedb100a00b26b4ac246e7948dd4c3166795f8f2
2 #include <cassert>
3 #include <cstdio>
4 using namespace std;
6 #include "memory-stream.hh"
8 extern "C" {
10 bool
11 is_memory_stream (void *foo)
13 Memory_out_stream *cookie = (Memory_out_stream *) foo;
14 return dynamic_cast<Memory_out_stream *> (cookie);
17 void *
18 lily_fopencookie (void *cookie,
19 char const * /* modes */,
20 lily_cookie_io_functions_t /* io_funcs */)
22 assert (is_memory_stream (cookie));
23 return (FILE *) cookie;
26 int
27 lily_cookie_fclose (void *file)
29 assert (is_memory_stream (file));
30 return Memory_out_stream::cleaner (file);
33 int
34 lily_cookie_fprintf (void *file, char const *format, ...)
36 assert (is_memory_stream (file));
37 va_list ap;
38 va_start (ap, format);
40 static char buf[65536];
41 int i = vsnprintf (buf, sizeof (buf), format, ap);
42 if (i == -1 || (unsigned) i > sizeof (buf))
43 assert (false);
44 va_end (ap);
45 return Memory_out_stream::writer (file, buf, i);
48 int
49 lily_cookie_putc (int c, void *file)
51 assert (is_memory_stream (file));
52 char buf[1];
53 buf[0] = (char) c;
54 return Memory_out_stream::writer (file, buf, 1);
56 } /* extern C */