]> git.mxchange.org Git - flightgear.git/blobdiff - DEM/dem.cxx
MacOS portability changes contributed by "Robert Puyol" <puyol@abvent.fr>
[flightgear.git] / DEM / dem.cxx
index 73bce2be1ecb168dabb3f55dbb2a67705594134e..8fce8cc977c6bce31c7dac294dee7d3e22831d37 100644 (file)
 #  include <config.h>
 #endif
 
+#include <Include/compiler.h>
+
 #include <ctype.h>    // isspace()
 #include <stdlib.h>   // atoi()
 #include <math.h>     // rint()
 #include <stdio.h>
 #include <string.h>
-#include <sys/stat.h> // stat()
-#include <unistd.h>   // stat()
-
+#ifdef HAVE_SYS_STAT_H
+#  include <sys/stat.h> // stat()
+#endif
+#ifdef FG_HAVE_STD_INCLUDES
+#  include <cerrno>
+#else
+#  include <errno.h>
+#endif
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>   // stat()
+#endif
 #include <string>
 
+#include STL_IOSTREAM
+
 // #include <zlib/zlib.h>
 #include <Misc/fgstream.hxx>
 #include <Misc/strutils.hxx>
+#include <Include/compiler.h>
+FG_USING_NAMESPACE(std);
 
 #include "dem.hxx"
 #include "leastsqs.hxx"
 
 #define MAX_EX_NODES 10000
 
+#if 0
 #ifdef WIN32
-#  define MKDIR(a) mkdir(a,S_IRWXU)     // I am just guessing at this flag (NHV)
+# ifdef __BORLANDC__
+#  include <dir.h>
+#  define MKDIR(a) mkdir(a)
+# else
+#  define MKDIR(a) mkdir(a,S_IRWXU)  // I am just guessing at this flag (NHV)
+# endif // __BORLANDC__
 #endif // WIN32
+#endif //0
 
 
 fgDEM::fgDEM( void ) {
@@ -62,10 +83,11 @@ fgDEM::fgDEM( void ) {
 }
 
 
+#if 0
 #ifdef WIN32
 
 // return the file path name ( foo/bar/file.ext = foo/bar )
-static void extract_path (char *in, char *base) {
+static void extract_path ( const char *in, char *base) {
     int len, i;
     
     len = strlen (in);
@@ -81,7 +103,7 @@ static void extract_path (char *in, char *base) {
 
 
 // Make a subdirectory
-static int my_mkdir (char *dir) {
+static int my_mkdir (const char *dir) {
     struct stat stat_buf;
     int result;
 
@@ -105,6 +127,7 @@ static int my_mkdir (char *dir) {
 }
 
 #endif // WIN32
+#endif //0
 
 
 // open a DEM file
@@ -118,11 +141,11 @@ int fgDEM::open ( const string& file ) {
        return 0;
     } else {
        in = new fg_gzifstream( file );
-       if ( !in ) {
-           cout << "Cannot open " + file + "\n";
+       if ( !(*in) ) {
+           cout << "Cannot open " << file << endl;
            return 0;
        }
-       cout << "Loading DEM data file: " + file + "\n";
+       cout << "Loading DEM data file: " << file << endl;
     }
 
     return 1;
@@ -133,7 +156,7 @@ int fgDEM::open ( const string& file ) {
 int fgDEM::close () {
     // the fg_gzifstream doesn't seem to have a close()
 
-    delete(in);
+    delete in;
 
     return 1;
 }
@@ -143,9 +166,9 @@ int fgDEM::close () {
 string fgDEM::next_token() {
     string token;
 
-    in->stream() >> token;
+    *in >> token;
 
-    cout << "    returning " + token + "\n";
+    // cout << "    returning " + token + "\n";
 
     return token;
 }
@@ -154,8 +177,8 @@ string fgDEM::next_token() {
 // return next integer from input stream
 int fgDEM::next_int() {
     int result;
-
-    in->stream() >> result;
+    
+    *in >> result;
 
     return result;
 }
@@ -165,14 +188,14 @@ int fgDEM::next_int() {
 double fgDEM::next_double() {
     double result;
 
-    in->stream() >> result;
+    *in >> result;
 
     return result;
 }
 
 
 // return next exponential num from input stream
-int fgDEM::next_exp() {
+double fgDEM::next_exp() {
     string token;
     double mantissa;
     int exp, acc;
@@ -180,9 +203,24 @@ int fgDEM::next_exp() {
 
     token = next_token();
 
+#if 1
+    const char* p = token.c_str();
+    char buf[64];
+    char* bp = buf;
+    
+    for ( ; *p != 0; ++p )
+    {
+       if ( *p == 'D' )
+           *bp++ = 'E';
+       else
+           *bp++ = *p;
+    }
+    *bp = 0;
+    return ::atof( buf );
+#else
     sscanf(token.c_str(), "%lfD%d", &mantissa, &exp);
 
-    cout << "    Mantissa = " << mantissa << "  Exp = " << exp << "\n";
+    // cout << "    Mantissa = " << mantissa << "  Exp = " << exp << "\n";
 
     acc = 1;
     if ( exp > 0 ) {
@@ -196,6 +234,7 @@ int fgDEM::next_exp() {
     }
 
     return( (int)rint(mantissa * (double)acc) );
+#endif
 }
 
 
@@ -214,8 +253,8 @@ int fgDEM::read_a_record() {
     }
   
     // clean off the trailing whitespace
-    name = trimleft(name);
-    cout << "    Quad name field: " + name + "\n";
+    name = trim(name);
+    cout << "    Quad name field: " << name << endl;
 
     // DEM level code, 3 reflects processing by DMA
     inum = next_int();
@@ -240,7 +279,7 @@ int fgDEM::read_a_record() {
 
     // Map projection parameters (ignored)
     for ( i = 0; i < 15; i++ ) {
-       dnum = next_double();
+       dnum = next_exp();
        // printf("%d: %f\n",i,dnum);
     }
 
@@ -285,7 +324,7 @@ int fgDEM::read_a_record() {
     // Minimum/maximum elevations in meters
     dem_z1 = next_exp();
     dem_z2 = next_exp();
-    cout << "    Elevation range " << dem_z1 << dem_z2 << "\n";
+    cout << "    Elevation range " << dem_z1 << " to " << dem_z2 << "\n";
 
     // Counterclockwise angle from the primary axis of ground
     // planimetric referenced to the primary axis of the DEM local
@@ -301,10 +340,16 @@ int fgDEM::read_a_record() {
     // I will eventually have to do something with this for data at
     // higher latitudes */
     token = next_token();
-    cout << "    accuracy & spacial resolution string = " + token + "\n";
+    cout << "    accuracy & spacial resolution string = " << token << endl;
     i = token.length();
     cout << "    length = " << i << "\n";
 
+#if 1
+    inum = atoi( token.substr( 0, i - 36 ) );
+    row_step = atof( token.substr( i - 36, 12 ) );
+    col_step = atof( token.substr( i - 24, 12 ) );
+    //token.substr( 25, 12 )
+#else
     ptr = token.c_str() + i - 12;
     cout << "    last field = " << ptr << " = " << atof(ptr) << "\n";
     ptr[0] = '\0';
@@ -322,6 +367,7 @@ int fgDEM::read_a_record() {
     // accuracy code = atod(token)
     ptr = ptr - 12;
     inum = atoi(ptr);
+#endif
     cout << "    Accuracy code = " << inum << "\n";
 
     cout << "    column step = " << col_step << 
@@ -391,7 +437,7 @@ int fgDEM::parse( ) {
        cur_col++;
 
        if ( cur_col % 100 == 0 ) {
-           cout << "    loaded " << cur_col << "profiles of data\n";
+           cout << "    loaded " << cur_col << " profiles of data\n";
        }
     }
 
@@ -520,8 +566,9 @@ void fgDEM::fit( double error, fgBUCKET *p ) {
     double x[DEM_SIZE_1], y[DEM_SIZE_1];
     double m, b, ave_error, max_error;
     double cury, lasty;
-    int n, row, start, end, good_fit;
+    int n, row, start, end;
     int colmin, colmax, rowmin, rowmax;
+    bool good_fit;
     // FILE *dem, *fit, *fit1;
 
     printf("Initializing output mesh structure\n");
@@ -552,7 +599,7 @@ void fgDEM::fit( double error, fgBUCKET *p ) {
 
        while ( start < colmax ) {
            end = start + 1;
-           good_fit = 1;
+           good_fit = true;
 
            x[(end - start) - 1] = 0.0 + ( start * col_step );
            y[(end - start) - 1] = dem_data[start][row];
@@ -585,7 +632,7 @@ void fgDEM::fit( double error, fgBUCKET *p ) {
                */
 
                if ( max_error > error ) {
-                   good_fit = 0;
+                   good_fit = false;
                }
                
                end++;
@@ -681,7 +728,6 @@ void fgDEM::outputmesh_set_pt( int i, int j, double value ) {
 // since they are referenced by position from the .poly file.
 void fgDEM::outputmesh_output_nodes( const string& fg_root, fgBUCKET *p ) {
     double exnodes[MAX_EX_NODES][3];
-    double junk1, junk2, junk3;
     struct stat stat_buf;
     string dir;
     char base_path[256], file[256], exfile[256];
@@ -704,21 +750,23 @@ void fgDEM::outputmesh_output_nodes( const string& fg_root, fgBUCKET *p ) {
 
     // generate the base directory
     fgBucketGenBasePath(p, base_path);
-    cout << "fg_root = " + fg_root + "  Base Path = " + base_path + "\n";
+    cout << "fg_root = " << fg_root << "  Base Path = " << base_path << endl;
     dir = fg_root + "/Scenery/" + base_path;
-    cout << "Dir = " + dir + "\n";
+    cout << "Dir = " << dir << endl;
     
     // stat() directory and create if needed
+    errno = 0;
     result = stat(dir.c_str(), &stat_buf);
-    if ( result != 0 ) {
-       cout << "Stat error need to create directory\n";
+    if ( result != 0 && errno == ENOENT ) {
+       cout << "Creating directory\n";
 
-#ifndef WIN32
+// #ifndef WIN32
 
        command = "mkdir -p " + dir + "\n";
        system( command.c_str() );
 
-#else // WIN32
+#if 0
+// #else // WIN32
 
        // Cygwin crashes when trying to output to node file
        // explicitly making directory structure seems OK on Win95
@@ -728,13 +776,14 @@ void fgDEM::outputmesh_output_nodes( const string& fg_root, fgBUCKET *p ) {
        dir = fg_root + "/Scenery";
        if (my_mkdir ( dir.c_str() )) { exit (-1); }
 
-       dir += fg_root + "/Scenery/" + tmp_path;
+       dir = fg_root + "/Scenery/" + tmp_path;
        if (my_mkdir ( dir.c_str() )) { exit (-1); }
 
-       dir += fg_root + "/Scenery/" + base_path;
+       dir = fg_root + "/Scenery/" + base_path;
        if (my_mkdir ( dir.c_str() )) { exit (-1); }
 
-#endif // WIN32
+// #endif // WIN32
+#endif //0
 
     } else {
        // assume directory exists
@@ -752,7 +801,8 @@ void fgDEM::outputmesh_output_nodes( const string& fg_root, fgBUCKET *p ) {
     // load extra nodes if they exist
     excount = 0;
     if ( (fd = fopen(exfile, "r")) != NULL ) {
-       fscanf(fd, "%d %d %d %d", &excount, &junk1, &junk2, &junk3);
+       int junki;
+       fscanf(fd, "%d %d %d %d", &excount, &junki, &junki, &junki);
 
        if ( excount > MAX_EX_NODES - 1 ) {
            printf("Error, too many 'extra' nodes, increase array size\n");
@@ -762,7 +812,7 @@ void fgDEM::outputmesh_output_nodes( const string& fg_root, fgBUCKET *p ) {
        }
 
        for ( i = 1; i <= excount; i++ ) {
-           fscanf(fd, "%d %lf %lf %lf\n", &junk1
+           fscanf(fd, "%d %lf %lf %lf\n", &junki
                   &exnodes[i][0], &exnodes[i][1], &exnodes[i][2]);
            printf("(extra) %d %.2f %.2f %.2f\n", 
                    i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
@@ -812,12 +862,39 @@ void fgDEM::outputmesh_output_nodes( const string& fg_root, fgBUCKET *p ) {
 
 fgDEM::~fgDEM( void ) {
     // printf("class fgDEM DEstructor called.\n");
-    delete(dem_data);
-    delete(output_data);
+    delete [] dem_data;
+    delete [] output_data;
 }
 
 
 // $Log$
+// Revision 1.22  1999/01/19 20:56:56  curt
+// MacOS portability changes contributed by "Robert Puyol" <puyol@abvent.fr>
+//
+// Revision 1.21  1998/11/06 14:04:32  curt
+// Changes due to updates in fgstream.
+//
+// Revision 1.20  1998/10/28 19:38:20  curt
+// Elliminate some unnecessary win32 specific stuff (by Norman Vine)
+//
+// Revision 1.19  1998/10/22 21:59:19  curt
+// Fixed a couple subtle bugs that resulted from some of my c++ conversions.
+// One bug could cause a segfault on certain input, and the other bug could
+// cause the whole procedure to go balistic and generate huge files (also only
+// on rare input combinations.)
+//
+// Revision 1.18  1998/10/18 01:17:09  curt
+// Point3D tweaks.
+//
+// Revision 1.17  1998/10/16 19:08:12  curt
+// Portability updates from Bernie Bright.
+//
+// Revision 1.16  1998/10/02 21:41:39  curt
+// Fixes for win32.
+//
+// Revision 1.15  1998/09/21 20:53:59  curt
+// minor tweaks to clean a few additional things up after the rewrite.
+//
 // Revision 1.14  1998/09/19 17:59:45  curt
 // Use c++ streams (fg_gzifstream).  Also converted many character arrays to
 // the string class.