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