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