1 /* UI_FILE - a generic STDIO like output stream.
2 Copyright (C) 1999, 2000 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. */
27 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
29 static ui_file_isatty_ftype null_file_isatty;
30 static ui_file_write_ftype null_file_write;
31 static ui_file_fputs_ftype null_file_fputs;
32 static ui_file_flush_ftype null_file_flush;
33 static ui_file_delete_ftype null_file_delete;
34 static ui_file_rewind_ftype null_file_rewind;
35 static ui_file_put_ftype null_file_put;
40 ui_file_flush_ftype *to_flush;
41 ui_file_write_ftype *to_write;
42 ui_file_fputs_ftype *to_fputs;
43 ui_file_delete_ftype *to_delete;
44 ui_file_isatty_ftype *to_isatty;
45 ui_file_rewind_ftype *to_rewind;
46 ui_file_put_ftype *to_put;
54 struct ui_file *file = xmalloc (sizeof (struct ui_file));
55 file->magic = &ui_file_magic;
56 set_ui_file_data (file, NULL, null_file_delete);
57 set_ui_file_flush (file, null_file_flush);
58 set_ui_file_write (file, null_file_write);
59 set_ui_file_fputs (file, null_file_fputs);
60 set_ui_file_isatty (file, null_file_isatty);
61 set_ui_file_rewind (file, null_file_rewind);
62 set_ui_file_put (file, null_file_put);
70 file->to_delete (file);
75 null_file_isatty (file)
82 null_file_rewind (file)
89 null_file_put (struct ui_file *file,
90 ui_file_put_method_ftype *write,
97 null_file_flush (file)
104 null_file_write (struct ui_file *file,
108 if (file->to_fputs == null_file_fputs)
109 /* Both the write and fputs methods are null. Discard the
114 /* The fputs method isn't null, slowly pass the write request
115 onto that. FYI, this isn't as bad as it may look - the
116 current (as of 1999-11-07) printf_* function calls fputc and
117 fputc does exactly the below. By having a write function it
118 is possible to clean up that code. */
122 for (i = 0; i < sizeof_buf; i++)
125 file->to_fputs (b, file);
132 null_file_fputs (buf, file)
134 struct ui_file *file;
136 if (file->to_write == null_file_write)
137 /* Both the write and fputs methods are null. Discard the
142 /* The write method was implemented, use that. */
143 file->to_write (file, buf, strlen (buf));
148 null_file_delete (file)
149 struct ui_file *file;
156 struct ui_file *file;
158 if (file->magic != &ui_file_magic)
159 internal_error ("ui_file_data: bad magic number");
160 return file->to_data;
165 struct ui_file *file;
167 file->to_flush (file);
171 ui_file_isatty (file)
172 struct ui_file *file;
174 return file->to_isatty (file);
178 ui_file_rewind (file)
179 struct ui_file *file;
181 file->to_rewind (file);
185 ui_file_put (struct ui_file *file,
186 ui_file_put_method_ftype *write,
189 file->to_put (file, write, dest);
193 ui_file_write (struct ui_file *file,
197 file->to_write (file, buf, length_buf);
201 fputs_unfiltered (buf, file)
203 struct ui_file *file;
205 file->to_fputs (buf, file);
209 set_ui_file_flush (file, flush)
210 struct ui_file *file;
211 ui_file_flush_ftype *flush;
213 file->to_flush = flush;
217 set_ui_file_isatty (file, isatty)
218 struct ui_file *file;
219 ui_file_isatty_ftype *isatty;
221 file->to_isatty = isatty;
225 set_ui_file_rewind (file, rewind)
226 struct ui_file *file;
227 ui_file_rewind_ftype *rewind;
229 file->to_rewind = rewind;
233 set_ui_file_put (file, put)
234 struct ui_file *file;
235 ui_file_put_ftype *put;
241 set_ui_file_write (struct ui_file *file,
242 ui_file_write_ftype *write)
244 file->to_write = write;
248 set_ui_file_fputs (file, fputs)
249 struct ui_file *file;
250 ui_file_fputs_ftype *fputs;
252 file->to_fputs = fputs;
256 set_ui_file_data (file, data, delete)
257 struct ui_file *file;
259 ui_file_delete_ftype *delete;
261 file->to_data = data;
262 file->to_delete = delete;
265 /* ui_file utility function for converting a ``struct ui_file'' into
266 a memory buffer''. */
268 struct accumulated_ui_file
275 do_ui_file_xstrdup (void *context, const char *buffer, long length)
277 struct accumulated_ui_file *acc = context;
278 if (acc->buffer == NULL)
279 acc->buffer = xmalloc (length + 1);
281 acc->buffer = xrealloc (acc->buffer, acc->length + length + 1);
282 memcpy (acc->buffer + acc->length, buffer, length);
283 acc->length += length;
284 acc->buffer[acc->length] = '\0';
288 ui_file_xstrdup (struct ui_file *file,
291 struct accumulated_ui_file acc;
294 ui_file_put (file, do_ui_file_xstrdup, &acc);
295 if (acc.buffer == NULL)
296 acc.buffer = xstrdup ("");
297 *length = acc.length;
301 /* A pure memory based ``struct ui_file'' that can be used an output
302 buffer. The buffers accumulated contents are available via
313 static ui_file_rewind_ftype mem_file_rewind;
314 static ui_file_put_ftype mem_file_put;
315 static ui_file_write_ftype mem_file_write;
316 static ui_file_delete_ftype mem_file_delete;
317 static struct ui_file *mem_file_new PARAMS ((void));
318 static int mem_file_magic;
320 static struct ui_file *
323 struct mem_file *stream = XMALLOC (struct mem_file);
324 struct ui_file *file = ui_file_new ();
325 set_ui_file_data (file, stream, mem_file_delete);
326 set_ui_file_rewind (file, mem_file_rewind);
327 set_ui_file_put (file, mem_file_put);
328 set_ui_file_write (file, mem_file_write);
329 stream->magic = &mem_file_magic;
330 stream->buffer = NULL;
331 stream->sizeof_buffer = 0;
332 stream->length_buffer = 0;
337 mem_file_delete (struct ui_file *file)
339 struct mem_file *stream = ui_file_data (file);
340 if (stream->magic != &mem_file_magic)
341 internal_error ("mem_file_delete: bad magic number");
342 if (stream->buffer != NULL)
343 free (stream->buffer);
350 return mem_file_new ();
354 mem_file_rewind (struct ui_file *file)
356 struct mem_file *stream = ui_file_data (file);
357 if (stream->magic != &mem_file_magic)
358 internal_error ("mem_file_rewind: bad magic number");
359 stream->length_buffer = 0;
363 mem_file_put (struct ui_file *file,
364 ui_file_put_method_ftype *write,
367 struct mem_file *stream = ui_file_data (file);
368 if (stream->magic != &mem_file_magic)
369 internal_error ("mem_file_put: bad magic number");
370 if (stream->length_buffer > 0)
371 write (dest, stream->buffer, stream->length_buffer);
375 mem_file_write (struct ui_file *file,
379 struct mem_file *stream = ui_file_data (file);
380 if (stream->magic != &mem_file_magic)
381 internal_error ("mem_file_write: bad magic number");
382 if (stream->buffer == NULL)
384 stream->length_buffer = length_buffer;
385 stream->sizeof_buffer = length_buffer;
386 stream->buffer = xmalloc (stream->sizeof_buffer);
387 memcpy (stream->buffer, buffer, length_buffer);
391 int new_length = stream->length_buffer + length_buffer;
392 if (new_length >= stream->sizeof_buffer)
394 stream->sizeof_buffer = new_length;
395 stream->buffer = xrealloc (stream->buffer, stream->sizeof_buffer);
397 memcpy (stream->buffer + stream->length_buffer, buffer, length_buffer);
398 stream->length_buffer = new_length;
402 /* ``struct ui_file'' implementation that maps directly onto
405 static ui_file_write_ftype stdio_file_write;
406 static ui_file_fputs_ftype stdio_file_fputs;
407 static ui_file_isatty_ftype stdio_file_isatty;
408 static ui_file_delete_ftype stdio_file_delete;
409 static struct ui_file *stdio_file_new PARAMS ((FILE * file, int close_p));
410 static ui_file_flush_ftype stdio_file_flush;
412 static int stdio_file_magic;
421 static struct ui_file *
422 stdio_file_new (file, close_p)
426 struct ui_file *ui_file = ui_file_new ();
427 struct stdio_file *stdio = xmalloc (sizeof (struct stdio_file));
428 stdio->magic = &stdio_file_magic;
430 stdio->close_p = close_p;
431 set_ui_file_data (ui_file, stdio, stdio_file_delete);
432 set_ui_file_flush (ui_file, stdio_file_flush);
433 set_ui_file_write (ui_file, stdio_file_write);
434 set_ui_file_fputs (ui_file, stdio_file_fputs);
435 set_ui_file_isatty (ui_file, stdio_file_isatty);
440 stdio_file_delete (file)
441 struct ui_file *file;
443 struct stdio_file *stdio = ui_file_data (file);
444 if (stdio->magic != &stdio_file_magic)
445 internal_error ("stdio_file_delete: bad magic number");
448 fclose (stdio->file);
454 stdio_file_flush (file)
455 struct ui_file *file;
457 struct stdio_file *stdio = ui_file_data (file);
458 if (stdio->magic != &stdio_file_magic)
459 internal_error ("stdio_file_flush: bad magic number");
460 fflush (stdio->file);
464 stdio_file_write (struct ui_file *file, const char *buf, long length_buf)
466 struct stdio_file *stdio = ui_file_data (file);
467 if (stdio->magic != &stdio_file_magic)
468 internal_error ("stdio_file_write: bad magic number");
469 fwrite (buf, length_buf, 1, stdio->file);
473 stdio_file_fputs (linebuffer, file)
474 const char *linebuffer;
475 struct ui_file *file;
477 struct stdio_file *stdio = ui_file_data (file);
478 if (stdio->magic != &stdio_file_magic)
479 internal_error ("stdio_file_fputs: bad magic number");
480 fputs (linebuffer, stdio->file);
484 stdio_file_isatty (file)
485 struct ui_file *file;
487 struct stdio_file *stdio = ui_file_data (file);
488 if (stdio->magic != &stdio_file_magic)
489 internal_error ("stdio_file_isatty: bad magic number");
490 return (isatty (fileno (stdio->file)));
493 /* Like fdopen(). Create a ui_file from a previously opened FILE. */
496 stdio_fileopen (file)
499 return stdio_file_new (file, 0);
503 gdb_fopen (name, mode)
507 FILE *f = fopen (name, mode);
510 return stdio_file_new (f, 1);