]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sgstream.cxx
Removal of PLIB/SG from SimGear
[simgear.git] / simgear / misc / sgstream.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 General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #include <simgear/compiler.h>
24 #include <string>
25 #include <ctype.h> // isspace()
26 #include <cerrno>
27
28 #include "sgstream.hxx"
29
30 using std::string;
31 using std::istream;
32
33 sg_gzifstream::sg_gzifstream()
34     : istream(&gzbuf)
35 {
36 }
37
38 //-----------------------------------------------------------------------------
39 //
40 // Open a possibly gzipped file for reading.
41 //
42 sg_gzifstream::sg_gzifstream( const string& name, ios_openmode io_mode )
43     : istream(&gzbuf)
44 {
45     this->open( name, io_mode );
46 }
47
48 //-----------------------------------------------------------------------------
49 //
50 // Attach a stream to an already opened file descriptor.
51 //
52 sg_gzifstream::sg_gzifstream( int fd, ios_openmode io_mode )
53     : istream(&gzbuf)
54 {
55     gzbuf.attach( fd, io_mode );
56 }
57
58 //-----------------------------------------------------------------------------
59 //
60 // Open a possibly gzipped file for reading.
61 // If the initial open fails and the filename has a ".gz" extension then
62 // remove the extension and try again.
63 // If the initial open fails and the filename doesn't have a ".gz" extension
64 // then append ".gz" and try again.
65 //
66 void
67 sg_gzifstream::open( const string& name, ios_openmode io_mode )
68 {
69     gzbuf.open( name.c_str(), io_mode );
70     if ( ! gzbuf.is_open() )
71     {
72         string s = name;
73         if ( s.substr( s.length() - 3, 3 ) == ".gz" )
74         {
75             // remove ".gz" suffix
76             s.replace( s.length() - 3, 3, "" );
77 //          s.erase( s.length() - 3, 3 );
78         }
79         else
80         {
81             // Append ".gz" suffix
82             s += ".gz";
83         }
84
85         // Try again.
86         gzbuf.open( s.c_str(), io_mode );
87     }
88 }
89
90 void
91 sg_gzifstream::attach( int fd, ios_openmode io_mode )
92 {
93     gzbuf.attach( fd, io_mode );
94 }
95
96 //
97 // Manipulators
98 //
99
100 istream&
101 skipeol( istream& in )
102 {
103     char c = '\0';
104     // skip to end of line.
105
106     while ( in.get(c) ) {
107         if ( (c == '\n') || (c == '\r') ) {
108             break;
109         }       
110     }
111
112     return in;
113 }
114
115 istream&
116 skipws( istream& in ) {
117     char c;
118     while ( in.get(c) ) {
119
120         if ( ! isspace( c ) ) {
121             // put pack the non-space character
122             in.putback(c);
123             break;
124         }
125     }
126     return in;
127 }
128
129 istream&
130 skipcomment( istream& in )
131 {
132     while ( in )
133     {
134         // skip whitespace
135         in >> skipws;
136
137         char c;
138         if ( in.get( c ) && c != '#' )
139         {
140             // not a comment
141             in.putback(c);
142             break;
143         }
144         in >> skipeol;
145     }
146     return in;
147 }
148