]> git.mxchange.org Git - flightgear.git/blob - src/Main/util.cxx
More search functions exposed to Nasal, also airport parking.
[flightgear.git] / src / Main / util.cxx
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 <math.h>
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 "fg_io.hxx"
37 #include "fg_props.hxx"
38 #include "globals.hxx"
39 #include "util.hxx"
40
41 #ifdef OSG_LIBRARY_STATIC
42 #include "osgDB/Registry"
43 #endif
44
45 using std::vector;
46
47 // Originally written by Alex Perry.
48 double
49 fgGetLowPass (double current, double target, double timeratio)
50 {
51     if ( timeratio < 0.0 ) {
52         if ( timeratio < -1.0 ) {
53                                 // time went backwards; kill the filter
54                 current = target;
55         } else {
56                                 // ignore mildly negative time
57         }
58     } else if ( timeratio < 0.2 ) {
59                                 // Normal mode of operation; fast
60                                 // approximation to exp(-timeratio)
61         current = current * (1.0 - timeratio) + target * timeratio;
62     } else if ( timeratio > 5.0 ) {
63                                 // Huge time step; assume filter has settled
64         current = target;
65     } else {
66                                 // Moderate time step; non linear response
67         double keep = exp(-timeratio);
68         current = current * keep + target * (1.0 - keep);
69     }
70
71     return current;
72 }
73
74
75 std::string
76 fgUnescape (const char *s)
77 {
78     std::string r;
79     while (*s) {
80         if (*s != '\\') {
81             r += *s++;
82             continue;
83         }
84         if (!*++s)
85             break;
86         if (*s == '\\') {
87             r += '\\';
88         } else if (*s == 'n') {
89             r += '\n';
90         } else if (*s == 'r') {
91             r += '\r';
92         } else if (*s == 't') {
93             r += '\t';
94         } else if (*s == 'v') {
95             r += '\v';
96         } else if (*s == 'f') {
97             r += '\f';
98         } else if (*s == 'a') {
99             r += '\a';
100         } else if (*s == 'b') {
101             r += '\b';
102         } else if (*s == 'x') {
103             if (!*++s)
104                 break;
105             int v = 0;
106             for (int i = 0; i < 2 && isxdigit(*s); i++, s++)
107                 v = v * 16 + (isdigit(*s) ? *s - '0' : 10 + tolower(*s) - 'a');
108             r += v;
109             continue;
110
111         } else if (*s >= '0' && *s <= '7') {
112             int v = *s++ - '0';
113             for (int i = 0; i < 3 && *s >= '0' && *s <= '7'; i++, s++)
114                 v = v * 8 + *s - '0';
115             r += v;
116             continue;
117
118         } else {
119             r += *s;
120         }
121         s++;
122     }
123     return r;
124 }
125
126
127 // Write out path to validation node and read it back in. A Nasal
128 // listener is supposed to replace the path with a validated version
129 // or an empty string otherwise.
130 const char *fgValidatePath (const char *str, bool write)
131 {
132     static SGPropertyNode_ptr r, w;
133     if (!r) {
134         r = fgGetNode("/sim/paths/validate/read", true);
135         r->setAttribute(SGPropertyNode::READ, true);
136         r->setAttribute(SGPropertyNode::WRITE, true);
137
138         w = fgGetNode("/sim/paths/validate/write", true);
139         w->setAttribute(SGPropertyNode::READ, true);
140         w->setAttribute(SGPropertyNode::WRITE, true);
141     }
142     SGPropertyNode *prop = write ? w : r;
143     prop->setStringValue(str);
144     const char *result = prop->getStringValue();
145     return result[0] ? result : 0;
146 }
147
148 // end of util.cxx
149