]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sgstream.cxx
HTTP: Rename urlretrieve/urlload to save/load.
[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
106     // make sure we detect LF, CR and CR/LF
107     while ( in.get(c) ) {
108         if (c == '\n')
109             break;
110         else if (c == '\r') {
111             if (in.peek() == '\n')
112                 in.get(c);
113             break;
114         }
115     }
116     return in;
117 }
118
119 istream&
120 skipws( istream& in ) {
121     char c;
122     while ( in.get(c) ) {
123
124         if ( ! isspace( c ) ) {
125             // put pack the non-space character
126             in.putback(c);
127             break;
128         }
129     }
130     return in;
131 }
132
133 istream&
134 skipcomment( istream& in )
135 {
136     while ( in )
137     {
138         // skip whitespace
139         in >> skipws;
140
141         char c;
142         if ( in.get( c ) && c != '#' )
143         {
144             // not a comment
145             in.putback(c);
146             break;
147         }
148         in >> skipeol;
149     }
150     return in;
151 }
152
153
154 sg_gzofstream::sg_gzofstream()
155     : ostream(&gzbuf)
156 {
157 }
158
159 //-----------------------------------------------------------------------------
160 //
161 // Open a file for gzipped writing.
162 //
163 sg_gzofstream::sg_gzofstream( const string& name, ios_openmode io_mode )
164     : ostream(&gzbuf)
165 {
166     this->open( name, io_mode );
167 }
168
169 //-----------------------------------------------------------------------------
170 //
171 // Attach a stream to an already opened file descriptor.
172 //
173 sg_gzofstream::sg_gzofstream( int fd, ios_openmode io_mode )
174     : ostream(&gzbuf)
175 {
176     gzbuf.attach( fd, io_mode );
177 }
178
179 //-----------------------------------------------------------------------------
180 //
181 // Open a file for gzipped writing.
182 //
183 void
184 sg_gzofstream::open( const string& name, ios_openmode io_mode )
185 {
186     gzbuf.open( name.c_str(), io_mode );
187 }
188
189 void
190 sg_gzofstream::attach( int fd, ios_openmode io_mode )
191 {
192     gzbuf.attach( fd, io_mode );
193 }