FlightGear next
util.cxx
Go to the documentation of this file.
1// util.cxx - general-purpose utility functions.
2// Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
3//
4// This program is free software; you can redistribute it and/or
5// modify it under the terms of the GNU General Public License as
6// published by the Free Software Foundation; either version 2 of the
7// License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17//
18// $Id$
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include <simgear/compiler.h>
25
26#include <cmath>
27
28#include <cstdlib>
29
30#include <vector>
31
32#include <simgear/debug/logstream.hxx>
33#include <simgear/math/SGLimits.hxx>
34#include <simgear/math/SGMisc.hxx>
35
36#include <GUI/MessageBox.hxx>
37#include "fg_io.hxx"
38#include "fg_props.hxx"
39#include "globals.hxx"
40#include "util.hxx"
41
42using std::vector;
43
44// Originally written by Alex Perry.
45double
46fgGetLowPass (double current, double target, double timeratio)
47{
48 if ( timeratio < 0.0 ) {
49 if ( timeratio < -1.0 ) {
50 // time went backwards; kill the filter
51 current = target;
52 } else {
53 // ignore mildly negative time
54 }
55 } else if ( timeratio < 0.2 ) {
56 // Normal mode of operation; fast
57 // approximation to exp(-timeratio)
58 current = current * (1.0 - timeratio) + target * timeratio;
59 } else if ( timeratio > 5.0 ) {
60 // Huge time step; assume filter has settled
61 current = target;
62 } else {
63 // Moderate time step; non linear response
64 double keep = exp(-timeratio);
65 current = current * keep + target * (1.0 - keep);
66 }
67
68 return current;
69}
70
71namespace {
72double innerLowPassPeriodic(double current, double target, double timeratio)
73{
74 auto currentSigned = SGMiscd::normalizePeriodic(-180.0, 180.0, current);
75 auto targetSigned = SGMiscd::normalizePeriodic(-180.0, 180.0, target);
76 if (fabs(currentSigned - targetSigned) > 180.0) {
77 // value are 'opposite ends', bring them close together so
78 // fgGetLowPass doesn't see the discontinuity
79 // we offset target by 360 in the 'same direction' as current
80 // via copysign
81 targetSigned += copysign(360, currentSigned);
82 }
83
84 return fgGetLowPass(currentSigned, targetSigned, timeratio);
85}
86} // namespace
87
88double
89flightgear::lowPassPeriodicDegreesPositive(double current, double target, double timeratio)
90{
91 return SGMiscd::normalizePeriodic(0.0, 360.0, innerLowPassPeriodic(current, target, timeratio));
92}
93
94double
95flightgear::lowPassPeriodicDegreesSigned(double current, double target, double timeratio)
96{
97 return SGMiscd::normalizePeriodic(-180.0, 180.0, innerLowPassPeriodic(current, target, timeratio));
98}
99
100double
101flightgear::filterExponential(double current, double target, double timeratio)
102{
103 double factor = 2.0 / (timeratio + 1.0);
104 return (target - current) * factor + current;
105}
106
107
113{
114 if(SGPath("ygjmyfvhhnvdoesnotexist").realpath().utf8Str() == "ygjmyfvhhnvdoesnotexist"){
115 // Abort in case this is used with older versions of realpath()
116 // that don't normalize non-existent files, as that would be a security
117 // hole.
119 "Nasal initialization error",
120 "Version mismatch - please update simgear");
121 }
122 SGPath::clearListOfAllowedPaths(false); // clear list of read-allowed paths
123 SGPath::clearListOfAllowedPaths(true); // clear list of write-allowed paths
124
125 PathList read_paths = globals->get_extra_read_allowed_paths();
126 read_paths.push_back(globals->get_fg_root());
127 read_paths.push_back(globals->get_fg_home());
128
129 for (const auto& path: read_paths) {
130 // If we get the initialization order wrong, better to have an
131 // obvious error than a can-read-everything security hole...
132 if (path.isNull()) {
134 "Nasal initialization error",
135 "Empty string in FG_ROOT, FG_HOME, FG_AIRCRAFT, FG_SCENERY or "
136 "--allow-nasal-read, or fgInitAllowedPaths() called too early");
137 }
138 SGPath::addAllowedPathPattern(path.realpath().utf8Str() + "/*",
139 false /* write */);
140 SGPath::addAllowedPathPattern(path.realpath().utf8Str(), false);
141 }
142
143 const std::string fg_home = globals->get_fg_home().realpath().utf8Str();
144 SGPath::addAllowedPathPattern(fg_home + "/*.sav", true /* write */);
145 SGPath::addAllowedPathPattern(fg_home + "/*.log", true);
146 SGPath::addAllowedPathPattern(fg_home + "/cache/*", true);
147 SGPath::addAllowedPathPattern(fg_home + "/Export/*", true);
148 SGPath::addAllowedPathPattern(fg_home + "/state/*.xml", true);
149 SGPath::addAllowedPathPattern(fg_home + "/aircraft-data/*.xml", true);
150 SGPath::addAllowedPathPattern(fg_home + "/Wildfire/*.xml", true);
151 SGPath::addAllowedPathPattern(fg_home + "/runtime-jetways/*.xml", true);
152 SGPath::addAllowedPathPattern(fg_home + "/Input/Joysticks/*.xml", true);
153
154 // Check that it works
155 const std::string homePath = globals->get_fg_home().utf8Str();
156 if (! SGPath(homePath + "/../no.log").validate(true).isNull() ||
157 ! SGPath(homePath + "/no.logt").validate(true).isNull() ||
158 ! SGPath(homePath + "/nolog").validate(true).isNull() ||
159 ! SGPath(homePath + "no.log").validate(true).isNull() ||
160 ! SGPath(homePath + "\\..\\no.log").validate(false).isNull() ||
161 SGPath(homePath + "/aircraft-data/yes..xml").validate(true).isNull() ||
162 SGPath(homePath + "/.\\yes.bmp").validate(false).isNull()) {
164 "Nasal initialization error",
165 "The FG_HOME directory must not be inside any of the FG_ROOT, "
166 "FG_AIRCRAFT, FG_SCENERY or --allow-nasal-read directories",
167 "(check that you have not accidentally included an extra ':', "
168 "as an empty part means the current directory)");
169 }
170}
171
172std::string generateAuthorsText(SGPropertyNode* authors)
173{
174 std::string result;
175 for (auto a : authors->getChildren("author")) {
176 const std::string name = a->getStringValue("name");
177 if (name.empty())
178 continue;
179
180 if (!result.empty())
181 result += ", ";
182 result += a->getStringValue("name");
183 }
184 return result;
185}
186
188{
189 const auto authorsNode = fgGetNode("sim/authors");
190 if (authorsNode) {
191 // we have structured authors data
192 return generateAuthorsText(authorsNode);
193 }
194
195 // if we hit this point, there is no strucutred authors data
196 return fgGetString("/sim/author");
197}
198
199// end of util.cxx
const char * name
std::string fgGetString(const char *name, const char *defaultValue)
Get a string value for a property.
Definition fg_props.cxx:556
FGGlobals * globals
Definition globals.cxx:142
std::vector< SGPath > PathList
Definition globals.hxx:37
std::string getAircraftAuthorsText()
getAircraftAuthorsText - get the aircraft authors as a single string value.
Definition util.cxx:187
double lowPassPeriodicDegreesPositive(double current, double target, double timeratio)
low-pass filter for bearings, etc in degrees.
Definition util.cxx:89
double lowPassPeriodicDegreesSigned(double current, double target, double timeratio)
low-pass filter for bearings, etc in degrees.
Definition util.cxx:95
void fatalMessageBoxThenExit(const std::string &caption, const std::string &msg, const std::string &moreText, int exitStatus, bool reportToSentry)
double filterExponential(double current, double target, double timeratio)
exponential filter
Definition util.cxx:101
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27
double fgGetLowPass(double current, double target, double timeratio)
Move a value towards a target.
Definition util.cxx:46
void fgInitAllowedPaths()
Allowed paths here are absolute, and may contain one *, which matches any string.
Definition util.cxx:112
std::string generateAuthorsText(SGPropertyNode *authors)
Definition util.cxx:172