1 /* UI_FILE - a generic STDIO like output stream.
2 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Implement the ``struct ui_file'' object. */
25 #include "gdb_string.h"
28 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
30 static ui_file_isatty_ftype null_file_isatty;
31 static ui_file_write_ftype null_file_write;
32 static ui_file_fputs_ftype null_file_fputs;
33 static ui_file_flush_ftype null_file_flush;
34 static ui_file_delete_ftype null_file_delete;
35 static ui_file_rewind_ftype null_file_rewind;
36 static ui_file_put_ftype null_file_put;
41 ui_file_flush_ftype *to_flush;
42 ui_file_write_ftype *to_write;
43 ui_file_fputs_ftype *to_fputs;
44 ui_file_delete_ftype *to_delete;
45 ui_file_isatty_ftype *to_isatty;
46 ui_file_rewind_ftype *to_rewind;
47 ui_file_put_ftype *to_put;
55 struct ui_file *file = xmalloc (sizeof (struct ui_file));
56 file->magic = &ui_file_magic;
57 set_ui_file_data (file, NULL, null_file_delete);
58 set_ui_file_flush (file, null_file_flush);
59 set_ui_file_write (file, null_file_write);
60 set_ui_file_fputs (file, null_file_fputs);
61 set_ui_file_isatty (file, null_file_isatty);
62 set_ui_file_rewind (file, null_file_rewind);
63 set_ui_file_put (file, null_file_put);
68 ui_file_delete (struct ui_file *file)
70 file->to_delete (file);
75 null_file_isatty (struct ui_file *file)
81 null_file_rewind (struct ui_file *file)
87 null_file_put (struct ui_file *file,
88 ui_file_put_method_ftype *write,
95 null_file_flush (struct ui_file *file)
101 null_file_write (struct ui_file *file,
105 if (file->to_fputs == null_file_fputs)
106 /* Both the write and fputs methods are null. Discard the
111 /* The fputs method isn't null, slowly pass the write request
112 onto that. FYI, this isn't as bad as it may look - the
113 current (as of 1999-11-07) printf_* function calls fputc and
114 fputc does exactly the below. By having a write function it
115 is possible to clean up that code. */
119 for (i = 0; i < sizeof_buf; i++)
122 file->to_fputs (b, file);
129 null_file_fputs (const char *buf, struct ui_file *file)
131 if (file->to_write == null_file_write)
132 /* Both the write and fputs methods are null. Discard the
137 /* The write method was implemented, use that. */
138 file->to_write (file, buf, strlen (buf));
143 null_file_delete (struct ui_file *file)
149 ui_file_data (struct ui_file *file)
151 if (file->magic != &ui_file_magic)
152 internal_error (__FILE__, __LINE__,
153 "ui_file_data: bad magic number");
154 return file->to_data;
158 gdb_flush (struct ui_file *file)
160 file->to_flush (file);
164 ui_file_isatty (struct ui_file *file)
166 return file->to_isatty (file);
170 ui_file_rewind (struct ui_file *file)
172 file->to_rewind (file);
176 ui_file_put (struct ui_file *file,
177 ui_file_put_method_ftype *write,
180 file->to_put (file, write, dest);
184 ui_file_write (struct ui_file *file,
188 file->to_write (file, buf, length_buf);
192 fputs_unfiltered (const char *buf, struct ui_file *file)
194 file->to_fputs (buf, file);
198 set_ui_file_flush (struct ui_file *file, ui_file_flush_ftype *flush)
200 file->to_flush = flush;
204 set_ui_file_isatty (struct ui_file *file, ui_file_isatty_ftype *isatty)
206 file->to_isatty = isatty;
210 set_ui_file_rewind (struct ui_file *file, ui_file_rewind_ftype *rewind)
212 file->to_rewind = rewind;
216 set_ui_file_put (struct ui_file *file, ui_file_put_ftype *put)
222 set_ui_file_write (struct ui_file *file,
223 ui_file_write_ftype *write)
225 file->to_write = write;
229 set_ui_file_fputs (struct ui_file *file, ui_file_fputs_ftype *fputs)
231 file->to_fputs = fputs;
235 set_ui_file_data (struct ui_file *file, void *data,
236 ui_file_delete_ftype *delete)
238 file->to_data = data;
239 file->to_delete = delete;
242 /* ui_file utility function for converting a ``struct ui_file'' into
243 a memory buffer''. */
245 struct accumulated_ui_file
252 do_ui_file_xstrdup (void *context, const char *buffer, long length)
254 struct accumulated_ui_file *acc = context;
255 if (acc->buffer == NULL)
256 acc->buffer = xmalloc (length + 1);
258 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
259 memcpy (acc->buffer + acc->length, buffer, length);
260 acc->length += length;
261 acc->buffer[acc->length] = '\0';
265 ui_file_xstrdup (struct ui_file *file,
268 struct accumulated_ui_file acc;
271 ui_file_put (file, do_ui_file_xstrdup, &acc);
272 if (acc.buffer == NULL)
273 acc.buffer = xstrdup ("");
274 *length = acc.length;
278 /* A pure memory based ``struct ui_file'' that can be used an output
279 buffer. The buffers accumulated contents are available via
290 static ui_file_rewind_ftype mem_file_rewind;
291 static ui_file_put_ftype mem_file_put;
292 static ui_file_write_ftype mem_file_write;
293 static ui_file_delete_ftype mem_file_delete;
294 static struct ui_file *mem_file_new (void);
295 static int mem_file_magic;
297 static struct ui_file *
300 struct mem_file *stream = XMALLOC (struct mem_file);
301 struct ui_file *file = ui_file_new ();
302 set_ui_file_data (file, stream, mem_file_delete);
303 set_ui_file_rewind (file, mem_file_rewind);
304 set_ui_file_put (file, mem_file_put);
305 set_ui_file_write (file, mem_file_write);
306 stream->magic = &mem_file_magic;
307 stream->buffer = NULL;
308 stream->sizeof_buffer = 0;
309 stream->length_buffer = 0;
314 mem_file_delete (struct ui_file *file)
316 struct mem_file *stream = ui_file_data (file);
317 if (stream->magic != &mem_file_magic)
318 internal_error (__FILE__, __LINE__,
319 "mem_file_delete: bad magic number");
320 if (stream->buffer != NULL)
321 xfree (stream->buffer);
328 return mem_file_new ();
332 mem_file_rewind (struct ui_file *file)
334 struct mem_file *stream = ui_file_data (file);
335 if (stream->magic != &mem_file_magic)
336 internal_error (__FILE__, __LINE__,
337 "mem_file_rewind: bad magic number");
338 stream->length_buffer = 0;
342 mem_file_put (struct ui_file *file,
343 ui_file_put_method_ftype *write,
346 struct mem_file *stream = ui_file_data (file);
347 if (stream->magic != &mem_file_magic)
348 internal_error (__FILE__, __LINE__,
349 "mem_file_put: bad magic number");
350 if (stream->length_buffer > 0)
351 write (dest, stream->buffer, stream->length_buffer);
355 mem_file_write (struct ui_file *file,
359 struct mem_file *stream = ui_file_data (file);
360 if (stream->magic != &mem_file_magic)
361 internal_error (__FILE__, __LINE__,
362 "mem_file_write: bad magic number");
363 if (stream->buffer == NULL)
365 stream->length_buffer = length_buffer;
366 stream->sizeof_buffer = length_buffer;
367 stream->buffer = xmalloc (stream->sizeof_buffer);
368 memcpy (stream->buffer, buffer, length_buffer);
372 int new_length = stream->length_buffer + length_buffer;
373 if (new_length >= stream->sizeof_buffer)
375 stream->sizeof_buffer = new_length;
376 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
378 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
379 stream->length_buffer = new_length;
383 /* ``struct ui_file'' implementation that maps directly onto
386 static ui_file_write_ftype stdio_file_write;
387 static ui_file_fputs_ftype stdio_file_fputs;
388 static ui_file_isatty_ftype stdio_file_isatty;
389 static ui_file_delete_ftype stdio_file_delete;
390 static struct ui_file *stdio_file_new (FILE * file, int close_p);
391 static ui_file_flush_ftype stdio_file_flush;
393 static int stdio_file_magic;
402 static struct ui_file *
403 stdio_file_new (FILE *file, int close_p)
405 struct ui_file *ui_file = ui_file_new ();
406 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
407 stdio->magic = &stdio_file_magic;
409 stdio->close_p = close_p;
410 set_ui_file_data (ui_file, stdio, stdio_file_delete);
411 set_ui_file_flush (ui_file, stdio_file_flush);
412 set_ui_file_write (ui_file, stdio_file_write);
413 set_ui_file_fputs (ui_file, stdio_file_fputs);
414 set_ui_file_isatty (ui_file, stdio_file_isatty);
419 stdio_file_delete (struct ui_file *file)
421 struct stdio_file *stdio = ui_file_data (file);
422 if (stdio->magic != &stdio_file_magic)
423 internal_error (__FILE__, __LINE__,
424 "stdio_file_delete: bad magic number");
427 fclose (stdio->file);
433 stdio_file_flush (struct ui_file *file)
435 struct stdio_file *stdio = ui_file_data (file);
436 if (stdio->magic != &stdio_file_magic)
437 internal_error (__FILE__, __LINE__,
438 "stdio_file_flush: bad magic number");
439 fflush (stdio->file);
443 stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
445 struct stdio_file *stdio = ui_file_data (file);
446 if (stdio->magic != &stdio_file_magic)
447 internal_error (__FILE__, __LINE__,
448 "stdio_file_write: bad magic number");
449 fwrite (buf, length_buf, 1, stdio->file);
453 stdio_file_fputs (const char *linebuffer, struct ui_file *file)
455 struct stdio_file *stdio = ui_file_data (file);
456 if (stdio->magic != &stdio_file_magic)
457 internal_error (__FILE__, __LINE__,
458 "stdio_file_fputs: bad magic number");
459 fputs (linebuffer, stdio->file);
463 stdio_file_isatty (struct ui_file *file)
465 struct stdio_file *stdio = ui_file_data (file);
466 if (stdio->magic != &stdio_file_magic)
467 internal_error (__FILE__, __LINE__,
468 "stdio_file_isatty: bad magic number");
469 return (isatty (fileno (stdio->file)));
472 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
475 stdio_fileopen (FILE *file)
477 return stdio_file_new (file, 0);
481 gdb_fopen (char *name, char *mode)
483 FILE *f = fopen (name, mode);
486 return stdio_file_new (f, 1);