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 General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <simgear/compiler.h>
26 #include <ctype.h> // isspace()
28 #ifdef SG_HAVE_STD_INCLUDES
34 #include "sgstream.hxx"
36 sg_gzifstream::sg_gzifstream()
41 //-----------------------------------------------------------------------------
43 // Open a possibly gzipped file for reading.
45 sg_gzifstream::sg_gzifstream( const string& name, ios_openmode io_mode )
48 this->open( name, io_mode );
51 //-----------------------------------------------------------------------------
53 // Attach a stream to an already opened file descriptor.
55 sg_gzifstream::sg_gzifstream( int fd, ios_openmode io_mode )
58 gzbuf.attach( fd, io_mode );
61 //-----------------------------------------------------------------------------
63 // Open a possibly gzipped file for reading.
64 // If the initial open fails and the filename has a ".gz" extension then
65 // remove the extension and try again.
66 // If the initial open fails and the filename doesn't have a ".gz" extension
67 // then append ".gz" and try again.
70 sg_gzifstream::open( const string& name, ios_openmode io_mode )
72 gzbuf.open( name.c_str(), io_mode );
73 if ( ! gzbuf.is_open() )
76 if ( s.substr( s.length() - 3, 3 ) == ".gz" )
78 // remove ".gz" suffix
79 s.replace( s.length() - 3, 3, "" );
80 // s.erase( s.length() - 3, 3 );
84 // Append ".gz" suffix
89 gzbuf.open( s.c_str(), io_mode );
94 sg_gzifstream::attach( int fd, ios_openmode io_mode )
96 gzbuf.attach( fd, io_mode );
104 skipeol( istream& in )
107 // skip to end of line.
110 while ( in.get(c) && c != '\0' ) {
112 while ( in.get(c) ) {
114 if ( (c == '\n') || (c == '\r') ) {
123 skipws( istream& in ) {
126 while ( in.get(c) && c != '\0' ) {
128 while ( in.get(c) ) {
132 if ( ! isspace( c ) && c != '\n' ) {
134 if ( ! isspace( c ) ) {
136 // put pack the non-space character
145 skipcomment( istream& in )
157 if ( in.get( c ) && c != '#' )