summaryrefslogtreecommitdiff
path: root/flower/file-cookie.cc
blob: c173b087bcbb664ea28d19a14050e82b4f7d5beb (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
57
#include <assert.h>
#include <cstdio>

#include "memory-stream.hh"

extern "C" {

  static 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)
  {
    (void) cookie;
    (void) modes;
    (void) io_funcs;
    assert (is_memory_stream (cookie));
    return (FILE *) cookie;
  }

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

  int
  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 == -1 || (unsigned) i > sizeof (buf))
      assert (false);
    va_end (ap);
    return Memory_out_stream::writer (file, buf, i);
  }

  int
  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 */