]> git.mxchange.org Git - flightgear.git/blobdiff - DEM/dem.cxx
Added #ifdef HAVE_CONFIG_H
[flightgear.git] / DEM / dem.cxx
index b328c39e8a20d2e52d61f34d78182baebfd2b807..52612143a3166fe9bda1422159792c1dd750a901 100644 (file)
 // (Log is kept at end of this file)
 
 
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include <ctype.h>    // isspace()
 #include <math.h>     // rint()
 #include <stdio.h>
@@ -32,6 +36,8 @@
 #include <sys/stat.h> // stat()
 #include <unistd.h>   // stat()
 
+#include <zlib/zlib.h>
+
 #include "dem.hxx"
 #include "leastsqs.hxx"
 
@@ -100,10 +106,11 @@ int fgDEM::open ( char *file ) {
     // open input file (or read from stdin)
     if ( strcmp(file, "-") == 0 ) {
        printf("Loading DEM data file: stdin\n");
-       fd = stdin;
+       // fd = stdin;
+       fd = gzdopen(STDIN_FILENO, "r");
     } else {
-       if ( (fd = fopen(file, "r")) == NULL ) {
-           printf("Cannot open %s\n", file);
+       if ( (fd = gzopen(file, "rb")) == NULL ) {
+           printf("Cannot gzopen %s\n", file);
            return(0);
        }
        printf("Loading DEM data file: %s\n", file);
@@ -115,19 +122,31 @@ int fgDEM::open ( char *file ) {
 
 // close a DEM file
 int fgDEM::close ( void ) {
-    fclose(fd);
+    gzclose(fd);
 
     return(1);
 }
 
 
 // return next token from input stream
-static void next_token(FILE *fd, char *token) {
-    int result;
-
-    result = fscanf(fd, "%s", token);
+static void next_token(gzFile fd, char *token) {
+    int i, result;
+    char c;
+
+    i = 0;
+    c = gzgetc(fd);
+    // skip past spaces
+    while ( (c != -1) && (c == ' ') ) {
+       c = gzgetc(fd);
+    }
+    while ( (c != -1) && (c != ' ') && (c != '\n') ){
+       token[i] = c;
+       i++;
+       c = gzgetc(fd);
+    }
+    token[i] = '\0';
 
-    if ( result == EOF ) {
+    if ( c == -1 ) {
        strcpy(token, "__END_OF_FILE__");
        printf("    Warning:  Reached end of file!\n");
     }
@@ -137,7 +156,7 @@ static void next_token(FILE *fd, char *token) {
 
 
 // return next integer from input stream
-static int next_int(FILE *fd) {
+static int next_int(gzFile fd) {
     char token[80];
 
     next_token(fd, token);
@@ -146,7 +165,7 @@ static int next_int(FILE *fd) {
 
 
 // return next double from input stream
-static double next_double(FILE *fd) {
+static double next_double(gzFile fd) {
     char token[80];
 
     next_token(fd, token);
@@ -155,12 +174,15 @@ static double next_double(FILE *fd) {
 
 
 // return next exponential num from input stream
-static int next_exp(FILE *fd) {
+static int next_exp(gzFile fd) {
+    char token[80];
     double mantissa;
     int exp, acc;
     int i;
 
-    fscanf(fd, "%lfD%d", &mantissa, &exp);
+    next_token(fd, token);
+
+    sscanf(token, "%lfD%d", &mantissa, &exp);
 
     // printf("    Mantissa = %.4f  Exp = %d\n", mantissa, exp);
 
@@ -180,7 +202,7 @@ static int next_exp(FILE *fd) {
 
 
 // read and parse DEM "A" record
-void fgDEM::read_a_record( void ) {
+int fgDEM::read_a_record( void ) {
     int i, inum;
     double dnum;
     char name[144];
@@ -189,7 +211,7 @@ void fgDEM::read_a_record( void ) {
 
     // get the name field (144 characters)
     for ( i = 0; i < 144; i++ ) {
-       name[i] = fgetc(fd);
+       name[i] = gzgetc(fd);
     }
     name[i+1] = '\0';
 
@@ -207,6 +229,10 @@ void fgDEM::read_a_record( void ) {
     inum = next_int(fd);
     printf("    DEM level code = %d\n", inum);
 
+    if ( inum > 3 ) {
+       return(0);
+    }
+
     // Pattern code, 1 indicates a regular elevation pattern
     inum = next_int(fd);
     printf("    Pattern code = %d\n", inum);
@@ -313,6 +339,8 @@ void fgDEM::read_a_record( void ) {
     // number of profiles
     dem_num_profiles = cols = next_int(fd);
     printf("    Expecting %d profiles\n", dem_num_profiles);
+
+    return(1);
 }
 
 
@@ -357,9 +385,11 @@ void fgDEM::read_b_record( void ) {
 int fgDEM::parse( void ) {
     int i;
 
-    cur_row = 0;
+    cur_col = 0;
 
-    read_a_record();
+    if ( !read_a_record() ) {
+       return(0);
+    }
 
     for ( i = 0; i < dem_num_profiles; i++ ) {
        // printf("Ready to read next b record\n");
@@ -373,7 +403,7 @@ int fgDEM::parse( void ) {
 
     printf("    Done parsing\n");
 
-    return(0);
+    return(1);
 }
 
 
@@ -492,7 +522,7 @@ double fgDEM::interpolate_altitude( double lon, double lat ) {
 
 
 // Use least squares to fit a simpler data set to dem data
-void fgDEM::fit( char *fg_root, double error, struct fgBUCKET *p ) {
+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;
@@ -622,7 +652,7 @@ void fgDEM::fit( char *fg_root, double error, struct fgBUCKET *p ) {
        // printf("Please hit return: "); gets(junk);
     }
 
-    outputmesh_output_nodes(fg_root, p);
+    // outputmesh_output_nodes(fg_root, p);
 }
 
 
@@ -652,7 +682,7 @@ void fgDEM::outputmesh_set_pt( int i, int j, double value ) {
 
 
 // Write out a node file that can be used by the "triangle" program
-void fgDEM::outputmesh_output_nodes( char *fg_root, struct fgBUCKET *p ) {
+void fgDEM::outputmesh_output_nodes( char *fg_root, fgBUCKET *p ) {
     struct stat stat_buf;
     char base_path[256], dir[256], file[256];
 #ifdef WIN32
@@ -750,10 +780,34 @@ void fgDEM::outputmesh_output_nodes( char *fg_root, struct fgBUCKET *p ) {
 
 fgDEM::~fgDEM( void ) {
     // printf("class fgDEM DEstructor called.\n");
+    free(dem_data);
+    free(output_data);
 }
 
 
 // $Log$
+// Revision 1.9  1998/07/13 15:29:49  curt
+// Added #ifdef HAVE_CONFIG_H
+//
+// Revision 1.8  1998/07/04 00:47:18  curt
+// typedef'd struct fgBUCKET.
+//
+// Revision 1.7  1998/06/05 18:14:39  curt
+// Abort out early when reading the "A" record if it doesn't look like
+// a proper DEM file.
+//
+// Revision 1.6  1998/05/02 01:49:21  curt
+// Fixed a bug where the wrong variable was being initialized.
+//
+// Revision 1.5  1998/04/25 15:00:32  curt
+// Changed "r" to "rb" in gzopen() options.  This fixes bad behavior in win32.
+//
+// Revision 1.4  1998/04/22 13:14:46  curt
+// Fixed a bug in zlib usage.
+//
+// Revision 1.3  1998/04/18 03:53:05  curt
+// Added zlib support.
+//
 // Revision 1.2  1998/04/14 02:43:27  curt
 // Used "new" to auto-allocate large DEM parsing arrays in class constructor.
 //