]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sgstream.cxx
sgstream: implement gzipped output file stream (gzofstream)
[simgear.git] / simgear / misc / sgstream.cxx
1 // zlib input file stream wrapper.
2 //
3 // Written by Bernie Bright, 1998
4 //
5 // Copyright (C) 1998  Bernie Bright - bbright@c031.aone.net.au
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #include <simgear/compiler.h>
24 #include <string>
25 #include <ctype.h> // isspace()
26 #include <cerrno>
27
28 #include "sgstream.hxx"
29
30 using std::string;
31 using std::istream;
32 using std::ostream;
33
34 sg_gzifstream::sg_gzifstream()
35     : istream(&gzbuf)
36 {
37 }
38
39 //-----------------------------------------------------------------------------
40 //
41 // Open a possibly gzipped file for reading.
42 //
43 sg_gzifstream::sg_gzifstream( const string& name, ios_openmode io_mode )
44     : istream(&gzbuf)
45 {
46     this->open( name, io_mode );
47 }
48
49 //-----------------------------------------------------------------------------
50 //
51 // Attach a stream to an already opened file descriptor.
52 //
53 sg_gzifstream::sg_gzifstream( int fd, ios_openmode io_mode )
54     : istream(&gzbuf)
55 {
56     gzbuf.attach( fd, io_mode );
57 }
58
59 //-----------------------------------------------------------------------------
60 //
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.
66 //
67 void
68 sg_gzifstream::open( const string& name, ios_openmode io_mode )
69 {
70     gzbuf.open( name.c_str(), io_mode );
71     if ( ! gzbuf.is_open() )
72     {
73         string s = name;
74         if ( s.substr( s.length() - 3, 3 ) == ".gz" )
75         {
76             // remove ".gz" suffix
77             s.replace( s.length() - 3, 3, "" );
78 //          s.erase( s.length() - 3, 3 );
79         }
80         else
81         {
82             // Append ".gz" suffix
83             s += ".gz";
84         }
85
86         // Try again.
87         gzbuf.open( s.c_str(), io_mode );
88     }
89 }
90
91 void
92 sg_gzifstream::attach( int fd, ios_openmode io_mode )
93 {
94     gzbuf.attach( fd, io_mode );
95 }
96
97 //
98 // Manipulators
99 //
100
101 istream&
102 skipeol( istream& in )
103 {
104     char c = '\0';
105     // skip to end of line.
106
107     while ( in.get(c) ) {
108         if ( (c == '\n') || (c == '\r') ) {
109             break;
110         }       
111     }
112
113     return in;
114 }
115
116 istream&
117 skipws( istream& in ) {
118     char c;
119     while ( in.get(c) ) {
120
121         if ( ! isspace( c ) ) {
122             // put pack the non-space character
123             in.putback(c);
124             break;
125         }
126     }
127     return in;
128 }
129
130 istream&
131 skipcomment( istream& in )
132 {
133     while ( in )
134     {
135         // skip whitespace
136         in >> skipws;
137
138         char c;
139         if ( in.get( c ) && c != '#' )
140         {
141             // not a comment
142             in.putback(c);
143             break;
144         }
145         in >> skipeol;
146     }
147     return in;
148 }
149
150
151 sg_gzofstream::sg_gzofstream()
152     : ostream(&gzbuf)
153 {
154 }
155
156 //-----------------------------------------------------------------------------
157 //
158 // Open a file for gzipped writing.
159 //
160 sg_gzofstream::sg_gzofstream( const string& name, ios_openmode io_mode )
161     : ostream(&gzbuf)
162 {
163     this->open( name, io_mode );
164 }
165
166 //-----------------------------------------------------------------------------
167 //
168 // Attach a stream to an already opened file descriptor.
169 //
170 sg_gzofstream::sg_gzofstream( int fd, ios_openmode io_mode )
171     : ostream(&gzbuf)
172 {
173     gzbuf.attach( fd, io_mode );
174 }
175
176 //-----------------------------------------------------------------------------
177 //
178 // Open a file for gzipped writing.
179 //
180 void
181 sg_gzofstream::open( const string& name, ios_openmode io_mode )
182 {
183     gzbuf.open( name.c_str(), io_mode );
184 }
185
186 void
187 sg_gzofstream::attach( int fd, ios_openmode io_mode )
188 {
189     gzbuf.attach( fd, io_mode );
190 }