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