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