summaryrefslogtreecommitdiff
path: root/flower/file-cookie.cc
blob: 762acb1c065d210f92bc3ba0bf1c1087810f3a6c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <cassert>
#include <cstdio>
using namespace std;

#include "memory-stream.hh"

extern "C" {

  bool
  is_memory_stream (void *foo)
  {
    Memory_out_stream *cookie = (Memory_out_stream *) foo;
    return dynamic_cast<Memory_out_stream *> (cookie);
  }

  void *
  lily_fopencookie (void *cookie,
                    char const * /* modes */,
                    lily_cookie_io_functions_t /* io_funcs */)
  {
    assert (is_memory_stream (cookie));
    return (FILE *) cookie;
  }

  ssize_t
  lily_cookie_fclose (void *file)
  {
    assert (is_memory_stream (file));
    return Memory_out_stream::cleaner (file);
  }

  ssize_t
  lily_cookie_fprintf (void *file, char const *format, ...)
  {
    assert (is_memory_stream (file));
    va_list ap;
    va_start (ap, format);

    static char buf[65536];
    int i = vsnprintf (buf, sizeof (buf), format, ap);
    if (i < 0 || (unsigned) i > sizeof (buf))
      assert (false);
    va_end (ap);
    return Memory_out_stream::writer (file, buf, (unsigned)i);
  }

  ssize_t
  lily_cookie_putc (int c, void *file)
  {
    assert (is_memory_stream (file));
    char buf[1];
    buf[0] = (char) c;
    return Memory_out_stream::writer (file, buf, 1);
  }
} /* extern C */