From 78f7950fa66bbda7f086aebe24d1a88df536caf4 Mon Sep 17 00:00:00 2001 From: Henning Stahlke Date: Fri, 1 Dec 2017 22:59:07 +0100 Subject: [PATCH] YASim: move common helper functions to new yasim-common.cpp --- src/FDM/YASim/CMakeLists.txt | 1 + src/FDM/YASim/ControlMap.cpp | 21 +------------ src/FDM/YASim/ControlMap.hpp | 4 --- src/FDM/YASim/FGFDM.cpp | 18 ------------ src/FDM/YASim/FGFDM.hpp | 3 -- src/FDM/YASim/Vector.hpp | 4 ++- src/FDM/YASim/yasim-common.cpp | 23 +++++++++++++++ src/FDM/YASim/yasim-common.hpp | 54 +++++++++++++++++++--------------- 8 files changed, 58 insertions(+), 70 deletions(-) create mode 100644 src/FDM/YASim/yasim-common.cpp diff --git a/src/FDM/YASim/CMakeLists.txt b/src/FDM/YASim/CMakeLists.txt index 0d7efa881..3065af68a 100644 --- a/src/FDM/YASim/CMakeLists.txt +++ b/src/FDM/YASim/CMakeLists.txt @@ -26,6 +26,7 @@ set(COMMON Turbulence.cpp Wing.cpp Version.cpp + yasim-common.cpp ) set(SOURCES diff --git a/src/FDM/YASim/ControlMap.cpp b/src/FDM/YASim/ControlMap.cpp index 5e6de0478..27dddc72c 100644 --- a/src/FDM/YASim/ControlMap.cpp +++ b/src/FDM/YASim/ControlMap.cpp @@ -2,6 +2,7 @@ # include "config.h" #endif +#include "yasim-common.hpp" #include "Jet.hpp" #include "Thruster.hpp" #include "PropEngine.hpp" @@ -281,26 +282,6 @@ float ControlMap::rangeMax(Control control) } } -/// duplicate null-terminated string -char* ControlMap::dup(const char* s) -{ - int len=0; - while(s[len++]); - char* s2 = new char[len+1]; - char* p = s2; - while((*p++ = *s++)); - s2[len] = 0; - return s2; -} - -/// compare null-terminated strings -bool ControlMap::eq(const char* a, const char* b) -{ - while(*a && *b && *a == *b) { a++; b++; } - // equal if both a and b points to null chars - return !(*a || *b); -} - /// register property name, return ID (int) int ControlMap::propertyHandle(const char* name) { diff --git a/src/FDM/YASim/ControlMap.hpp b/src/FDM/YASim/ControlMap.hpp index 464676ac9..b385187bb 100644 --- a/src/FDM/YASim/ControlMap.hpp +++ b/src/FDM/YASim/ControlMap.hpp @@ -142,10 +142,6 @@ private: Vector _outputs; // control properties Vector _properties; - - // helper - char* dup(const char* s); - bool eq(const char* a, const char* b); }; }; // namespace yasim diff --git a/src/FDM/YASim/FGFDM.cpp b/src/FDM/YASim/FGFDM.cpp index dba161335..f5545fc7e 100644 --- a/src/FDM/YASim/FGFDM.cpp +++ b/src/FDM/YASim/FGFDM.cpp @@ -1114,24 +1114,6 @@ void FGFDM::parseWeight(XMLAttributes* a) _weights.add(wr); } -bool FGFDM::eq(const char* a, const char* b) -{ - // Figure it out for yourself. :) - while(*a && *b && *a == *b) { a++; b++; } - return !(*a || *b); -} - -char* FGFDM::dup(const char* s) -{ - int len=0; - while(s[len++]); - char* s2 = new char[len+1]; - char* p = s2; - while((*p++ = *s++)); - s2[len] = 0; - return s2; -} - int FGFDM::attri(XMLAttributes* atts, const char* attr) { if(!atts->hasAttribute(attr)) { diff --git a/src/FDM/YASim/FGFDM.hpp b/src/FDM/YASim/FGFDM.hpp index 333d0c1a2..49644163f 100644 --- a/src/FDM/YASim/FGFDM.hpp +++ b/src/FDM/YASim/FGFDM.hpp @@ -59,9 +59,6 @@ private: void parseTurbineEngine(XMLAttributes* a); void parsePistonEngine(XMLAttributes* a); void parsePropeller(XMLAttributes* a); - bool eq(const char* a, const char* b); - bool caseeq(const char* a, const char* b); - char* dup(const char* s); int attri(XMLAttributes* atts, const char* attr); int attri(XMLAttributes* atts, const char* attr, int def); float attrf(XMLAttributes* atts, const char* attr); diff --git a/src/FDM/YASim/Vector.hpp b/src/FDM/YASim/Vector.hpp index f290f0ae0..5dbf8b42f 100644 --- a/src/FDM/YASim/Vector.hpp +++ b/src/FDM/YASim/Vector.hpp @@ -3,6 +3,8 @@ #include #include + +namespace yasim { // // Excruciatingly simple vector-of-pointers class. Easy & useful. // No support for addition of elements anywhere but at the end of the @@ -78,5 +80,5 @@ inline void Vector::realloc() delete[] _array; _array = array; } - +}; //namespace yasim #endif // _VECTOR_HPP diff --git a/src/FDM/YASim/yasim-common.cpp b/src/FDM/YASim/yasim-common.cpp new file mode 100644 index 000000000..23bbf7f6a --- /dev/null +++ b/src/FDM/YASim/yasim-common.cpp @@ -0,0 +1,23 @@ +#include "yasim-common.hpp" + +namespace yasim { + /// duplicate null-terminated string + char* dup(const char* s) + { + int len=0; + while(s[len++]); + char* s2 = new char[len+1]; + char* p = s2; + while((*p++ = *s++)); + s2[len] = 0; + return s2; + } + + /// compare null-terminated strings + bool eq(const char* a, const char* b) + { + while(*a && *b && *a == *b) { a++; b++; } + // equal if both a and b points to null chars + return !(*a || *b); + } +}; //namespace yasim diff --git a/src/FDM/YASim/yasim-common.hpp b/src/FDM/YASim/yasim-common.hpp index ed1c4f018..32097af7e 100644 --- a/src/FDM/YASim/yasim-common.hpp +++ b/src/FDM/YASim/yasim-common.hpp @@ -1,34 +1,40 @@ #ifndef _YASIM_COMMON_HPP #define _YASIM_COMMON_HPP +/* + common file for YASim wide constants and static helper functions + */ namespace yasim { - static const float YASIM_PI = 3.14159265358979323846f; - static const float PI2 = YASIM_PI*2; - static const float RAD2DEG = 180/YASIM_PI; - static const float DEG2RAD = YASIM_PI/180; - static const float RPM2RAD = YASIM_PI/30; + static const float YASIM_PI = 3.14159265358979323846f; + static const float PI2 = YASIM_PI*2; + static const float RAD2DEG = 180/YASIM_PI; + static const float DEG2RAD = YASIM_PI/180; + static const float RPM2RAD = YASIM_PI/30; - static const float KTS2MPS = 1852.0f/3600.0f; - static const float MPS2KTS = 3600.0f/1852.0f; - static const float KMH2MPS = 1/3.6f; + static const float KTS2MPS = 1852.0f/3600.0f; + static const float MPS2KTS = 3600.0f/1852.0f; + static const float KMH2MPS = 1/3.6f; - static const float FT2M = 0.3048f; - static const float M2FT = 1/FT2M; + static const float FT2M = 0.3048f; + static const float M2FT = 1/FT2M; - static const float LBS2N = 4.44822f; - static const float N2LB = 1/LBS2N; - static const float LBS2KG = 0.45359237f; - static const float KG2LBS = 1/LBS2KG; - static const float CM2GALS = 264.172037284f; - static const float HP2W = 745.700f; - static const float INHG2PA = 3386.389f; - static const float K2DEGF = 1.8f; - static const float K2DEGFOFFSET = -459.4f; - static const float CIN2CM = 1.6387064e-5f; - - static const float NM2FTLB = (1/(LBS2N*FT2M)); - static const float SLUG2KG = 14.59390f; + static const float LBS2N = 4.44822f; + static const float N2LB = 1/LBS2N; + static const float LBS2KG = 0.45359237f; + static const float KG2LBS = 1/LBS2KG; + static const float CM2GALS = 264.172037284f; + static const float HP2W = 745.700f; + static const float INHG2PA = 3386.389f; + static const float K2DEGF = 1.8f; + static const float K2DEGFOFFSET = -459.4f; + static const float CIN2CM = 1.6387064e-5f; -}; + static const float NM2FTLB = (1/(LBS2N*FT2M)); + static const float SLUG2KG = 14.59390f; + + char* dup(const char* s); + bool eq(const char* a, const char* b); + +}; //namespace yasim #endif // ifndef _YASIM_COMMON_HPP