Debian Codegraph @

src/Main/util.cxx

raw symbols SCIP
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
42 #ifdef OSG_LIBRARY_STATIC
43 #include "osgDB/Registry"
44 #endif
45
46 using std::vector;
47
48 // Originally written by Alex Perry.
49 double
50 fgGetLowPass (double current, double target, double timeratio)
51 {
52 if ( timeratio < 0.0 ) {
53 if ( timeratio < -1.0 ) {
54 // time went backwards; kill the filter
55 current = target;
56 } else {
57 // ignore mildly negative time
58 }
59 } else if ( timeratio < 0.2 ) {
60 // Normal mode of operation; fast
61 // approximation to exp(-timeratio)
62 current = current * (1.0 - timeratio) + target * timeratio;
63 } else if ( timeratio > 5.0 ) {
64 // Huge time step; assume filter has settled
65 current = target;
66 } else {
67 // Moderate time step; non linear response
68 double keep = exp(-timeratio);
69 current = current * keep + target * (1.0 - keep);
70 }
71
72 return current;
73 }
74
75 namespace {
76 double innerLowPassPeriodic(double current, double target, double timeratio)
77 {
78 auto currentSigned = SGMiscd::normalizePeriodic(-180.0, 180.0, current);
79 auto targetSigned = SGMiscd::normalizePeriodic(-180.0, 180.0, target);
80 if (fabs(currentSigned - targetSigned) > 180.0) {
81 // value are 'opposite ends', bring them close together so
82 // fgGetLowPass doesn't see the discontinuity
83 // we offset target by 360 in the 'same direction' as current
84 // via copysign
85 targetSigned += copysign(360, currentSigned);
86 }
87
88 return fgGetLowPass(currentSigned, targetSigned, timeratio);
89 }
90 } // namespace
91
92 double
93 flightgear::lowPassPeriodicDegreesPositive(double current, double target, double timeratio)
94 {
95 return SGMiscd::normalizePeriodic(0.0, 360.0, innerLowPassPeriodic(current, target, timeratio));
96 }
97
98 double
99 flightgear::lowPassPeriodicDegreesSigned(double current, double target, double timeratio)
100 {
101 return SGMiscd::normalizePeriodic(-180.0, 180.0, innerLowPassPeriodic(current, target, timeratio));
102 }
103
104 double
105 flightgear::filterExponential(double current, double target, double timeratio)
106 {
107 double factor = 2.0 / (timeratio + 1.0);
108 return (target - current) * factor + current;
109 }
110
111
112 /**
113 * Allowed paths here are absolute, and may contain _one_ *,
114 * which matches any string
115 */
116 void fgInitAllowedPaths()
117 {
118 if(SGPath("ygjmyfvhhnvdoesnotexist").realpath().utf8Str() == "ygjmyfvhhnvdoesnotexist"){
119 // Abort in case this is used with older versions of realpath()
120 // that don't normalize non-existent files, as that would be a security
121 // hole.
122 flightgear::fatalMessageBoxThenExit(
123 "Nasal initialization error",
124 "Version mismatch - please update simgear");
125 }
126 SGPath::clearListOfAllowedPaths(false); // clear list of read-allowed paths
127 SGPath::clearListOfAllowedPaths(true); // clear list of write-allowed paths
128
129 PathList read_paths = globals->get_extra_read_allowed_paths();
130 read_paths.push_back(globals->get_fg_root());
131 read_paths.push_back(globals->get_fg_home());
132
133 for (const auto& path: read_paths) {
134 // If we get the initialization order wrong, better to have an
135 // obvious error than a can-read-everything security hole...
136 if (path.isNull()) {
137 flightgear::fatalMessageBoxThenExit(
138 "Nasal initialization error",
139 "Empty string in FG_ROOT, FG_HOME, FG_AIRCRAFT, FG_SCENERY or "
140 "--allow-nasal-read, or fgInitAllowedPaths() called too early");
141 }
142 SGPath::addAllowedPathPattern(path.realpath().utf8Str() + "/*",
143 false /* write */);
144 SGPath::addAllowedPathPattern(path.realpath().utf8Str(), false);
145 }
146
147 const std::string fg_home = globals->get_fg_home().realpath().utf8Str();
148 SGPath::addAllowedPathPattern(fg_home + "/*.sav", true /* write */);
149 SGPath::addAllowedPathPattern(fg_home + "/*.log", true);
150 SGPath::addAllowedPathPattern(fg_home + "/cache/*", true);
151 SGPath::addAllowedPathPattern(fg_home + "/Export/*", true);
152 SGPath::addAllowedPathPattern(fg_home + "/state/*.xml", true);
153 SGPath::addAllowedPathPattern(fg_home + "/aircraft-data/*.xml", true);
154 SGPath::addAllowedPathPattern(fg_home + "/Wildfire/*.xml", true);
155 SGPath::addAllowedPathPattern(fg_home + "/runtime-jetways/*.xml", true);
156 SGPath::addAllowedPathPattern(fg_home + "/Input/Joysticks/*.xml", true);
157
158 // Check that it works
159 const std::string homePath = globals->get_fg_home().utf8Str();
160 if (! SGPath(homePath + "/../no.log").validate(true).isNull() ||
161 ! SGPath(homePath + "/no.logt").validate(true).isNull() ||
162 ! SGPath(homePath + "/nolog").validate(true).isNull() ||
163 ! SGPath(homePath + "no.log").validate(true).isNull() ||
164 ! SGPath(homePath + "\\..\\no.log").validate(false).isNull() ||
165 SGPath(homePath + "/aircraft-data/yes..xml").validate(true).isNull() ||
166 SGPath(homePath + "/.\\yes.bmp").validate(false).isNull()) {
167 flightgear::fatalMessageBoxThenExit(
168 "Nasal initialization error",
169 "The FG_HOME directory must not be inside any of the FG_ROOT, "
170 "FG_AIRCRAFT, FG_SCENERY or --allow-nasal-read directories",
171 "(check that you have not accidentally included an extra ':', "
172 "as an empty part means the current directory)");
173 }
174 }
175
176 std::string generateAuthorsText(SGPropertyNode* authors)
177 {
178 std::string result;
179 for (auto a : authors->getChildren("author")) {
180 const std::string name = a->getStringValue("name");
181 if (name.empty())
182 continue;
183
184 if (!result.empty())
185 result += ", ";
186 result += a->getStringValue("name");
187 }
188 return result;
189 }
190
191 std::string flightgear::getAircraftAuthorsText()
192 {
193 const auto authorsNode = fgGetNode("sim/authors");
194 if (authorsNode) {
195 // we have structured authors data
196 return generateAuthorsText(authorsNode);
197 }
198
199 // if we hit this point, there is no strucutred authors data
200 return fgGetString("/sim/author");
201 }
202
203 // end of util.cxx