1 // zlib input file stream wrapper.
3 // Written by Bernie Bright, 1998
5 // Copyright (C) 1998 Bernie Bright - bbright@c031.aone.net.au
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
17 // You should have received a copy of the GNU Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA 02111-1307, USA.
24 #include <simgear/compiler.h>
27 #include <ctype.h> // isspace()
29 #ifdef SG_HAVE_STD_INCLUDES
35 #include "sgstream.hxx"
37 sg_gzifstream::sg_gzifstream()
42 //-----------------------------------------------------------------------------
44 // Open a possibly gzipped file for reading.
46 sg_gzifstream::sg_gzifstream( const string& name, ios_openmode io_mode )
49 this->open( name, io_mode );
52 //-----------------------------------------------------------------------------
54 // Attach a stream to an already opened file descriptor.
56 sg_gzifstream::sg_gzifstream( int fd, ios_openmode io_mode )
59 gzbuf.attach( fd, io_mode );
62 //-----------------------------------------------------------------------------
64 // Open a possibly gzipped file for reading.
65 // If the initial open fails and the filename has a ".gz" extension then
66 // remove the extension and try again.
67 // If the initial open fails and the filename doesn't have a ".gz" extension
68 // then append ".gz" and try again.
71 sg_gzifstream::open( const string& name, ios_openmode io_mode )
73 gzbuf.open( name.c_str(), io_mode );
74 if ( ! gzbuf.is_open() )
77 if ( s.substr( s.length() - 3, 3 ) == ".gz" )
79 // remove ".gz" suffix
80 s.replace( s.length() - 3, 3, "" );
81 // s.erase( s.length() - 3, 3 );
85 // Append ".gz" suffix
90 gzbuf.open( s.c_str(), io_mode );
95 sg_gzifstream::attach( int fd, ios_openmode io_mode )
97 gzbuf.attach( fd, io_mode );
105 skipeol( istream& in )
108 // skip to end of line.
111 while ( in.get(c) && c != '\0' ) {
113 while ( in.get(c) ) {
115 if ( (c == '\n') || (c == '\r') ) {
124 skipws( istream& in ) {
127 while ( in.get(c) && c != '\0' ) {
129 while ( in.get(c) ) {
133 if ( ! isspace( c ) && c != '\n' ) {
135 if ( ! isspace( c ) ) {
137 // put pack the non-space character
146 skipcomment( istream& in )
158 if ( in.get( c ) && c != '#' )