1 /* Copyright (C) 1992, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 /* Output-room function for obstack streams. */
27 grow (FILE *stream
, int c
)
29 struct obstack
*const obstack
= (struct obstack
*) stream
->__cookie
;
31 /* Move the end of the object back to include only the portion
32 of the buffer which the user has already written into. */
33 obstack_blank_fast (obstack
, - (stream
->__put_limit
- stream
->__bufp
));
35 if ((size_t) stream
->__target
> obstack_object_size (obstack
))
37 /* Our target (where the buffer maps to) is always zero except when
38 the user just did a SEEK_END fseek. If he sought within the
39 buffer, we need do nothing and will zero the target below. If he
40 sought past the end of the object, grow and zero-fill the object
41 up to the target address. */
43 obstack_blank (obstack
,
44 stream
->__target
- obstack_object_size (obstack
));
45 /* fseek has just flushed us, so the put limit points
46 to the end of the written data. */
47 bzero (stream
->__put_limit
,
48 stream
->__target
- stream
->__bufsize
);
52 obstack_1grow (obstack
, (unsigned char) c
);
54 /* The stream buffer always maps exactly to the object on the top
55 of the obstack. The start of the buffer is the start of the object.
56 The put limit points just past the end of the object. On fflush, the
57 obstack is sync'd so the end of the object points just past the last
58 character written to the stream. */
60 stream
->__target
= stream
->__offset
= 0;
61 stream
->__buffer
= obstack_base (obstack
);
62 stream
->__bufsize
= obstack_room (obstack
);
63 stream
->__bufp
= obstack_next_free (obstack
);
64 stream
->__get_limit
= stream
->__bufp
;
67 /* This is fflush. Make the stream buffer, the object,
68 and the characters actually written all match. */
69 stream
->__put_limit
= stream
->__get_limit
;
72 /* Extend the buffer (and the object) to include
73 the rest of the obstack chunk (which is uninitialized).
74 Data past bufp is undefined. */
75 stream
->__put_limit
= stream
->__buffer
+ stream
->__bufsize
;
76 obstack_blank_fast (obstack
, stream
->__put_limit
- stream
->__bufp
);
80 /* Seek function for obstack streams.
81 There is no external state to munge. */
84 seek (void *cookie
, fpos_t *pos
, int whence
)
93 /* Return the position relative to the end of the object.
94 fseek has just flushed us, so the obstack is consistent. */
95 *pos
+= obstack_object_size ((struct obstack
*) cookie
);
99 __libc_fatal ("obstream::seek called with bogus WHENCE\n");
104 /* Input room function for obstack streams.
105 Only what has been written to the stream can be read back. */
110 /* Re-sync with the obstack, growing the object if necessary. */
113 if (stream
->__bufp
< stream
->__get_limit
)
114 return (unsigned char) *stream
->__bufp
++;
120 /* Initialize STREAM to talk to OBSTACK. */
123 init_obstream (FILE *stream
, struct obstack
*obstack
)
125 stream
->__mode
.__write
= 1;
126 stream
->__mode
.__read
= 1;
128 /* Input can read only what has been written. */
129 stream
->__room_funcs
.__input
= input
;
131 /* Do nothing for close. */
132 stream
->__io_funcs
.__close
= NULL
;
134 /* When the buffer is full, grow the obstack. */
135 stream
->__room_funcs
.__output
= grow
;
137 /* Seek within the object, and extend it. */
138 stream
->__io_funcs
.__seek
= seek
;
139 stream
->__target
= stream
->__offset
= 0;
143 /* Don't deallocate that buffer! */
144 stream
->__userbuf
= 1;
146 /* We don't have to initialize the buffer.
147 The first read attempt will call grow, which will do all the work. */
151 open_obstack_stream (obstack
)
152 struct obstack
*obstack
;
154 register FILE *stream
;
156 stream
= __newstream ();
160 init_obstream (stream
, obstack
);
165 obstack_vprintf (obstack
, format
, args
)
166 struct obstack
*obstack
;
171 bzero (&f
, sizeof (f
));
172 init_obstream (&f
, obstack
);
173 return vfprintf (&f
, format
, args
);
177 obstack_printf (struct obstack
*obstack
, const char *format
, ...)
181 va_start (ap
, format
);
182 result
= obstack_vprintf (obstack
, format
, ap
);