fdiostream.h

Go to the documentation of this file.
00001 // -*- mode: c++; c-basic-offset:4 -*-
00002 
00003 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00004 // Access Protocol.
00005 
00006 // Copyright (c) 2009 OPeNDAP, Inc.
00007 // Author: James Gallagher <jgallagher@opendap.org>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 //
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00024 //
00025 // Portions of this code were taken verbatim from  Josuttis,
00026 // "The C++ Standard Library," p.672
00027 
00028 #ifndef _fdiostream_h
00029 #define _fdiostream_h
00030 
00031 #ifdef HAVE_UNISTD_H
00032 #include <unistd.h>
00033 #endif
00034 
00035 #include <iostream>
00036 #include <streambuf>
00037 #include <algorithm>
00038 #include <cstdio>
00039 
00040 namespace libdap {
00041 
00050 class fdoutbuf : public std::streambuf {
00051 protected:
00052     int fd;     // file descriptor
00053     bool close;
00054     static const int bufferSize = 4096; // Size of the data buffer
00055     char buffer[bufferSize];    // data buffer
00056 
00057 public:
00058     fdoutbuf(int _fd, bool _close);
00059     virtual ~fdoutbuf();
00060 
00061 protected:
00062     int flushBuffer();
00063 
00064     virtual int overflow(int c);
00065     virtual int sync();
00066     virtual std::streamsize xsputn(const char *s, std::streamsize num);
00067 };
00068 
00077 class fdostream : public std::ostream {
00078 protected:
00079     fdoutbuf buf;
00080 public:
00087     fdostream (int _fd, bool _close = false)
00088         : std::ostream(&buf), buf(_fd, _close) { }
00089 };
00090 
00099 class fdinbuf : public std::streambuf {
00100 protected:
00101     int fd;     // file descriptor
00102     bool close;
00103     static const int bufferSize = 4096; // Size of the data buffer
00104     static const int putBack = 128;
00105     char buffer[bufferSize];    // data buffer
00106 
00107 public:
00108     fdinbuf(int _fd, bool close);
00109     virtual ~fdinbuf();
00110 
00111 protected:
00112     virtual int underflow();
00113 };
00114 
00124 class fdistream : public std::istream {
00125 protected:
00126     fdinbuf buf;
00127 public:
00128     fdistream (int fd, bool close = false)
00129         : std::istream(&buf), buf(fd, close) { }
00130 };
00131 
00140 class fpinbuf : public std::streambuf {
00141 protected:
00142     FILE *fp;   // FILE *
00143     bool close;
00144     static const int bufferSize = 4096; // Size of the data buffer
00145     static const int putBack = 128;
00146     char buffer[bufferSize];    // data buffer
00147 
00148 public:
00149     fpinbuf(FILE *_fp, bool _close);
00150     virtual ~fpinbuf();
00151 
00152 protected:
00153     virtual int underflow();
00154 };
00155 
00166 class fpistream : public std::istream {
00167 protected:
00168     fpinbuf buf;
00169 public:
00170     fpistream (FILE *fp, bool close = false)
00171         : std::istream(&buf), buf(fp, close) { }
00172 };
00173 
00174 }
00175 
00176 #endif

Generated on Wed Feb 10 16:08:02 2010 for libdap++ by  doxygen 1.4.7