]> git.mxchange.org Git - simgear.git/blobdiff - Misc/fgstream.cxx
Tweak for SGI portability.
[simgear.git] / Misc / fgstream.cxx
index 32c6919282c79861f26be029e1b191f03c2fe311..b695315cbef6374580b4e01fa22efaba5fe00bc8 100644 (file)
 // (Log is kept at end of this file)
 
 #include <ctype.h> // isspace()
-#include "fgstream.hxx"
+#include <Misc/fgstream.hxx>
+
+fg_gzifstream::fg_gzifstream()
+    : istream(&gzbuf)
+{
+}
 
 //-----------------------------------------------------------------------------
 //
 // Open a possibly gzipped file for reading.
 //
-fg_gzifstream::fg_gzifstream( const string& name, int io_mode )
+fg_gzifstream::fg_gzifstream( const string& name, ios_openmode io_mode )
+    : istream(&gzbuf)
 {
-    open( name, io_mode );
+    this->open( name, io_mode );
+}
+
+//-----------------------------------------------------------------------------
+//
+// Attach a stream to an already opened file descriptor.
+//
+fg_gzifstream::fg_gzifstream( int fd, ios_openmode io_mode )
+    : istream(&gzbuf)
+{
+    gzbuf.attach( fd, io_mode );
 }
 
 //-----------------------------------------------------------------------------
@@ -42,16 +58,17 @@ fg_gzifstream::fg_gzifstream( const string& name, int io_mode )
 // then append ".gz" and try again.
 //
 void
-fg_gzifstream::open( const string& name, int io_mode )
+fg_gzifstream::open( const string& name, ios_openmode io_mode )
 {
-    gzstream.open( name.c_str(), io_mode );
-    if ( gzstream.fail() )
+    gzbuf.open( name.c_str(), io_mode );
+    if ( ! gzbuf.is_open() )
     {
        string s = name;
        if ( s.substr( s.length() - 3, 3 ) == ".gz" )
        {
            // remove ".gz" suffix
-           s.erase( s.length() - 3, 3 );
+           s.replace( s.length() - 3, 3, "" );
+//         s.erase( s.length() - 3, 3 );
        }
        else
        {
@@ -60,60 +77,75 @@ fg_gzifstream::open( const string& name, int io_mode )
        }
 
        // Try again.
-       gzstream.open( s.c_str(), io_mode );
+       gzbuf.open( s.c_str(), io_mode );
     }
 }
 
-//-----------------------------------------------------------------------------
+void
+fg_gzifstream::attach( int fd, ios_openmode io_mode )
+{
+    gzbuf.attach( fd, io_mode );
+}
+
 //
-// Remove whitespace characters from the stream.
+// Manipulators
 //
+
+istream&
+skipeol( istream& in )
+{
+    char c = 0;
+    // skip to end of line.
+    while ( in.get(c) && (c != '\n' && c != '\r') )
+       ;
+
+    // \r\n ?
+    return in;
+}
+
 istream&
-fg_gzifstream::eat_whitespace()
+skipws( istream& in )
 {
     char c;
-    while ( gzstream.get(c) )
+    while ( in.get(c) )
     {
        if ( ! isspace( c ) )
        {
            // put pack the non-space character
-           gzstream.putback(c);
+           in.putback(c);
            break;
        }
     }
-    return gzstream;
+    return in;
 }
 
-//-----------------------------------------------------------------------------
-// 
-// Remove whitspace chatacters and comment lines from a stream.
-//
 istream&
-fg_gzifstream::eat_comments()
+skipcomment( istream& in )
 {
-    for (;;)
+    while ( in )
     {
        // skip whitespace
-       eat_whitespace();
+       in >> skipws;
 
        char c;
-       gzstream.get( c );
-       if ( c != '#' )
+       if ( in.get( c ) && c != '#' )
        {
            // not a comment
-           gzstream.putback(c);
+           in.putback(c);
            break;
        }
-
-       // skip to end of line.
-       while ( gzstream.get(c) && c != '\n' )
-           ;
+       in >> skipeol;
     }
-    return gzstream;
+    return in;
 }
 
-
 // $Log$
+// Revision 1.3  1998/11/06 14:05:12  curt
+// More portability improvements by Bernie Bright.
+//
+// Revision 1.2  1998/09/24 15:22:17  curt
+// Additional enhancements.
+//
 // Revision 1.1  1998/09/01 19:06:29  curt
 // Initial revision.
 //