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