head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.40; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.58; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.04; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.22.01; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# sgstream.cxx

    00001 // zlib input file stream wrapper.
    00002 //
    00003 // Written by Bernie Bright, 1998
    00004 //
    00005 // Copyright (C) 1998  Bernie Bright - bbright@@c031.aone.net.au
    00006 //
    00007 // This library is free software; you can redistribute it and/or
    00008 // modify it under the terms of the GNU Library General Public
    00009 // License as published by the Free Software Foundation; either
    00010 // version 2 of the License, or (at your option) any later version.
    00011 //
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015 // Library General Public License for more details.
    00016 //
    00017 // You should have received a copy of the GNU Library General Public
    00018 // License along with this library; if not, write to the
    00019 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00020 // Boston, MA  02111-1307, USA.
    00021 //
    00022 // $Id: sgstream.cxx,v 1.1 2001/03/25 12:06:10 curt Exp $
    00023
    00024 #include <ctype.h> // isspace()
    00025
    00026 #ifdef SG_HAVE_STD_INCLUDES
    00027 # include <cerrno>
    00028 #else
    00029 # include <errno.h>
    00030 #endif
    00031
    00032 #include "sgstream.hxx"
    00033
    00034 sg_gzifstream::sg_gzifstream()
    00035     : istream(&gzbuf)
    00036 {
    00037 }
    00038
    00039 //-----------------------------------------------------------------------------
    00040 //
    00041 // Open a possibly gzipped file for reading.
    00042 //
    00043 sg_gzifstream::sg_gzifstream( const string& name, ios_openmode io_mode )
    00044     : istream(&gzbuf)
    00045 {
    00046     this->open( name, io_mode );
    00047 }
    00048
    00049 //-----------------------------------------------------------------------------
    00050 //
    00051 // Attach a stream to an already opened file descriptor.
    00052 //
    00053 sg_gzifstream::sg_gzifstream( int fd, ios_openmode io_mode )
    00054     : istream(&gzbuf)
    00055 {
    00056     gzbuf.attach( fd, io_mode );
    00057 }
    00058
    00059 //-----------------------------------------------------------------------------
    00060 //
    00061 // Open a possibly gzipped file for reading.
    00062 // If the initial open fails and the filename has a ".gz" extension then
    00063 // remove the extension and try again.
    00064 // If the initial open fails and the filename doesn't have a ".gz" extension
    00065 // then append ".gz" and try again.
    00066 //
    00067 void
    00068 sg_gzifstream::open( const string& name, ios_openmode io_mode )
    00069 {
    00070     gzbuf.open( name.c_str(), io_mode );
    00071     if ( ! gzbuf.is_open() )
    00072     {
    00073         string s = name;
    00074         if ( s.substr( s.length() - 3, 3 ) == ".gz" )
    00075         {
    00076             // remove ".gz" suffix
    00077             s.replace( s.length() - 3, 3, "" );
    00078 //          s.erase( s.length() - 3, 3 );
    00079         }
    00080         else
    00081         {
    00082             // Append ".gz" suffix
    00083             s += ".gz";
    00084         }
    00085
    00086         // Try again.
    00087         gzbuf.open( s.c_str(), io_mode );
    00088     }
    00089 }
    00090
    00091 void
    00092 sg_gzifstream::attach( int fd, ios_openmode io_mode )
    00093 {
    00094     gzbuf.attach( fd, io_mode );
    00095 }
    00096
    00097 //
    00098 // Manipulators
    00099 //
    00100
    00101 istream&
    00102 skipeol( istream& in )
    00103 {
    00104     char c = '\0';
    00105     // skip to end of line.
    00106
    00107 #ifdef __MWERKS__
    00108     while ( in.get(c) && c != '\0' ) {
    00109 #else
    00110     while ( in.get(c) ) {
    00111 #endif
    00112         if ( (c == '\n') || (c == '\r') ) {
    00113             break;
    00114         }
    00115     }
    00116
    00117     return in;
    00118 }
    00119
    00120 istream&
    00121 skipws( istream& in ) {
    00122     char c;
    00123 #ifdef __MWERKS__
    00124     while ( in.get(c) && c != '\0' ) {
    00125 #else
    00126     while ( in.get(c) ) {
    00127 #endif
    00128
    00129 #ifdef __MWERKS__
    00130         if ( ! isspace( c ) && c != '\n' ) {
    00131 #else
    00132         if ( ! isspace( c ) ) {
    00133 #endif
    00134             // put pack the non-space character
    00135             in.putback(c);
    00136             break;
    00137         }
    00138     }
    00139     return in;
    00140 }
    00141
    00142 istream&
    00143 skipcomment( istream& in )
    00144 {
    00145     while ( in )
    00146     {
    00147         // skip whitespace
    00148 #ifdef __MWERKS__
    00149         in >> ::skipws;
    00150 #else
    00151         in >> skipws;
    00152 #endif
    00153
    00154         char c;
    00155         if ( in.get( c ) && c != '#' )
    00156         {
    00157             // not a comment
    00158             in.putback(c);
    00159             break;
    00160         }
    00161         in >> skipeol;
    00162     }
    00163     return in;
    00164 }
    00165

@ 1.3 log @Oops, try again. @ text @d174 1 a174 1