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