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"
33 sg_gzifstream::sg_gzifstream()
38 //-----------------------------------------------------------------------------
40 // Open a possibly gzipped file for reading.
42 sg_gzifstream::sg_gzifstream( const std::string& name, ios_openmode io_mode )
45 this->open( name, io_mode );
48 //-----------------------------------------------------------------------------
50 // Attach a stream to an already opened file descriptor.
52 sg_gzifstream::sg_gzifstream( int fd, ios_openmode io_mode )
55 gzbuf.attach( fd, io_mode );
58 //-----------------------------------------------------------------------------
60 // Open a possibly gzipped file for reading.
61 // If the initial open fails and the filename has a ".gz" extension then
62 // remove the extension and try again.
63 // If the initial open fails and the filename doesn't have a ".gz" extension
64 // then append ".gz" and try again.
67 sg_gzifstream::open( const std::string& name, ios_openmode io_mode )
69 gzbuf.open( name.c_str(), io_mode );
70 if ( ! gzbuf.is_open() )
73 if ( s.substr( s.length() - 3, 3 ) == ".gz" )
75 // remove ".gz" suffix
76 s.replace( s.length() - 3, 3, "" );
77 // s.erase( s.length() - 3, 3 );
81 // Append ".gz" suffix
86 gzbuf.open( s.c_str(), io_mode );
91 sg_gzifstream::attach( int fd, ios_openmode io_mode )
93 gzbuf.attach( fd, io_mode );
101 skipeol( istream& in )
105 // make sure we detect LF, CR and CR/LF
106 while ( in.get(c) ) {
109 else if (c == '\r') {
110 if (in.peek() == '\n')
119 skipws( istream& in ) {
121 while ( in.get(c) ) {
123 if ( ! isspace( c ) ) {
124 // put pack the non-space character
133 skipcomment( istream& in )
141 if ( in.get( c ) && c != '#' )
153 sg_gzofstream::sg_gzofstream()
158 //-----------------------------------------------------------------------------
160 // Open a file for gzipped writing.
162 sg_gzofstream::sg_gzofstream( const std::string& name, ios_openmode io_mode )
165 this->open( name, io_mode );
168 //-----------------------------------------------------------------------------
170 // Attach a stream to an already opened file descriptor.
172 sg_gzofstream::sg_gzofstream( int fd, ios_openmode io_mode )
175 gzbuf.attach( fd, io_mode );
178 //-----------------------------------------------------------------------------
180 // Open a file for gzipped writing.
183 sg_gzofstream::open( const std::string& name, ios_openmode io_mode )
185 gzbuf.open( name.c_str(), io_mode );
189 sg_gzofstream::attach( int fd, ios_openmode io_mode )
191 gzbuf.attach( fd, io_mode );