]> git.street.me.uk Git - andy/viking.git/blobdiff - src/dem.c
Move split track menu options into a new submenu
[andy/viking.git] / src / dem.c
index 3f29bce0c6703a4d3632d532b715f9b0bf041747..84c257e94cbf8e40af9fdc8d138b8da00d180bee 100644 (file)
--- a/src/dem.c
+++ b/src/dem.c
@@ -1,15 +1,48 @@
+/*
+ * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
+ *
+ * Copyright (C) 2003-2008, Evan Battaglia <gtoevan@gmx.net>
+ * Copyright (C) 2007, Quy Tonthat <qtonthat@gmail.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.
+ *
+ * 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 General Public License for more details.
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <stdio.h>
+#ifdef HAVE_STRING_H
 #include <string.h>
+#endif
 #include <glib.h>
+#ifdef HAVE_MATH_H
 #include <math.h>
+#endif
+#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
+#endif
 #include <zlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#include <sys/mman.h>
-
+#endif
 
+#include <glib/gstdio.h>
 #include <glib/gi18n.h>
 
 #include "dem.h"
@@ -285,19 +318,19 @@ end:
        return(unzip_data);
 }
 
-static VikDEM *vik_dem_read_srtm_hgt(FILE *f, const gchar *basename, gboolean zip)
+static VikDEM *vik_dem_read_srtm_hgt(const gchar *file_name, const gchar *basename, gboolean zip)
 {
   gint i, j;
   VikDEM *dem;
-  struct stat stat;
   off_t file_size;
   gint16 *dem_mem = NULL;
-  gint16 *dem_file = NULL;
+  gchar *dem_file = NULL;
   const gint num_rows_3sec = 1201;
   const gint num_rows_1sec = 3601;
   gint num_rows;
-  int fd = fileno(f);
+  GMappedFile *mf;
   gint arcsec;
+  GError *error = NULL;
 
   dem = g_malloc(sizeof(VikDEM));
 
@@ -318,19 +351,21 @@ static VikDEM *vik_dem_read_srtm_hgt(FILE *f, const gchar *basename, gboolean zi
   dem->columns = g_ptr_array_new();
   dem->n_columns = 0;
 
-  if (fstat(fd, &stat) == -1)
-    g_error("%s(): fstat failed on %s\n", __PRETTY_FUNCTION__, basename);
-  if ((dem_file = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *) -1)
-    g_error("%s(): mmap failed on %s\n", __PRETTY_FUNCTION__, basename);
-
-  file_size = stat.st_size;
-  dem_mem = dem_file;
+  if ((mf = g_mapped_file_new(file_name, FALSE, &error)) == NULL) {
+    g_error(_("Couldn't map file %s: %s"), file_name, error->message);
+    g_error_free(error);
+    g_free(dem);
+    return NULL;
+  }
+  file_size = g_mapped_file_get_length(mf);
+  dem_file = g_mapped_file_get_contents(mf);
+  
   if (zip) {
     void *unzip_mem = NULL;
     gulong ucsize;
 
-    if ((unzip_mem = unzip_hgt_file((gchar *)dem_file, &ucsize)) == NULL) {
-      munmap(dem_file, file_size);
+    if ((unzip_mem = unzip_hgt_file(dem_file, &ucsize)) == NULL) {
+      g_mapped_file_free(mf);
       g_ptr_array_free(dem->columns, TRUE);
       g_free(dem);
       return NULL;
@@ -346,8 +381,7 @@ static VikDEM *vik_dem_read_srtm_hgt(FILE *f, const gchar *basename, gboolean zi
     arcsec = 1;
   else {
     g_warning("%s(): file %s does not have right size", __PRETTY_FUNCTION__, basename);
-    munmap(dem_file, file_size);
-    g_ptr_array_free(dem->columns, TRUE);
+    g_mapped_file_free(mf);
     g_free(dem);
     return NULL;
   }
@@ -375,7 +409,7 @@ static VikDEM *vik_dem_read_srtm_hgt(FILE *f, const gchar *basename, gboolean zi
 
   if (zip)
     g_free(dem_mem);
-  munmap(dem_file, stat.st_size);
+  g_mapped_file_free(mf);
   return dem;
 }
 
@@ -383,7 +417,7 @@ static VikDEM *vik_dem_read_srtm_hgt(FILE *f, const gchar *basename, gboolean zi
 
 VikDEM *vik_dem_new_from_file(const gchar *file)
 {
-  FILE *f;
+  FILE *f=NULL;
   VikDEM *rv;
   gchar buffer[DEM_BLOCK_SIZE+1];
 
@@ -392,17 +426,14 @@ VikDEM *vik_dem_new_from_file(const gchar *file)
   gint cur_row = -1;
   const gchar *basename = a_file_basename(file);
 
-      /* FILE IO */
-  f = fopen(file, "r");
-  if ( !f )
+  if ( g_access ( file, R_OK ) != 0 )
     return NULL;
 
   if ( (strlen(basename)==11 || ((strlen(basename) == 15) && (basename[11] == '.' && basename[12] == 'z' && basename[13] == 'i' && basename[14] == 'p'))) &&
        basename[7]=='.' && basename[8]=='h' && basename[9]=='g' && basename[10]=='t' &&
        (basename[0] == 'N' || basename[0] == 'S') && (basename[3] == 'E' || basename[3] =='W')) {
     gboolean is_zip_file = (strlen(basename) == 15);
-    rv = vik_dem_read_srtm_hgt(f, basename, is_zip_file);
-    fclose(f);
+    rv = vik_dem_read_srtm_hgt(file, basename, is_zip_file);
     return(rv);
   }
 
@@ -410,9 +441,15 @@ VikDEM *vik_dem_new_from_file(const gchar *file)
   rv = g_malloc(sizeof(VikDEM));
 
       /* Header */
+  f = g_fopen(file, "r");
+  if ( !f ) {
+    g_free ( rv );
+    return NULL;
+  }
   buffer[fread(buffer, 1, DEM_BLOCK_SIZE, f)] = '\0';
   if ( ! dem_parse_header ( buffer, rv ) ) {
     g_free ( rv );
+    fclose(f);
     return NULL;
   }
   /* TODO: actually use header -- i.e. GET # OF COLUMNS EXPECTED */
@@ -441,6 +478,7 @@ VikDEM *vik_dem_new_from_file(const gchar *file)
      /* TODO - class C records (right now says 'Invalid' and dies) */
 
   fclose(f);
+  f = NULL;
 
   /* 24k scale */
   if ( rv->horiz_units == VIK_DEM_HORIZ_UTM_METERS && rv->n_columns >= 2 )