Compare commits

...

15 Commits

Author SHA1 Message Date
curt
0bcdb7eb70 Ready for 0.0.13 release. 2000-09-14 16:56:13 +00:00
curt
a92b71fffc MSVC changes ... 2000-09-13 20:21:39 +00:00
curt
89960ab715 MacOS portability tweaks from Darrell Walisser. 2000-09-13 19:29:01 +00:00
curt
2a369803ae MACOS -> macintosh 2000-09-09 20:53:44 +00:00
curt
b0134a377e Added a touch of error checking to the screen dump routine, i.e. don't
die if you can't open the output file.
2000-09-09 13:31:45 +00:00
curt
502c650cd9 Sep. 8, 2000 updates from David Megginson. 2000-09-08 18:34:45 +00:00
curt
a1bf8d2229 9/05/2000 updates from David Megginson relating to the property manager and
easyxml support.  This is a bit of simgear side support to facilitate a
configurable instrument panel in flightgear.
2000-09-05 21:38:00 +00:00
curt
25b743bdec Preserve initial "state" by careful use of glPushAttrib() and glPopAttrib()
being careful to manually call getState()->apply() before the glPushAttrib()
so that the ssg state handling mechanism doesn't get derailed accidentally.
2000-08-22 02:40:13 +00:00
curt
dbf7db02a2 Tiny bug fix/tweak. 2000-08-08 14:54:09 +00:00
curt
8c998659c6 Missed some files needed in make dist. 2000-08-08 03:21:26 +00:00
curt
4e23b3dff5 Clean up constructor a bit. 2000-08-08 03:20:02 +00:00
curt
159d3c4c6c Restructured the magvar code a bit to make it more usable. 2000-08-07 18:26:35 +00:00
curt
612b5ae0bc Added simgear/xml. 2000-07-27 02:55:47 +00:00
curt
60cbe9c1d4 Initial revision. 2000-07-26 19:17:43 +00:00
curt
4dadd29727 Updates for 0.0.13 2000-07-22 21:34:58 +00:00
53 changed files with 11191 additions and 665 deletions

10
NEWS
View File

@@ -1,3 +1,13 @@
New in 0.0.13
* September 14, 2000
* Added support for reading and writing xml files (easyxml)
* Then updates to property manager and xml code.
* Update magnetic variation code.
* Sky code now uses glPushAttrib and glPopAttrib so it plays better with
other ssg based apps.
* MacOS tweaks.
* MSVC tweaks.
New in 0.0.12
* July 19, 2000
* Converted project license from GPL to LGPL.

View File

@@ -6,7 +6,7 @@ dnl $Id$
AC_INIT(simgear/bucket/newbucket.cxx)
dnl Initialize the automake stuff
AM_INIT_AUTOMAKE(SimGear, 0.0.12)
AM_INIT_AUTOMAKE(SimGear, 0.0.13)
dnl Checks for programs.
AC_PROG_MAKE_SET
@@ -262,6 +262,7 @@ AC_OUTPUT( \
simgear/sky/Makefile \
simgear/timing/Makefile \
simgear/xgl/Makefile \
simgear/xml/Makefile \
simgear/zlib/Makefile \
)

View File

@@ -38,5 +38,6 @@ SUBDIRS = \
sky \
timing \
xgl \
xml \
$(ZLIB_DIRS)

View File

@@ -118,7 +118,7 @@
# define bcopy(from, to, n) memcpy(to, from, n)
// -rp- please use FG_MEM_COPY everywhere !
# define FG_MEM_COPY(to,from,n) memcpy(to, from, n)
// #define FG_MEM_COPY(to,from,n) memcpy(to, from, n)
// -dw- currently used glut has no game mode stuff
# define GLUT_WRONG_VERSION

View File

@@ -36,16 +36,16 @@
#include <plib/sg.h>
#include "star.hxx"
#include "moon.hxx"
#include "mercury.hxx"
#include "venus.hxx"
#include "mars.hxx"
#include "jupiter.hxx"
#include "saturn.hxx"
#include "uranus.hxx"
#include "neptune.hxx"
#include "stars.hxx"
#include <simgear/ephemeris/star.hxx>
#include <simgear/ephemeris/moon.hxx>
#include <simgear/ephemeris/mercury.hxx>
#include <simgear/ephemeris/venus.hxx>
#include <simgear/ephemeris/mars.hxx>
#include <simgear/ephemeris/jupiter.hxx>
#include <simgear/ephemeris/saturn.hxx>
#include <simgear/ephemeris/uranus.hxx>
#include <simgear/ephemeris/neptune.hxx>
#include <simgear/ephemeris/stars.hxx>
class SGEphemeris {

View File

@@ -25,8 +25,8 @@
#ifndef _MERCURY_HXX_
#define _MERCURY_HXX_
#include "celestialBody.hxx"
#include "star.hxx"
#include <simgear/ephemeris/celestialBody.hxx>
#include <simgear/ephemeris/star.hxx>
class Mercury : public CelestialBody
{

View File

@@ -28,8 +28,8 @@
#include <simgear/constants.h>
#include "celestialBody.hxx"
#include "star.hxx"
#include <simgear/ephemeris/celestialBody.hxx>
#include <simgear/ephemeris/star.hxx>
class Moon : public CelestialBody
{

View File

@@ -26,7 +26,7 @@
#define _STAR_HXX_
#include "celestialBody.hxx"
#include <simgear/ephemeris/celestialBody.hxx>
class Star : public CelestialBody

View File

@@ -4,7 +4,9 @@ lib_LIBRARIES = libsgmagvar.a
include_HEADERS = magvar.hxx
libsgmagvar_a_SOURCES = magvar.cxx
libsgmagvar_a_SOURCES = \
coremag.hxx coremag.cxx \
magvar.cxx
noinst_PROGRAMS = testmagvar

453
simgear/magvar/coremag.cxx Normal file
View File

@@ -0,0 +1,453 @@
// coremag.cxx -- compute local magnetic variation given position,
// altitude, and date
//
// This is an implementation of the NIMA (formerly DMA) WMM2000
//
// http://www.nima.mil/GandG/ngdc-wmm2000.html
//
// Copyright (C) 2000 Edward A Williams <Ed_Williams@compuserve.com>
//
// Adapted from Excel 3.0 version 3/27/94 EAW
// Recoded in C++ by Starry Chan
// WMM95 added and rearranged in ANSI-C EAW 7/9/95
// Put shell around program and made Borland & GCC compatible EAW 11/22/95
// IGRF95 added 2/96 EAW
// WMM2000 IGR2000 added 2/00 EAW
// Released under GPL 3/26/00 EAW
// Adaptions and modifications for the SimGear project 3/27/2000 CLO
//
// Removed all pow() calls and made static roots[][] arrays to
// save many sqrt() calls on subsequent invocations
// left old code as SGMagVarOrig() for testing purposes
// 3/28/2000 Norman Vine -- nhv@yahoo.com
//
// Put in some bullet-proofing to handle magnetic and geographic poles.
// 3/28/2000 EAW
// The routine uses a spherical harmonic expansion of the magnetic
// potential up to twelfth order, together with its time variation, as
// described in Chapter 4 of "Geomagnetism, Vol 1, Ed. J.A.Jacobs,
// Academic Press (London 1987)". The program first converts geodetic
// coordinates (lat/long on elliptic earth and altitude) to spherical
// geocentric (spherical lat/long and radius) coordinates. Using this,
// the spherical (B_r, B_theta, B_phi) magnetic field components are
// computed from the model. These are finally referred to surface (X, Y,
// Z) coordinates.
//
// Fields are accurate to better than 200nT, variation and dip to
// better than 0.5 degrees, with the exception of the declination near
// the magnetic poles (where it is ill-defined) where the error may reach
// 4 degrees or more.
//
// Variation is undefined at both the geographic and
// magnetic poles, even though the field itself is well-behaved. To
// avoid the routine blowing up, latitude entries corresponding to
// the geographic poles are slightly offset. At the magnetic poles,
// the routine returns zero variation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// $Id$
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <simgear/constants.h>
#include "coremag.hxx"
#define max(a,b) (((a) > (b)) ? (a) : (b))
static const double pi = 3.14159265358979;
static const double a = 6378.16; /* major radius (km) IAU66 ellipsoid */
static const double f = 1.0 / 298.25; /* inverse flattening IAU66 ellipsoid */
static const double b = 6378.16 * (1.0 -1.0 / 298.25 );
/* minor radius b=a*(1-f) */
static const double r_0 = 6371.2; /* "mean radius" for spherical harmonic expansion */
static double gnm_wmm2000[13][13] =
{
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-29616.0, -1722.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-2266.7, 3070.2, 1677.6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{1322.4, -2291.5, 1255.9, 724.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{932.1, 786.3, 250.6, -401.5, 106.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-211.9, 351.6, 220.8, -134.5, -168.8, -13.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{73.8, 68.2, 74.1, -163.5, -3.8, 17.1, -85.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{77.4, -73.9, 2.2, 35.7, 7.3, 5.2, 8.4, -1.5, 0.0, 0.0, 0.0, 0.0, 0.0},
{23.3, 7.3, -8.5, -6.6, -16.9, 8.6, 4.9, -7.8, -7.6, 0.0, 0.0, 0.0, 0.0},
{5.7, 8.5, 2.0, -9.8, 7.6, -7.0, -2.0, 9.2, -2.2, -6.6, 0.0, 0.0, 0.0},
{-2.2, -5.7, 1.6, -3.7, -0.6, 4.1, 2.2, 2.2, 4.6, 2.3, 0.1, 0.0, 0.0},
{3.3, -1.1, -2.4, 2.6, -1.3, -1.7, -0.6, 0.4, 0.7, -0.3, 2.3, 4.2, 0.0},
{-1.5, -0.2, -0.3, 0.5, 0.2, 0.9, -1.4, 0.6, -0.6, -1.0, -0.3, 0.3, 0.4},
};
static double hnm_wmm2000[13][13]=
{
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 5194.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -2484.8, -467.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -224.7, 293.0, -486.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 273.3, -227.9, 120.9, -302.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 42.0, 173.8, -135.0, -38.6, 105.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -17.4, 61.2, 63.2, -62.9, 0.2, 43.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -62.3, -24.5, 8.9, 23.4, 15.0, -27.6, -7.8, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 12.4, -20.8, 8.4, -21.2, 15.5, 9.1, -15.5, -5.4, 0.0, 0.0, 0.0, 0.0},
{0.0, -20.4, 13.9, 12.0, -6.2, -8.6, 9.4, 5.0, -8.4, 3.2, 0.0, 0.0, 0.0},
{0.0, 0.9, -0.7, 3.9, 4.8, -5.3, -1.0, -2.4, 1.3, -2.3, -6.4, 0.0, 0.0},
{0.0, -1.5, 0.7, -1.1, -2.3, 1.3, -0.6, -2.8, -1.6, -0.1, -1.9, 1.4, 0.0},
{0.0, -1.0, 0.7, 2.2, -2.5, -0.2, 0.0, -0.2, 0.0, 0.2, -0.9, -0.2, 1.0},
};
static double gtnm_wmm2000[13][13]=
{
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{14.7, 11.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-13.6, -0.7, -1.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.3, -4.3, 0.9, -8.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-1.6, 0.9, -7.6, 2.2, -3.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-0.9, -0.2, -2.5, -2.7, -0.9, 1.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{1.2, 0.2, 1.7, 1.6, -0.1, -0.3, 0.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-0.4, -0.8, -0.2, 1.1, 0.4, 0.0, -0.2, -0.2, 0.0, 0.0, 0.0, 0.0, 0.0},
{-0.3, 0.6, -0.8, 0.3, -0.2, 0.5, 0.0, -0.6, 0.1, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
};
static double htnm_wmm2000[13][13]=
{
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -20.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -21.5, -9.6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 6.4, -1.3, -13.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 2.3, 0.7, 3.7, -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 2.1, 2.3, 3.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -0.3, -1.7, -0.9, -1.0, -0.1, 1.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 1.4, 0.2, 0.7, 0.4, -0.3, -0.8, -0.1, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -0.5, 0.1, -0.2, 0.0, 0.1, -0.1, 0.3, 0.2, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
};
static const int nmax = 12;
static double P[13][13];
static double DP[13][13];
static double gnm[13][13];
static double hnm[13][13];
static double sm[13];
static double cm[13];
static double root[13];
static double roots[13][13][2];
/* Convert date to Julian day 1950-2049 */
unsigned long int yymmdd_to_julian_days( int yy, int mm, int dd )
{
unsigned long jd;
yy = (yy < 50) ? (2000 + yy) : (1900 + yy);
jd = dd - 32075L + 1461L * (yy + 4800L + (mm - 14) / 12 ) / 4;
jd = jd + 367L * (mm - 2 - (mm - 14) / 12*12) / 12;
jd = jd - 3 * ((yy + 4900L + (mm - 14) / 12) / 100) / 4;
/* printf("julian date = %d\n", jd ); */
return jd;
}
/*
* return variation (in radians) given geodetic latitude (radians),
* longitude(radians), height (km) and (Julian) date
* N and E lat and long are positive, S and W negative
*/
double calc_magvar( double lat, double lon, double h, long dat, double* field )
{
/* output field B_r,B_th,B_phi,B_x,B_y,B_z */
int n,m;
/* reference dates */
long date0_wmm2000 = yymmdd_to_julian_days(0,1,1);
double yearfrac,sr,r,theta,c,s,psi,fn,fn_0,B_r,B_theta,B_phi,X,Y,Z;
double sinpsi, cospsi, inv_s;
static int been_here = 0;
double sinlat = sin(lat);
double coslat = cos(lat);
/* convert to geocentric coords: */
// sr = sqrt(pow(a*coslat,2.0)+pow(b*sinlat,2.0));
sr = sqrt(a*a*coslat*coslat + b*b*sinlat*sinlat);
/* sr is effective radius */
theta = atan2(coslat * (h*sr + a*a),
sinlat * (h*sr + b*b));
/* theta is geocentric co-latitude */
r = h*h + 2.0*h * sr +
(a*a*a*a - ( a*a*a*a - b*b*b*b ) * sinlat*sinlat ) /
(a*a - (a*a - b*b) * sinlat*sinlat );
r = sqrt(r);
/* r is geocentric radial distance */
c = cos(theta);
s = sin(theta);
/* protect against zero divide at geographic poles */
inv_s = 1.0 / (s + (s == 0.)*1.0e-8);
/* zero out arrays */
for ( n = 0; n <= nmax; n++ ) {
for ( m = 0; m <= n; m++ ) {
P[n][m] = 0;
DP[n][m] = 0;
}
}
/* diagonal elements */
P[0][0] = 1;
P[1][1] = s;
DP[0][0] = 0;
DP[1][1] = c;
P[1][0] = c ;
DP[1][0] = -s;
// these values will not change for subsequent function calls
if( !been_here ) {
for ( n = 2; n <= nmax; n++ ) {
root[n] = sqrt((2.0*n-1) / (2.0*n));
}
for ( m = 0; m <= nmax; m++ ) {
double mm = m*m;
for ( n = max(m + 1, 2); n <= nmax; n++ ) {
roots[m][n][0] = sqrt((n-1)*(n-1) - mm);
roots[m][n][1] = 1.0 / sqrt( n*n - mm);
}
}
been_here = 1;
}
for ( n=2; n <= nmax; n++ ) {
// double root = sqrt((2.0*n-1) / (2.0*n));
P[n][n] = P[n-1][n-1] * s * root[n];
DP[n][n] = (DP[n-1][n-1] * s + P[n-1][n-1] * c) *
root[n];
}
/* lower triangle */
for ( m = 0; m <= nmax; m++ ) {
// double mm = m*m;
for ( n = max(m + 1, 2); n <= nmax; n++ ) {
// double root1 = sqrt((n-1)*(n-1) - mm);
// double root2 = 1.0 / sqrt( n*n - mm);
P[n][m] = (P[n-1][m] * c * (2.0*n-1) -
P[n-2][m] * roots[m][n][0]) *
roots[m][n][1];
DP[n][m] = ((DP[n-1][m] * c - P[n-1][m] * s) *
(2.0*n-1) - DP[n-2][m] * roots[m][n][0]) *
roots[m][n][1];
}
}
/* compute gnm, hnm at dat */
/* WMM2000 */
yearfrac = (dat - date0_wmm2000) / 365.25;
for ( n = 1; n <= nmax; n++ ) {
for ( m = 0; m <= nmax; m++ ) {
gnm[n][m] = gnm_wmm2000[n][m] + yearfrac * gtnm_wmm2000[n][m];
hnm[n][m] = hnm_wmm2000[n][m] + yearfrac * htnm_wmm2000[n][m];
}
}
/* compute sm (sin(m lon) and cm (cos(m lon)) */
for ( m = 0; m <= nmax; m++ ) {
sm[m] = sin(m * lon);
cm[m] = cos(m * lon);
}
/* compute B fields */
B_r = 0.0;
B_theta = 0.0;
B_phi = 0.0;
fn_0 = r_0/r;
fn = fn_0 * fn_0;
for ( n = 1; n <= nmax; n++ ) {
double c1_n=0;
double c2_n=0;
double c3_n=0;
for ( m = 0; m <= n; m++ ) {
double tmp = (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]);
c1_n=c1_n + tmp * P[n][m];
c2_n=c2_n + tmp * DP[n][m];
c3_n=c3_n + m * (gnm[n][m] * sm[m] - hnm[n][m] * cm[m]) * P[n][m];
}
// fn=pow(r_0/r,n+2.0);
fn *= fn_0;
B_r = B_r + (n + 1) * c1_n * fn;
B_theta = B_theta - c2_n * fn;
B_phi = B_phi + c3_n * fn * inv_s;
}
/* Find geodetic field components: */
psi = theta - ((pi / 2.0) - lat);
sinpsi = sin(psi);
cospsi = cos(psi);
X = -B_theta * cospsi - B_r * sinpsi;
Y = B_phi;
Z = B_theta * sinpsi - B_r * cospsi;
field[0]=B_r;
field[1]=B_theta;
field[2]=B_phi;
field[3]=X;
field[4]=Y;
field[5]=Z; /* output fields */
/* find variation in radians */
/* return zero variation at magnetic pole X=Y=0. */
/* E is positive */
return (X != 0. || Y != 0.) ? atan2(Y, X) : (double) 0.;
}
#ifdef TEST_NHV_HACKS
double SGMagVarOrig( double lat, double lon, double h, long dat, double* field )
{
/* output field B_r,B_th,B_phi,B_x,B_y,B_z */
int n,m;
/* reference dates */
long date0_wmm2000 = yymmdd_to_julian_days(0,1,1);
double yearfrac,sr,r,theta,c,s,psi,fn,B_r,B_theta,B_phi,X,Y,Z;
/* convert to geocentric coords: */
sr = sqrt(pow(a*cos(lat),2.0)+pow(b*sin(lat),2.0));
/* sr is effective radius */
theta = atan2(cos(lat) * (h * sr + a * a),
sin(lat) * (h * sr + b * b));
/* theta is geocentric co-latitude */
r = h * h + 2.0*h * sr +
(pow(a,4.0) - (pow(a,4.0) - pow(b,4.0)) * pow(sin(lat),2.0)) /
(a * a - (a * a - b * b) * pow(sin(lat),2.0));
r = sqrt(r);
/* r is geocentric radial distance */
c = cos(theta);
s = sin(theta);
/* zero out arrays */
for ( n = 0; n <= nmax; n++ ) {
for ( m = 0; m <= n; m++ ) {
P[n][m] = 0;
DP[n][m] = 0;
}
}
/* diagonal elements */
P[0][0] = 1;
P[1][1] = s;
DP[0][0] = 0;
DP[1][1] = c;
P[1][0] = c ;
DP[1][0] = -s;
for ( n = 2; n <= nmax; n++ ) {
P[n][n] = P[n-1][n-1] * s * sqrt((2.0*n-1) / (2.0*n));
DP[n][n] = (DP[n-1][n-1] * s + P[n-1][n-1] * c) *
sqrt((2.0*n-1) / (2.0*n));
}
/* lower triangle */
for ( m = 0; m <= nmax; m++ ) {
for ( n = max(m + 1, 2); n <= nmax; n++ ) {
P[n][m] = (P[n-1][m] * c * (2.0*n-1) - P[n-2][m] *
sqrt(1.0*(n-1)*(n-1) - m * m)) /
sqrt(1.0* n * n - m * m);
DP[n][m] = ((DP[n-1][m] * c - P[n-1][m] * s) *
(2.0*n-1) - DP[n-2][m] *
sqrt(1.0*(n-1) * (n-1) - m * m)) /
sqrt(1.0* n * n - m * m);
}
}
/* compute gnm, hnm at dat */
/* WMM2000 */
yearfrac = (dat - date0_wmm2000) / 365.25;
for ( n = 1; n <= nmax; n++ ) {
for ( m = 0; m <= nmax; m++ ) {
gnm[n][m] = gnm_wmm2000[n][m] + yearfrac * gtnm_wmm2000[n][m];
hnm[n][m] = hnm_wmm2000[n][m] + yearfrac * htnm_wmm2000[n][m];
}
}
/* compute sm (sin(m lon) and cm (cos(m lon)) */
for ( m = 0; m <= nmax; m++ ) {
sm[m] = sin(m * lon);
cm[m] = cos(m * lon);
}
/* compute B fields */
B_r = 0.0;
B_theta = 0.0;
B_phi = 0.0;
for ( n = 1; n <= nmax; n++ ) {
double c1_n=0;
double c2_n=0;
double c3_n=0;
for ( m = 0; m <= n; m++ ) {
c1_n=c1_n + (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]) * P[n][m];
c2_n=c2_n + (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]) * DP[n][m];
c3_n=c3_n + m * (gnm[n][m] * sm[m] - hnm[n][m] * cm[m]) * P[n][m];
}
fn=pow(r_0/r,n+2.0);
B_r = B_r + (n + 1) * c1_n * fn;
B_theta = B_theta - c2_n * fn;
B_phi = B_phi + c3_n * fn / s;
}
/* Find geodetic field components: */
psi = theta - (pi / 2.0 - lat);
X = -B_theta * cos(psi) - B_r * sin(psi);
Y = B_phi;
Z = B_theta * sin(psi) - B_r * cos(psi);
field[0]=B_r;
field[1]=B_theta;
field[2]=B_phi;
field[3]=X;
field[4]=Y;
field[5]=Z; /* output fields */
/* find variation, leave in radians! */
return atan2(Y, X); /* E is positive */
}
#endif // TEST_NHV_HACKS

View File

@@ -0,0 +1,51 @@
// coremag.hxx -- compute local magnetic variation given position,
// altitude, and date
//
// This is an implimentation of the NIMA WMM 2000
//
// http://www.nima.mil/GandG/ngdc-wmm2000.html
//
// Copyright (C) 2000 Edward A Williams <Ed_Williams@compuserve.com>
//
// Adapted from Excel 3.0 version 3/27/94 EAW
// Recoded in C++ by Starry Chan
// WMM95 added and rearranged in ANSI-C EAW 7/9/95
// Put shell around program and made Borland & GCC compatible EAW 11/22/95
// IGRF95 added 2/96 EAW
// WMM2000 IGR2000 added 2/00 EAW
// Released under GPL 3/26/00 EAW
// Adaptions and modifications for the SimGear project 3/27/2000 CLO
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// $Id$
#ifndef SG_MAGVAR_HXX
#define SG_MAGVAR_HXX
/* Convert date to Julian day 1950-2049 */
unsigned long int yymmdd_to_julian_days( int yy, int mm, int dd );
/* return variation (in degrees) given geodetic latitude (radians), longitude
(radians) ,height (km) and (Julian) date
N and E lat and long are positive, S and W negative
*/
double calc_magvar( double lat, double lon, double h, long dat, double* field );
#endif // SG_MAGVAR_HXX

View File

@@ -1,465 +1,51 @@
// magvar.cxx -- compute local magnetic variation given position,
// altitude, and date
// magvar.cxx -- magnetic variation wrapper class
//
// This is an implementation of the NIMA (formerly DMA) WMM2000
// Written by Curtis Olson, started July 2000.
//
// http://www.nima.mil/GandG/ngdc-wmm2000.html
// Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
//
// Copyright (C) 2000 Edward A Williams <Ed_Williams@compuserve.com>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// Adapted from Excel 3.0 version 3/27/94 EAW
// Recoded in C++ by Starry Chan
// WMM95 added and rearranged in ANSI-C EAW 7/9/95
// Put shell around program and made Borland & GCC compatible EAW 11/22/95
// IGRF95 added 2/96 EAW
// WMM2000 IGR2000 added 2/00 EAW
// Released under GPL 3/26/00 EAW
// Adaptions and modifications for the SimGear project 3/27/2000 CLO
//
// Removed all pow() calls and made static roots[][] arrays to
// save many sqrt() calls on subsequent invocations
// left old code as SGMagVarOrig() for testing purposes
// 3/28/2000 Norman Vine -- nhv@yahoo.com
//
// Put in some bullet-proofing to handle magnetic and geographic poles.
// 3/28/2000 EAW
// The routine uses a spherical harmonic expansion of the magnetic
// potential up to twelfth order, together with its time variation, as
// described in Chapter 4 of "Geomagnetism, Vol 1, Ed. J.A.Jacobs,
// Academic Press (London 1987)". The program first converts geodetic
// coordinates (lat/long on elliptic earth and altitude) to spherical
// geocentric (spherical lat/long and radius) coordinates. Using this,
// the spherical (B_r, B_theta, B_phi) magnetic field components are
// computed from the model. These are finally referred to surface (X, Y,
// Z) coordinates.
//
// Fields are accurate to better than 200nT, variation and dip to
// better than 0.5 degrees, with the exception of the declination near
// the magnetic poles (where it is ill-defined) where the error may reach
// 4 degrees or more.
//
// Variation is undefined at both the geographic and
// magnetic poles, even though the field itself is well-behaved. To
// avoid the routine blowing up, latitude entries corresponding to
// the geographic poles are slightly offset. At the magnetic poles,
// the routine returns zero variation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
// General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// $Id$
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <math.h>
#include <simgear/misc/fgpath.hxx>
#include <simgear/magvar/magvar.hxx>
#include "coremag.hxx"
#include "magvar.hxx"
#define max(a,b) (((a) > (b)) ? (a) : (b))
SGMagVar::SGMagVar() {
}
static const double pi = 3.14159265358979;
static const double a = 6378.16; /* major radius (km) IAU66 ellipsoid */
static const double f = 1.0 / 298.25; /* inverse flattening IAU66 ellipsoid */
static const double b = 6378.16 * (1.0 -1.0 / 298.25 );
/* minor radius b=a*(1-f) */
static const double r_0 = 6371.2; /* "mean radius" for spherical harmonic expansion */
static double gnm_wmm2000[13][13] =
{
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-29616.0, -1722.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-2266.7, 3070.2, 1677.6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{1322.4, -2291.5, 1255.9, 724.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{932.1, 786.3, 250.6, -401.5, 106.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-211.9, 351.6, 220.8, -134.5, -168.8, -13.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{73.8, 68.2, 74.1, -163.5, -3.8, 17.1, -85.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{77.4, -73.9, 2.2, 35.7, 7.3, 5.2, 8.4, -1.5, 0.0, 0.0, 0.0, 0.0, 0.0},
{23.3, 7.3, -8.5, -6.6, -16.9, 8.6, 4.9, -7.8, -7.6, 0.0, 0.0, 0.0, 0.0},
{5.7, 8.5, 2.0, -9.8, 7.6, -7.0, -2.0, 9.2, -2.2, -6.6, 0.0, 0.0, 0.0},
{-2.2, -5.7, 1.6, -3.7, -0.6, 4.1, 2.2, 2.2, 4.6, 2.3, 0.1, 0.0, 0.0},
{3.3, -1.1, -2.4, 2.6, -1.3, -1.7, -0.6, 0.4, 0.7, -0.3, 2.3, 4.2, 0.0},
{-1.5, -0.2, -0.3, 0.5, 0.2, 0.9, -1.4, 0.6, -0.6, -1.0, -0.3, 0.3, 0.4},
};
static double hnm_wmm2000[13][13]=
{
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 5194.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -2484.8, -467.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -224.7, 293.0, -486.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 273.3, -227.9, 120.9, -302.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 42.0, 173.8, -135.0, -38.6, 105.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -17.4, 61.2, 63.2, -62.9, 0.2, 43.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -62.3, -24.5, 8.9, 23.4, 15.0, -27.6, -7.8, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 12.4, -20.8, 8.4, -21.2, 15.5, 9.1, -15.5, -5.4, 0.0, 0.0, 0.0, 0.0},
{0.0, -20.4, 13.9, 12.0, -6.2, -8.6, 9.4, 5.0, -8.4, 3.2, 0.0, 0.0, 0.0},
{0.0, 0.9, -0.7, 3.9, 4.8, -5.3, -1.0, -2.4, 1.3, -2.3, -6.4, 0.0, 0.0},
{0.0, -1.5, 0.7, -1.1, -2.3, 1.3, -0.6, -2.8, -1.6, -0.1, -1.9, 1.4, 0.0},
{0.0, -1.0, 0.7, 2.2, -2.5, -0.2, 0.0, -0.2, 0.0, 0.2, -0.9, -0.2, 1.0},
};
static double gtnm_wmm2000[13][13]=
{
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{14.7, 11.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-13.6, -0.7, -1.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.3, -4.3, 0.9, -8.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-1.6, 0.9, -7.6, 2.2, -3.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-0.9, -0.2, -2.5, -2.7, -0.9, 1.7, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{1.2, 0.2, 1.7, 1.6, -0.1, -0.3, 0.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{-0.4, -0.8, -0.2, 1.1, 0.4, 0.0, -0.2, -0.2, 0.0, 0.0, 0.0, 0.0, 0.0},
{-0.3, 0.6, -0.8, 0.3, -0.2, 0.5, 0.0, -0.6, 0.1, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
};
static double htnm_wmm2000[13][13]=
{
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -20.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -21.5, -9.6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 6.4, -1.3, -13.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 2.3, 0.7, 3.7, -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 2.1, 2.3, 3.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -0.3, -1.7, -0.9, -1.0, -0.1, 1.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 1.4, 0.2, 0.7, 0.4, -0.3, -0.8, -0.1, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, -0.5, 0.1, -0.2, 0.0, 0.1, -0.1, 0.3, 0.2, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
};
static const int nmax = 12;
static double P[13][13];
static double DP[13][13];
static double gnm[13][13];
static double hnm[13][13];
static double sm[13];
static double cm[13];
static double root[13];
static double roots[13][13][2];
/* Convert date to Julian day 1950-2049 */
unsigned long int yymmdd_to_julian_days( int yy, int mm, int dd )
{
unsigned long jd;
yy = (yy < 50) ? (2000 + yy) : (1900 + yy);
jd = dd - 32075L + 1461L * (yy + 4800L + (mm - 14) / 12 ) / 4;
jd = jd + 367L * (mm - 2 - (mm - 14) / 12*12) / 12;
jd = jd - 3 * ((yy + 4900L + (mm - 14) / 12) / 100) / 4;
/* printf("julian date = %d\n", jd ); */
return jd;
}
/* Convert degrees to radians */
double deg_to_rad( double deg )
{
return deg*pi/180.;
SGMagVar::~SGMagVar() {
}
/* Convert radians to degrees */
double rad_to_deg( double rad )
{
return rad*180./pi;
}
/*
* return variation (in radians) given geodetic latitude (radians),
* longitude(radians), height (km) and (Julian) date
* N and E lat and long are positive, S and W negative
*/
double SGMagVar( double lat, double lon, double h, long dat, double* field )
{
/* output field B_r,B_th,B_phi,B_x,B_y,B_z */
int n,m;
/* reference dates */
long date0_wmm2000 = yymmdd_to_julian_days(0,1,1);
double yearfrac,sr,r,theta,c,s,psi,fn,fn_0,B_r,B_theta,B_phi,X,Y,Z;
double sinpsi, cospsi, inv_s;
static int been_here = 0;
double sinlat = sin(lat);
double coslat = cos(lat);
/* convert to geocentric coords: */
// sr = sqrt(pow(a*coslat,2.0)+pow(b*sinlat,2.0));
sr = sqrt(a*a*coslat*coslat + b*b*sinlat*sinlat);
/* sr is effective radius */
theta = atan2(coslat * (h*sr + a*a),
sinlat * (h*sr + b*b));
/* theta is geocentric co-latitude */
r = h*h + 2.0*h * sr +
(a*a*a*a - ( a*a*a*a - b*b*b*b ) * sinlat*sinlat ) /
(a*a - (a*a - b*b) * sinlat*sinlat );
r = sqrt(r);
/* r is geocentric radial distance */
c = cos(theta);
s = sin(theta);
/* protect against zero divide at geographic poles */
inv_s = 1.0 / (s + (s == 0.)*1.0e-8);
/* zero out arrays */
for ( n = 0; n <= nmax; n++ ) {
for ( m = 0; m <= n; m++ ) {
P[n][m] = 0;
DP[n][m] = 0;
}
}
/* diagonal elements */
P[0][0] = 1;
P[1][1] = s;
DP[0][0] = 0;
DP[1][1] = c;
P[1][0] = c ;
DP[1][0] = -s;
// these values will not change for subsequent function calls
if( !been_here ) {
for ( n = 2; n <= nmax; n++ ) {
root[n] = sqrt((2.0*n-1) / (2.0*n));
}
for ( m = 0; m <= nmax; m++ ) {
double mm = m*m;
for ( n = max(m + 1, 2); n <= nmax; n++ ) {
roots[m][n][0] = sqrt((n-1)*(n-1) - mm);
roots[m][n][1] = 1.0 / sqrt( n*n - mm);
}
}
been_here = 1;
}
for ( n=2; n <= nmax; n++ ) {
// double root = sqrt((2.0*n-1) / (2.0*n));
P[n][n] = P[n-1][n-1] * s * root[n];
DP[n][n] = (DP[n-1][n-1] * s + P[n-1][n-1] * c) *
root[n];
}
/* lower triangle */
for ( m = 0; m <= nmax; m++ ) {
// double mm = m*m;
for ( n = max(m + 1, 2); n <= nmax; n++ ) {
// double root1 = sqrt((n-1)*(n-1) - mm);
// double root2 = 1.0 / sqrt( n*n - mm);
P[n][m] = (P[n-1][m] * c * (2.0*n-1) -
P[n-2][m] * roots[m][n][0]) *
roots[m][n][1];
DP[n][m] = ((DP[n-1][m] * c - P[n-1][m] * s) *
(2.0*n-1) - DP[n-2][m] * roots[m][n][0]) *
roots[m][n][1];
}
}
/* compute gnm, hnm at dat */
/* WMM2000 */
yearfrac = (dat - date0_wmm2000) / 365.25;
for ( n = 1; n <= nmax; n++ ) {
for ( m = 0; m <= nmax; m++ ) {
gnm[n][m] = gnm_wmm2000[n][m] + yearfrac * gtnm_wmm2000[n][m];
hnm[n][m] = hnm_wmm2000[n][m] + yearfrac * htnm_wmm2000[n][m];
}
}
/* compute sm (sin(m lon) and cm (cos(m lon)) */
for ( m = 0; m <= nmax; m++ ) {
sm[m] = sin(m * lon);
cm[m] = cos(m * lon);
}
/* compute B fields */
B_r = 0.0;
B_theta = 0.0;
B_phi = 0.0;
fn_0 = r_0/r;
fn = fn_0 * fn_0;
for ( n = 1; n <= nmax; n++ ) {
double c1_n=0;
double c2_n=0;
double c3_n=0;
for ( m = 0; m <= n; m++ ) {
double tmp = (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]);
c1_n=c1_n + tmp * P[n][m];
c2_n=c2_n + tmp * DP[n][m];
c3_n=c3_n + m * (gnm[n][m] * sm[m] - hnm[n][m] * cm[m]) * P[n][m];
}
// fn=pow(r_0/r,n+2.0);
fn *= fn_0;
B_r = B_r + (n + 1) * c1_n * fn;
B_theta = B_theta - c2_n * fn;
B_phi = B_phi + c3_n * fn * inv_s;
}
/* Find geodetic field components: */
psi = theta - ((pi / 2.0) - lat);
sinpsi = sin(psi);
cospsi = cos(psi);
X = -B_theta * cospsi - B_r * sinpsi;
Y = B_phi;
Z = B_theta * sinpsi - B_r * cospsi;
field[0]=B_r;
field[1]=B_theta;
field[2]=B_phi;
field[3]=X;
field[4]=Y;
field[5]=Z; /* output fields */
/* find variation in radians */
/* return zero variation at magnetic pole X=Y=0. */
/* E is positive */
return (X != 0. || Y != 0.) ? atan2(Y, X) : (double) 0.;
void SGMagVar::update( double lon, double lat, double alt_m, double jd ) {
// Calculate local magnetic variation
double field[6];
// cout << "alt_m = " << alt_m << endl;
magvar = calc_magvar( lat, lon, alt_m / 1000.0, (long)jd, field );
magdip = atan(field[5]/sqrt(field[3]*field[3]+field[4]*field[4]));
}
#ifdef TEST_NHV_HACKS
double SGMagVarOrig( double lat, double lon, double h, long dat, double* field )
{
/* output field B_r,B_th,B_phi,B_x,B_y,B_z */
int n,m;
/* reference dates */
long date0_wmm2000 = yymmdd_to_julian_days(0,1,1);
double yearfrac,sr,r,theta,c,s,psi,fn,B_r,B_theta,B_phi,X,Y,Z;
/* convert to geocentric coords: */
sr = sqrt(pow(a*cos(lat),2.0)+pow(b*sin(lat),2.0));
/* sr is effective radius */
theta = atan2(cos(lat) * (h * sr + a * a),
sin(lat) * (h * sr + b * b));
/* theta is geocentric co-latitude */
r = h * h + 2.0*h * sr +
(pow(a,4.0) - (pow(a,4.0) - pow(b,4.0)) * pow(sin(lat),2.0)) /
(a * a - (a * a - b * b) * pow(sin(lat),2.0));
r = sqrt(r);
/* r is geocentric radial distance */
c = cos(theta);
s = sin(theta);
/* zero out arrays */
for ( n = 0; n <= nmax; n++ ) {
for ( m = 0; m <= n; m++ ) {
P[n][m] = 0;
DP[n][m] = 0;
}
}
/* diagonal elements */
P[0][0] = 1;
P[1][1] = s;
DP[0][0] = 0;
DP[1][1] = c;
P[1][0] = c ;
DP[1][0] = -s;
for ( n = 2; n <= nmax; n++ ) {
P[n][n] = P[n-1][n-1] * s * sqrt((2.0*n-1) / (2.0*n));
DP[n][n] = (DP[n-1][n-1] * s + P[n-1][n-1] * c) *
sqrt((2.0*n-1) / (2.0*n));
}
/* lower triangle */
for ( m = 0; m <= nmax; m++ ) {
for ( n = max(m + 1, 2); n <= nmax; n++ ) {
P[n][m] = (P[n-1][m] * c * (2.0*n-1) - P[n-2][m] *
sqrt(1.0*(n-1)*(n-1) - m * m)) /
sqrt(1.0* n * n - m * m);
DP[n][m] = ((DP[n-1][m] * c - P[n-1][m] * s) *
(2.0*n-1) - DP[n-2][m] *
sqrt(1.0*(n-1) * (n-1) - m * m)) /
sqrt(1.0* n * n - m * m);
}
}
/* compute gnm, hnm at dat */
/* WMM2000 */
yearfrac = (dat - date0_wmm2000) / 365.25;
for ( n = 1; n <= nmax; n++ ) {
for ( m = 0; m <= nmax; m++ ) {
gnm[n][m] = gnm_wmm2000[n][m] + yearfrac * gtnm_wmm2000[n][m];
hnm[n][m] = hnm_wmm2000[n][m] + yearfrac * htnm_wmm2000[n][m];
}
}
/* compute sm (sin(m lon) and cm (cos(m lon)) */
for ( m = 0; m <= nmax; m++ ) {
sm[m] = sin(m * lon);
cm[m] = cos(m * lon);
}
/* compute B fields */
B_r = 0.0;
B_theta = 0.0;
B_phi = 0.0;
for ( n = 1; n <= nmax; n++ ) {
double c1_n=0;
double c2_n=0;
double c3_n=0;
for ( m = 0; m <= n; m++ ) {
c1_n=c1_n + (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]) * P[n][m];
c2_n=c2_n + (gnm[n][m] * cm[m] + hnm[n][m] * sm[m]) * DP[n][m];
c3_n=c3_n + m * (gnm[n][m] * sm[m] - hnm[n][m] * cm[m]) * P[n][m];
}
fn=pow(r_0/r,n+2.0);
B_r = B_r + (n + 1) * c1_n * fn;
B_theta = B_theta - c2_n * fn;
B_phi = B_phi + c3_n * fn / s;
}
/* Find geodetic field components: */
psi = theta - (pi / 2.0 - lat);
X = -B_theta * cos(psi) - B_r * sin(psi);
Y = B_phi;
Z = B_theta * sin(psi) - B_r * cos(psi);
field[0]=B_r;
field[1]=B_theta;
field[2]=B_phi;
field[3]=X;
field[4]=Y;
field[5]=Z; /* output fields */
/* find variation, leave in radians! */
return atan2(Y, X); /* E is positive */
}
#endif // TEST_NHV_HACKS

View File

@@ -1,57 +1,58 @@
// magvar.hxx -- compute local magnetic variation given position,
// altitude, and date
// magvar.hxx -- magnetic variation wrapper class
//
// This is an implimentation of the NIMA WMM 2000
// Written by Curtis Olson, started July 2000.
//
// http://www.nima.mil/GandG/ngdc-wmm2000.html
// Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
//
// Copyright (C) 2000 Edward A Williams <Ed_Williams@compuserve.com>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// Adapted from Excel 3.0 version 3/27/94 EAW
// Recoded in C++ by Starry Chan
// WMM95 added and rearranged in ANSI-C EAW 7/9/95
// Put shell around program and made Borland & GCC compatible EAW 11/22/95
// IGRF95 added 2/96 EAW
// WMM2000 IGR2000 added 2/00 EAW
// Released under GPL 3/26/00 EAW
// Adaptions and modifications for the SimGear project 3/27/2000 CLO
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
// General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// $Id$
#ifndef SG_MAGVAR_HXX
#define SG_MAGVAR_HXX
#ifndef _MAGVAR_HXX
#define _MAGVAR_HXX
/* Convert date to Julian day 1950-2049 */
unsigned long int yymmdd_to_julian_days( int yy, int mm, int dd );
/* Convert degrees to radians */
double deg_to_rad( double deg );
/* Convert radians to degrees */
double rad_to_deg( double rad );
/* return variation (in degrees) given geodetic latitude (radians), longitude
(radians) ,height (km) and (Julian) date
N and E lat and long are positive, S and W negative
*/
double SGMagVar( double lat, double lon, double h, long dat, double* field );
#ifndef __cplusplus
# error This library requires C++
#endif
#endif // SG_MAGVAR_HXX
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
class SGMagVar {
private:
double magvar;
double magdip;
public:
SGMagVar();
~SGMagVar();
// recalculate the magnetic offset and dip
void update( double lon, double lat, double alt_m, double jd );
double get_magvar() const { return magvar; }
double get_magdip() const { return magdip; }
};
#endif // _LIGHT_HXX

View File

@@ -4,7 +4,9 @@
#include <stdlib.h>
#include <math.h>
#include "magvar.hxx"
#include <simgear/constants.h>
#include "coremag.hxx"
int main(int argc, char *argv[])
@@ -40,15 +42,15 @@ if (argc == 8){
}
var = SGMagVar( deg_to_rad(lat_deg), deg_to_rad(lon_deg), h,
yymmdd_to_julian_days(yy,mm,dd), field );
var = calc_magvar( DEG_TO_RAD * lat_deg, DEG_TO_RAD * lon_deg, h,
yymmdd_to_julian_days(yy,mm,dd), field );
fprintf(stdout,"%6.0lf %6.0lf %6.0lf\n", field[0], field[1], field[2] );
fprintf(stdout,"%6.0lf %6.0lf %6.0lf\n", field[3], field[4], field[5] );
fprintf(stdout,"%6.0lf %6.0lf %6.0lf %4.2lf %4.2lf \n",
field[3],field[4],field[5],
rad_to_deg(atan(field[5]/pow(field[3]*field[3]+field[4]*field[4],0.5))),
rad_to_deg(var));
RAD_TO_DEG * (atan(field[5]/pow(field[3]*field[3]+field[4]*field[4],0.5))),
RAD_TO_DEG * var);
exit(0);
}

View File

@@ -100,44 +100,42 @@ CMetarStation::CMetarStation(
int CMetarStation::initialize()
{
// Read the list of metar stations, decoding and adding to global list.
// Read the list of metar stations, decoding and adding to global list.
CMetarStation *m;
char buf[256];
CMetarStation *m;
char buf[256];
// Goto the Flight Gear installation directory
// Goto the Flight Gear installation directory
#ifdef TESTPROG
FGPath weatherPath( "/mkv/Build/FlightGear" );
//FGPath weatherPath( "/mkv/Build/FlightGear" );
FGPath weatherPath( ":Data" );
#else
FGPath weatherPath( current_options.get_fg_root() );
#endif
weatherPath.append( "Weather/MetarStations" );
// Open the metar station list
FILE *f = fopen( weatherPath.c_str(), "r" );
weatherPath.append( "Weather" );
weatherPath.append( "MetarStations" );
// Open the metar station list
FILE *f = fopen( weatherPath.c_str(), "r" );
if ( f != NULL )
{
// Read each line, create an instance of a station, and add it to the vector
while ( fgets( buf, 256, f) != NULL && feof( f ) == 0 )
{
//std::cout << buf << std::endl;
m = new CMetarStation( buf );
//m->dump();
METAR_Stations.push_back( m );
}
if ( f != NULL ) {
// Read each line, create an instance of a station, and add it to the vector
while ( fgets( buf, 256, f) != NULL && feof( f ) == 0 ) {
//std::cout << buf << std::endl;
m = new CMetarStation( buf );
//m->dump();
METAR_Stations.push_back( m );
}
// Close the list
fclose( f );
std::cout << METAR_Stations.size() << " Metar stations" << std::endl;
return 1;
}
else
{
std::cout << "Could not open MetarStations file " << std::endl;
return 0;
}
// Close the list
fclose( f );
// std::cout << METAR_Stations.size() << " Metar stations" << std::endl;
return 1;
} else {
// std::cout << "Could not open MetarStations file " << std::endl;
return 0;
}
}

View File

@@ -21,6 +21,7 @@ libsgmisc_a_SOURCES = \
fgpath.cxx \
fgstream.cxx \
props.cxx \
props_io.cxx \
strutils.cxx \
texcoord.cxx \
zfstream.cxx

View File

@@ -39,7 +39,7 @@
FG_USING_STD(string);
#ifdef MACOS
#ifdef macintosh
# define FG_PATH_SEP ':'
# define FG_BAD_PATH_SEP '/'
#else

View File

@@ -304,8 +304,6 @@ float
SGValue::getFloatValue () const
{
switch (_type) {
case UNKNOWN:
return 0.0;
case BOOL:
return (float)(getRawBool());
case INT:
@@ -314,6 +312,7 @@ SGValue::getFloatValue () const
return getRawFloat();
case DOUBLE:
return (float)(getRawDouble());
case UNKNOWN:
case STRING:
return (float)atof(getRawString().c_str());
}
@@ -330,8 +329,6 @@ double
SGValue::getDoubleValue () const
{
switch (_type) {
case UNKNOWN:
return 0.0;
case BOOL:
return (double)(getRawBool());
case INT:
@@ -340,6 +337,7 @@ SGValue::getDoubleValue () const
return (double)(getRawFloat());
case DOUBLE:
return getRawDouble();
case UNKNOWN:
case STRING:
return atof(getRawString().c_str());
}
@@ -357,8 +355,6 @@ SGValue::getStringValue () const
{
char buf[512];
switch (_type) {
case UNKNOWN:
return getRawString();
case BOOL:
if (getRawBool())
string_val = "true";
@@ -377,6 +373,7 @@ SGValue::getStringValue () const
sprintf(buf, "%f", getRawDouble());
string_val = buf;
return string_val;
case UNKNOWN:
case STRING:
return getRawString();
}
@@ -494,8 +491,8 @@ SGValue::setUnknownValue (const string &value)
* Returns true on success (i.e. the value is not currently tied).
*/
bool
SGValue::tieBool (bool_getter getter, bool_setter setter = 0,
bool useDefault = true)
SGValue::tieBool (bool_getter getter, bool_setter setter,
bool useDefault)
{
if (_tied) {
return false;
@@ -520,8 +517,8 @@ SGValue::tieBool (bool_getter getter, bool_setter setter = 0,
* Returns true on success (i.e. the value is not currently tied).
*/
bool
SGValue::tieInt (int_getter getter, int_setter setter = 0,
bool useDefault = true)
SGValue::tieInt (int_getter getter, int_setter setter,
bool useDefault)
{
if (_tied) {
return false;
@@ -546,8 +543,8 @@ SGValue::tieInt (int_getter getter, int_setter setter = 0,
* Returns true on success (i.e. the value is not currently tied).
*/
bool
SGValue::tieFloat (float_getter getter, float_setter setter = 0,
bool useDefault = true)
SGValue::tieFloat (float_getter getter, float_setter setter,
bool useDefault)
{
if (_tied) {
return false;
@@ -572,8 +569,8 @@ SGValue::tieFloat (float_getter getter, float_setter setter = 0,
* Returns true on success (i.e. the value is not currently tied).
*/
bool
SGValue::tieDouble (double_getter getter, double_setter setter = 0,
bool useDefault = true)
SGValue::tieDouble (double_getter getter, double_setter setter,
bool useDefault)
{
if (_tied) {
return false;
@@ -598,8 +595,8 @@ SGValue::tieDouble (double_getter getter, double_setter setter = 0,
* Returns true on success (i.e. the value is not currently tied).
*/
bool
SGValue::tieString (string_getter getter, string_setter setter = 0,
bool useDefault = true)
SGValue::tieString (string_getter getter, string_setter setter,
bool useDefault)
{
if (_tied) {
return false;
@@ -696,7 +693,7 @@ SGPropertyList::~SGPropertyList ()
* and must not end with '/'.
*/
SGValue *
SGPropertyList::getValue (const string &name, bool create = false)
SGPropertyList::getValue (const string &name, bool create)
{
const_iterator el = _props.find(name);
if (el == _props.end()) {
@@ -739,11 +736,11 @@ SGPropertyList::getValue (const string &name) const
* better to get the SGValue and query it repeatedly.
*/
bool
SGPropertyList::getBoolValue (const string &name) const
SGPropertyList::getBoolValue (const string &name, bool defaultValue) const
{
const SGValue * val = getValue(name);
if (val == 0)
return false;
return defaultValue;
else
return val->getBoolValue();
}
@@ -756,11 +753,11 @@ SGPropertyList::getBoolValue (const string &name) const
* better to get the SGValue and query it repeatedly.
*/
int
SGPropertyList::getIntValue (const string &name) const
SGPropertyList::getIntValue (const string &name, int defaultValue) const
{
const SGValue * val = getValue(name);
if (val == 0)
return 0;
return defaultValue;
else
return val->getIntValue();
}
@@ -773,11 +770,11 @@ SGPropertyList::getIntValue (const string &name) const
* better to get the SGValue and query it repeatedly.
*/
float
SGPropertyList::getFloatValue (const string &name) const
SGPropertyList::getFloatValue (const string &name, float defaultValue) const
{
const SGValue * val = getValue(name);
if (val == 0)
return 0.0;
return defaultValue;
else
return val->getFloatValue();
}
@@ -790,11 +787,11 @@ SGPropertyList::getFloatValue (const string &name) const
* better to get the SGValue and query it repeatedly.
*/
double
SGPropertyList::getDoubleValue (const string &name) const
SGPropertyList::getDoubleValue (const string &name, double defaultValue) const
{
const SGValue * val = getValue(name);
if (val == 0)
return 0.0;
return defaultValue;
else
return val->getDoubleValue();
}
@@ -807,11 +804,12 @@ SGPropertyList::getDoubleValue (const string &name) const
* better to save the SGValue and query it repeatedly.
*/
const string &
SGPropertyList::getStringValue (const string &name) const
SGPropertyList::getStringValue (const string &name,
const string &defaultValue) const
{
const SGValue * val = getValue(name);
if (val == 0)
return empty_string;
return defaultValue;
else
return val->getStringValue();
}
@@ -916,7 +914,7 @@ bool
SGPropertyList::tieBool (const string &name,
bool_getter getter,
bool_setter setter,
bool useDefault = true)
bool useDefault)
{
FG_LOG(FG_GENERAL, FG_INFO, "Tying bool property '" << name << '\'');
return getValue(name, true)->tieBool(getter, setter, useDefault);
@@ -932,7 +930,7 @@ bool
SGPropertyList::tieInt (const string &name,
int_getter getter,
int_setter setter,
bool useDefault = true)
bool useDefault)
{
FG_LOG(FG_GENERAL, FG_INFO, "Tying int property '" << name << '\'');
return getValue(name, true)->tieInt(getter, setter, useDefault);
@@ -948,7 +946,7 @@ bool
SGPropertyList::tieFloat (const string &name,
float_getter getter,
float_setter setter,
bool useDefault = true)
bool useDefault)
{
FG_LOG(FG_GENERAL, FG_INFO, "Tying float property '" << name << '\'');
return getValue(name, true)->tieFloat(getter, setter, useDefault);
@@ -964,7 +962,7 @@ bool
SGPropertyList::tieDouble (const string &name,
double_getter getter,
double_setter setter,
bool useDefault = true)
bool useDefault)
{
FG_LOG(FG_GENERAL, FG_INFO, "Tying double property '" << name << '\'');
return getValue(name, true)->tieDouble(getter, setter, useDefault);
@@ -980,7 +978,7 @@ bool
SGPropertyList::tieString (const string &name,
string_getter getter,
string_setter setter,
bool useDefault = true)
bool useDefault)
{
FG_LOG(FG_GENERAL, FG_INFO, "Tying string property '" << name << '\'');
return getValue(name, true)->tieString(getter, setter, useDefault);
@@ -1039,9 +1037,9 @@ get_base (const string &parent, const string &child,
/**
* Constructor.
*/
SGPropertyNode::SGPropertyNode (const string &path = "",
SGPropertyList * props = 0)
: _props(props)
SGPropertyNode::SGPropertyNode (const string &path,
SGPropertyList * props)
: _props(props), _node(0)
{
setPath(path);
}
@@ -1052,6 +1050,8 @@ SGPropertyNode::SGPropertyNode (const string &path = "",
*/
SGPropertyNode::~SGPropertyNode ()
{
delete _node;
_node = 0;
}
@@ -1089,24 +1089,6 @@ SGPropertyNode::getName () const
}
/**
* Return the value of the current node.
*
* Currently, this does a lookup each time, but we could cache the
* value safely as long as it's non-zero.
*
* Note that this will not create the value if it doesn't already exist.
*/
SGValue *
SGPropertyNode::getValue ()
{
if (_props == 0 || _path.size() == 0)
return 0;
else
return _props->getValue(_path);
}
/**
* Return the number of children for the current node.
*/
@@ -1143,17 +1125,18 @@ SGPropertyNode::size () const
* A return value of true means success; otherwise, the node supplied
* is unmodified.
*/
bool
SGPropertyNode::getParent (SGPropertyNode &parent) const
SGPropertyNode &
SGPropertyNode::getParent () const
{
if (_node == 0)
_node = new SGPropertyNode();
string::size_type pos = _path.rfind('/');
if (pos != string::npos) {
parent.setPath(_path.substr(0, pos-1));
parent.setPropertyList(_props);
return true;
} else {
return false;
_node->setPropertyList(_props);
_node->setPath(_path.substr(0, pos-1));
}
return *_node;
}
@@ -1163,11 +1146,14 @@ SGPropertyNode::getParent (SGPropertyNode &parent) const
* A return value of true means success; otherwise, the node supplied
* is unmodified.
*/
bool
SGPropertyNode::getChild (SGPropertyNode &child, int n) const
SGPropertyNode &
SGPropertyNode::getChild (int n) const
{
if (_node == 0)
_node = new SGPropertyNode();
if (_props == 0)
return false;
return *_node;
int s = 0;
string base;
@@ -1180,12 +1166,9 @@ SGPropertyNode::getChild (SGPropertyNode &child, int n) const
while (it != end) {
if (get_base(pattern, it->first, base) && base != lastBase) {
if (s == n) {
string path = _path;
path += '/';
path += base;
child.setPath(path);
child.setPropertyList(_props);
return true;
_node->setPropertyList(_props);
_node->setPath(_path + string("/") + base);
return *_node;
} else {
s++;
lastBase = base;
@@ -1194,7 +1177,133 @@ SGPropertyNode::getChild (SGPropertyNode &child, int n) const
it++;
}
return false;
return *_node;
}
/**
* Return a node for an arbitrary subpath.
*
* Never returns 0.
*/
SGPropertyNode &
SGPropertyNode::getSubNode (const string &subpath) const
{
if (_node == 0)
_node = new SGPropertyNode();
_node->setPropertyList(_props);
_node->setPath(_path + string("/") + subpath);
return *_node;
}
/**
* Return the value of the current node.
*
* Currently, this does a lookup each time, but we could cache the
* value safely as long as it's non-zero.
*
* Note that this will not create the value if it doesn't already exist.
*/
SGValue *
SGPropertyNode::getValue (const string &subpath)
{
if (_props == 0)
return 0;
if (subpath.size() == 0)
return _props->getValue(_path);
else
return _props->getValue(_path + string("/") + subpath);
}
/**
* Return a bool value.
*/
bool
SGPropertyNode::getBoolValue (const string &subpath, bool defaultValue) const
{
if (_props == 0)
return defaultValue;
if (subpath == "")
return _props->getBoolValue(_path, defaultValue);
else
return _props->getBoolValue(_path + string("/") + subpath,
defaultValue);
}
/**
* Return an int value.
*/
int
SGPropertyNode::getIntValue (const string &subpath, int defaultValue) const
{
if (_props == 0)
return defaultValue;
if (subpath == "")
return _props->getIntValue(_path, defaultValue);
else
return _props->getIntValue(_path + string("/") + subpath,
defaultValue);
}
/**
* Return a float value.
*/
float
SGPropertyNode::getFloatValue (const string &subpath, float defaultValue) const
{
if (_props == 0)
return defaultValue;
if (subpath == "")
return _props->getFloatValue(_path, defaultValue);
else
return _props->getFloatValue(_path + string("/") + subpath,
defaultValue);
}
/**
* Return a double value.
*/
double
SGPropertyNode::getDoubleValue (const string &subpath,
double defaultValue) const
{
if (_props == 0)
return defaultValue;
if (subpath == "")
return _props->getDoubleValue(_path, defaultValue);
else
return _props->getDoubleValue(_path + string("/") + subpath,
defaultValue);
}
/**
* Return a string value.
*/
const string &
SGPropertyNode::getStringValue (const string &subpath,
const string &defaultValue) const
{
if (_props == 0)
return defaultValue;
if (subpath == "")
return _props->getStringValue(_path, defaultValue);
else
return _props->getStringValue(_path + string("/") + subpath,
defaultValue);
}
// end of props.cxx

View File

@@ -19,9 +19,42 @@
#include <string>
#include <map>
#include <iostream>
using std::string;
using std::map;
using std::istream;
using std::ostream;
#ifdef UNKNOWN
#pragma warn A sloppy coder has defined UNKNOWN as a macro!
#undef UNKNOWN
#endif
#ifdef BOOL
#pragma warn A sloppy coder has defined BOOL as a macro!
#undef BOOL
#endif
#ifdef INT
#pragma warn A sloppy coder has defined INT as a macro!
#undef INT
#endif
#ifdef FLOAT
#pragma warn A sloppy coder has defined FLOAT as a macro!
#undef FLOAT
#endif
#ifdef DOUBLE
#pragma warn A sloppy coder has defined DOUBLE as a macro!
#undef DOUBLE
#endif
#ifdef STRING
#pragma warn A sloppy coder has defined STRING as a macro!
#undef STRING
#endif
@@ -80,7 +113,7 @@ public:
virtual int getIntValue () const;
virtual float getFloatValue () const;
virtual double getDoubleValue () const;
virtual const string &getStringValue () const;
virtual const string & getStringValue () const;
// Setters.
virtual bool setBoolValue (bool value);
@@ -231,11 +264,17 @@ public:
virtual SGValue * getValue (const string &name, bool create = false);
virtual const SGValue * getValue (const string &name) const;
virtual bool getBoolValue (const string &name) const;
virtual int getIntValue (const string &name) const;
virtual float getFloatValue (const string &name) const;
virtual double getDoubleValue (const string &name) const;
virtual const string &getStringValue (const string &name) const;
virtual bool getBoolValue (const string &name,
bool defaultValue = false) const;
virtual int getIntValue (const string &name,
int defaultValue = 0) const;
virtual float getFloatValue (const string &name,
float defaultValue = 0.0) const;
virtual double getDoubleValue (const string &name,
double defaultValue = 0.0L) const;
virtual const string & getStringValue (const string &name,
const string &defaultValue = "")
const;
virtual bool setBoolValue (const string &name, bool value);
virtual bool setIntValue (const string &name, int value);
@@ -331,17 +370,48 @@ public:
// Accessors for derived information.
virtual int size () const;
virtual const string &getName () const;
virtual SGValue * getValue ();
virtual bool getParent (SGPropertyNode &parent) const;
virtual bool getChild (SGPropertyNode &child, int n) const;
virtual SGPropertyNode &getParent () const;
virtual SGPropertyNode &getChild (int n) const;
virtual SGPropertyNode &getSubNode (const string &subpath) const;
// Get values directly.
virtual SGValue * getValue (const string &subpath = "");
virtual bool getBoolValue (const string &subpath = "",
bool defaultValue = false) const;
virtual int getIntValue (const string &subpath = "",
int defaultValue = 0) const;
virtual float getFloatValue (const string &subpath = "",
float defaultValue = 0.0) const;
virtual double getDoubleValue (const string &subpath = "",
double defaultValue = 0.0L) const;
virtual const string &
getStringValue (const string &subpath = "",
const string &defaultValue = "") const;
private:
string _path;
mutable string _name; // for pointer persistence only
SGPropertyList * _props;
// for pointer persistence...
// NOT THREAD SAFE!!!
// (each thread must have its own node
// object)
mutable string _name;
mutable SGPropertyNode * _node;
};
////////////////////////////////////////////////////////////////////////
// Input and output.
////////////////////////////////////////////////////////////////////////
extern bool readPropertyList (istream &input, SGPropertyList * props);
extern bool readPropertyList (const string &file, SGPropertyList * props);
extern bool writePropertyList (ostream &output, const SGPropertyList * props);
extern bool writePropertyList (const string &file,
const SGPropertyList * props);
////////////////////////////////////////////////////////////////////////
// Global property manager.
@@ -350,6 +420,6 @@ private:
extern SGPropertyList current_properties;
#endif __PROPS_HXX
#endif // __PROPS_HXX
// end of props.hxx

360
simgear/misc/props_io.cxx Normal file
View File

@@ -0,0 +1,360 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/debug/logstream.hxx>
#include <simgear/xml/easyxml.hxx>
#include "props.hxx"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using std::istream;
using std::ifstream;
using std::ostream;
using std::string;
using std::vector;
////////////////////////////////////////////////////////////////////////
// Visitor class for building the property list.
////////////////////////////////////////////////////////////////////////
class PropVisitor : public XMLVisitor
{
public:
PropVisitor (SGPropertyList * props) : _props(props), _level(0), _ok(true) {}
void startDocument ();
void startElement (const char * name, const XMLAttributes &atts);
void endElement (const char * name);
void data (const char * s, int length);
void warning (const char * message, int line, int col);
void error (const char * message, int line, int col);
bool isOK () const { return _ok; }
private:
void pushState (const char * name) {
_states.push_back(_state);
_level++;
_state.name = name;
_state.type = SGValue::UNKNOWN;
_state.data = "";
_state.hasChildren = false;
_state.hasData = false;
}
void popState () {
_state = _states.back();
_states.pop_back();
_level--;
}
struct State
{
State () : hasChildren(false), hasData(false) {}
string name;
SGValue::Type type;
string data;
bool hasChildren;
bool hasData;
};
SGPropertyList * _props;
State _state;
vector<State> _states;
int _level;
bool _ok;
};
void
PropVisitor::startDocument ()
{
_level = 0;
_ok = true;
}
void
PropVisitor::startElement (const char * name, const XMLAttributes &atts)
{
if (!_ok)
return;
if (_level == 0 && strcmp(name, "PropertyList")) {
_ok = false;
FG_LOG(FG_INPUT, FG_ALERT, "XML document has root element \""
<< name << "\" instead of \"PropertyList\"");
return;
}
// Mixed content?
_state.hasChildren = true;
if (_state.hasData) {
FG_LOG(FG_INPUT, FG_ALERT,
"XML element has mixed elements and data in element "
<< _state.name);
_ok = false;
return;
}
// Start a new state.
pushState(name);
// See if there's a type specified.
const char * type = atts.getValue("type");
if (type == 0 || !strcmp("unknown", type))
_state.type = SGValue::UNKNOWN;
else if (!strcmp("bool", type))
_state.type = SGValue::BOOL;
else if (!strcmp("int", type))
_state.type = SGValue::INT;
else if (!strcmp("float", type))
_state.type = SGValue::FLOAT;
else if (!strcmp("double", type))
_state.type = SGValue::DOUBLE;
else if (!strcmp("string", type))
_state.type = SGValue::STRING;
else
FG_LOG(FG_INPUT, FG_ALERT, "Unrecognized type " << type
<< ", using UNKNOWN");
}
void
PropVisitor::endElement (const char * name)
{
if (!_ok)
return;
// See if there's a property to add.
if (_state.hasData) {
bool status = false;
// Figure out the path name.
string path = "";
for (int i = 2; i < _level; i++) {
path += '/';
path += _states[i].name;
}
path += '/';
path += _state.name;
// Set the value
switch (_state.type) {
case SGValue::BOOL:
if (_state.data == "true" || _state.data == "TRUE") {
status = _props->setBoolValue(path, true);
} else if (atof(_state.data.c_str()) != 0.0) {
status = _props->setBoolValue(path, true);
} else {
status =_props->setBoolValue(path, false);
}
break;
case SGValue::INT :
status = _props->setIntValue(path, atoi(_state.data.c_str()));
break;
case SGValue::FLOAT:
status = _props->setFloatValue(path, atof(_state.data.c_str()));
break;
case SGValue::DOUBLE:
status = _props->setDoubleValue(path, atof(_state.data.c_str()));
break;
case SGValue::STRING:
status = _props->setStringValue(path, _state.data);
break;
default:
status = _props->setUnknownValue(path, _state.data);
break;
}
if (!status)
FG_LOG(FG_INPUT, FG_ALERT, "Failed to set property "
<< path << " to " << _state.data);
}
// Pop the stack.
popState();
}
void
PropVisitor::data (const char * s, int length)
{
if (!_ok)
return;
// Check if there is any non-whitespace
if (!_state.hasData)
for (int i = 0; i < length; i++)
if (s[i] != ' ' && s[i] != '\t' && s[i] != '\n' && s[i] != '\r')
_state.hasData = true;
_state.data += string(s, length); // FIXME: inefficient
}
void
PropVisitor::warning (const char * message, int line, int col)
{
FG_LOG(FG_INPUT, FG_ALERT, "Warning importing property list: "
<< message << " (" << line << ',' << col << ')');
}
void
PropVisitor::error (const char * message, int line, int col)
{
FG_LOG(FG_INPUT, FG_ALERT, "Error importing property list: "
<< message << " (" << line << ',' << col << ')');
_ok = false;
}
////////////////////////////////////////////////////////////////////////
// Property list reader.
////////////////////////////////////////////////////////////////////////
bool
readPropertyList (istream &input, SGPropertyList * props)
{
PropVisitor visitor(props);
return readXML(input, visitor) && visitor.isOK();
}
bool
readPropertyList (const string &file, SGPropertyList * props)
{
ifstream input(file.c_str());
if (input.good()) {
return readPropertyList(input, props);
} else {
FG_LOG(FG_INPUT, FG_ALERT, "Error reading property list from file "
<< file);
return false;
}
}
////////////////////////////////////////////////////////////////////////
// Property list writer.
////////////////////////////////////////////////////////////////////////
#define INDENT_STEP 2
/**
* Return the type name.
*/
static const char *
getTypeName (SGValue::Type type)
{
switch (type) {
case SGValue::UNKNOWN:
return "unknown";
case SGValue::BOOL:
return "bool";
case SGValue::INT:
return "int";
case SGValue::FLOAT:
return "float";
case SGValue::DOUBLE:
return "double";
case SGValue::STRING:
return "string";
}
}
/**
* Escape characters for output.
*/
static void
writeData (ostream &output, const string &data)
{
for (int i = 0; i < data.size(); i++) {
switch (data[i]) {
case '&':
output << "&amp;";
break;
case '<':
output << "&lt;";
break;
case '>':
output << "&gt;";
break;
default:
output << data[i];
break;
}
}
}
static void
doIndent (ostream &output, int indent)
{
while (indent-- > 0) {
output << ' ';
}
}
static bool
writeNode (ostream &output, SGPropertyNode node, int indent)
{
const string &name = node.getName();
int size = node.size();
// Write out the literal value, if any.
SGValue * value = node.getValue();
if (value != 0) {
SGValue::Type type = value->getType();
doIndent(output, indent);
output << '<' << name;
if (type != SGValue::UNKNOWN)
output << " type=\"" << getTypeName(type) << '"';
output << '>';
writeData(output, value->getStringValue());
output << "</" << name << '>' << endl;
}
// Write out the children, if any.
if (size > 0) {
doIndent(output, indent);
output << '<' << name << '>' << endl;;
for (int i = 0; i < size; i++) {
writeNode(output, node.getChild(i), indent + INDENT_STEP);
}
doIndent(output, indent);
output << "</" << name << '>' << endl;
}
}
bool
writePropertyList (ostream &output, const SGPropertyList * props)
{
SGPropertyNode root ("/", (SGPropertyList *)props); // FIXME
output << "<?xml version=\"1.0\"?>" << endl << endl;
output << "<PropertyList>" << endl;
for (int i = 0; i < root.size(); i++) {
writeNode(output, root.getChild(i), INDENT_STEP);
}
output << "</PropertyList>" << endl;
}
bool
writePropertyList (const string &file, const SGPropertyList * props)
{
ofstream output(file.c_str());
if (output.good()) {
return writePropertyList(output, props);
} else {
FG_LOG(FG_INPUT, FG_ALERT, "Cannot write property list to file "
<< file);
return false;
}
}

View File

@@ -50,7 +50,11 @@ void my_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int
ibuffer = (unsigned char *) malloc(win_width*win_height*RGB);
fp = fopen(filename, "wb");
if ( (fp = fopen(filename, "wb")) == NULL ) {
printf("Warning: cannot open %s\n", filename);
return;
}
fprintf(fp, "P6\n# CREATOR: glReadPixel()\n%d %d\n%d\n",
win_width, win_height, UCHAR_MAX);
q = 0;

View File

@@ -69,6 +69,13 @@
static int sgSkyDomePreDraw( ssgEntity *e ) {
/* cout << endl << "Dome Pre Draw" << endl << "----------------"
<< endl << endl; */
ssgLeaf *f = (ssgLeaf *)e;
if ( f -> hasState () ) f->getState()->apply() ;
glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
// cout << "push error = " << glGetError() << endl;
glDisable( GL_DEPTH_TEST );
glDisable( GL_FOG );
@@ -78,9 +85,10 @@ static int sgSkyDomePreDraw( ssgEntity *e ) {
static int sgSkyDomePostDraw( ssgEntity *e ) {
/* cout << endl << "Dome Post Draw" << endl << "----------------"
<< endl << endl; */
glEnable( GL_DEPTH_TEST );
glEnable( GL_FOG );
glPopAttrib();
// cout << "pop error = " << glGetError() << endl;
return true;
}

View File

@@ -45,6 +45,13 @@
static int sgMoonOrbPreDraw( ssgEntity *e ) {
/* cout << endl << "Moon orb pre draw" << endl << "----------------"
<< endl << endl; */
ssgLeaf *f = (ssgLeaf *)e;
if ( f -> hasState () ) f->getState()->apply() ;
glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT );
// cout << "push error = " << glGetError() << endl;
glDisable( GL_DEPTH_TEST );
glDisable( GL_FOG );
glBlendFunc ( GL_SRC_ALPHA, GL_ONE ) ;
@@ -55,10 +62,13 @@ static int sgMoonOrbPreDraw( ssgEntity *e ) {
static int sgMoonOrbPostDraw( ssgEntity *e ) {
/* cout << endl << "Moon orb post draw" << endl << "----------------"
<< endl << endl; */
glEnable( GL_DEPTH_TEST );
glEnable( GL_FOG );
glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
// glEnable( GL_DEPTH_TEST );
// glEnable( GL_FOG );
// glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
glPopAttrib();
// cout << "pop error = " << glGetError() << endl;
/* test
glDisable( GL_LIGHTING );
glDisable( GL_CULL_FACE );
@@ -73,6 +83,13 @@ static int sgMoonOrbPostDraw( ssgEntity *e ) {
static int sgMoonHaloPreDraw( ssgEntity *e ) {
/* cout << endl << "Moon halo pre draw" << endl << "----------------"
<< endl << endl; */
ssgLeaf *f = (ssgLeaf *)e;
if ( f -> hasState () ) f->getState()->apply() ;
glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT | GL_COLOR_BUFFER_BIT);
cout << "push error = " << glGetError() << endl;
glDisable( GL_DEPTH_TEST );
glDisable( GL_FOG );
glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
@@ -83,9 +100,12 @@ static int sgMoonHaloPreDraw( ssgEntity *e ) {
static int sgMoonHaloPostDraw( ssgEntity *e ) {
/* cout << endl << "Moon halo post draw" << endl << "----------------"
<< endl << endl; */
glEnable( GL_DEPTH_TEST );
glEnable( GL_FOG );
glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
// glEnable( GL_DEPTH_TEST );
// glEnable( GL_FOG );
// glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
glPopAttrib();
cout << "pop error = " << glGetError() << endl;
return true;
}

View File

@@ -43,6 +43,13 @@
static int sgSunOrbPreDraw( ssgEntity *e ) {
/* cout << endl << "Sun orb pre draw" << endl << "----------------"
<< endl << endl; */
ssgLeaf *f = (ssgLeaf *)e;
if ( f -> hasState () ) f->getState()->apply() ;
glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
// cout << "push error = " << glGetError() << endl;
glDisable( GL_DEPTH_TEST );
glDisable( GL_FOG );
@@ -52,8 +59,12 @@ static int sgSunOrbPreDraw( ssgEntity *e ) {
static int sgSunOrbPostDraw( ssgEntity *e ) {
/* cout << endl << "Sun orb post draw" << endl << "----------------"
<< endl << endl; */
glEnable( GL_DEPTH_TEST );
glEnable( GL_FOG );
glPopAttrib();
// cout << "pop error = " << glGetError() << endl;
// glEnable( GL_DEPTH_TEST );
// glEnable( GL_FOG );
return true;
}
@@ -61,6 +72,13 @@ static int sgSunOrbPostDraw( ssgEntity *e ) {
static int sgSunHaloPreDraw( ssgEntity *e ) {
/* cout << endl << "Sun halo pre draw" << endl << "----------------"
<< endl << endl; */
ssgLeaf *f = (ssgLeaf *)e;
if ( f -> hasState () ) f->getState()->apply() ;
glPushAttrib( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
// cout << "push error = " << glGetError() << endl;
glDisable( GL_DEPTH_TEST );
glDisable( GL_FOG );
glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
@@ -71,8 +89,12 @@ static int sgSunHaloPreDraw( ssgEntity *e ) {
static int sgSunHaloPostDraw( ssgEntity *e ) {
/* cout << endl << "Sun halo post draw" << endl << "----------------"
<< endl << endl; */
glEnable( GL_DEPTH_TEST );
glEnable( GL_FOG );
glPopAttrib();
// cout << "pop error = " << glGetError() << endl;
// glEnable( GL_DEPTH_TEST );
// glEnable( GL_FOG );
return true;
}

View File

@@ -193,8 +193,10 @@ void SGSky::postDraw( float alt ) {
int in_cloud = -1; // cloud we are in
int i;
// check where we are relative to the cloud layers
for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
float asl = cloud_layers[i]->get_asl();
float thickness = cloud_layers[i]->get_thickness();
@@ -220,14 +222,14 @@ void SGSky::postDraw( float alt ) {
if ( pos == 0 ) {
// we are below all the cloud layers, draw top to bottom
for ( int i = cloud_layers.size() - 1; i >= 0; --i ) {
for ( i = cloud_layers.size() - 1; i >= 0; --i ) {
if ( i != in_cloud ) {
cloud_layers[i]->draw();
}
}
} else if ( pos >= (int)cloud_layers.size() ) {
// we are above all the cloud layers, draw bottom to top
for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
for ( i = 0; i < (int)cloud_layers.size(); ++i ) {
if ( i != in_cloud ) {
cloud_layers[i]->draw();
}
@@ -235,12 +237,12 @@ void SGSky::postDraw( float alt ) {
} else {
// we are between cloud layers, draw lower layers bottom to
// top and upper layers top to bottom
for ( int i = 0; i < pos; ++i ) {
for ( i = 0; i < pos; ++i ) {
if ( i != in_cloud ) {
cloud_layers[i]->draw();
}
}
for ( int i = cloud_layers.size() - 1; i >= pos; --i ) {
for ( i = cloud_layers.size() - 1; i >= pos; --i ) {
if ( i != in_cloud ) {
cloud_layers[i]->draw();
}
@@ -267,10 +269,10 @@ void SGSky::add_cloud_layer( double asl, double thickness,
cloud_layers.push_back( layer );
}
for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
}
cout << endl;
// for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
// cout << "layer " << i << " = " << cloud_layers[i]->get_asl() << endl;
// }
// cout << endl;
}

View File

@@ -39,11 +39,11 @@
#include <vector>
#include "cloud.hxx"
#include "dome.hxx"
#include "moon.hxx"
#include "oursun.hxx"
#include "stars.hxx"
#include <simgear/sky/cloud.hxx>
#include <simgear/sky/dome.hxx>
#include <simgear/sky/moon.hxx>
#include <simgear/sky/oursun.hxx>
#include <simgear/sky/stars.hxx>
FG_USING_STD(vector);

View File

@@ -34,6 +34,7 @@
#include <plib/sg.h>
#include <plib/ssg.h>
FG_USING_NAMESPACE(std);
// return a sphere object as an ssgBranch
ssgBranch *ssgMakeSphere( ssgSimpleState *state, ssgColourArray *cl,
@@ -113,11 +114,11 @@ ssgBranch *ssgMakeSphere( ssgSimpleState *state, ssgColourArray *cl,
new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
if ( vl->getNum() != nl->getNum() ) {
cout << "bad sphere1\n";
std::cout << "bad sphere1\n";
exit(-1);
}
if ( vl->getNum() != tl->getNum() ) {
cout << "bad sphere2\n";
std::cout << "bad sphere2\n";
exit(-1);
}
slice->setState( state );

View File

@@ -35,6 +35,8 @@
#include <plib/sg.h>
#include <plib/ssg.h>
#include <compiler.h>
#include "stars.hxx"
#ifdef _MSC_VER
@@ -47,9 +49,15 @@ FG_USING_STD(endl);
static int sgStarPreDraw( ssgEntity *e ) {
/* cout << endl << "Star pre draw" << endl << "----------------"
<< endl << endl; */
ssgLeaf *f = (ssgLeaf *)e;
if ( f -> hasState () ) f->getState()->apply() ;
glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_FOG_BIT );
glDisable( GL_DEPTH_TEST );
glDisable( GL_FOG );
glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
// glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
return true;
}
@@ -57,8 +65,11 @@ static int sgStarPreDraw( ssgEntity *e ) {
static int sgStarPostDraw( ssgEntity *e ) {
/* cout << endl << "Star post draw" << endl << "----------------"
<< endl << endl; */
glEnable( GL_DEPTH_TEST );
glEnable( GL_FOG );
glPopAttrib();
// glEnable( GL_DEPTH_TEST );
// glEnable( GL_FOG );
return true;
}

View File

@@ -75,18 +75,24 @@ SGTime::SGTime( double lon, double lat, const string& root )
// cout << "Current local time = "
// << asctime(localtime(&cur_time)) << endl;
FGPath zone( root );
zone.append( "zone.tab" );
FG_LOG( FG_EVENT, FG_DEBUG, "Reading timezone info from: " << zone.str() );
tzContainer = new TimezoneContainer( zone.c_str() );
if ( root != "" ) {
FGPath zone( root );
zone.append( "zone.tab" );
FG_LOG( FG_EVENT, FG_DEBUG, "Reading timezone info from: "
<< zone.str() );
tzContainer = new TimezoneContainer( zone.c_str() );
GeoCoord location( RAD_TO_DEG * lat, RAD_TO_DEG * lon );
GeoCoord* nearestTz = tzContainer->getNearest(location);
GeoCoord location( RAD_TO_DEG * lat, RAD_TO_DEG * lon );
GeoCoord* nearestTz = tzContainer->getNearest(location);
FGPath name( root );
name.append( nearestTz->getDescription() );
zonename = strdup( name.c_str() );
// cout << "Using zonename = " << zonename << endl;
FGPath name( root );
name.append( nearestTz->getDescription() );
zonename = strdup( name.c_str() );
// cout << "Using zonename = " << zonename << endl;
} else {
tzContainer = NULL;
zonename = NULL;
}
}
@@ -95,9 +101,16 @@ SGTime::SGTime( const string& root ) {
}
SGTime::SGTime() {
SGTime( 0.0, 0.0, "" );
}
SGTime::~SGTime()
{
delete tzContainer;
if ( tzContainer != NULL ) {
delete tzContainer;
}
if ( zonename != NULL ) {
delete zonename;

View File

@@ -43,7 +43,7 @@
# include <time.h>
#endif
#include "timezone.h"
#include <simgear/timing/timezone.h>
// Define a structure containing time parameters
@@ -86,6 +86,7 @@ public:
SGTime( double lon, double lat, const string& root );
SGTime( const string& root );
SGTime();
~SGTime();
// Update the time related variables

View File

@@ -30,9 +30,10 @@
#ifndef _TIMEZONE_H_
#define _TIMEZONE_H_
#include "geocoord.h"
#include <stdio.h>
#include <simgear/timing/geocoord.h>
class Timezone : public GeoCoord
{
private:

25
simgear/xml/Makefile.am Normal file
View File

@@ -0,0 +1,25 @@
if HAVE_ZLIB
ZLIB_INCL =
else
XLIB_INCL = -I$(top_builddir)/src/zlib
endif
lib_LIBRARIES = libsgxml.a
include_HEADERS = \
easyxml.hxx
noinst_HEADERS = \
xmltok_impl.c xmltok_ns.c
libsgxml_a_SOURCES = \
asciitab.h \
easyxml.cxx \
hashtable.h hashtable.c \
iasciitab.h latin1tab.h nametab.h utf8tab.h xmldef.h \
xmlparse.h xmlparse.c \
xmlrole.h xmlrole.c \
xmltok.h xmltok.c \
xmltok_impl.h
INCLUDES += -I$(top_builddir) $(ZLIB_INCL)

62
simgear/xml/asciitab.h Normal file
View File

@@ -0,0 +1,62 @@
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is expat.
The Initial Developer of the Original Code is James Clark.
Portions created by James Clark are Copyright (C) 1998, 1999
James Clark. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms
of the GNU General Public License (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above. If you
wish to allow use of your version of this file only under the terms of
the GPL and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the
GPL. If you do not delete the provisions above, a recipient may use
your version of this file under either the MPL or the GPL.
*/
/* 0x00 */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0x04 */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0x08 */ BT_NONXML, BT_S, BT_LF, BT_NONXML,
/* 0x0C */ BT_NONXML, BT_CR, BT_NONXML, BT_NONXML,
/* 0x10 */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0x14 */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0x18 */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0x1C */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0x20 */ BT_S, BT_EXCL, BT_QUOT, BT_NUM,
/* 0x24 */ BT_OTHER, BT_PERCNT, BT_AMP, BT_APOS,
/* 0x28 */ BT_LPAR, BT_RPAR, BT_AST, BT_PLUS,
/* 0x2C */ BT_COMMA, BT_MINUS, BT_NAME, BT_SOL,
/* 0x30 */ BT_DIGIT, BT_DIGIT, BT_DIGIT, BT_DIGIT,
/* 0x34 */ BT_DIGIT, BT_DIGIT, BT_DIGIT, BT_DIGIT,
/* 0x38 */ BT_DIGIT, BT_DIGIT, BT_COLON, BT_SEMI,
/* 0x3C */ BT_LT, BT_EQUALS, BT_GT, BT_QUEST,
/* 0x40 */ BT_OTHER, BT_HEX, BT_HEX, BT_HEX,
/* 0x44 */ BT_HEX, BT_HEX, BT_HEX, BT_NMSTRT,
/* 0x48 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x4C */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x50 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x54 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x58 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_LSQB,
/* 0x5C */ BT_OTHER, BT_RSQB, BT_OTHER, BT_NMSTRT,
/* 0x60 */ BT_OTHER, BT_HEX, BT_HEX, BT_HEX,
/* 0x64 */ BT_HEX, BT_HEX, BT_HEX, BT_NMSTRT,
/* 0x68 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x6C */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x70 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x74 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x78 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_OTHER,
/* 0x7C */ BT_VERBAR, BT_OTHER, BT_OTHER, BT_OTHER,

231
simgear/xml/easyxml.cxx Normal file
View File

@@ -0,0 +1,231 @@
// easyxml.cxx - implementation of EasyXML interfaces.
#include "easyxml.hxx"
#include "xmlparse.h"
////////////////////////////////////////////////////////////////////////
// Implementation of XMLAttributes.
////////////////////////////////////////////////////////////////////////
XMLAttributes::XMLAttributes ()
{
}
XMLAttributes::~XMLAttributes ()
{
}
int
XMLAttributes::findAttribute (const char * name) const
{
int s = size();
for (int i = 0; i < s; i++) {
if (strcmp(name, getName(i)) == 0)
return i;
}
return -1;
}
bool
XMLAttributes::hasAttribute (const char * name) const
{
return (findAttribute(name) != -1);
}
const char *
XMLAttributes::getValue (const char * name) const
{
int pos = findAttribute(name);
if (pos >= 0)
return getValue(pos);
else
return 0;
}
////////////////////////////////////////////////////////////////////////
// Implementation of XMLAttributesDefault.
////////////////////////////////////////////////////////////////////////
XMLAttributesDefault::XMLAttributesDefault ()
{
}
XMLAttributesDefault::XMLAttributesDefault (const XMLAttributes &atts)
{
int s = atts.size();
for (int i = 0; i < s; i++)
addAttribute(atts.getName(i), atts.getValue(i));
}
XMLAttributesDefault::~XMLAttributesDefault ()
{
}
int
XMLAttributesDefault::size () const
{
return _atts.size() / 2;
}
const char *
XMLAttributesDefault::getName (int i) const
{
return _atts[i*2].c_str();
}
const char *
XMLAttributesDefault::getValue (int i) const
{
return _atts[i*2+1].c_str();
}
void
XMLAttributesDefault::addAttribute (const char * name, const char * value)
{
_atts.push_back(name);
_atts.push_back(value);
}
void
XMLAttributesDefault::setName (int i, const char * name)
{
_atts[i*2] = name;
}
void
XMLAttributesDefault::setValue (int i, const char * name)
{
_atts[i*2+1] = name;
}
void
XMLAttributesDefault::setValue (const char * name, const char * value)
{
int pos = findAttribute(name);
if (pos >= 0) {
setName(pos, name);
setValue(pos, value);
} else {
addAttribute(name, value);
}
}
////////////////////////////////////////////////////////////////////////
// Attribute list wrapper for Expat.
////////////////////////////////////////////////////////////////////////
class ExpatAtts : public XMLAttributes
{
public:
ExpatAtts (const char ** atts) : _atts(atts) {}
virtual int size () const;
virtual const char * getName (int i) const;
virtual const char * getValue (int i) const;
private:
const char ** _atts;
};
int
ExpatAtts::size () const
{
int s = 0;
for (int i = 0; _atts[i] != 0; i += 2)
s++;
return s;
}
const char *
ExpatAtts::getName (int i) const
{
return _atts[i*2];
}
const char *
ExpatAtts::getValue (int i) const
{
return _atts[i*2+1];
}
////////////////////////////////////////////////////////////////////////
// Static callback functions for Expat.
////////////////////////////////////////////////////////////////////////
#define VISITOR (*((XMLVisitor*)userData))
static void
start_element (void * userData, const char * name, const char ** atts)
{
VISITOR.startElement(name, ExpatAtts(atts));
}
static void
end_element (void * userData, const char * name)
{
VISITOR.endElement(name);
}
static void
character_data (void * userData, const char * s, int len)
{
VISITOR.data(s, len);
}
static void
processing_instruction (void * userData,
const char * target,
const char * data)
{
VISITOR.pi(target, data);
}
#undef VISITOR
////////////////////////////////////////////////////////////////////////
// Implementation of XMLReader.
////////////////////////////////////////////////////////////////////////
bool
readXML (istream &input, XMLVisitor &visitor)
{
bool retval = true;
XML_Parser parser = XML_ParserCreate(0);
XML_SetUserData(parser, &visitor);
XML_SetElementHandler(parser, start_element, end_element);
XML_SetCharacterDataHandler(parser, character_data);
XML_SetProcessingInstructionHandler(parser, processing_instruction);
visitor.startXML();
char buf[16384];
while (!input.eof()) {
input.read(buf,16384);
// TODO: deal with bad stream.
if (!XML_Parse(parser, buf, input.gcount(), false)) {
visitor.error(XML_ErrorString(XML_GetErrorCode(parser)),
XML_GetCurrentColumnNumber(parser),
XML_GetCurrentLineNumber(parser));
retval = false;
}
}
if (retval)
visitor.endXML();
XML_ParserFree(parser);
return retval;
}
// end of easyxml.cxx

91
simgear/xml/easyxml.hxx Normal file
View File

@@ -0,0 +1,91 @@
#ifndef __EASYXML_HXX
#define __EASYXML_HXX
#include <iostream>
#include <string>
#include <vector>
using std::istream;
using std::string;
using std::vector;
/**
* Interface for XML attributes.
*/
class XMLAttributes
{
public:
XMLAttributes ();
virtual ~ XMLAttributes ();
virtual int size () const = 0;
virtual const char * getName (int i) const = 0;
virtual const char * getValue (int i) const = 0;
virtual int findAttribute (const char * name) const;
virtual bool hasAttribute (const char * name) const;
virtual const char * getValue (const char * name) const;
};
/**
* Default mutable attributes implementation.
*/
class XMLAttributesDefault : public XMLAttributes
{
public:
XMLAttributesDefault ();
XMLAttributesDefault (const XMLAttributes & atts);
virtual ~XMLAttributesDefault ();
virtual int size () const;
virtual const char * getName (int i) const;
virtual const char * getValue (int i) const;
virtual void addAttribute (const char * name, const char * value);
virtual void setName (int i, const char * name);
virtual void setValue (int i, const char * value);
virtual void setValue (const char * name, const char * value);
private:
vector<string> _atts;
};
/**
* Visitor class for an XML document.
*
* To read an XML document, a module must subclass the visitor to do
* something useful for the different XML events.
*/
class XMLVisitor
{
public:
// start an XML document
virtual void startXML () {}
// end an XML document
virtual void endXML () {}
// start an element
virtual void startElement (const char * name, const XMLAttributes &atts) {}
// end an element
virtual void endElement (const char * name) {}
// character data
virtual void data (const char * s, int length) {}
// processing instruction
virtual void pi (const char * target, const char * data) {}
// non-fatal warning
virtual void warning (const char * message, int line, int column) {}
// fatal error
virtual void error (const char * message, int line, int column) = 0;
};
/**
* Read an XML document.
*/
extern bool readXML (istream &input, XMLVisitor &visitor);
#endif // __EASYXML_HXX

151
simgear/xml/hashtable.c Normal file
View File

@@ -0,0 +1,151 @@
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
csompliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is expat.
The Initial Developer of the Original Code is James Clark.
Portions created by James Clark are Copyright (C) 1998, 1999
James Clark. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms
of the GNU General Public License (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above. If you
wish to allow use of your version of this file only under the terms of
the GPL and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the
GPL. If you do not delete the provisions above, a recipient may use
your version of this file under either the MPL or the GPL.
*/
#include "xmldef.h"
#ifdef XML_UNICODE_WCHAR_T
#ifndef XML_UNICODE
#define XML_UNICODE
#endif
#endif
#include "hashtable.h"
#define INIT_SIZE 64
static
int keyeq(KEY s1, KEY s2)
{
for (; *s1 == *s2; s1++, s2++)
if (*s1 == 0)
return 1;
return 0;
}
static
unsigned long hash(KEY s)
{
unsigned long h = 0;
while (*s)
h = (h << 5) + h + (unsigned char)*s++;
return h;
}
NAMED *lookup(HASH_TABLE *table, KEY name, size_t createSize)
{
size_t i;
if (table->size == 0) {
if (!createSize)
return 0;
table->v = calloc(INIT_SIZE, sizeof(NAMED *));
if (!table->v)
return 0;
table->size = INIT_SIZE;
table->usedLim = INIT_SIZE / 2;
i = hash(name) & (table->size - 1);
}
else {
unsigned long h = hash(name);
for (i = h & (table->size - 1);
table->v[i];
i == 0 ? i = table->size - 1 : --i) {
if (keyeq(name, table->v[i]->name))
return table->v[i];
}
if (!createSize)
return 0;
if (table->used == table->usedLim) {
/* check for overflow */
size_t newSize = table->size * 2;
NAMED **newV = calloc(newSize, sizeof(NAMED *));
if (!newV)
return 0;
for (i = 0; i < table->size; i++)
if (table->v[i]) {
size_t j;
for (j = hash(table->v[i]->name) & (newSize - 1);
newV[j];
j == 0 ? j = newSize - 1 : --j)
;
newV[j] = table->v[i];
}
free(table->v);
table->v = newV;
table->size = newSize;
table->usedLim = newSize/2;
for (i = h & (table->size - 1);
table->v[i];
i == 0 ? i = table->size - 1 : --i)
;
}
}
table->v[i] = calloc(1, createSize);
if (!table->v[i])
return 0;
table->v[i]->name = name;
(table->used)++;
return table->v[i];
}
void hashTableDestroy(HASH_TABLE *table)
{
size_t i;
for (i = 0; i < table->size; i++) {
NAMED *p = table->v[i];
if (p)
free(p);
}
free(table->v);
}
void hashTableInit(HASH_TABLE *p)
{
p->size = 0;
p->usedLim = 0;
p->used = 0;
p->v = 0;
}
void hashTableIterInit(HASH_TABLE_ITER *iter, const HASH_TABLE *table)
{
iter->p = table->v;
iter->end = iter->p + table->size;
}
NAMED *hashTableIterNext(HASH_TABLE_ITER *iter)
{
while (iter->p != iter->end) {
NAMED *tem = *(iter->p)++;
if (tem)
return tem;
}
return 0;
}

69
simgear/xml/hashtable.h Normal file
View File

@@ -0,0 +1,69 @@
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is expat.
The Initial Developer of the Original Code is James Clark.
Portions created by James Clark are Copyright (C) 1998, 1999
James Clark. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms
of the GNU General Public License (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above. If you
wish to allow use of your version of this file only under the terms of
the GPL and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the
GPL. If you do not delete the provisions above, a recipient may use
your version of this file under either the MPL or the GPL.
*/
#include <stddef.h>
#ifdef XML_UNICODE
#ifdef XML_UNICODE_WCHAR_T
typedef const wchar_t *KEY;
#else /* not XML_UNICODE_WCHAR_T */
typedef const unsigned short *KEY;
#endif /* not XML_UNICODE_WCHAR_T */
#else /* not XML_UNICODE */
typedef const char *KEY;
#endif /* not XML_UNICODE */
typedef struct {
KEY name;
} NAMED;
typedef struct {
NAMED **v;
size_t size;
size_t used;
size_t usedLim;
} HASH_TABLE;
NAMED *lookup(HASH_TABLE *table, KEY name, size_t createSize);
void hashTableInit(HASH_TABLE *);
void hashTableDestroy(HASH_TABLE *);
typedef struct {
NAMED **p;
NAMED **end;
} HASH_TABLE_ITER;
void hashTableIterInit(HASH_TABLE_ITER *, const HASH_TABLE *);
NAMED *hashTableIterNext(HASH_TABLE_ITER *);

63
simgear/xml/iasciitab.h Normal file
View File

@@ -0,0 +1,63 @@
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is expat.
The Initial Developer of the Original Code is James Clark.
Portions created by James Clark are Copyright (C) 1998, 1999
James Clark. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms
of the GNU General Public License (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above. If you
wish to allow use of your version of this file only under the terms of
the GPL and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the
GPL. If you do not delete the provisions above, a recipient may use
your version of this file under either the MPL or the GPL.
*/
/* Like asciitab.h, except that 0xD has code BT_S rather than BT_CR */
/* 0x00 */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0x04 */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0x08 */ BT_NONXML, BT_S, BT_LF, BT_NONXML,
/* 0x0C */ BT_NONXML, BT_S, BT_NONXML, BT_NONXML,
/* 0x10 */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0x14 */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0x18 */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0x1C */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0x20 */ BT_S, BT_EXCL, BT_QUOT, BT_NUM,
/* 0x24 */ BT_OTHER, BT_PERCNT, BT_AMP, BT_APOS,
/* 0x28 */ BT_LPAR, BT_RPAR, BT_AST, BT_PLUS,
/* 0x2C */ BT_COMMA, BT_MINUS, BT_NAME, BT_SOL,
/* 0x30 */ BT_DIGIT, BT_DIGIT, BT_DIGIT, BT_DIGIT,
/* 0x34 */ BT_DIGIT, BT_DIGIT, BT_DIGIT, BT_DIGIT,
/* 0x38 */ BT_DIGIT, BT_DIGIT, BT_COLON, BT_SEMI,
/* 0x3C */ BT_LT, BT_EQUALS, BT_GT, BT_QUEST,
/* 0x40 */ BT_OTHER, BT_HEX, BT_HEX, BT_HEX,
/* 0x44 */ BT_HEX, BT_HEX, BT_HEX, BT_NMSTRT,
/* 0x48 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x4C */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x50 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x54 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x58 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_LSQB,
/* 0x5C */ BT_OTHER, BT_RSQB, BT_OTHER, BT_NMSTRT,
/* 0x60 */ BT_OTHER, BT_HEX, BT_HEX, BT_HEX,
/* 0x64 */ BT_HEX, BT_HEX, BT_HEX, BT_NMSTRT,
/* 0x68 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x6C */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x70 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x74 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0x78 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_OTHER,
/* 0x7C */ BT_VERBAR, BT_OTHER, BT_OTHER, BT_OTHER,

62
simgear/xml/latin1tab.h Normal file
View File

@@ -0,0 +1,62 @@
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is expat.
The Initial Developer of the Original Code is James Clark.
Portions created by James Clark are Copyright (C) 1998, 1999
James Clark. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms
of the GNU General Public License (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above. If you
wish to allow use of your version of this file only under the terms of
the GPL and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the
GPL. If you do not delete the provisions above, a recipient may use
your version of this file under either the MPL or the GPL.
*/
/* 0x80 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0x84 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0x88 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0x8C */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0x90 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0x94 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0x98 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0x9C */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0xA0 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0xA4 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0xA8 */ BT_OTHER, BT_OTHER, BT_NMSTRT, BT_OTHER,
/* 0xAC */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0xB0 */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0xB4 */ BT_OTHER, BT_NMSTRT, BT_OTHER, BT_NAME,
/* 0xB8 */ BT_OTHER, BT_OTHER, BT_NMSTRT, BT_OTHER,
/* 0xBC */ BT_OTHER, BT_OTHER, BT_OTHER, BT_OTHER,
/* 0xC0 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xC4 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xC8 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xCC */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xD0 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xD4 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_OTHER,
/* 0xD8 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xDC */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xE0 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xE4 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xE8 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xEC */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xF0 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xF4 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_OTHER,
/* 0xF8 */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,
/* 0xFC */ BT_NMSTRT, BT_NMSTRT, BT_NMSTRT, BT_NMSTRT,

150
simgear/xml/nametab.h Normal file
View File

@@ -0,0 +1,150 @@
static const unsigned namingBitmap[] = {
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
0x00000000, 0x04000000, 0x87FFFFFE, 0x07FFFFFE,
0x00000000, 0x00000000, 0xFF7FFFFF, 0xFF7FFFFF,
0xFFFFFFFF, 0x7FF3FFFF, 0xFFFFFDFE, 0x7FFFFFFF,
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFE00F, 0xFC31FFFF,
0x00FFFFFF, 0x00000000, 0xFFFF0000, 0xFFFFFFFF,
0xFFFFFFFF, 0xF80001FF, 0x00000003, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0xFFFFD740, 0xFFFFFFFB, 0x547F7FFF, 0x000FFFFD,
0xFFFFDFFE, 0xFFFFFFFF, 0xDFFEFFFF, 0xFFFFFFFF,
0xFFFF0003, 0xFFFFFFFF, 0xFFFF199F, 0x033FCFFF,
0x00000000, 0xFFFE0000, 0x027FFFFF, 0xFFFFFFFE,
0x0000007F, 0x00000000, 0xFFFF0000, 0x000707FF,
0x00000000, 0x07FFFFFE, 0x000007FE, 0xFFFE0000,
0xFFFFFFFF, 0x7CFFFFFF, 0x002F7FFF, 0x00000060,
0xFFFFFFE0, 0x23FFFFFF, 0xFF000000, 0x00000003,
0xFFF99FE0, 0x03C5FDFF, 0xB0000000, 0x00030003,
0xFFF987E0, 0x036DFDFF, 0x5E000000, 0x001C0000,
0xFFFBAFE0, 0x23EDFDFF, 0x00000000, 0x00000001,
0xFFF99FE0, 0x23CDFDFF, 0xB0000000, 0x00000003,
0xD63DC7E0, 0x03BFC718, 0x00000000, 0x00000000,
0xFFFDDFE0, 0x03EFFDFF, 0x00000000, 0x00000003,
0xFFFDDFE0, 0x03EFFDFF, 0x40000000, 0x00000003,
0xFFFDDFE0, 0x03FFFDFF, 0x00000000, 0x00000003,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0xFFFFFFFE, 0x000D7FFF, 0x0000003F, 0x00000000,
0xFEF02596, 0x200D6CAE, 0x0000001F, 0x00000000,
0x00000000, 0x00000000, 0xFFFFFEFF, 0x000003FF,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0xFFFFFFFF, 0xFFFF003F, 0x007FFFFF,
0x0007DAED, 0x50000000, 0x82315001, 0x002C62AB,
0x40000000, 0xF580C900, 0x00000007, 0x02010800,
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
0x0FFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x03FFFFFF,
0x3F3FFFFF, 0xFFFFFFFF, 0xAAFF3F3F, 0x3FFFFFFF,
0xFFFFFFFF, 0x5FDFFFFF, 0x0FCF1FDC, 0x1FDC1FFF,
0x00000000, 0x00004C40, 0x00000000, 0x00000000,
0x00000007, 0x00000000, 0x00000000, 0x00000000,
0x00000080, 0x000003FE, 0xFFFFFFFE, 0xFFFFFFFF,
0x001FFFFF, 0xFFFFFFFE, 0xFFFFFFFF, 0x07FFFFFF,
0xFFFFFFE0, 0x00001FFF, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
0xFFFFFFFF, 0x0000003F, 0x00000000, 0x00000000,
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
0xFFFFFFFF, 0x0000000F, 0x00000000, 0x00000000,
0x00000000, 0x07FF6000, 0x87FFFFFE, 0x07FFFFFE,
0x00000000, 0x00800000, 0xFF7FFFFF, 0xFF7FFFFF,
0x00FFFFFF, 0x00000000, 0xFFFF0000, 0xFFFFFFFF,
0xFFFFFFFF, 0xF80001FF, 0x00030003, 0x00000000,
0xFFFFFFFF, 0xFFFFFFFF, 0x0000003F, 0x00000003,
0xFFFFD7C0, 0xFFFFFFFB, 0x547F7FFF, 0x000FFFFD,
0xFFFFDFFE, 0xFFFFFFFF, 0xDFFEFFFF, 0xFFFFFFFF,
0xFFFF007B, 0xFFFFFFFF, 0xFFFF199F, 0x033FCFFF,
0x00000000, 0xFFFE0000, 0x027FFFFF, 0xFFFFFFFE,
0xFFFE007F, 0xBBFFFFFB, 0xFFFF0016, 0x000707FF,
0x00000000, 0x07FFFFFE, 0x0007FFFF, 0xFFFF03FF,
0xFFFFFFFF, 0x7CFFFFFF, 0xFFEF7FFF, 0x03FF3DFF,
0xFFFFFFEE, 0xF3FFFFFF, 0xFF1E3FFF, 0x0000FFCF,
0xFFF99FEE, 0xD3C5FDFF, 0xB080399F, 0x0003FFCF,
0xFFF987E4, 0xD36DFDFF, 0x5E003987, 0x001FFFC0,
0xFFFBAFEE, 0xF3EDFDFF, 0x00003BBF, 0x0000FFC1,
0xFFF99FEE, 0xF3CDFDFF, 0xB0C0398F, 0x0000FFC3,
0xD63DC7EC, 0xC3BFC718, 0x00803DC7, 0x0000FF80,
0xFFFDDFEE, 0xC3EFFDFF, 0x00603DDF, 0x0000FFC3,
0xFFFDDFEC, 0xC3EFFDFF, 0x40603DDF, 0x0000FFC3,
0xFFFDDFEC, 0xC3FFFDFF, 0x00803DCF, 0x0000FFC3,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0xFFFFFFFE, 0x07FF7FFF, 0x03FF7FFF, 0x00000000,
0xFEF02596, 0x3BFF6CAE, 0x03FF3F5F, 0x00000000,
0x03000000, 0xC2A003FF, 0xFFFFFEFF, 0xFFFE03FF,
0xFEBF0FDF, 0x02FE3FFF, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x1FFF0000, 0x00000002,
0x000000A0, 0x003EFFFE, 0xFFFFFFFE, 0xFFFFFFFF,
0x661FFFFF, 0xFFFFFFFE, 0xFFFFFFFF, 0x77FFFFFF,
};
static const unsigned char nmstrtPages[] = {
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00,
0x00, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x13,
0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x15, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x17,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x18,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
static const unsigned char namePages[] = {
0x19, 0x03, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x00,
0x00, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
0x10, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x13,
0x26, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x27, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x17,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x18,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

9
simgear/xml/sample.xml Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<poem shortname="roses" version="1.0">
<title>Roses are Red</title>
<l n="1">Roses are red,</l>
<l n="2">Violets are blue;</l>
<l n="3">Sugar is sweet,</l>
<l n="4">And I love you.</l>
</poem>

View File

@@ -0,0 +1,56 @@
#include <string>
#include <iostream>
#include <fstream>
#include "easyxml.hxx"
using std::string;
using std::ifstream;
using std::cout;
using std::cerr;
using std::endl;
class MyVisitor : public XMLVisitor
{
public:
virtual void startXML () {
cout << "Start XML" << endl;
}
virtual void endXML () {
cout << "End XML" << endl;
}
virtual void startElement (const char * name, const XMLAttributes &atts) {
cout << "Start element " << name << endl;
for (int i = 0; i < atts.size(); i++)
cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
}
virtual void endElement (const char * name) {
cout << "End element " << name << endl;
}
virtual void data (const char * s, int len) {
cout << "Character data " << string(s,len) << endl;
}
virtual void pi (const char * target, const char * data) {
cout << "Processing instruction " << target << ' ' << data << endl;
}
virtual void warning (const char * message, int line, int column) {
cout << "Warning: " << message << " (" << line << ',' << column << ')'
<< endl;
}
virtual void error (const char * message, int line, int column) {
cout << "Error: " << message << " (" << line << ',' << column << ')'
<< endl;
}
};
main (int ac, const char ** av)
{
MyVisitor visitor;
for (int i = 1; i < ac; i++) {
ifstream input(av[i]);
cout << "Reading " << av[i] << endl;
if (!readXML(input, visitor)) {
cerr << "Error reading from " << av[i] << endl;
}
}
}

63
simgear/xml/utf8tab.h Normal file
View File

@@ -0,0 +1,63 @@
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is expat.
The Initial Developer of the Original Code is James Clark.
Portions created by James Clark are Copyright (C) 1998, 1999
James Clark. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms
of the GNU General Public License (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above. If you
wish to allow use of your version of this file only under the terms of
the GPL and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the
GPL. If you do not delete the provisions above, a recipient may use
your version of this file under either the MPL or the GPL.
*/
/* 0x80 */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0x84 */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0x88 */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0x8C */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0x90 */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0x94 */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0x98 */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0x9C */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0xA0 */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0xA4 */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0xA8 */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0xAC */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0xB0 */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0xB4 */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0xB8 */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0xBC */ BT_TRAIL, BT_TRAIL, BT_TRAIL, BT_TRAIL,
/* 0xC0 */ BT_LEAD2, BT_LEAD2, BT_LEAD2, BT_LEAD2,
/* 0xC4 */ BT_LEAD2, BT_LEAD2, BT_LEAD2, BT_LEAD2,
/* 0xC8 */ BT_LEAD2, BT_LEAD2, BT_LEAD2, BT_LEAD2,
/* 0xCC */ BT_LEAD2, BT_LEAD2, BT_LEAD2, BT_LEAD2,
/* 0xD0 */ BT_LEAD2, BT_LEAD2, BT_LEAD2, BT_LEAD2,
/* 0xD4 */ BT_LEAD2, BT_LEAD2, BT_LEAD2, BT_LEAD2,
/* 0xD8 */ BT_LEAD2, BT_LEAD2, BT_LEAD2, BT_LEAD2,
/* 0xDC */ BT_LEAD2, BT_LEAD2, BT_LEAD2, BT_LEAD2,
/* 0xE0 */ BT_LEAD3, BT_LEAD3, BT_LEAD3, BT_LEAD3,
/* 0xE4 */ BT_LEAD3, BT_LEAD3, BT_LEAD3, BT_LEAD3,
/* 0xE8 */ BT_LEAD3, BT_LEAD3, BT_LEAD3, BT_LEAD3,
/* 0xEC */ BT_LEAD3, BT_LEAD3, BT_LEAD3, BT_LEAD3,
/* 0xF0 */ BT_LEAD4, BT_LEAD4, BT_LEAD4, BT_LEAD4,
/* 0xF4 */ BT_LEAD4, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0xF8 */ BT_NONXML, BT_NONXML, BT_NONXML, BT_NONXML,
/* 0xFC */ BT_NONXML, BT_NONXML, BT_MALFORM, BT_MALFORM,

63
simgear/xml/xmldef.h Normal file
View File

@@ -0,0 +1,63 @@
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is expat.
The Initial Developer of the Original Code is James Clark.
Portions created by James Clark are Copyright (C) 1998, 1999
James Clark. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms
of the GNU General Public License (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above. If you
wish to allow use of your version of this file only under the terms of
the GPL and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the
GPL. If you do not delete the provisions above, a recipient may use
your version of this file under either the MPL or the GPL.
*/
#include <string.h>
#ifdef XML_WINLIB
#define WIN32_LEAN_AND_MEAN
#define STRICT
#include <windows.h>
#define malloc(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define calloc(x, y) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (x)*(y))
#define free(x) HeapFree(GetProcessHeap(), 0, (x))
#define realloc(x, y) HeapReAlloc(GetProcessHeap(), 0, x, y)
#define abort() /* as nothing */
#else /* not XML_WINLIB */
#include <stdlib.h>
#endif /* not XML_WINLIB */
/* This file can be used for any definitions needed in
particular environments. */
#ifdef MOZILLA
#include "nspr.h"
#define malloc(x) PR_Malloc(x)
#define realloc(x, y) PR_Realloc((x), (y))
#define calloc(x, y) PR_Calloc((x),(y))
#define free(x) PR_Free(x)
#define int int32
#endif /* MOZILLA */

3250
simgear/xml/xmlparse.c Normal file

File diff suppressed because it is too large Load Diff

482
simgear/xml/xmlparse.h Normal file
View File

@@ -0,0 +1,482 @@
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is expat.
The Initial Developer of the Original Code is James Clark.
Portions created by James Clark are Copyright (C) 1998, 1999
James Clark. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms
of the GNU General Public License (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above. If you
wish to allow use of your version of this file only under the terms of
the GPL and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the
GPL. If you do not delete the provisions above, a recipient may use
your version of this file under either the MPL or the GPL.
*/
#ifndef XmlParse_INCLUDED
#define XmlParse_INCLUDED 1
#ifdef __cplusplus
extern "C" {
#endif
#ifndef XMLPARSEAPI
#define XMLPARSEAPI /* as nothing */
#endif
typedef void *XML_Parser;
#ifdef XML_UNICODE_WCHAR_T
/* XML_UNICODE_WCHAR_T will work only if sizeof(wchar_t) == 2 and wchar_t
uses Unicode. */
/* Information is UTF-16 encoded as wchar_ts */
#ifndef XML_UNICODE
#define XML_UNICODE
#endif
#include <stddef.h>
typedef wchar_t XML_Char;
typedef wchar_t XML_LChar;
#else /* not XML_UNICODE_WCHAR_T */
#ifdef XML_UNICODE
/* Information is UTF-16 encoded as unsigned shorts */
typedef unsigned short XML_Char;
typedef char XML_LChar;
#else /* not XML_UNICODE */
/* Information is UTF-8 encoded. */
typedef char XML_Char;
typedef char XML_LChar;
#endif /* not XML_UNICODE */
#endif /* not XML_UNICODE_WCHAR_T */
/* Constructs a new parser; encoding is the encoding specified by the external
protocol or null if there is none specified. */
XML_Parser XMLPARSEAPI
XML_ParserCreate(const XML_Char *encoding);
/* Constructs a new parser and namespace processor. Element type names
and attribute names that belong to a namespace will be expanded;
unprefixed attribute names are never expanded; unprefixed element type
names are expanded only if there is a default namespace. The expanded
name is the concatenation of the namespace URI, the namespace separator character,
and the local part of the name. If the namespace separator is '\0' then
the namespace URI and the local part will be concatenated without any
separator. When a namespace is not declared, the name and prefix will be
passed through without expansion. */
XML_Parser XMLPARSEAPI
XML_ParserCreateNS(const XML_Char *encoding, XML_Char namespaceSeparator);
/* atts is array of name/value pairs, terminated by 0;
names and values are 0 terminated. */
typedef void (*XML_StartElementHandler)(void *userData,
const XML_Char *name,
const XML_Char **atts);
typedef void (*XML_EndElementHandler)(void *userData,
const XML_Char *name);
/* s is not 0 terminated. */
typedef void (*XML_CharacterDataHandler)(void *userData,
const XML_Char *s,
int len);
/* target and data are 0 terminated */
typedef void (*XML_ProcessingInstructionHandler)(void *userData,
const XML_Char *target,
const XML_Char *data);
/* data is 0 terminated */
typedef void (*XML_CommentHandler)(void *userData, const XML_Char *data);
typedef void (*XML_StartCdataSectionHandler)(void *userData);
typedef void (*XML_EndCdataSectionHandler)(void *userData);
/* This is called for any characters in the XML document for
which there is no applicable handler. This includes both
characters that are part of markup which is of a kind that is
not reported (comments, markup declarations), or characters
that are part of a construct which could be reported but
for which no handler has been supplied. The characters are passed
exactly as they were in the XML document except that
they will be encoded in UTF-8. Line boundaries are not normalized.
Note that a byte order mark character is not passed to the default handler.
There are no guarantees about how characters are divided between calls
to the default handler: for example, a comment might be split between
multiple calls. */
typedef void (*XML_DefaultHandler)(void *userData,
const XML_Char *s,
int len);
/* This is called for a declaration of an unparsed (NDATA)
entity. The base argument is whatever was set by XML_SetBase.
The entityName, systemId and notationName arguments will never be null.
The other arguments may be. */
typedef void (*XML_UnparsedEntityDeclHandler)(void *userData,
const XML_Char *entityName,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId,
const XML_Char *notationName);
/* This is called for a declaration of notation.
The base argument is whatever was set by XML_SetBase.
The notationName will never be null. The other arguments can be. */
typedef void (*XML_NotationDeclHandler)(void *userData,
const XML_Char *notationName,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId);
/* When namespace processing is enabled, these are called once for
each namespace declaration. The call to the start and end element
handlers occur between the calls to the start and end namespace
declaration handlers. For an xmlns attribute, prefix will be null.
For an xmlns="" attribute, uri will be null. */
typedef void (*XML_StartNamespaceDeclHandler)(void *userData,
const XML_Char *prefix,
const XML_Char *uri);
typedef void (*XML_EndNamespaceDeclHandler)(void *userData,
const XML_Char *prefix);
/* This is called if the document is not standalone (it has an
external subset or a reference to a parameter entity, but does not
have standalone="yes"). If this handler returns 0, then processing
will not continue, and the parser will return a
XML_ERROR_NOT_STANDALONE error. */
typedef int (*XML_NotStandaloneHandler)(void *userData);
/* This is called for a reference to an external parsed general entity.
The referenced entity is not automatically parsed.
The application can parse it immediately or later using
XML_ExternalEntityParserCreate.
The parser argument is the parser parsing the entity containing the reference;
it can be passed as the parser argument to XML_ExternalEntityParserCreate.
The systemId argument is the system identifier as specified in the entity declaration;
it will not be null.
The base argument is the system identifier that should be used as the base for
resolving systemId if systemId was relative; this is set by XML_SetBase;
it may be null.
The publicId argument is the public identifier as specified in the entity declaration,
or null if none was specified; the whitespace in the public identifier
will have been normalized as required by the XML spec.
The context argument specifies the parsing context in the format
expected by the context argument to
XML_ExternalEntityParserCreate; context is valid only until the handler
returns, so if the referenced entity is to be parsed later, it must be copied.
The handler should return 0 if processing should not continue because of
a fatal error in the handling of the external entity.
In this case the calling parser will return an XML_ERROR_EXTERNAL_ENTITY_HANDLING
error.
Note that unlike other handlers the first argument is the parser, not userData. */
typedef int (*XML_ExternalEntityRefHandler)(XML_Parser parser,
const XML_Char *context,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId);
/* This structure is filled in by the XML_UnknownEncodingHandler
to provide information to the parser about encodings that are unknown
to the parser.
The map[b] member gives information about byte sequences
whose first byte is b.
If map[b] is c where c is >= 0, then b by itself encodes the Unicode scalar value c.
If map[b] is -1, then the byte sequence is malformed.
If map[b] is -n, where n >= 2, then b is the first byte of an n-byte
sequence that encodes a single Unicode scalar value.
The data member will be passed as the first argument to the convert function.
The convert function is used to convert multibyte sequences;
s will point to a n-byte sequence where map[(unsigned char)*s] == -n.
The convert function must return the Unicode scalar value
represented by this byte sequence or -1 if the byte sequence is malformed.
The convert function may be null if the encoding is a single-byte encoding,
that is if map[b] >= -1 for all bytes b.
When the parser is finished with the encoding, then if release is not null,
it will call release passing it the data member;
once release has been called, the convert function will not be called again.
Expat places certain restrictions on the encodings that are supported
using this mechanism.
1. Every ASCII character that can appear in a well-formed XML document,
other than the characters
$@\^`{}~
must be represented by a single byte, and that byte must be the
same byte that represents that character in ASCII.
2. No character may require more than 4 bytes to encode.
3. All characters encoded must have Unicode scalar values <= 0xFFFF,
(ie characters that would be encoded by surrogates in UTF-16
are not allowed). Note that this restriction doesn't apply to
the built-in support for UTF-8 and UTF-16.
4. No Unicode character may be encoded by more than one distinct sequence
of bytes. */
typedef struct {
int map[256];
void *data;
int (*convert)(void *data, const char *s);
void (*release)(void *data);
} XML_Encoding;
/* This is called for an encoding that is unknown to the parser.
The encodingHandlerData argument is that which was passed as the
second argument to XML_SetUnknownEncodingHandler.
The name argument gives the name of the encoding as specified in
the encoding declaration.
If the callback can provide information about the encoding,
it must fill in the XML_Encoding structure, and return 1.
Otherwise it must return 0.
If info does not describe a suitable encoding,
then the parser will return an XML_UNKNOWN_ENCODING error. */
typedef int (*XML_UnknownEncodingHandler)(void *encodingHandlerData,
const XML_Char *name,
XML_Encoding *info);
void XMLPARSEAPI
XML_SetElementHandler(XML_Parser parser,
XML_StartElementHandler start,
XML_EndElementHandler end);
void XMLPARSEAPI
XML_SetCharacterDataHandler(XML_Parser parser,
XML_CharacterDataHandler handler);
void XMLPARSEAPI
XML_SetProcessingInstructionHandler(XML_Parser parser,
XML_ProcessingInstructionHandler handler);
void XMLPARSEAPI
XML_SetCommentHandler(XML_Parser parser,
XML_CommentHandler handler);
void XMLPARSEAPI
XML_SetCdataSectionHandler(XML_Parser parser,
XML_StartCdataSectionHandler start,
XML_EndCdataSectionHandler end);
/* This sets the default handler and also inhibits expansion of internal entities.
The entity reference will be passed to the default handler. */
void XMLPARSEAPI
XML_SetDefaultHandler(XML_Parser parser,
XML_DefaultHandler handler);
/* This sets the default handler but does not inhibit expansion of internal entities.
The entity reference will not be passed to the default handler. */
void XMLPARSEAPI
XML_SetDefaultHandlerExpand(XML_Parser parser,
XML_DefaultHandler handler);
void XMLPARSEAPI
XML_SetUnparsedEntityDeclHandler(XML_Parser parser,
XML_UnparsedEntityDeclHandler handler);
void XMLPARSEAPI
XML_SetNotationDeclHandler(XML_Parser parser,
XML_NotationDeclHandler handler);
void XMLPARSEAPI
XML_SetNamespaceDeclHandler(XML_Parser parser,
XML_StartNamespaceDeclHandler start,
XML_EndNamespaceDeclHandler end);
void XMLPARSEAPI
XML_SetNotStandaloneHandler(XML_Parser parser,
XML_NotStandaloneHandler handler);
void XMLPARSEAPI
XML_SetExternalEntityRefHandler(XML_Parser parser,
XML_ExternalEntityRefHandler handler);
/* If a non-null value for arg is specified here, then it will be passed
as the first argument to the external entity ref handler instead
of the parser object. */
void XMLPARSEAPI
XML_SetExternalEntityRefHandlerArg(XML_Parser, void *arg);
void XMLPARSEAPI
XML_SetUnknownEncodingHandler(XML_Parser parser,
XML_UnknownEncodingHandler handler,
void *encodingHandlerData);
/* This can be called within a handler for a start element, end element,
processing instruction or character data. It causes the corresponding
markup to be passed to the default handler. */
void XMLPARSEAPI XML_DefaultCurrent(XML_Parser parser);
/* This value is passed as the userData argument to callbacks. */
void XMLPARSEAPI
XML_SetUserData(XML_Parser parser, void *userData);
/* Returns the last value set by XML_SetUserData or null. */
#define XML_GetUserData(parser) (*(void **)(parser))
/* This is equivalent to supplying an encoding argument
to XML_CreateParser. It must not be called after XML_Parse
or XML_ParseBuffer. */
int XMLPARSEAPI
XML_SetEncoding(XML_Parser parser, const XML_Char *encoding);
/* If this function is called, then the parser will be passed
as the first argument to callbacks instead of userData.
The userData will still be accessible using XML_GetUserData. */
void XMLPARSEAPI
XML_UseParserAsHandlerArg(XML_Parser parser);
/* Sets the base to be used for resolving relative URIs in system identifiers in
declarations. Resolving relative identifiers is left to the application:
this value will be passed through as the base argument to the
XML_ExternalEntityRefHandler, XML_NotationDeclHandler
and XML_UnparsedEntityDeclHandler. The base argument will be copied.
Returns zero if out of memory, non-zero otherwise. */
int XMLPARSEAPI
XML_SetBase(XML_Parser parser, const XML_Char *base);
const XML_Char XMLPARSEAPI *
XML_GetBase(XML_Parser parser);
/* Returns the number of the attributes passed in last call to the
XML_StartElementHandler that were specified in the start-tag rather
than defaulted. */
int XMLPARSEAPI XML_GetSpecifiedAttributeCount(XML_Parser parser);
/* Parses some input. Returns 0 if a fatal error is detected.
The last call to XML_Parse must have isFinal true;
len may be zero for this call (or any other). */
int XMLPARSEAPI
XML_Parse(XML_Parser parser, const char *s, int len, int isFinal);
void XMLPARSEAPI *
XML_GetBuffer(XML_Parser parser, int len);
int XMLPARSEAPI
XML_ParseBuffer(XML_Parser parser, int len, int isFinal);
/* Creates an XML_Parser object that can parse an external general entity;
context is a '\0'-terminated string specifying the parse context;
encoding is a '\0'-terminated string giving the name of the externally specified encoding,
or null if there is no externally specified encoding.
The context string consists of a sequence of tokens separated by formfeeds (\f);
a token consisting of a name specifies that the general entity of the name
is open; a token of the form prefix=uri specifies the namespace for a particular
prefix; a token of the form =uri specifies the default namespace.
This can be called at any point after the first call to an ExternalEntityRefHandler
so longer as the parser has not yet been freed.
The new parser is completely independent and may safely be used in a separate thread.
The handlers and userData are initialized from the parser argument.
Returns 0 if out of memory. Otherwise returns a new XML_Parser object. */
XML_Parser XMLPARSEAPI
XML_ExternalEntityParserCreate(XML_Parser parser,
const XML_Char *context,
const XML_Char *encoding);
enum XML_Error {
XML_ERROR_NONE,
XML_ERROR_NO_MEMORY,
XML_ERROR_SYNTAX,
XML_ERROR_NO_ELEMENTS,
XML_ERROR_INVALID_TOKEN,
XML_ERROR_UNCLOSED_TOKEN,
XML_ERROR_PARTIAL_CHAR,
XML_ERROR_TAG_MISMATCH,
XML_ERROR_DUPLICATE_ATTRIBUTE,
XML_ERROR_JUNK_AFTER_DOC_ELEMENT,
XML_ERROR_PARAM_ENTITY_REF,
XML_ERROR_UNDEFINED_ENTITY,
XML_ERROR_RECURSIVE_ENTITY_REF,
XML_ERROR_ASYNC_ENTITY,
XML_ERROR_BAD_CHAR_REF,
XML_ERROR_BINARY_ENTITY_REF,
XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF,
XML_ERROR_MISPLACED_XML_PI,
XML_ERROR_UNKNOWN_ENCODING,
XML_ERROR_INCORRECT_ENCODING,
XML_ERROR_UNCLOSED_CDATA_SECTION,
XML_ERROR_EXTERNAL_ENTITY_HANDLING,
XML_ERROR_NOT_STANDALONE
};
/* If XML_Parse or XML_ParseBuffer have returned 0, then XML_GetErrorCode
returns information about the error. */
enum XML_Error XMLPARSEAPI XML_GetErrorCode(XML_Parser parser);
/* These functions return information about the current parse location.
They may be called when XML_Parse or XML_ParseBuffer return 0;
in this case the location is the location of the character at which
the error was detected.
They may also be called from any other callback called to report
some parse event; in this the location is the location of the first
of the sequence of characters that generated the event. */
int XMLPARSEAPI XML_GetCurrentLineNumber(XML_Parser parser);
int XMLPARSEAPI XML_GetCurrentColumnNumber(XML_Parser parser);
long XMLPARSEAPI XML_GetCurrentByteIndex(XML_Parser parser);
/* Return the number of bytes in the current event.
Returns 0 if the event is in an internal entity. */
int XMLPARSEAPI XML_GetCurrentByteCount(XML_Parser parser);
/* For backwards compatibility with previous versions. */
#define XML_GetErrorLineNumber XML_GetCurrentLineNumber
#define XML_GetErrorColumnNumber XML_GetCurrentColumnNumber
#define XML_GetErrorByteIndex XML_GetCurrentByteIndex
/* Frees memory used by the parser. */
void XMLPARSEAPI
XML_ParserFree(XML_Parser parser);
/* Returns a string describing the error. */
const XML_LChar XMLPARSEAPI *XML_ErrorString(int code);
#ifdef __cplusplus
}
#endif
#endif /* not XmlParse_INCLUDED */

1113
simgear/xml/xmlrole.c Normal file

File diff suppressed because it is too large Load Diff

111
simgear/xml/xmlrole.h Normal file
View File

@@ -0,0 +1,111 @@
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is expat.
The Initial Developer of the Original Code is James Clark.
Portions created by James Clark are Copyright (C) 1998, 1999
James Clark. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms
of the GNU General Public License (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above. If you
wish to allow use of your version of this file only under the terms of
the GPL and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the
GPL. If you do not delete the provisions above, a recipient may use
your version of this file under either the MPL or the GPL.
*/
#ifndef XmlRole_INCLUDED
#define XmlRole_INCLUDED 1
#include "xmltok.h"
#ifdef __cplusplus
extern "C" {
#endif
enum {
XML_ROLE_ERROR = -1,
XML_ROLE_NONE = 0,
XML_ROLE_XML_DECL,
XML_ROLE_INSTANCE_START,
XML_ROLE_DOCTYPE_NAME,
XML_ROLE_DOCTYPE_SYSTEM_ID,
XML_ROLE_DOCTYPE_PUBLIC_ID,
XML_ROLE_DOCTYPE_CLOSE,
XML_ROLE_GENERAL_ENTITY_NAME,
XML_ROLE_PARAM_ENTITY_NAME,
XML_ROLE_ENTITY_VALUE,
XML_ROLE_ENTITY_SYSTEM_ID,
XML_ROLE_ENTITY_PUBLIC_ID,
XML_ROLE_ENTITY_NOTATION_NAME,
XML_ROLE_NOTATION_NAME,
XML_ROLE_NOTATION_SYSTEM_ID,
XML_ROLE_NOTATION_NO_SYSTEM_ID,
XML_ROLE_NOTATION_PUBLIC_ID,
XML_ROLE_ATTRIBUTE_NAME,
XML_ROLE_ATTRIBUTE_TYPE_CDATA,
XML_ROLE_ATTRIBUTE_TYPE_ID,
XML_ROLE_ATTRIBUTE_TYPE_IDREF,
XML_ROLE_ATTRIBUTE_TYPE_IDREFS,
XML_ROLE_ATTRIBUTE_TYPE_ENTITY,
XML_ROLE_ATTRIBUTE_TYPE_ENTITIES,
XML_ROLE_ATTRIBUTE_TYPE_NMTOKEN,
XML_ROLE_ATTRIBUTE_TYPE_NMTOKENS,
XML_ROLE_ATTRIBUTE_ENUM_VALUE,
XML_ROLE_ATTRIBUTE_NOTATION_VALUE,
XML_ROLE_ATTLIST_ELEMENT_NAME,
XML_ROLE_IMPLIED_ATTRIBUTE_VALUE,
XML_ROLE_REQUIRED_ATTRIBUTE_VALUE,
XML_ROLE_DEFAULT_ATTRIBUTE_VALUE,
XML_ROLE_FIXED_ATTRIBUTE_VALUE,
XML_ROLE_ELEMENT_NAME,
XML_ROLE_CONTENT_ANY,
XML_ROLE_CONTENT_EMPTY,
XML_ROLE_CONTENT_PCDATA,
XML_ROLE_GROUP_OPEN,
XML_ROLE_GROUP_CLOSE,
XML_ROLE_GROUP_CLOSE_REP,
XML_ROLE_GROUP_CLOSE_OPT,
XML_ROLE_GROUP_CLOSE_PLUS,
XML_ROLE_GROUP_CHOICE,
XML_ROLE_GROUP_SEQUENCE,
XML_ROLE_CONTENT_ELEMENT,
XML_ROLE_CONTENT_ELEMENT_REP,
XML_ROLE_CONTENT_ELEMENT_OPT,
XML_ROLE_CONTENT_ELEMENT_PLUS,
XML_ROLE_PARAM_ENTITY_REF
};
typedef struct prolog_state {
int (*handler)(struct prolog_state *state,
int tok,
const char *ptr,
const char *end,
const ENCODING *enc);
unsigned level;
} PROLOG_STATE;
void XMLTOKAPI XmlPrologStateInit(PROLOG_STATE *);
#define XmlTokenRole(state, tok, ptr, end, enc) \
(((state)->handler)(state, tok, ptr, end, enc))
#ifdef __cplusplus
}
#endif
#endif /* not XmlRole_INCLUDED */

1527
simgear/xml/xmltok.c Normal file

File diff suppressed because it is too large Load Diff

307
simgear/xml/xmltok.h Normal file
View File

@@ -0,0 +1,307 @@
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is expat.
The Initial Developer of the Original Code is James Clark.
Portions created by James Clark are Copyright (C) 1998, 1999
James Clark. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms
of the GNU General Public License (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above. If you
wish to allow use of your version of this file only under the terms of
the GPL and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the
GPL. If you do not delete the provisions above, a recipient may use
your version of this file under either the MPL or the GPL.
*/
#ifndef XmlTok_INCLUDED
#define XmlTok_INCLUDED 1
#ifdef __cplusplus
extern "C" {
#endif
#ifndef XMLTOKAPI
#define XMLTOKAPI /* as nothing */
#endif
/* The following token may be returned by XmlContentTok */
#define XML_TOK_TRAILING_RSQB -5 /* ] or ]] at the end of the scan; might be start of
illegal ]]> sequence */
/* The following tokens may be returned by both XmlPrologTok and XmlContentTok */
#define XML_TOK_NONE -4 /* The string to be scanned is empty */
#define XML_TOK_TRAILING_CR -3 /* A CR at the end of the scan;
might be part of CRLF sequence */
#define XML_TOK_PARTIAL_CHAR -2 /* only part of a multibyte sequence */
#define XML_TOK_PARTIAL -1 /* only part of a token */
#define XML_TOK_INVALID 0
/* The following tokens are returned by XmlContentTok; some are also
returned by XmlAttributeValueTok, XmlEntityTok, XmlCdataSectionTok */
#define XML_TOK_START_TAG_WITH_ATTS 1
#define XML_TOK_START_TAG_NO_ATTS 2
#define XML_TOK_EMPTY_ELEMENT_WITH_ATTS 3 /* empty element tag <e/> */
#define XML_TOK_EMPTY_ELEMENT_NO_ATTS 4
#define XML_TOK_END_TAG 5
#define XML_TOK_DATA_CHARS 6
#define XML_TOK_DATA_NEWLINE 7
#define XML_TOK_CDATA_SECT_OPEN 8
#define XML_TOK_ENTITY_REF 9
#define XML_TOK_CHAR_REF 10 /* numeric character reference */
/* The following tokens may be returned by both XmlPrologTok and XmlContentTok */
#define XML_TOK_PI 11 /* processing instruction */
#define XML_TOK_XML_DECL 12 /* XML decl or text decl */
#define XML_TOK_COMMENT 13
#define XML_TOK_BOM 14 /* Byte order mark */
/* The following tokens are returned only by XmlPrologTok */
#define XML_TOK_PROLOG_S 15
#define XML_TOK_DECL_OPEN 16 /* <!foo */
#define XML_TOK_DECL_CLOSE 17 /* > */
#define XML_TOK_NAME 18
#define XML_TOK_NMTOKEN 19
#define XML_TOK_POUND_NAME 20 /* #name */
#define XML_TOK_OR 21 /* | */
#define XML_TOK_PERCENT 22
#define XML_TOK_OPEN_PAREN 23
#define XML_TOK_CLOSE_PAREN 24
#define XML_TOK_OPEN_BRACKET 25
#define XML_TOK_CLOSE_BRACKET 26
#define XML_TOK_LITERAL 27
#define XML_TOK_PARAM_ENTITY_REF 28
#define XML_TOK_INSTANCE_START 29
/* The following occur only in element type declarations */
#define XML_TOK_NAME_QUESTION 30 /* name? */
#define XML_TOK_NAME_ASTERISK 31 /* name* */
#define XML_TOK_NAME_PLUS 32 /* name+ */
#define XML_TOK_COND_SECT_OPEN 33 /* <![ */
#define XML_TOK_COND_SECT_CLOSE 34 /* ]]> */
#define XML_TOK_CLOSE_PAREN_QUESTION 35 /* )? */
#define XML_TOK_CLOSE_PAREN_ASTERISK 36 /* )* */
#define XML_TOK_CLOSE_PAREN_PLUS 37 /* )+ */
#define XML_TOK_COMMA 38
/* The following token is returned only by XmlAttributeValueTok */
#define XML_TOK_ATTRIBUTE_VALUE_S 39
/* The following token is returned only by XmlCdataSectionTok */
#define XML_TOK_CDATA_SECT_CLOSE 40
/* With namespace processing this is returned by XmlPrologTok
for a name with a colon. */
#define XML_TOK_PREFIXED_NAME 41
#define XML_N_STATES 3
#define XML_PROLOG_STATE 0
#define XML_CONTENT_STATE 1
#define XML_CDATA_SECTION_STATE 2
#define XML_N_LITERAL_TYPES 2
#define XML_ATTRIBUTE_VALUE_LITERAL 0
#define XML_ENTITY_VALUE_LITERAL 1
/* The size of the buffer passed to XmlUtf8Encode must be at least this. */
#define XML_UTF8_ENCODE_MAX 4
/* The size of the buffer passed to XmlUtf16Encode must be at least this. */
#define XML_UTF16_ENCODE_MAX 2
typedef struct position {
/* first line and first column are 0 not 1 */
unsigned long lineNumber;
unsigned long columnNumber;
} POSITION;
typedef struct {
const char *name;
const char *valuePtr;
const char *valueEnd;
char normalized;
} ATTRIBUTE;
struct encoding;
typedef struct encoding ENCODING;
struct encoding {
int (*scanners[XML_N_STATES])(const ENCODING *,
const char *,
const char *,
const char **);
int (*literalScanners[XML_N_LITERAL_TYPES])(const ENCODING *,
const char *,
const char *,
const char **);
int (*sameName)(const ENCODING *,
const char *, const char *);
int (*nameMatchesAscii)(const ENCODING *,
const char *, const char *);
int (*nameLength)(const ENCODING *, const char *);
const char *(*skipS)(const ENCODING *, const char *);
int (*getAtts)(const ENCODING *enc, const char *ptr,
int attsMax, ATTRIBUTE *atts);
int (*charRefNumber)(const ENCODING *enc, const char *ptr);
int (*predefinedEntityName)(const ENCODING *, const char *, const char *);
void (*updatePosition)(const ENCODING *,
const char *ptr,
const char *end,
POSITION *);
int (*isPublicId)(const ENCODING *enc, const char *ptr, const char *end,
const char **badPtr);
void (*utf8Convert)(const ENCODING *enc,
const char **fromP,
const char *fromLim,
char **toP,
const char *toLim);
void (*utf16Convert)(const ENCODING *enc,
const char **fromP,
const char *fromLim,
unsigned short **toP,
const unsigned short *toLim);
int minBytesPerChar;
char isUtf8;
char isUtf16;
};
/*
Scan the string starting at ptr until the end of the next complete token,
but do not scan past eptr. Return an integer giving the type of token.
Return XML_TOK_NONE when ptr == eptr; nextTokPtr will not be set.
Return XML_TOK_PARTIAL when the string does not contain a complete token;
nextTokPtr will not be set.
Return XML_TOK_INVALID when the string does not start a valid token; nextTokPtr
will be set to point to the character which made the token invalid.
Otherwise the string starts with a valid token; nextTokPtr will be set to point
to the character following the end of that token.
Each data character counts as a single token, but adjacent data characters
may be returned together. Similarly for characters in the prolog outside
literals, comments and processing instructions.
*/
#define XmlTok(enc, state, ptr, end, nextTokPtr) \
(((enc)->scanners[state])(enc, ptr, end, nextTokPtr))
#define XmlPrologTok(enc, ptr, end, nextTokPtr) \
XmlTok(enc, XML_PROLOG_STATE, ptr, end, nextTokPtr)
#define XmlContentTok(enc, ptr, end, nextTokPtr) \
XmlTok(enc, XML_CONTENT_STATE, ptr, end, nextTokPtr)
#define XmlCdataSectionTok(enc, ptr, end, nextTokPtr) \
XmlTok(enc, XML_CDATA_SECTION_STATE, ptr, end, nextTokPtr)
/* This is used for performing a 2nd-level tokenization on
the content of a literal that has already been returned by XmlTok. */
#define XmlLiteralTok(enc, literalType, ptr, end, nextTokPtr) \
(((enc)->literalScanners[literalType])(enc, ptr, end, nextTokPtr))
#define XmlAttributeValueTok(enc, ptr, end, nextTokPtr) \
XmlLiteralTok(enc, XML_ATTRIBUTE_VALUE_LITERAL, ptr, end, nextTokPtr)
#define XmlEntityValueTok(enc, ptr, end, nextTokPtr) \
XmlLiteralTok(enc, XML_ENTITY_VALUE_LITERAL, ptr, end, nextTokPtr)
#define XmlSameName(enc, ptr1, ptr2) (((enc)->sameName)(enc, ptr1, ptr2))
#define XmlNameMatchesAscii(enc, ptr1, ptr2) \
(((enc)->nameMatchesAscii)(enc, ptr1, ptr2))
#define XmlNameLength(enc, ptr) \
(((enc)->nameLength)(enc, ptr))
#define XmlSkipS(enc, ptr) \
(((enc)->skipS)(enc, ptr))
#define XmlGetAttributes(enc, ptr, attsMax, atts) \
(((enc)->getAtts)(enc, ptr, attsMax, atts))
#define XmlCharRefNumber(enc, ptr) \
(((enc)->charRefNumber)(enc, ptr))
#define XmlPredefinedEntityName(enc, ptr, end) \
(((enc)->predefinedEntityName)(enc, ptr, end))
#define XmlUpdatePosition(enc, ptr, end, pos) \
(((enc)->updatePosition)(enc, ptr, end, pos))
#define XmlIsPublicId(enc, ptr, end, badPtr) \
(((enc)->isPublicId)(enc, ptr, end, badPtr))
#define XmlUtf8Convert(enc, fromP, fromLim, toP, toLim) \
(((enc)->utf8Convert)(enc, fromP, fromLim, toP, toLim))
#define XmlUtf16Convert(enc, fromP, fromLim, toP, toLim) \
(((enc)->utf16Convert)(enc, fromP, fromLim, toP, toLim))
typedef struct {
ENCODING initEnc;
const ENCODING **encPtr;
} INIT_ENCODING;
int XMLTOKAPI XmlParseXmlDecl(int isGeneralTextEntity,
const ENCODING *enc,
const char *ptr,
const char *end,
const char **badPtr,
const char **versionPtr,
const char **encodingNamePtr,
const ENCODING **namedEncodingPtr,
int *standalonePtr);
int XMLTOKAPI XmlInitEncoding(INIT_ENCODING *, const ENCODING **, const char *name);
const ENCODING XMLTOKAPI *XmlGetUtf8InternalEncoding();
const ENCODING XMLTOKAPI *XmlGetUtf16InternalEncoding();
int XMLTOKAPI XmlUtf8Encode(int charNumber, char *buf);
int XMLTOKAPI XmlUtf16Encode(int charNumber, unsigned short *buf);
int XMLTOKAPI XmlSizeOfUnknownEncoding();
ENCODING XMLTOKAPI *
XmlInitUnknownEncoding(void *mem,
int *table,
int (*conv)(void *userData, const char *p),
void *userData);
int XMLTOKAPI XmlParseXmlDeclNS(int isGeneralTextEntity,
const ENCODING *enc,
const char *ptr,
const char *end,
const char **badPtr,
const char **versionPtr,
const char **encodingNamePtr,
const ENCODING **namedEncodingPtr,
int *standalonePtr);
int XMLTOKAPI XmlInitEncodingNS(INIT_ENCODING *, const ENCODING **, const char *name);
const ENCODING XMLTOKAPI *XmlGetUtf8InternalEncodingNS();
const ENCODING XMLTOKAPI *XmlGetUtf16InternalEncodingNS();
ENCODING XMLTOKAPI *
XmlInitUnknownEncodingNS(void *mem,
int *table,
int (*conv)(void *userData, const char *p),
void *userData);
#ifdef __cplusplus
}
#endif
#endif /* not XmlTok_INCLUDED */

1746
simgear/xml/xmltok_impl.c Normal file

File diff suppressed because it is too large Load Diff

71
simgear/xml/xmltok_impl.h Normal file
View File

@@ -0,0 +1,71 @@
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is expat.
The Initial Developer of the Original Code is James Clark.
Portions created by James Clark are Copyright (C) 1998, 1999
James Clark. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms
of the GNU General Public License (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above. If you
wish to allow use of your version of this file only under the terms of
the GPL and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the
GPL. If you do not delete the provisions above, a recipient may use
your version of this file under either the MPL or the GPL.
*/
enum {
BT_NONXML,
BT_MALFORM,
BT_LT,
BT_AMP,
BT_RSQB,
BT_LEAD2,
BT_LEAD3,
BT_LEAD4,
BT_TRAIL,
BT_CR,
BT_LF,
BT_GT,
BT_QUOT,
BT_APOS,
BT_EQUALS,
BT_QUEST,
BT_EXCL,
BT_SOL,
BT_SEMI,
BT_NUM,
BT_LSQB,
BT_S,
BT_NMSTRT,
BT_COLON,
BT_HEX,
BT_DIGIT,
BT_NAME,
BT_MINUS,
BT_OTHER, /* known not to be a name or name start character */
BT_NONASCII, /* might be a name or name start character */
BT_PERCNT,
BT_LPAR,
BT_RPAR,
BT_AST,
BT_PLUS,
BT_COMMA,
BT_VERBAR
};
#include <stddef.h>

96
simgear/xml/xmltok_ns.c Normal file
View File

@@ -0,0 +1,96 @@
const ENCODING *NS(XmlGetUtf8InternalEncoding)()
{
return &ns(internal_utf8_encoding).enc;
}
const ENCODING *NS(XmlGetUtf16InternalEncoding)()
{
#if XML_BYTE_ORDER == 12
return &ns(internal_little2_encoding).enc;
#elif XML_BYTE_ORDER == 21
return &ns(internal_big2_encoding).enc;
#else
const short n = 1;
return *(const char *)&n ? &ns(internal_little2_encoding).enc : &ns(internal_big2_encoding).enc;
#endif
}
static
const ENCODING *NS(encodings)[] = {
&ns(latin1_encoding).enc,
&ns(ascii_encoding).enc,
&ns(utf8_encoding).enc,
&ns(big2_encoding).enc,
&ns(big2_encoding).enc,
&ns(little2_encoding).enc,
&ns(utf8_encoding).enc /* NO_ENC */
};
static
int NS(initScanProlog)(const ENCODING *enc, const char *ptr, const char *end,
const char **nextTokPtr)
{
return initScan(NS(encodings), (const INIT_ENCODING *)enc, XML_PROLOG_STATE, ptr, end, nextTokPtr);
}
static
int NS(initScanContent)(const ENCODING *enc, const char *ptr, const char *end,
const char **nextTokPtr)
{
return initScan(NS(encodings), (const INIT_ENCODING *)enc, XML_CONTENT_STATE, ptr, end, nextTokPtr);
}
int NS(XmlInitEncoding)(INIT_ENCODING *p, const ENCODING **encPtr, const char *name)
{
int i = getEncodingIndex(name);
if (i == UNKNOWN_ENC)
return 0;
INIT_ENC_INDEX(p) = (char)i;
p->initEnc.scanners[XML_PROLOG_STATE] = NS(initScanProlog);
p->initEnc.scanners[XML_CONTENT_STATE] = NS(initScanContent);
p->initEnc.updatePosition = initUpdatePosition;
p->encPtr = encPtr;
*encPtr = &(p->initEnc);
return 1;
}
static
const ENCODING *NS(findEncoding)(const ENCODING *enc, const char *ptr, const char *end)
{
#define ENCODING_MAX 128
char buf[ENCODING_MAX];
char *p = buf;
int i;
XmlUtf8Convert(enc, &ptr, end, &p, p + ENCODING_MAX - 1);
if (ptr != end)
return 0;
*p = 0;
if (streqci(buf, "UTF-16") && enc->minBytesPerChar == 2)
return enc;
i = getEncodingIndex(buf);
if (i == UNKNOWN_ENC)
return 0;
return NS(encodings)[i];
}
int NS(XmlParseXmlDecl)(int isGeneralTextEntity,
const ENCODING *enc,
const char *ptr,
const char *end,
const char **badPtr,
const char **versionPtr,
const char **encodingName,
const ENCODING **encoding,
int *standalone)
{
return doParseXmlDecl(NS(findEncoding),
isGeneralTextEntity,
enc,
ptr,
end,
badPtr,
versionPtr,
encodingName,
encoding,
standalone);
}