]> git.donarmstrong.com Git - lilypond.git/blob - flower/file-cookie.cc
Doc: Included/compile.itexi - CG 4.2 - Updated notes on reqs for compiling
[lilypond.git] / flower / file-cookie.cc
1
2 #include <cassert>
3 #include <cstdio>
4
5 #include "memory-stream.hh"
6
7 extern "C" {
8
9   bool
10   is_memory_stream (void *foo)
11   {
12     Memory_out_stream *cookie = (Memory_out_stream *) foo;
13     return dynamic_cast<Memory_out_stream *> (cookie);
14   }
15
16   void *
17   lily_fopencookie (void *cookie,
18                     char const * /* modes */,
19                     lily_cookie_io_functions_t /* io_funcs */)
20   {
21     assert (is_memory_stream (cookie));
22     return (FILE *) cookie;
23   }
24
25   ssize_t
26   lily_cookie_fclose (void *file)
27   {
28     assert (is_memory_stream (file));
29     return Memory_out_stream::cleaner (file);
30   }
31
32   ssize_t
33   lily_cookie_fprintf (void *file, char const *format, ...)
34   {
35     assert (is_memory_stream (file));
36     va_list ap;
37     va_start (ap, format);
38
39     static char buf[65536];
40     int i = vsnprintf (buf, sizeof (buf), format, ap);
41     if (i < 0 || (unsigned) i > sizeof (buf))
42       assert (false);
43     va_end (ap);
44     return Memory_out_stream::writer (file, buf, (unsigned)i);
45   }
46
47   ssize_t
48   lily_cookie_putc (int c, void *file)
49   {
50     assert (is_memory_stream (file));
51     char buf[1];
52     buf[0] = (char) c;
53     return Memory_out_stream::writer (file, buf, 1);
54   }
55 } /* extern C */