]> git.mxchange.org Git - flightgear.git/blobdiff - src/Network/generic.cxx
Fix shared library build for metar executable
[flightgear.git] / src / Network / generic.cxx
index 9ab7d76999e13597ebf7336a16fe9a0bfd3b9504..eb0bd63ba66c5f01569643f0d3563417e9ba215d 100644 (file)
@@ -37,8 +37,8 @@
 
 #include <Main/globals.hxx>
 #include <Main/fg_props.hxx>
+#include <Main/fg_os.hxx>
 #include <Main/util.hxx>
-
 #include "generic.hxx"
 
 
@@ -64,7 +64,7 @@ FGGeneric::FGGeneric(vector<string> tokens) : exitOnError(false)
     file_name = config+".xml";
     direction = tokens[2];
 
-    if (direction != "in" && direction != "out") {
+    if (direction != "in" && direction != "out" && direction != "bi") {
         SG_LOG(SG_GENERAL, SG_ALERT, "Unsuported protocol direction: "
                << direction);
     }
@@ -272,13 +272,13 @@ bool FGGeneric::gen_message() {
     }
 }
 
-bool FGGeneric::parse_message_binary() {
+bool FGGeneric::parse_message_binary(int length) {
     char *p2, *p1 = buf;
     int32_t tmp32;
     double val;
     int i = -1;
 
-    p2 = p1 + FG_MAX_MSG_SIZE;
+    p2 = p1 + length;
     while ((++i < (int)_in_message.size()) && (p1  < p2)) {
 
         switch (_in_message[i].type) {
@@ -353,14 +353,23 @@ bool FGGeneric::parse_message_binary() {
     return true;
 }
 
-bool FGGeneric::parse_message_ascii() {
+bool FGGeneric::parse_message_ascii(int length) {
     char *p2, *p1 = buf;
     double val;
     int i = -1;
+    int chunks = _in_message.size();
+    int line_separator_size = line_separator.size();
+
+    if (length < line_separator_size ||
+        line_separator.compare(buf + length - line_separator_size) != 0) {
 
-    while ((++i < (int)_in_message.size()) &&
-           p1 && strcmp(p1, line_separator.c_str())) {
+        SG_LOG(SG_IO, SG_WARN,
+               "Input line does not end with expected line separator." );
+    } else {
+        buf[length - line_separator_size] = 0;
+    }
 
+    while ((++i < chunks) && p1) {
         p2 = strstr(p1, var_separator.c_str());
         if (p2) {
             *p2 = 0;
@@ -398,11 +407,11 @@ bool FGGeneric::parse_message_ascii() {
     return true;
 }
 
-bool FGGeneric::parse_message() {
+bool FGGeneric::parse_message(int length) {
     if (binary_mode) {
-        return parse_message_binary(); 
+        return parse_message_binary(length);
     } else {
-        return parse_message_ascii();
+        return parse_message_ascii(length);
     }
 }
 
@@ -424,7 +433,9 @@ bool FGGeneric::open() {
 
     set_enabled( true );
 
-    if ( get_direction() == SG_IO_OUT && ! preamble.empty() ) {
+    if ( ((get_direction() == SG_IO_OUT )||
+          (get_direction() == SG_IO_BI))
+          && ! preamble.empty() ) {
         if ( ! io->write( preamble.c_str(), preamble.size() ) ) {
             SG_LOG( SG_IO, SG_WARN, "Error writing preamble." );
             return false;
@@ -439,18 +450,22 @@ bool FGGeneric::open() {
 bool FGGeneric::process() {
     SGIOChannel *io = get_io_channel();
 
-    if ( get_direction() == SG_IO_OUT ) {
+    if ( (get_direction() == SG_IO_OUT) ||
+         (get_direction() == SG_IO_BI) ) {
         gen_message();
         if ( ! io->write( buf, length ) ) {
             SG_LOG( SG_IO, SG_WARN, "Error writing data." );
             goto error_out;
         }
-    } else if ( get_direction() == SG_IO_IN ) {
+    }
+
+    if (( get_direction() == SG_IO_IN ) ||
+        (get_direction() == SG_IO_BI) ) {
         if ( io->get_type() == sgFileType ) {
             if (!binary_mode) {
                 length = io->readline( buf, FG_MAX_MSG_SIZE );
                 if ( length > 0 ) {
-                    parse_message();
+                    parse_message( length );
                 } else {
                     SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
                     return false;
@@ -458,7 +473,7 @@ bool FGGeneric::process() {
             } else {
                 length = io->read( buf, binary_record_length );
                 if ( length == binary_record_length ) {
-                    parse_message();
+                    parse_message( length );
                 } else {
                     SG_LOG( SG_IO, SG_ALERT,
                             "Generic protocol: Received binary "
@@ -470,12 +485,12 @@ bool FGGeneric::process() {
         } else {
             if (!binary_mode) {
                 while ((length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
-                    parse_message();
+                    parse_message( length );
                 }
             } else {
                 while ((length = io->read( buf, binary_record_length )) 
                           == binary_record_length ) {
-                    parse_message();
+                    parse_message( length );
                 }
 
                 if ( length > 0 ) {
@@ -491,7 +506,7 @@ bool FGGeneric::process() {
     return true;
 error_out:
     if (exitOnError) {
-        fgExit(1);
+        fgOSExit(1);
         return true; // should not get there, but please the compiler
     } else
         return false;
@@ -502,7 +517,9 @@ error_out:
 bool FGGeneric::close() {
     SGIOChannel *io = get_io_channel();
 
-    if ( get_direction() == SG_IO_OUT && ! postamble.empty() ) {
+    if ( ((get_direction() == SG_IO_OUT)||
+          (get_direction() == SG_IO_BI))
+          && ! postamble.empty() ) {
         if ( ! io->write( postamble.c_str(), postamble.size() ) ) {
             SG_LOG( SG_IO, SG_ALERT, "Error writing postamble." );
             return false;
@@ -532,9 +549,9 @@ FGGeneric::reinit()
     SGPropertyNode root;
     try {
         readProperties(path.str(), &root);
-    } catch (const sg_exception &) {
+    } catch (const sg_exception & ex) {
         SG_LOG(SG_GENERAL, SG_ALERT,
-         "Unable to load the protocol configuration file");
+         "Unable to load the protocol configuration file: " << ex.getFormattedMessage() );
          return;
     }
 
@@ -549,6 +566,13 @@ FGGeneric::reinit()
         if (input) {
             _in_message.clear();
             read_config(input, _in_message);
+            if (!binary_mode && (line_separator.size() == 0 ||
+                *line_separator.rbegin() != '\n')) {
+
+                SG_LOG(SG_IO, SG_WARN,
+                    "Warning: Appending newline to line separator in generic input.");
+                line_separator.push_back('\n');
+            }
         }
     }
 }