]> git.mxchange.org Git - simgear.git/blob - simgear/misc/fgstream.cxx
Ed Williams: Added some bulletproofing at the poles.
[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 program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // 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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 #include <ctype.h> // isspace()
24
25 #include "fgstream.hxx"
26
27 fg_gzifstream::fg_gzifstream()
28     : istream(&gzbuf)
29 {
30 }
31
32 //-----------------------------------------------------------------------------
33 //
34 // Open a possibly gzipped file for reading.
35 //
36 fg_gzifstream::fg_gzifstream( const string& name, ios_openmode io_mode )
37     : istream(&gzbuf)
38 {
39     this->open( name, io_mode );
40 }
41
42 //-----------------------------------------------------------------------------
43 //
44 // Attach a stream to an already opened file descriptor.
45 //
46 fg_gzifstream::fg_gzifstream( int fd, ios_openmode io_mode )
47     : istream(&gzbuf)
48 {
49     gzbuf.attach( fd, io_mode );
50 }
51
52 //-----------------------------------------------------------------------------
53 //
54 // Open a possibly gzipped file for reading.
55 // If the initial open fails and the filename has a ".gz" extension then
56 // remove the extension and try again.
57 // If the initial open fails and the filename doesn't have a ".gz" extension
58 // then append ".gz" and try again.
59 //
60 void
61 fg_gzifstream::open( const string& name, ios_openmode io_mode )
62 {
63     gzbuf.open( name.c_str(), io_mode );
64     if ( ! gzbuf.is_open() )
65     {
66         string s = name;
67         if ( s.substr( s.length() - 3, 3 ) == ".gz" )
68         {
69             // remove ".gz" suffix
70             s.replace( s.length() - 3, 3, "" );
71 //          s.erase( s.length() - 3, 3 );
72         }
73         else
74         {
75             // Append ".gz" suffix
76             s += ".gz";
77         }
78
79         // Try again.
80         gzbuf.open( s.c_str(), io_mode );
81     }
82 }
83
84 void
85 fg_gzifstream::attach( int fd, ios_openmode io_mode )
86 {
87     gzbuf.attach( fd, io_mode );
88 }
89
90 //
91 // Manipulators
92 //
93
94 istream&
95 skipeol( istream& in )
96 {
97     char c = '\0';
98     // skip to end of line.
99
100 #ifdef __MWERKS__
101     while ( in.get(c) && c != '\0' ) {
102 #else
103     while ( in.get(c) ) {
104 #endif
105         if ( (c == '\n') || (c == '\r') ) {
106             break;
107         }       
108     }
109
110     return in;
111 }
112
113 istream&
114 skipws( istream& in ) {
115     char c;
116 #ifdef __MWERKS__
117     while ( in.get(c) && c != '\0' ) {
118 #else
119     while ( in.get(c) ) {
120 #endif
121
122 #ifdef __MWERKS__
123         if ( ! isspace( c ) && c != '\n' ) {
124 #else
125         if ( ! isspace( c ) ) {
126 #endif
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 #ifdef __MWERKS__
142         in >> ::skipws;
143 #else
144         in >> skipws;
145 #endif
146
147         char c;
148         if ( in.get( c ) && c != '#' )
149         {
150             // not a comment
151             in.putback(c);
152             break;
153         }
154         in >> skipeol;
155     }
156     return in;
157 }
158