Mercurial > hg > octave-lyh
changeset 3612:fcdf0897e705
[project @ 2000-03-17 09:49:14 by jwe]
author | jwe |
---|---|
date | Fri, 17 Mar 2000 09:49:15 +0000 |
parents | b1ff6597576f |
children | 0a93682f89c8 |
files | src/c_file_ptr_stream.cc src/c_file_ptr_stream.h |
diffstat | 2 files changed, 230 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/src/c_file_ptr_stream.cc @@ -0,0 +1,126 @@ +/* + +Copyright (C) 2000 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave 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 General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#if defined (__GNUG__) +#pragma implementation +#endif + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "c_file_ptr_stream.h" + +#ifndef SEEK_SET +#define SEEK_SET 0 +#endif + +#ifndef SEEK_CUR +#define SEEK_CUR 1 +#endif + +#ifndef SEEK_END +#define SEEK_END 2 +#endif + +c_file_ptr_buf::~c_file_ptr_buf (void) { fflush (f); } + +// XXX FIXME XXX -- I'm sure there is room for improvement here... + +int +c_file_ptr_buf::overflow (int c) +{ + return (c != EOF) ? fputc (c, f) : fflush (f); +} + +int +c_file_ptr_buf::underflow (void) +{ + return fgetc (f); +} + +int +c_file_ptr_buf::uflow (void) +{ + return underflow (); +} + +int +c_file_ptr_buf::pbackfail (int c) +{ + return (c != EOF) ? ungetc (c, f) : EOF; +} + +std::streamsize +c_file_ptr_buf::xsputn (const char* s, std::streamsize n) +{ + return fwrite (s, 1, n, f); +} + +std::streamsize +c_file_ptr_buf::xsgetn (char *s, std::streamsize n) +{ + return fread (s, 1, n, f); +} + +static inline int +seekdir_to_whence (std::ios::seekdir dir) +{ + return ((dir == ios::beg) ? SEEK_SET : + (dir == ios::cur) ? SEEK_CUR : + (dir == ios::end) ? SEEK_END : + dir); +} + +std::streampos +c_file_ptr_buf::seekoff (std::streamoff offset, std::ios::seekdir dir, + std::ios::openmode) +{ + // XXX FIXME XXX -- is this the right thing to do? + + fseek (f, offset, seekdir_to_whence (dir)); + + return ftell (f); +} + +std::streampos +c_file_ptr_buf::seekpos (std::streampos offset, std::ios::openmode) +{ + // XXX FIXME XXX -- is this the right thing to do? + + fseek (f, offset, SEEK_SET); + + return ftell (f); +} + +int +c_file_ptr_buf::sync (void) +{ + return 0; +} + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/ +
new file mode 100644 --- /dev/null +++ b/src/c_file_ptr_stream.h @@ -0,0 +1,104 @@ +/* + +Copyright (C) 2000 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave 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 General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#if !defined (octave_c_file_ptr_stream_h) +#define octave_c_file_ptr_stream_h 1 + +#ifdef __GNUG__ +#pragma interface +#endif + +#include <iostream.h> +#include <stdio.h> + +class +c_file_ptr_buf : public streambuf +{ +protected: + + FILE *f; + +public: + + FILE* stdiofile (void) const { return f; } + + c_file_ptr_buf (FILE *f_arg) : streambuf (), f (f_arg) { } + + ~c_file_ptr_buf (void); + + int overflow (int); + + int underflow (void); + + int uflow (void); + + int pbackfail (int); + + std::streamsize xsputn (const char*, std::streamsize); + + std::streamsize xsgetn (char *, std::streamsize); + + std::streampos seekoff (std::streamoff, std::ios::seekdir, + std::ios::openmode = ios::in | ios::out); + + std::streampos seekpos (std::streampos, + std::ios::openmode = ios::in | ios::out); + + int sync (void); +}; + +class +i_c_file_ptr_stream : public istream +{ +private: + + c_file_ptr_buf f; + +public: + + i_c_file_ptr_stream (FILE* f_arg) : istream (), f (f_arg) { init (&f); } + + c_file_ptr_buf *rdbuf (void) { return &f; } +}; + +class +o_c_file_ptr_stream : public ostream +{ +private: + + c_file_ptr_buf f; + +public: + + o_c_file_ptr_stream (FILE* f_arg) : ostream (), f (f_arg) { init (&f); } + + c_file_ptr_buf *rdbuf (void) { return &f; } +}; + +#endif + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/ +