]> git.mxchange.org Git - simgear.git/blob - Misc/fgstream.cxx
Added the Serial subdirectory
[simgear.git] / Misc / fgstream.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 program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // 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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24 #include <ctype.h> // isspace()
25 #include <Misc/fgstream.hxx>
26
27 fg_gzifstream::fg_gzifstream()
28     : istream(&gzbuf)
29 {
30 }
31
32 //-----------------------------------------------------------------------------
33 //
34 // Open a possibly gzipped file for reading.
35 //
36 fg_gzifstream::fg_gzifstream( const string& name, ios_openmode io_mode )
37     : istream(&gzbuf)
38 {
39     this->open( name, io_mode );
40 }
41
42 //-----------------------------------------------------------------------------
43 //
44 // Attach a stream to an already opened file descriptor.
45 //
46 fg_gzifstream::fg_gzifstream( int fd, ios_openmode io_mode )
47     : istream(&gzbuf)
48 {
49     gzbuf.attach( fd, io_mode );
50 }
51
52 //-----------------------------------------------------------------------------
53 //
54 // Open a possibly gzipped file for reading.
55 // If the initial open fails and the filename has a ".gz" extension then
56 // remove the extension and try again.
57 // If the initial open fails and the filename doesn't have a ".gz" extension
58 // then append ".gz" and try again.
59 //
60 void
61 fg_gzifstream::open( const string& name, ios_openmode io_mode )
62 {
63     gzbuf.open( name.c_str(), io_mode );
64     if ( ! gzbuf.is_open() )
65     {
66         string s = name;
67         if ( s.substr( s.length() - 3, 3 ) == ".gz" )
68         {
69             // remove ".gz" suffix
70             s.replace( s.length() - 3, 3, "" );
71 //          s.erase( s.length() - 3, 3 );
72         }
73         else
74         {
75             // Append ".gz" suffix
76             s += ".gz";
77         }
78
79         // Try again.
80         gzbuf.open( s.c_str(), io_mode );
81     }
82 }
83
84 void
85 fg_gzifstream::attach( int fd, ios_openmode io_mode )
86 {
87     gzbuf.attach( fd, io_mode );
88 }
89
90 //
91 // Manipulators
92 //
93
94 istream&
95 skipeol( istream& in )
96 {
97     char c = 0;
98     // skip to end of line.
99     while ( in.get(c) && (c != '\n' && c != '\r') )
100         ;
101
102     // \r\n ?
103     return in;
104 }
105
106 istream&
107 skipws( istream& in )
108 {
109     char c;
110     while ( in.get(c) )
111     {
112         if ( ! isspace( c ) )
113         {
114             // put pack the non-space character
115             in.putback(c);
116             break;
117         }
118     }
119     return in;
120 }
121
122 istream&
123 skipcomment( istream& in )
124 {
125     while ( in )
126     {
127         // skip whitespace
128         in >> skipws;
129
130         char c;
131         if ( in.get( c ) && c != '#' )
132         {
133             // not a comment
134             in.putback(c);
135             break;
136         }
137         in >> skipeol;
138     }
139     return in;
140 }
141
142 // $Log$
143 // Revision 1.3  1998/11/06 14:05:12  curt
144 // More portability improvements by Bernie Bright.
145 //
146 // Revision 1.2  1998/09/24 15:22:17  curt
147 // Additional enhancements.
148 //
149 // Revision 1.1  1998/09/01 19:06:29  curt
150 // Initial revision.
151 //