about summary refs log tree commit diff
path: root/stdio-common/printf_buffer_as_file.h
blob: c0d65b9ee5f286c42e9d979c717dcb6022b4f92e (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* FILE * interface to a struct __*printf_buffer.
   Copyright (C) 2022-2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/* Registered printf format specifier callbacks produce data via a
   FILE * stream.  struct __printf_buffer_as_file enables vfprintf to
   create a suitable stdio stream.  Likewise struct
   __wprintf_buffer_as_file for vfwprintf.  */

#ifndef PRINTF_BUFFER_AS_FILE_H
#define PRINTF_BUFFER_AS_FILE_H

#include <libio/libioP.h>

struct __printf_buffer;

struct __printf_buffer_as_file
{
  /* Interface to libio.  */
  FILE stream;
  const struct _IO_jump_t *vtable;

  /* Pointer to the underlying buffer.  */
  struct __printf_buffer *next;
};

/* Initialization *FP so that data written to its FILE * stream ends
   up in NEXT.  */
void __printf_buffer_as_file_init (struct __printf_buffer_as_file *fp,
                                   struct __printf_buffer *next)
  attribute_hidden;

/* Returns the FILE * that can be used to write data to the
   buffer.  */
static inline FILE *
__printf_buffer_as_file_get (struct __printf_buffer_as_file *file)
{
  return &file->stream;
}

/* Transfers all pending data from the FILE * to the underlying
   buffer.  Returns true if there have been no errors.  */
bool __printf_buffer_as_file_terminate (struct __printf_buffer_as_file *)
  attribute_hidden;

/* Wide variant follows.  */

struct __wprintf_buffer;
struct __wprintf_buffer_as_file
{
  /* Interface to libio.  */
  FILE stream;
  const struct _IO_jump_t *vtable;
  struct _IO_wide_data wide_stream;

  /* Pointer to the underlying buffer.  */
  struct __wprintf_buffer *next;
};

void __wprintf_buffer_as_file_init (struct __wprintf_buffer_as_file *fp,
                                    struct __wprintf_buffer *next)
  attribute_hidden;

static inline FILE *
__wprintf_buffer_as_file_get (struct __wprintf_buffer_as_file *file)
{
  return &file->stream;
}

bool __wprintf_buffer_as_file_terminate (struct __wprintf_buffer_as_file *)
  attribute_hidden;

#endif /* PRINTF_BUFFER_AS_FILE_H */