]> git.mxchange.org Git - simgear.git/blob - simgear/misc/fgstream.cxx
d3a58f0d89b116b1e979671e5bc58f9f7060f760
[simgear.git] / simgear / misc / fgstream.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 Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA  02111-1307, USA.
21 //
22 // $Id$
23
24 #include <ctype.h> // isspace()
25
26 #ifdef SG_HAVE_STD_INCLUDES
27 # include <cerrno>
28 #else
29 # include <errno.h>
30 #endif
31
32 #include "fgstream.hxx"
33
34 fg_gzifstream::fg_gzifstream()
35     : istream(&gzbuf)
36 {
37 }
38
39 //-----------------------------------------------------------------------------
40 //
41 // Open a possibly gzipped file for reading.
42 //
43 fg_gzifstream::fg_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 fg_gzifstream::fg_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 fg_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 fg_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 #ifdef __MWERKS__
108     while ( in.get(c) && c != '\0' ) {
109 #else
110     while ( in.get(c) ) {
111 #endif
112         if ( (c == '\n') || (c == '\r') ) {
113             break;
114         }       
115     }
116
117     return in;
118 }
119
120 istream&
121 skipws( istream& in ) {
122     char c;
123 #ifdef __MWERKS__
124     while ( in.get(c) && c != '\0' ) {
125 #else
126     while ( in.get(c) ) {
127 #endif
128
129 #ifdef __MWERKS__
130         if ( ! isspace( c ) && c != '\n' ) {
131 #else
132         if ( ! isspace( c ) ) {
133 #endif
134             // put pack the non-space character
135             in.putback(c);
136             break;
137         }
138     }
139     return in;
140 }
141
142 istream&
143 skipcomment( istream& in )
144 {
145     while ( in )
146     {
147         // skip whitespace
148 #ifdef __MWERKS__
149         in >> ::skipws;
150 #else
151         in >> skipws;
152 #endif
153
154         char c;
155         if ( in.get( c ) && c != '#' )
156         {
157             // not a comment
158             in.putback(c);
159             break;
160         }
161         in >> skipeol;
162     }
163     return in;
164 }
165