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>
25 #include <ctype.h> // isspace()
28 #include "sgstream.hxx"
34 sg_gzifstream::sg_gzifstream()
39 //-----------------------------------------------------------------------------
41 // Open a possibly gzipped file for reading.
43 sg_gzifstream::sg_gzifstream( const string& name, ios_openmode io_mode )
46 this->open( name, io_mode );
49 //-----------------------------------------------------------------------------
51 // Attach a stream to an already opened file descriptor.
53 sg_gzifstream::sg_gzifstream( int fd, ios_openmode io_mode )
56 gzbuf.attach( fd, io_mode );
59 //-----------------------------------------------------------------------------
61 // Open a possibly gzipped file for reading.
62 // If the initial open fails and the filename has a ".gz" extension then
63 // remove the extension and try again.
64 // If the initial open fails and the filename doesn't have a ".gz" extension
65 // then append ".gz" and try again.
68 sg_gzifstream::open( const string& name, ios_openmode io_mode )
70 gzbuf.open( name.c_str(), io_mode );
71 if ( ! gzbuf.is_open() )
74 if ( s.substr( s.length() - 3, 3 ) == ".gz" )
76 // remove ".gz" suffix
77 s.replace( s.length() - 3, 3, "" );
78 // s.erase( s.length() - 3, 3 );
82 // Append ".gz" suffix
87 gzbuf.open( s.c_str(), io_mode );
92 sg_gzifstream::attach( int fd, ios_openmode io_mode )
94 gzbuf.attach( fd, io_mode );
102 skipeol( istream& in )
105 // skip to end of line.
107 while ( in.get(c) ) {
108 if ( (c == '\n') || (c == '\r') ) {
117 skipws( istream& in ) {
119 while ( in.get(c) ) {
121 if ( ! isspace( c ) ) {
122 // put pack the non-space character
131 skipcomment( istream& in )
139 if ( in.get( c ) && c != '#' )
151 sg_gzofstream::sg_gzofstream()
156 //-----------------------------------------------------------------------------
158 // Open a file for gzipped writing.
160 sg_gzofstream::sg_gzofstream( const string& name, ios_openmode io_mode )
163 this->open( name, io_mode );
166 //-----------------------------------------------------------------------------
168 // Attach a stream to an already opened file descriptor.
170 sg_gzofstream::sg_gzofstream( int fd, ios_openmode io_mode )
173 gzbuf.attach( fd, io_mode );
176 //-----------------------------------------------------------------------------
178 // Open a file for gzipped writing.
181 sg_gzofstream::open( const string& name, ios_openmode io_mode )
183 gzbuf.open( name.c_str(), io_mode );
187 sg_gzofstream::attach( int fd, ios_openmode io_mode )
189 gzbuf.attach( fd, io_mode );