]> git.mxchange.org Git - simgear.git/blob - Misc/fgstream.cxx
Temporary destructor patch until Steve can release next version of PUI.
[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 // Open a possibly gzipped file for reading.
39 // If the initial open fails and the filename has a ".gz" extension then
40 // remove the extension and try again.
41 // If the initial open fails and the filename doesn't have a ".gz" extension
42 // then append ".gz" and try again.
43 //
44 void
45 fg_gzifstream::open( const string& name, int io_mode )
46 {
47     gzstream.open( name.c_str(), io_mode );
48     if ( gzstream.fail() )
49     {
50         string s = name;
51         if ( s.substr( s.length() - 3, 3 ) == ".gz" )
52         {
53             // remove ".gz" suffix
54             s.erase( s.length() - 3, 3 );
55         }
56         else
57         {
58             // Append ".gz" suffix
59             s += ".gz";
60         }
61
62         // Try again.
63         gzstream.open( s.c_str(), io_mode );
64     }
65 }
66
67 //-----------------------------------------------------------------------------
68 //
69 // Remove whitespace characters from the stream.
70 //
71 istream&
72 fg_gzifstream::eat_whitespace()
73 {
74     char c;
75     while ( gzstream.get(c) )
76     {
77         if ( ! isspace( c ) )
78         {
79             // put pack the non-space character
80             gzstream.putback(c);
81             break;
82         }
83     }
84     return gzstream;
85 }
86
87 //-----------------------------------------------------------------------------
88 // 
89 // Remove whitspace chatacters and comment lines from a stream.
90 //
91 istream&
92 fg_gzifstream::eat_comments()
93 {
94     for (;;)
95     {
96         // skip whitespace
97         eat_whitespace();
98
99         char c;
100         gzstream.get( c );
101         if ( c != '#' )
102         {
103             // not a comment
104             gzstream.putback(c);
105             break;
106         }
107
108         // skip to end of line.
109         while ( gzstream.get(c) && c != '\n' )
110             ;
111     }
112     return gzstream;
113 }
114
115
116 // $Log$
117 // Revision 1.1  1998/09/01 19:06:29  curt
118 // Initial revision.
119 //