]> git.mxchange.org Git - flightgear.git/commitdiff
Added a readline(buf) vs. read(buf,len)
authorcurt <curt>
Sat, 20 Nov 1999 00:28:30 +0000 (00:28 +0000)
committercurt <curt>
Sat, 20 Nov 1999 00:28:30 +0000 (00:28 +0000)
src/Network/Makefile.am
src/Network/fg_file.cxx
src/Network/fg_file.hxx
src/Network/fg_serial.cxx
src/Network/fg_serial.hxx
src/Network/garmin.cxx
src/Network/iochannel.cxx
src/Network/iochannel.hxx
src/Network/nmea.cxx

index 6d6c76e96493a1781726c278aa012b596ecb0d97..b5ecbb3ca5468b8a476d310aa3b40526b58de720 100644 (file)
@@ -4,6 +4,7 @@ libNetwork_a_SOURCES = \
        iochannel.cxx iochannel.hxx \
        fg_file.cxx fg_file.hxx \
        fg_serial.cxx fg_serial.hxx \
+       fg_socket.cxx fg_socket.hxx \
        protocol.cxx protocol.hxx \
        garmin.cxx garmin.hxx \
        nmea.cxx nmea.hxx \
index 748e5727da0012613c66cb612a1966cc32a2c7eb..4adb2b2204dc7f47236dc1b2dca3eebc707bc314 100644 (file)
@@ -26,7 +26,6 @@
 #include STL_STRING
 
 #include <Debug/logstream.hxx>
-#include <Time/fg_time.hxx>
 
 #include "fg_file.hxx"
 
@@ -64,40 +63,48 @@ bool FGFile::open( FGProtocol::fgProtocolDir dir ) {
 }
 
 
-// read data from file
-bool FGFile::read( char *buf, int *length ) {
+// read a block of data of specified size
+int FGFile::read( char *buf, int length ) {
+    // read a chunk
+    int result = std::read( fp, buf, length );
+
+    return result;
+}
+
+
+// read a line of data, length is max size of input buffer
+int FGFile::readline( char *buf, int length ) {
     // save our current position
     int pos = lseek( fp, 0, SEEK_CUR );
 
     // read a chunk
-    int result = std::read( fp, buf, FG_MAX_MSG_SIZE );
+    int result = std::read( fp, buf, length );
 
     // find the end of line and reset position
     int i;
     for ( i = 0; i < result && buf[i] != '\n'; ++i );
     if ( buf[i] == '\n' ) {
-       *length = i + 1;
+       result = i + 1;
     } else {
-       *length = i;
+       result = i;
     }
-    lseek( fp, pos + *length, SEEK_SET );
+    lseek( fp, pos + result, SEEK_SET );
     
     // just in case ...
-    buf[ *length ] = '\0';
+    buf[ result ] = '\0';
 
-    return true;
+    return result;
 }
 
 
 // write data to a file
-bool FGFile::write( char *buf, int length ) {
+int FGFile::write( char *buf, int length ) {
     int result = std::write( fp, buf, length );
     if ( result != length ) {
        FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << file_name );
-       return false;
     }
 
-    return true;
+    return result;
 }
 
 
index 852fc4936c2d888204481857d0f8f63efdecd397..469be7b6f6c225eb90b5d11174afdf6f4ce1ae6b 100644 (file)
@@ -57,11 +57,14 @@ public:
     // open the file based on specified direction
     bool open( FGProtocol::fgProtocolDir dir );
 
-    // read data from file
-    bool read( char *buf, int *length );
+    // read a block of data of specified size
+    int read( char *buf, int length );
+
+    // read a line of data, length is max size of input buffer
+    int readline( char *buf, int length );
 
     // write data to a file
-    bool write( char *buf, int length );
+    int write( char *buf, int length );
 
     // close file
     bool close();
index 87dcb207ef462b84493dd049102a742da64dc352..162c62fc6b576022806caef46a8cd1b70c56e483 100644 (file)
 
 #include <Debug/logstream.hxx>
 #include <Aircraft/aircraft.hxx>
-#include <Include/fg_constants.h>
 #include <Serial/serial.hxx>
-#include <Time/fg_time.hxx>
 
 #include "fg_serial.hxx"
 
 FG_USING_STD(string);
 
 
-FGSerial::FGSerial() {
+FGSerial::FGSerial() :
+    save_len(0)
+{
 }
 
 
@@ -62,27 +62,77 @@ bool FGSerial::open( FGProtocol::fgProtocolDir dir ) {
 }
 
 
-// read data from port
-bool FGSerial::read( char *buf, int *length ) {
-    // read a chunk
-    *length = port.read_port( buf );
+// Read data from port.  If we don't get enough data, save what we did
+// get in the save buffer and return 0.  The save buffer will be
+// prepended to subsequent reads until we get as much as is requested.
+
+int FGSerial::read( char *buf, int length ) {
+    int result;
+
+    // read a chunk, keep in the save buffer until we have the
+    // requested amount read
+
+    char *buf_ptr = save_buf + save_len;
+    result = port.read_port( buf_ptr, length - save_len );
     
-    // just in case ...
-    buf[ *length ] = '\0';
+    if ( result + save_len == length ) {
+       strncpy( buf, save_buf, length );
+       save_len = 0;
 
-    return true;
+       return length;
+    }
+    
+    return 0;
+}
+
+
+// read data from port
+int FGSerial::readline( char *buf, int length ) {
+    int result;
+
+    // read a chunk, keep in the save buffer until we have the
+    // requested amount read
+
+    char *buf_ptr = save_buf + save_len;
+    result = port.read_port( buf_ptr, FG_MAX_MSG_SIZE - save_len );
+    save_len += result;
+
+    // look for the end of line in save_buf
+    int i;
+    for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i );
+    if ( save_buf[i] == '\n' ) {
+       result = i + 1;
+    } else {
+       // no end of line yet
+       return 0;
+    }
+
+    // we found an end of line
+
+    // copy to external buffer
+    strncpy( buf, save_buf, result );
+    buf[result] = '\0';
+    cout << "fg_serial line = " << buf << endl;
+
+    // shift save buffer
+    for ( i = result; i < save_len; ++i ) {
+       save_buf[ i - result ] = save_buf[result];
+    }
+    save_len -= result;
+
+    return result;
 }
 
+
 // write data to port
-bool FGSerial::write( char *buf, int length ) {
+int FGSerial::write( char *buf, int length ) {
     int result = port.write_port( buf, length );
 
     if ( result != length ) {
        FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << device );
-       return false;
     }
 
-    return true;
+    return result;
 }
 
 
index 34075f76206dcb6354bba6971686faf9d84b0f72..ba88881136be9537f6ab87afcf5fcaf0e6735b7b 100644 (file)
@@ -53,6 +53,9 @@ class FGSerial : public FGIOChannel {
     string baud;
     FGSerialPort port;
 
+    char save_buf[ 2 * FG_MAX_MSG_SIZE ];
+    int save_len;
+
 public:
 
     FGSerial();
@@ -61,11 +64,14 @@ public:
     // open the serial port based on specified direction
     bool open( FGProtocol::fgProtocolDir dir );
 
-    // read data from port
-    bool read( char *buf, int *length );
+    // read a block of data of specified size
+    int read( char *buf, int length );
+
+    // read a line of data, length is max size of input buffer
+    int readline( char *buf, int length );
 
-    // write data to port
-    bool write( char *buf, int length );
+    // write data to a file
+    int write( char *buf, int length );
 
     // close port
     bool close();
index 56116255ccd86de2689135c2e63dd9861ee3d16f..81b279f04a70f4610d81cf4f1ab4f5e5f3ee4c25 100644 (file)
@@ -373,13 +373,13 @@ bool FGGarmin::process() {
            return false;
        }
     } else if ( get_direction() == in ) {
-       if ( io->read( buf, &length ) ) {
+       if ( io->readline( buf, FG_MAX_MSG_SIZE ) ) {
            parse_message();
        } else {
            FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
            return false;
        }
-       if ( io->read( buf, &length ) ) {
+       if ( io->readline( buf, FG_MAX_MSG_SIZE ) ) {
            parse_message();
        } else {
            FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
index c054f2c26f683ac0519fac947f6d28264321e73a..23f6c46e993dd10ad2a4f41f8fe3ca9459e63a89 100644 (file)
@@ -45,13 +45,19 @@ bool FGIOChannel::open( FGProtocol::fgProtocolDir dir ) {
 
 
 // dummy process routine
-bool FGIOChannel::read( char *buf, int *length ) {
-    return false;
+int FGIOChannel::read( char *buf, int length ) {
+    return 0;
+}
+
+
+// dummy process routine
+int FGIOChannel::readline( char *buf, int length ) {
+    return 0;
 }
 
 
 // dummy process routine
-bool FGIOChannel::write( char *buf, int length ) {
+int FGIOChannel::write( char *buf, int length ) {
     return false;
 }
 
index ee125b2d8bb31f11c766394ca929e00539994f7c..aae5c1932ba4e6ed24c657d8e970e9a3b9f2e017 100644 (file)
@@ -48,8 +48,9 @@ public:
     virtual ~FGIOChannel();
 
     virtual bool open( FGProtocol::fgProtocolDir dir );
-    virtual bool read( char *buf, int *length );
-    virtual bool write( char *buf, int length );
+    virtual int read( char *buf, int length );
+    virtual int readline( char *buf, int length );
+    virtual int write( char *buf, int length );
     virtual bool close();
 };
 
index 3da8ce42bf28b3f0ffaf2ed9b9137b6243e5d0b5..b1755f2e20fbe1809ca412fe5df35696e57ae7ff 100644 (file)
@@ -476,7 +476,13 @@ bool FGNMEA::process() {
            return false;
        }
     } else if ( get_direction() == in ) {
-       if ( io->read( buf, &length ) ) {
+       if ( io->readline( buf, FG_MAX_MSG_SIZE ) ) {
+           parse_message();
+       } else {
+           FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
+           return false;
+       }
+       if ( io->readline( buf, FG_MAX_MSG_SIZE ) ) {
            parse_message();
        } else {
            FG_LOG( FG_IO, FG_ALERT, "Error reading data." );