Skip to content

Commit

Permalink
Make atof_locale_c more pedantic.
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoconni committed Feb 9, 2025
1 parent 0ac6194 commit 8ec1ff7
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 82 deletions.
1 change: 0 additions & 1 deletion src/FGJSBBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ INCLUDES
#include <chrono>

#include "JSBSim_API.h"
#include "input_output/string_utilities.h"

#ifndef M_PI
# define M_PI 3.14159265358979323846
Expand Down
1 change: 1 addition & 0 deletions src/initialization/FGInitialCondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ INCLUDES
#include "input_output/FGXMLFileRead.h"
#include "FGTrim.h"
#include "FGFDMExec.h"
#include "input_output/string_utilities.h"

using namespace std;

Expand Down
19 changes: 8 additions & 11 deletions src/input_output/FGInputSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ INCLUDES
#include "FGFDMExec.h"
#include "models/FGAircraft.h"
#include "input_output/FGXMLElement.h"
#include "input_output/string_utilities.h"

using namespace std;

Expand Down Expand Up @@ -168,17 +169,13 @@ void FGInputSocket::Read(bool Holding)
socket->Reply("Not a leaf property\r\n");
break;
} else {
if (is_number(trim(str_value))) {
try {
double value = atof_locale_c(str_value);
node->setDoubleValue(value);
} catch(BaseException& e) {
socket->Reply(e.what());
break;
}
}
else {
socket->Reply("Invalid number\r\n");
try {
double value = atof_locale_c(str_value);
node->setDoubleValue(value);
} catch(InvalidNumber& e) {
string msg(e.what());
msg += "\r\n";
socket->Reply(msg);
break;
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/input_output/FGOutputSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ INCLUDES
#include "models/atmosphere/FGWinds.h"
#include "input_output/FGXMLElement.h"
#include "math/FGPropertyValue.h"
#include "input_output/string_utilities.h"

using namespace std;

Expand Down Expand Up @@ -85,25 +86,25 @@ void FGOutputSocket::SetOutputName(const string& fname)
// tokenize the output name
size_t dot_pos = fname.find(':', 0);
size_t slash_pos = fname.find('/', 0);

string name = fname.substr(0, dot_pos);

string proto = "TCP";
if(dot_pos + 1 < slash_pos)
proto = fname.substr(dot_pos + 1, slash_pos - dot_pos - 1);

string port = "1138";
if(slash_pos < string::npos)
port = fname.substr(slash_pos + 1, string::npos);

// set the model name
Name = name + ":" + port + "/" + proto;

// set the socket params
SockName = name;

SockPort = atoi(port.c_str());

if (to_upper(proto) == "UDP")
SockProtocol = FGfdmSocket::ptUDP;
else // Default to TCP
Expand Down
1 change: 1 addition & 0 deletions src/input_output/FGOutputTextFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ INCLUDES
#include "models/FGFCS.h"
#include "models/atmosphere/FGWinds.h"
#include "input_output/FGXMLElement.h"
#include "input_output/string_utilities.h"

using namespace std;

Expand Down
1 change: 1 addition & 0 deletions src/input_output/FGScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ INCLUDES
#include "models/FGInput.h"
#include "math/FGCondition.h"
#include "math/FGFunctionValue.h"
#include "input_output/string_utilities.h"

using namespace std;

Expand Down
17 changes: 8 additions & 9 deletions src/input_output/FGUDPInputSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ INCLUDES
#include "FGUDPInputSocket.h"
#include "FGFDMExec.h"
#include "input_output/FGXMLElement.h"
#include "input_output/string_utilities.h"

using namespace std;

Expand Down Expand Up @@ -106,15 +107,13 @@ void FGUDPInputSocket::Read(bool Holding)

vector<double> values;

for (string& token : tokens) {
if (is_number(trim(token))) {
try {
values.push_back(atof_locale_c(token));
} catch(BaseException& e) {
return;
}
}
}
try {
for (string& token : tokens)
values.push_back(atof_locale_c(token));
} catch(InvalidNumber& e) {
cerr << e.what() << endl;
return;
}

if (values[0] < oldTimeStamp) {
return;
Expand Down
14 changes: 8 additions & 6 deletions src/input_output/FGXMLElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ INCLUDES
#include <iostream>
#include <sstream> // for assembling the error messages / what of exceptions.
#include <stdexcept> // using domain_error, invalid_argument, and length_error.

#include "FGXMLElement.h"
#include "FGJSBBase.h"
#include "input_output/string_utilities.h"

using namespace std;

Expand Down Expand Up @@ -293,11 +295,11 @@ double Element::GetAttributeValueAsNumber(const string& attr)
}
else {
double number=0;
if (is_number(trim(attribute)))
try {
number = atof_locale_c(attribute);
else {
} catch (InvalidNumber& e) {
std::stringstream s;
s << ReadFrom() << "Expecting numeric attribute value, but got: " << attribute;
s << ReadFrom() << e.what();
cerr << s.str() << endl;
throw BaseException(s.str());
}
Expand Down Expand Up @@ -347,11 +349,11 @@ double Element::GetDataAsNumber(void)
{
if (data_lines.size() == 1) {
double number=0;
if (is_number(trim(data_lines[0])))
try {
number = atof_locale_c(data_lines[0]);
else {
} catch (InvalidNumber& e) {
std::stringstream s;
s << ReadFrom() << "Expected numeric value, but got: " << data_lines[0];
s << ReadFrom() << e.what();
cerr << s.str() << endl;
throw BaseException(s.str());
}
Expand Down
2 changes: 2 additions & 0 deletions src/input_output/FGfdmSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ INCLUDES
#include <sstream>
#include <cstring>
#include <assert.h>

#include "FGfdmSocket.h"
#include "input_output/string_utilities.h"

using std::cout;
using std::cerr;
Expand Down
35 changes: 28 additions & 7 deletions src/input_output/string_utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ typedef _locale_t locale_t;
#define strtod_l _strtod_l
#endif

using namespace std;

namespace JSBSim {
struct CNumericLocale
{
Expand All @@ -80,12 +82,25 @@ struct CNumericLocale
* Whatever is the current locale of the application, atof_locale_c() reads
* numbers assuming that the decimal point is the period (.)
*/
double atof_locale_c(const std::string& input)
double atof_locale_c(const string& input)
{
const char* first = input.c_str();
if (input.empty()) {
InvalidNumber e("Expecting numeric attribute value, but got an empty string");
cerr << e.what() << endl;
throw e;
}

string v = input;
trim(v);

if (v.find_first_not_of("+-.0123456789Ee") != std::string::npos) {
InvalidNumber e("Expecting numeric attribute value, but got: " + input);
cerr << e.what() << endl;
throw e;
}

const char* first = v.c_str();

// Skip leading whitespaces
while (isspace(*first)) ++first;
//Ignoring the leading '+' sign
if (*first == '+') ++first;

Expand All @@ -104,7 +119,7 @@ double atof_locale_c(const std::string& input)
return value;

std::cerr << s.str() << std::endl;
throw JSBSim::BaseException(s.str());
throw InvalidNumber(s.str());
}


Expand Down Expand Up @@ -158,8 +173,14 @@ bool is_number(const std::string& str)
{
if (str.empty())
return false;
else
return (str.find_first_not_of("+-.0123456789Ee") == std::string::npos);

try {
atof_locale_c(str);
} catch (InvalidNumber&) {
return false;
}

return true;
}

std::vector <std::string> split(std::string str, char d)
Expand Down
7 changes: 7 additions & 0 deletions src/input_output/string_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ INCLUDES
#include <string>
#include <vector>

#include "FGJSBBase.h"

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CLASS DECLARATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
Expand All @@ -56,6 +58,11 @@ JSBSIM_API std::string& to_lower(std::string& str);
JSBSIM_API bool is_number(const std::string& str);
JSBSIM_API std::vector <std::string> split(std::string str, char d);
JSBSIM_API std::string replace(std::string str, const std::string& old, const std::string& newstr);

class JSBSIM_API InvalidNumber : public BaseException {
public:
InvalidNumber(const std::string& msg) : BaseException(msg) {}
};
};

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down
37 changes: 17 additions & 20 deletions src/math/FGFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ INCLUDES
#include "FGRealValue.h"
#include "input_output/FGXMLElement.h"
#include "math/FGFunctionValue.h"
#include "input_output/string_utilities.h"


using namespace std;
Expand Down Expand Up @@ -611,21 +612,19 @@ void FGFunction::Load(Element* el, FGPropertyValue* var, FGFDMExec* fdmex,
string mean_attr = element->GetAttributeValue("mean");
string stddev_attr = element->GetAttributeValue("stddev");
if (!mean_attr.empty()) {
if (is_number(trim(mean_attr)))
try {
mean = atof_locale_c(mean_attr);
else {
cerr << element->ReadFrom()
<< "Expecting a number, but got: " << mean_attr <<endl;
throw BaseException("Invalid number");
} catch (InvalidNumber& e) {
cerr << element->ReadFrom() << e.what() << endl;
throw e;
}
}
if (!stddev_attr.empty()) {
if (is_number(trim(stddev_attr)))
try {
stddev = atof_locale_c(stddev_attr);
else {
cerr << element->ReadFrom()
<< "Expecting a number, but got: " << stddev_attr <<endl;
throw BaseException("Invalid number");
} catch (InvalidNumber& e) {
cerr << element->ReadFrom() << e.what() << endl;
throw e;
}
}
auto generator(makeRandomGenerator(element, fdmex));
Expand All @@ -641,21 +640,19 @@ void FGFunction::Load(Element* el, FGPropertyValue* var, FGFDMExec* fdmex,
string lower_attr = element->GetAttributeValue("lower");
string upper_attr = element->GetAttributeValue("upper");
if (!lower_attr.empty()) {
if (is_number(trim(lower_attr)))
try {
lower = atof_locale_c(lower_attr);
else {
cerr << element->ReadFrom()
<< "Expecting a number, but got: " << lower_attr <<endl;
throw BaseException("Invalid number");
} catch (InvalidNumber &e) {
cerr << element->ReadFrom() << e.what() << endl;
throw e;
}
}
if (!upper_attr.empty()) {
if (is_number(trim(upper_attr)))
try {
upper = atof_locale_c(upper_attr);
else {
cerr << element->ReadFrom()
<< "Expecting a number, but got: " << upper_attr <<endl;
throw BaseException("Invalid number");
} catch (InvalidNumber &e) {
cerr << element->ReadFrom() << e.what() << endl;
throw e;
}
}
auto generator(makeRandomGenerator(element, fdmex));
Expand Down
7 changes: 4 additions & 3 deletions src/math/FGParameterValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "math/FGRealValue.h"
#include "math/FGPropertyValue.h"
#include "input_output/FGXMLElement.h"
#include "input_output/string_utilities.h"

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FORWARD DECLARATIONS
Expand Down Expand Up @@ -77,9 +78,9 @@ class JSBSIM_API FGParameterValue : public FGParameter

FGParameterValue(const std::string& value, std::shared_ptr<FGPropertyManager> pm,
Element* el) {
if (is_number(value)) {
param = new FGRealValue(atof(value.c_str()));
} else {
try {
param = new FGRealValue(atof_locale_c(value.c_str()));
} catch (InvalidNumber&) {
// "value" must be a property if execution passes to here.
param = new FGPropertyValue(value, pm, el);
}
Expand Down
1 change: 1 addition & 0 deletions src/math/FGTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ INCLUDES

#include "FGTable.h"
#include "input_output/FGXMLElement.h"
#include "input_output/string_utilities.h"

using namespace std;

Expand Down
1 change: 1 addition & 0 deletions src/models/FGInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ INCLUDES
#include "input_output/FGXMLFileRead.h"
#include "input_output/FGModelLoader.h"
#include "input_output/FGLog.h"
#include "input_output/string_utilities.h"

using namespace std;

Expand Down
1 change: 1 addition & 0 deletions src/models/FGOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ INCLUDES
#include "input_output/FGXMLFileRead.h"
#include "input_output/FGModelLoader.h"
#include "input_output/FGLog.h"
#include "input_output/string_utilities.h"

using namespace std;

Expand Down
Loading

0 comments on commit 8ec1ff7

Please sign in to comment.