]> git.mxchange.org Git - simgear.git/blob - Misc/fgstream.cxx
Miscellaneous fixes under Steve's direction.
[simgear.git] / 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 // (Log is kept at end of this file)
23
24 #include <ctype.h> // isspace()
25 #include "fgstream.hxx"
26
27 //-----------------------------------------------------------------------------
28 //
29 // Open a possibly gzipped file for reading.
30 //
31 fg_gzifstream::fg_gzifstream( const string& name, int io_mode )
32 {
33     open( name, io_mode );
34 }
35
36 //-----------------------------------------------------------------------------
37 //
38 // Attach a stream to an already opened file descriptor.
39 //
40 fg_gzifstream::fg_gzifstream( int fd, int io_mode )
41     : gzstream( fd, io_mode )
42 {
43 }
44
45 //-----------------------------------------------------------------------------
46 //
47 // Open a possibly gzipped file for reading.
48 // If the initial open fails and the filename has a ".gz" extension then
49 // remove the extension and try again.
50 // If the initial open fails and the filename doesn't have a ".gz" extension
51 // then append ".gz" and try again.
52 //
53 void
54 fg_gzifstream::open( const string& name, int io_mode )
55 {
56     gzstream.open( name.c_str(), io_mode );
57     if ( gzstream.fail() )
58     {
59         string s = name;
60         if ( s.substr( s.length() - 3, 3 ) == ".gz" )
61         {
62             // remove ".gz" suffix
63             s.replace( s.length() - 3, 3, "" );
64 //          s.erase( s.length() - 3, 3 );
65         }
66         else
67         {
68             // Append ".gz" suffix
69             s += ".gz";
70         }
71
72         // Try again.
73         gzstream.open( s.c_str(), io_mode );
74     }
75 }
76
77 //-----------------------------------------------------------------------------
78 //
79 // Remove whitespace characters from the stream.
80 //
81 istream&
82 fg_gzifstream::eat_whitespace()
83 {
84     char c;
85     while ( gzstream.get(c) )
86     {
87         if ( ! isspace( c ) )
88         {
89             // put pack the non-space character
90             gzstream.putback(c);
91             break;
92         }
93     }
94     return gzstream;
95 }
96
97 //-----------------------------------------------------------------------------
98 // 
99 // Remove whitspace chatacters and comment lines from a stream.
100 //
101 istream&
102 fg_gzifstream::eat_comments()
103 {
104     for (;;)
105     {
106         // skip whitespace
107         eat_whitespace();
108
109         char c;
110         gzstream.get( c );
111         if ( c != '#' )
112         {
113             // not a comment
114             gzstream.putback(c);
115             break;
116         }
117
118         // skip to end of line.
119         while ( gzstream.get(c) && (c != '\n' && c != '\r') )
120             ;
121     }
122     return gzstream;
123 }
124
125 //
126 // Manipulators
127 //
128
129 istream&
130 skipeol( istream& in )
131 {
132     char c = 0;
133     // skip to end of line.
134     while ( in.get(c) && (c != '\n' && c != '\r') )
135         ;
136
137     // \r\n ?
138     return in;
139 }
140
141 istream&
142 skipws( istream& in )
143 {
144     char c;
145     while ( in.get(c) )
146     {
147         if ( ! isspace( c ) )
148         {
149             // put pack the non-space character
150             in.putback(c);
151             break;
152         }
153     }
154     return in;
155 }
156
157 istream&
158 skipcomment( istream& in )
159 {
160     while ( in )
161     {
162         // skip whitespace
163         in >> skipws;
164
165         char c;
166         if ( in.get( c ) && c != '#' )
167         {
168             // not a comment
169             in.putback(c);
170             break;
171         }
172         in >> skipeol;
173     }
174     return in;
175 }
176
177 // $Log$
178 // Revision 1.2  1998/09/24 15:22:17  curt
179 // Additional enhancements.
180 //
181 // Revision 1.1  1998/09/01 19:06:29  curt
182 // Initial revision.
183 //