]> git.mxchange.org Git - simgear.git/blob - simgear/misc/fgstream.cxx
Added a touch of error checking to the screen dump routine, i.e. don't
[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 #include "fgstream.hxx"
27
28 fg_gzifstream::fg_gzifstream()
29     : istream(&gzbuf)
30 {
31 }
32
33 //-----------------------------------------------------------------------------
34 //
35 // Open a possibly gzipped file for reading.
36 //
37 fg_gzifstream::fg_gzifstream( const string& name, ios_openmode io_mode )
38     : istream(&gzbuf)
39 {
40     this->open( name, io_mode );
41 }
42
43 //-----------------------------------------------------------------------------
44 //
45 // Attach a stream to an already opened file descriptor.
46 //
47 fg_gzifstream::fg_gzifstream( int fd, ios_openmode io_mode )
48     : istream(&gzbuf)
49 {
50     gzbuf.attach( fd, io_mode );
51 }
52
53 //-----------------------------------------------------------------------------
54 //
55 // Open a possibly gzipped file for reading.
56 // If the initial open fails and the filename has a ".gz" extension then
57 // remove the extension and try again.
58 // If the initial open fails and the filename doesn't have a ".gz" extension
59 // then append ".gz" and try again.
60 //
61 void
62 fg_gzifstream::open( const string& name, ios_openmode io_mode )
63 {
64     gzbuf.open( name.c_str(), io_mode );
65     if ( ! gzbuf.is_open() )
66     {
67         string s = name;
68         if ( s.substr( s.length() - 3, 3 ) == ".gz" )
69         {
70             // remove ".gz" suffix
71             s.replace( s.length() - 3, 3, "" );
72 //          s.erase( s.length() - 3, 3 );
73         }
74         else
75         {
76             // Append ".gz" suffix
77             s += ".gz";
78         }
79
80         // Try again.
81         gzbuf.open( s.c_str(), io_mode );
82     }
83 }
84
85 void
86 fg_gzifstream::attach( int fd, ios_openmode io_mode )
87 {
88     gzbuf.attach( fd, io_mode );
89 }
90
91 //
92 // Manipulators
93 //
94
95 istream&
96 skipeol( istream& in )
97 {
98     char c = '\0';
99     // skip to end of line.
100
101 #ifdef __MWERKS__
102     while ( in.get(c) && c != '\0' ) {
103 #else
104     while ( in.get(c) ) {
105 #endif
106         if ( (c == '\n') || (c == '\r') ) {
107             break;
108         }       
109     }
110
111     return in;
112 }
113
114 istream&
115 skipws( istream& in ) {
116     char c;
117 #ifdef __MWERKS__
118     while ( in.get(c) && c != '\0' ) {
119 #else
120     while ( in.get(c) ) {
121 #endif
122
123 #ifdef __MWERKS__
124         if ( ! isspace( c ) && c != '\n' ) {
125 #else
126         if ( ! isspace( c ) ) {
127 #endif
128             // put pack the non-space character
129             in.putback(c);
130             break;
131         }
132     }
133     return in;
134 }
135
136 istream&
137 skipcomment( istream& in )
138 {
139     while ( in )
140     {
141         // skip whitespace
142 #ifdef __MWERKS__
143         in >> ::skipws;
144 #else
145         in >> skipws;
146 #endif
147
148         char c;
149         if ( in.get( c ) && c != '#' )
150         {
151             // not a comment
152             in.putback(c);
153             break;
154         }
155         in >> skipeol;
156     }
157     return in;
158 }
159