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