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