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"
30 #include <simgear/misc/sg_path.hxx>
35 sg_gzifstream::sg_gzifstream()
40 //-----------------------------------------------------------------------------
42 // Open a possibly gzipped file for reading.
44 sg_gzifstream::sg_gzifstream( const SGPath& name, ios_openmode io_mode )
47 this->open( name, io_mode );
50 //-----------------------------------------------------------------------------
52 // Attach a stream to an already opened file descriptor.
54 sg_gzifstream::sg_gzifstream( int fd, ios_openmode io_mode )
57 gzbuf.attach( fd, io_mode );
60 //-----------------------------------------------------------------------------
62 // Open a possibly gzipped file for reading.
63 // If the initial open fails and the filename has a ".gz" extension then
64 // remove the extension and try again.
65 // If the initial open fails and the filename doesn't have a ".gz" extension
66 // then append ".gz" and try again.
69 sg_gzifstream::open( const SGPath& name, ios_openmode io_mode )
71 std::string s = name.local8BitStr();
73 gzbuf.open( s.c_str(), io_mode );
74 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 )
108 // make sure we detect LF, CR and CR/LF
109 while ( in.get(c) ) {
112 else if (c == '\r') {
113 if (in.peek() == '\n')
122 skipws( istream& in ) {
124 while ( in.get(c) ) {
126 if ( ! isspace( c ) ) {
127 // put pack the non-space character
136 skipcomment( istream& in )
144 if ( in.get( c ) && c != '#' )
156 sg_gzofstream::sg_gzofstream()
161 //-----------------------------------------------------------------------------
163 // Open a file for gzipped writing.
165 sg_gzofstream::sg_gzofstream( const SGPath& name, ios_openmode io_mode )
168 this->open( name, io_mode );
171 //-----------------------------------------------------------------------------
173 // Attach a stream to an already opened file descriptor.
175 sg_gzofstream::sg_gzofstream( int fd, ios_openmode io_mode )
178 gzbuf.attach( fd, io_mode );
181 //-----------------------------------------------------------------------------
183 // Open a file for gzipped writing.
186 sg_gzofstream::open( const SGPath& name, ios_openmode io_mode )
188 std::string ps = name.local8BitStr();
189 gzbuf.open( ps.c_str(), io_mode );
193 sg_gzofstream::attach( int fd, ios_openmode io_mode )
195 gzbuf.attach( fd, io_mode );
199 sg_ifstream::sg_ifstream(const SGPath& path, ios_openmode io_mode)
201 std::string ps = path.local8BitStr();
202 std::ifstream::open(ps.c_str(), io_mode);
205 void sg_ifstream::open( const SGPath& name, ios_openmode io_mode )
207 std::string ps = name.local8BitStr();
208 std::ifstream::open(ps.c_str(), io_mode);
211 sg_ofstream::sg_ofstream(const SGPath& path, ios_openmode io_mode)
213 std::string ps = path.local8BitStr();
214 std::ofstream::open(ps.c_str(), io_mode);
217 void sg_ofstream::open( const SGPath& name, ios_openmode io_mode )
219 std::string ps = name.local8BitStr();
220 std::ofstream::open(ps.c_str(), io_mode);