c-file-ptr-stream.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2000-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <iostream>
00028 
00029 #include "c-file-ptr-stream.h"
00030 
00031 #ifndef SEEK_SET
00032 #define SEEK_SET 0
00033 #endif
00034 
00035 #ifndef SEEK_CUR
00036 #define SEEK_CUR 1
00037 #endif
00038 
00039 #ifndef SEEK_END
00040 #define SEEK_END 2
00041 #endif
00042 
00043 c_file_ptr_buf::~c_file_ptr_buf (void)
00044 {
00045   buf_close ();
00046 }
00047 
00048 // FIXME -- I'm sure there is room for improvement here...
00049 
00050 c_file_ptr_buf::int_type
00051 c_file_ptr_buf::overflow (int_type c)
00052 {
00053 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
00054   if (f)
00055     return (c != traits_type::eof ()) ? gnulib::fputc (c, f) : flush ();
00056   else
00057     return traits_type::not_eof (c);
00058 #else
00059   if (f)
00060     return (c != EOF) ? gnulib::fputc (c, f) : flush ();
00061   else
00062     return EOF;
00063 #endif
00064 }
00065 
00066 c_file_ptr_buf::int_type
00067 c_file_ptr_buf::underflow_common (bool bump)
00068 {
00069   if (f)
00070     {
00071       int_type c = gnulib::fgetc (f);
00072 
00073       if (! bump
00074 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
00075           && c != traits_type::eof ())
00076 #else
00077           && c != EOF)
00078 #endif
00079         ungetc (c, f);
00080 
00081       return c;
00082     }
00083   else
00084 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
00085     return traits_type::eof ();
00086 #else
00087     return EOF;
00088 #endif
00089 }
00090 
00091 c_file_ptr_buf::int_type
00092 c_file_ptr_buf::pbackfail (int_type c)
00093 {
00094 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
00095   return (c != traits_type::eof () && f) ? ungetc (c, f) :
00096     traits_type::not_eof (c);
00097 #else
00098   return (c != EOF && f) ? ungetc (c, f) : EOF;
00099 #endif
00100 }
00101 
00102 std::streamsize
00103 c_file_ptr_buf::xsputn (const char* s, std::streamsize n)
00104 {
00105   if (f)
00106     return gnulib::fwrite (s, 1, n, f);
00107   else
00108     return 0;
00109 }
00110 
00111 std::streamsize
00112 c_file_ptr_buf::xsgetn (char *s, std::streamsize n)
00113 {
00114   if (f)
00115     return gnulib::fread (s, 1, n, f);
00116   else
00117     return 0;
00118 }
00119 
00120 static inline int
00121 seekdir_to_whence (std::ios::seekdir dir)
00122 {
00123   return ((dir == std::ios::beg) ? SEEK_SET :
00124           (dir == std::ios::cur) ? SEEK_CUR :
00125           (dir == std::ios::end) ? SEEK_END :
00126           dir);
00127 }
00128 
00129 std::streampos
00130 c_file_ptr_buf::seekoff (std::streamoff /* offset */,
00131                          std::ios::seekdir /* dir */,
00132                          std::ios::openmode)
00133 {
00134   // FIXME
00135 #if 0
00136   if (f)
00137     {
00138       fseek (f, offset, seekdir_to_whence (dir));
00139 
00140       return ftell (f);
00141     }
00142   else
00143     return 0;
00144 #endif
00145   return -1;
00146 }
00147 
00148 std::streampos
00149 c_file_ptr_buf::seekpos (std::streampos /* offset */, std::ios::openmode)
00150 {
00151   // FIXME
00152 #if 0
00153   if (f)
00154     {
00155       fseek (f, offset, SEEK_SET);
00156 
00157       return ftell (f);
00158     }
00159   else
00160     return 0;
00161 #endif
00162   return -1;
00163 }
00164 
00165 int
00166 c_file_ptr_buf::sync (void)
00167 {
00168   flush ();
00169 
00170   return 0;
00171 }
00172 
00173 int
00174 c_file_ptr_buf::flush (void)
00175 {
00176   return f ? gnulib::fflush (f) : EOF;
00177 }
00178 
00179 int
00180 c_file_ptr_buf::buf_close (void)
00181 {
00182   int retval = -1;
00183 
00184   flush ();
00185 
00186   if (f)
00187     {
00188       retval = cf (f);
00189       f = 0;
00190     }
00191 
00192   return retval;
00193 }
00194 
00195 int
00196 c_file_ptr_buf::seek (long offset, int origin)
00197 {
00198   return f ? gnulib::fseek (f, offset, origin) : -1;
00199 }
00200 
00201 long
00202 c_file_ptr_buf::tell (void)
00203 {
00204   return f ? gnulib::ftell (f) : -1;
00205 }
00206 
00207 int
00208 c_file_ptr_buf::file_close (FILE *f)
00209 {
00210   return gnulib::fclose (f);
00211 }
00212 
00213 #ifdef HAVE_ZLIB
00214 
00215 c_zfile_ptr_buf::~c_zfile_ptr_buf (void)
00216 {
00217   buf_close ();
00218 }
00219 
00220 // FIXME -- I'm sure there is room for improvement here...
00221 
00222 c_zfile_ptr_buf::int_type
00223 c_zfile_ptr_buf::overflow (int_type c)
00224 {
00225 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
00226   if (f)
00227     return (c != traits_type::eof ()) ? gzputc (f, c) : flush ();
00228   else
00229     return traits_type::not_eof (c);
00230 #else
00231   if (f)
00232     return (c != EOF) ? gzputc (f, c) : flush ();
00233   else
00234     return EOF;
00235 #endif
00236 }
00237 
00238 c_zfile_ptr_buf::int_type
00239 c_zfile_ptr_buf::underflow_common (bool bump)
00240 {
00241   if (f)
00242     {
00243       int_type c = gzgetc (f);
00244 
00245       if (! bump
00246 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
00247           && c != traits_type::eof ())
00248 #else
00249           && c != EOF)
00250 #endif
00251         gzungetc (c, f);
00252 
00253       return c;
00254     }
00255   else
00256 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
00257     return traits_type::eof ();
00258 #else
00259     return EOF;
00260 #endif
00261 }
00262 
00263 c_zfile_ptr_buf::int_type
00264 c_zfile_ptr_buf::pbackfail (int_type c)
00265 {
00266 #if defined (CXX_ISO_COMPLIANT_LIBRARY)
00267   return (c != traits_type::eof () && f) ? gzungetc (c, f) :
00268     traits_type::not_eof (c);
00269 #else
00270   return (c != EOF && f) ? gzungetc (c, f) : EOF;
00271 #endif
00272 }
00273 
00274 std::streamsize
00275 c_zfile_ptr_buf::xsputn (const char* s, std::streamsize n)
00276 {
00277   if (f)
00278     return gzwrite (f, s, n);
00279   else
00280     return 0;
00281 }
00282 
00283 std::streamsize
00284 c_zfile_ptr_buf::xsgetn (char *s, std::streamsize n)
00285 {
00286   if (f)
00287     return gzread (f, s, n);
00288   else
00289     return 0;
00290 }
00291 
00292 std::streampos
00293 c_zfile_ptr_buf::seekoff (std::streamoff /* offset */,
00294                           std::ios::seekdir /* dir */,
00295                           std::ios::openmode)
00296 {
00297   // FIXME
00298 #if 0
00299   if (f)
00300     {
00301       gzseek (f, offset, seekdir_to_whence (dir));
00302 
00303       return gztell (f);
00304     }
00305   else
00306     return 0;
00307 #endif
00308   return -1;
00309 }
00310 
00311 std::streampos
00312 c_zfile_ptr_buf::seekpos (std::streampos /* offset */, std::ios::openmode)
00313 {
00314   // FIXME
00315 #if 0
00316   if (f)
00317     {
00318       gzseek (f, offset, SEEK_SET);
00319 
00320       return gztell (f);
00321     }
00322   else
00323     return 0;
00324 #endif
00325   return -1;
00326 }
00327 
00328 int
00329 c_zfile_ptr_buf::sync (void)
00330 {
00331   flush ();
00332 
00333   return 0;
00334 }
00335 
00336 int
00337 c_zfile_ptr_buf::flush (void)
00338 {
00339   // FIXME -- do we need something more complex here, passing
00340   // something other than 0 for the second argument to gzflush and
00341   // checking the return value, etc.?
00342 
00343   return f ? gzflush (f, 0) : EOF;
00344 }
00345 
00346 int
00347 c_zfile_ptr_buf::buf_close (void)
00348 {
00349   int retval = -1;
00350 
00351   flush ();
00352 
00353   if (f)
00354     {
00355       retval = cf (f);
00356       f = 0;
00357     }
00358 
00359   return retval;
00360 }
00361 
00362 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines