1
0
Fork 0

Added a readline(buf) vs. read(buf,len)

This commit is contained in:
curt 1999-11-20 00:28:30 +00:00
parent 24caa82715
commit 4f294285fe
9 changed files with 121 additions and 41 deletions

View 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 \

View 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;
}

View 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();

View file

@ -27,16 +27,16 @@
#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.
// just in case ...
buf[ *length ] = '\0';
int FGSerial::read( char *buf, int length ) {
int result;
return true;
// 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 );
if ( result + save_len == length ) {
strncpy( buf, save_buf, length );
save_len = 0;
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;
}

View 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 );
// write data to port
bool write( 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
int write( char *buf, int length );
// close port
bool close();

View 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." );

View 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
bool FGIOChannel::write( char *buf, int length ) {
int FGIOChannel::readline( char *buf, int length ) {
return 0;
}
// dummy process routine
int FGIOChannel::write( char *buf, int length ) {
return false;
}

View 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();
};

View 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." );