]> git.mxchange.org Git - flightgear.git/blobdiff - src/Network/generic.cxx
Switch from bool to int for more deterministic structure packing.
[flightgear.git] / src / Network / generic.cxx
index 043ce2457b942e808c8dd603bc86ec6514beef9d..38481b08b238e19b2199c41924dd7c139a478450 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Written by Curtis Olson, started November 1999.
 //
-// Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
+// Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
@@ -57,15 +57,20 @@ FGGeneric::FGGeneric(string& config) {
          return;
     }
 
+    _out_message.clear();
     SGPropertyNode *output = root.getNode("generic/output");
-    read_config(output, _out_message);
+    if (output)
+        read_config(output, _out_message);
 
+    _in_message.clear();
     SGPropertyNode *input = root.getNode("generic/input");
-    read_config(input, _in_message);
+    if (input)
+        read_config(input, _in_message);
 }
 
 FGGeneric::~FGGeneric() {
     _out_message.clear();
+    _in_message.clear();
 }
 
 
@@ -85,7 +90,7 @@ bool FGGeneric::gen_message() {
         switch (_out_message[i].type) {
         case FG_INT:
             val = _out_message[i].offset +
-                    _out_message[i].prop->getIntValue() * _out_message[i].factor;
+                  _out_message[i].prop->getIntValue() * _out_message[i].factor;
             snprintf(tmp, 255, _out_message[i].format.c_str(), (int)val);
             break;
 
@@ -96,13 +101,13 @@ bool FGGeneric::gen_message() {
 
         case FG_DOUBLE:
             val = _out_message[i].offset +
-                       _out_message[i].prop->getDoubleValue() * _out_message[i].factor;
-            snprintf(tmp, 255, _out_message[i].format.c_str(), val);
+                _out_message[i].prop->getFloatValue() * _out_message[i].factor;
+            snprintf(tmp, 255, _out_message[i].format.c_str(), (float)val);
             break;
 
         default: // SG_STRING
              snprintf(tmp, 255, _out_message[i].format.c_str(),
-                               _out_message[i].prop->getStringValue());
+                                _out_message[i].prop->getStringValue());
         }
 
         generic_sentence += tmp;
@@ -123,35 +128,37 @@ bool FGGeneric::gen_message() {
 bool FGGeneric::parse_message() {
     char *p2, *p1 = buf;
     double val;
-    int i = 0;
+    int i = -1;
 
-    while (p1 && strcmp(p1, line_separator.c_str())) {
+    while ((++i < _in_message.size()) &&
+           p1 && strcmp(p1, line_separator.c_str())) {
 
         p2 = strstr(p1, var_separator.c_str());
-        if (p2)
-            *(p2++) = 0;
+        if (p2) {
+            *p2 = 0;
+            p2 += var_separator.length();
+        }
 
-        switch (_out_message[i].type) {
+        switch (_in_message[i].type) {
         case FG_INT:
-            val = _out_message[i].offset + atoi(p1) * _out_message[i].factor;
-            _out_message[i].prop->setIntValue(val);
+            val = _in_message[i].offset + atoi(p1) * _in_message[i].factor;
+            _in_message[i].prop->setIntValue((int)val);
             break;
 
         case FG_BOOL:
-            _out_message[i].prop->setIntValue( atoi(p1) );
+            _in_message[i].prop->setBoolValue( atof(p1) != 0.0 );
             break;
 
         case FG_DOUBLE:
-            val = _out_message[i].offset + strtod(p1, 0) * _out_message[i].factor;
-            _out_message[i].prop->setIntValue(val);
+            val = _in_message[i].offset + strtod(p1, 0) * _in_message[i].factor;
+            _in_message[i].prop->setFloatValue((float)val);
             break;
 
         default: // SG_STRING
-             _out_message[i].prop->setStringValue(p1);
+             _in_message[i].prop->setStringValue(p1);
         }
 
         p1 = p2;
-        i++;
     }
     
     return true;
@@ -267,9 +274,9 @@ FGGeneric::read_config(SGPropertyNode *root, vector<_serial_prot> &msg)
         _serial_prot chunk;
 
      // chunk.name = chunks[i]->getStringValue("name");
-        chunk.format = chunks[i]->getStringValue("format", "%f");
+        chunk.format = chunks[i]->getStringValue("format", "%d");
         chunk.offset = chunks[i]->getDoubleValue("offset");
-        chunk.factor = chunks[i]->getDoubleValue("offset", 1.0);
+        chunk.factor = chunks[i]->getDoubleValue("factor", 1.0);
 
         string node = chunks[i]->getStringValue("node");
         chunk.prop = fgGetNode(node.c_str(), true);