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