]> git.street.me.uk Git - andy/viking.git/commitdiff
Portability: reduce unistd.h dependency
authorMathieu Albinet <mathieu17@gmail.com>
Sat, 12 Apr 2008 20:15:16 +0000 (20:15 +0000)
committerMathieu Albinet <mathieu17@gmail.com>
Sat, 12 Apr 2008 20:15:16 +0000 (20:15 +0000)
Use Glib functions to increase portability.
g_file_test is used in place of access (or g_acces) for every test related
to existence of file/dir.

Based on work done by Mathieu Albinet for windows port

16 files changed:
ChangeLog
configure.ac
src/babel.c
src/clipboard.c
src/curl_download.c
src/dem.c
src/download.c
src/file.c
src/globals.h
src/osm-traces.c
src/thumbnails.c
src/vikdemlayer.c
src/viking.h
src/vikmapslayer.c
src/viktrwlayer.c
src/vikwindow.c

index d361bf5241ef515b8ee2ab273495b0f706a25581..a16f727140a7b0f4a5330636fbe68a719a0a8eeb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2008-04-12:
+Guilhem Bonnefille <guilhem.bonnefille@gmail.com>:
+Mathieu Albinet <mathieu17@gmail.com>:
+       * Portability: reduce unistd.h dependency
+
 2008-03-27:
 Mathieu Albinet <mathieu17@gmail.com>:
        * Replace GtkFileSelection by GtkFileChooser
index eeff0354f4789ac96966e89bf9e3f0b7d4712dc4..380e51a79fd2ada31b8ecb9b522ff9b4b2772c38 100644 (file)
@@ -31,7 +31,7 @@ AC_SUBST(ACLOCAL_AMFLAGS)
 
 # Checks for header files.
 AC_HEADER_STDC
-AC_CHECK_HEADERS([malloc.h netdb.h netinet/in.h stdlib.h string.h sys/param.h sys/socket.h unistd.h math.h])
+AC_CHECK_HEADERS([malloc.h netdb.h netinet/in.h stdlib.h string.h sys/param.h sys/socket.h sys/types.h unistd.h math.h])
 
 # Checks for typedefs, structures, and compiler characteristics.
 AC_C_CONST
index 940dfa70b1fbcefee1b5dd8aa507cce43599f6e2..520c4dd3b4f180818a7f6915e0d8dfb9d831e9ff 100644 (file)
  *   for an external program that outputs GPX.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include "viking.h"
 #include "gpx.h"
 #include "babel.h"
 #include <sys/wait.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <glib.h>
+#include <glib/gstdio.h>
 
 /* in the future we could have support for other shells (change command strings), or not use a shell at all */
 #define BASH_LOCATION "/bin/bash"
index afc16f98b6570b17eec5caa6bed781b386bf7e6c..88521a72b3a824df8e8d8d61686e9cb19844fc3a 100644 (file)
  *
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 
 #include <glib/gi18n.h>
 
index 4e38aafd32b5bdbe029b7c8abf22bfa02ac57d80..526b9fcb87fb1393bee4db9500b9b55207484fe7 100644 (file)
  *
  */
 
-#include <unistd.h>
-
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
+#include <glib.h>
+#include <glib/gstdio.h>
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 
@@ -43,14 +43,14 @@ static gchar *get_cookie_file(gboolean init)
     static gchar *cookie_fn = "cookies.txt";
     const gchar *viking_dir = a_get_viking_dir();
     cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
-    unlink(cookie_file);
+    g_unlink(cookie_file);
     return NULL;
   }
 
   g_assert(cookie_file != NULL);
 
   g_mutex_lock(mutex);
-  if (access(cookie_file, F_OK)) {  /* file not there */
+  if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) {  /* file not there */
     FILE * out_file = fopen("/dev/null", "w");
     CURLcode res;
     CURL *curl = curl_easy_init();
@@ -61,7 +61,7 @@ static gchar *get_cookie_file(gboolean init)
     if (res != CURLE_OK) {
       g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__,
           curl_easy_strerror(res));
-      unlink(cookie_file);
+      g_unlink(cookie_file);
     }
     curl_easy_cleanup(curl);
   }
index c06d8dac793461d1ac527cd4f3900b3b07ef948f..b5e632a3780ae62273e7d0ec1d62fbcb5abcbd28 100644 (file)
--- a/src/dem.c
+++ b/src/dem.c
@@ -27,7 +27,6 @@
 #include <zlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <unistd.h>
 #include <sys/mman.h>
 
 
index 2120d1d4c95147a3d3f50aa9839a5ff7ef19bbdd..827167f9d8ea984f178bc4812047126acd38fa9c 100644 (file)
@@ -40,7 +40,6 @@
 #ifdef WINDOWS
 
 #include <io.h>
-#define access(a,b) _access(a,b)
 #define close(a) closesocket(a)
 
 char *dirname ( char * dir )
@@ -54,7 +53,6 @@ char *dirname ( char * dir )
 
 #else
 
-#include <unistd.h>
 #include <sys/types.h>
 
 /* dirname */
@@ -104,7 +102,7 @@ static int download( const char *hostname, const char *uri, const char *fn, Down
   char *tmpfilename;
 
   /* Check file */
-  if ( access ( fn, F_OK ) == 0 )
+  if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE )
   {
     /* File exists: return */
     return -3;
index 4a9571501516145c9ce755e54e9f0798401d8f2f..aad59d0ecae75c2e6bb291e42afe317ca765b8c4 100644 (file)
@@ -28,6 +28,9 @@
 
 #include <string.h>
 #include <stdlib.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 #include <glib.h>
 #include <glib/gstdio.h>
 
@@ -626,22 +629,22 @@ const gchar *a_get_viking_dir()
 
   if (!viking_dir) {
     const gchar *home = g_getenv("HOME");
-    if (!home || access(home, W_OK))
+    if (!home || g_access(home, W_OK))
       home = g_get_home_dir ();
 #ifdef HAVE_MKDTEMP
-    if (!home || access(home, W_OK))
+    if (!home || g_access(home, W_OK))
     {
       static gchar temp[] = {"/tmp/vikXXXXXX"};
       home = mkdtemp(temp);
     }
 #endif
-    if (!home || access(home, W_OK))
+    if (!home || g_access(home, W_OK))
       /* Fatal error */
       g_critical("Unable to find a base directory");
 
     /* Build the name of the directory */
     viking_dir = g_build_filename(home, ".viking", NULL);
-    if (access(viking_dir, F_OK))
+    if (g_file_test(viking_dir, G_FILE_TEST_EXISTS) == FALSE)
       g_mkdir(viking_dir, 0755);
   }
 
index dc7fa31b4bdfd50e3521fe9569e5b358819c21e1..2f6160c29c85b4a8410b419478c785e6df8d3588 100644 (file)
 #define VIKING_VERSION_NAME "This Name For Rent"
 #define VIKING_URL "http://viking.sf.net/"
 
-#ifdef WINDOWS
-#define access(a,b) _access(a,b)
-#else
-#include <unistd.h>
-#endif
-
 #define ALTI_TO_MPP 1.4017295
 #define MPP_TO_ALTI 0.7134044
 
index ec6414409af17b4e6d895019a0752ca6dd7696b8..c56a379a2cc518152008b31825c0c2b23fa82171 100644 (file)
@@ -26,7 +26,6 @@
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <unistd.h>
 #include <errno.h>
 
 #include <curl/curl.h>
index e845e13353d028e9c6872761bba5b76dcd70f3e0..6d42bd5f8b4f0ee5b7f0001045f0d3787d27dba3 100644 (file)
  *
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <stdlib.h>
 #include <sys/stat.h>
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 #include <errno.h>
 #include <string.h>
 #include "viking.h"
index ca295e8a245582119ec2ac3743134ccb0d7f112d..d49eb340121d39289d31a998355d12ce4b038aba 100644 (file)
@@ -805,7 +805,7 @@ static void srtm_draw_existence ( VikViewport *vp )
                ABS(i),
                (j >= 0) ? 'E' : 'W',
                ABS(j) );
-      if ( access(buf, F_OK ) == 0 ) {
+      if ( g_file_test(buf, G_FILE_TEST_EXISTS ) == TRUE ) {
         VikCoord ne, sw;
         gint x1, y1, x2, y2;
         sw.north_south = i;
@@ -866,7 +866,7 @@ static void dem24k_draw_existence ( VikViewport *vp )
     g_snprintf(buf, sizeof(buf), "%sdem24k/%d/",
         MAPS_CACHE_DIR,
        (gint) i );
-    if ( access(buf, F_OK) != 0 )
+    if ( g_file_test(buf, G_FILE_TEST_EXISTS) == false )
       continue;
     for (j = floor(min_lon*8)/8; j <= floor(max_lon*8)/8; j+=0.125) {
       /* check lon dir first -- faster */
@@ -874,7 +874,7 @@ static void dem24k_draw_existence ( VikViewport *vp )
         MAPS_CACHE_DIR,
        (gint) i,
         (gint) j );
-      if ( access(buf, F_OK) != 0 )
+      if ( g_file_test(buf, G_FILE_TEST_EXISTS) == FALSE )
         continue;
       g_snprintf(buf, sizeof(buf), "%sdem24k/%d/%d/%.03f,%.03f.dem",
                MAPS_CACHE_DIR,
@@ -882,7 +882,7 @@ static void dem24k_draw_existence ( VikViewport *vp )
                (gint) j,
                floor(i*8)/8,
                floor(j*8)/8 );
-      if ( access(buf, F_OK ) == 0 ) {
+      if ( g_file_test(buf, G_FILE_TEST_EXISTS ) == TRUE ) {
         VikCoord ne, sw;
         gint x1, y1, x2, y2;
         sw.north_south = i;
@@ -922,7 +922,7 @@ static void weak_ref_cb ( gpointer ptr, GObject * dead_vdl )
  */
 static gboolean dem_layer_add_file ( VikDEMLayer *vdl, const gchar *full_path )
 {
-  if ( access(full_path, F_OK ) == 0 ) {
+  if ( g_file_test(full_path, G_FILE_TEST_EXISTS ) == TRUE ) {
     /* only load if file size is not 0 (not in progress */
     struct stat sb;
     stat (full_path, &sb);
index 5c851f7e062a84d7a57d16f118dc1f0cab433a34..c9c85c2d509860ce3f69b0e5a4e07ba894092b6b 100644 (file)
@@ -30,8 +30,6 @@
 #ifdef WINDOWS
 #include <io.h>
 #include <winsock.h>
-#else
-#include <unistd.h>
 #endif
 
 #include "config.h"
index f7c5895229bc08743417454309398ca120d9aca8..3deb773e1f631425ffb0d50be9457434f8afb25c 100644 (file)
@@ -32,6 +32,8 @@
 
 #include <gtk/gtk.h>
 #include <gdk-pixbuf/gdk-pixdata.h>
+#include <glib.h>
+#include <glib/gstdio.h>
 #include <glib/gi18n.h>
 
 #include <stdio.h>
@@ -46,7 +48,9 @@
 #include "vikmapslayer.h"
 #include "vikmapslayer_pixmap.h"
 
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #include <sys/stat.h>
 #include <sys/types.h>
 
@@ -267,11 +271,11 @@ gchar *maps_layer_default_dir ()
     const gchar *mapdir = g_getenv("VIKING_MAPS");
     if ( mapdir ) {
       defaultdir = g_strdup ( mapdir );
-    } else if ( access ( GLOBAL_MAPS_DIR, W_OK ) == 0 ) {
+    } else if ( g_access ( GLOBAL_MAPS_DIR, W_OK ) == 0 ) {
       defaultdir = g_strdup ( GLOBAL_MAPS_DIR );
     } else {
       const gchar *home = g_getenv("HOME");
-      if (!home || access(home, W_OK))
+      if (!home || g_access(home, W_OK))
         home = g_get_home_dir ();
       if ( home )
         defaultdir = g_build_filename ( home, ".viking-maps", NULL );
@@ -294,7 +298,7 @@ gchar *maps_layer_default_dir ()
 
 static void maps_layer_mkdir_if_default_dir ( VikMapsLayer *vml )
 {
-  if ( vml->cache_dir && strcmp ( vml->cache_dir, MAPS_CACHE_DIR ) == 0 && access ( vml->cache_dir, F_OK ) != 0 )
+  if ( vml->cache_dir && strcmp ( vml->cache_dir, MAPS_CACHE_DIR ) == 0 && g_file_test ( vml->cache_dir, G_FILE_TEST_EXISTS ) == FALSE )
   {
 #ifdef WINDOWS
     mkdir ( vml->cache_dir );
@@ -531,7 +535,7 @@ static GdkPixbuf *get_pixbuf( VikMapsLayer *vml, gint mode, MapCoord *mapcoord,
     g_snprintf ( filename_buf, buf_len, DIRSTRUCTURE,
                      vml->cache_dir, mode,
                      mapcoord->scale, mapcoord->z, mapcoord->x, mapcoord->y );
-    if ( access ( filename_buf, R_OK ) == 0) {
+    if ( g_file_test ( filename_buf, G_FILE_TEST_EXISTS ) == TRUE) {
     {
       GError *gx = NULL;
       pixbuf = gdk_pixbuf_new_from_file ( filename_buf, &gx );
@@ -688,7 +692,7 @@ static void maps_layer_draw_section ( VikMapsLayer *vml, VikViewport *vvp, VikCo
             g_snprintf ( path_buf, max_path_len, DIRSTRUCTURE,
                      vml->cache_dir, mode,
                      ulm.scale, ulm.z, ulm.x, ulm.y );
-            if ( access ( path_buf, F_OK ) == 0 ) {
+            if ( g_file_test ( path_buf, G_FILE_TEST_EXISTS ) == TRUE ) {
               vik_viewport_draw_line ( vvp, black_gc, xx+tilesize_x_ceil, yy, xx, yy+tilesize_y_ceil );
             }
           } else {
@@ -792,7 +796,7 @@ static void map_download_thread ( MapDownloadInfo *mdi, gpointer threaddata )
       if ( mdi->redownload == REDOWNLOAD_ALL)
         remove ( mdi->filename_buf );
 
-      else if ( (mdi->redownload == REDOWNLOAD_BAD) && (access ( mdi->filename_buf, F_OK ) == 0) )
+      else if ( (mdi->redownload == REDOWNLOAD_BAD) && (g_file_test ( mdi->filename_buf, G_FILE_TEST_EXISTS ) == TRUE) )
       {
         /* see if this one is bad or what */
         GError *gx = NULL;
@@ -805,7 +809,7 @@ static void map_download_thread ( MapDownloadInfo *mdi, gpointer threaddata )
           g_error_free ( gx );
       }
 
-      if ( access ( mdi->filename_buf, F_OK ) != 0 )
+      if ( g_file_test ( mdi->filename_buf, G_FILE_TEST_EXISTS ) == FALSE )
       {
         need_download = TRUE;
         if (( mdi->redownload != REDOWNLOAD_NONE ) &&
@@ -850,7 +854,7 @@ static void mdi_cancel_cleanup ( MapDownloadInfo *mdi )
     g_snprintf ( mdi->filename_buf, mdi->maxlen, DIRSTRUCTURE,
                      mdi->cache_dir, MAPS_LAYER_NTH_TYPE(mdi->maptype)->uniq_id,
                      mdi->mapcoord.scale, mdi->mapcoord.z, mdi->mapcoord.x, mdi->mapcoord.y );
-    if ( access ( mdi->filename_buf, F_OK ) == 0)
+    if ( g_file_test ( mdi->filename_buf, G_FILE_TEST_EXISTS ) == TRUE)
     {
       remove ( mdi->filename_buf );
     }
@@ -903,7 +907,7 @@ static void start_download_thread ( VikMapsLayer *vml, VikViewport *vvp, const V
           g_snprintf ( mdi->filename_buf, mdi->maxlen, DIRSTRUCTURE,
                        vml->cache_dir, map_type->uniq_id, ulm.scale,
                        ulm.z, a, b );
-          if ( access ( mdi->filename_buf, F_OK ) != 0)
+          if ( g_file_test ( mdi->filename_buf, G_FILE_TEST_EXISTS ) == FALSE )
             mdi->mapstoget++;
         }
       }
@@ -986,7 +990,7 @@ void maps_layer_download_section_without_redraw( VikMapsLayer *vml, VikViewport
       g_snprintf ( mdi->filename_buf, mdi->maxlen, DIRSTRUCTURE,
                    vml->cache_dir, map_type->uniq_id, ulm.scale,
                    ulm.z, i, j );
-      if ( access ( mdi->filename_buf, F_OK ) != 0)
+      if ( g_file_test ( mdi->filename_buf, G_FILE_TEST_EXISTS ) == FALSE )
             mdi->mapstoget++;
     }
   }
index 89737105e539e6c85557f8b413cda7a7a603a2c9..1a6268e810db51782f44ea0f4117eb1888cc8797 100644 (file)
@@ -53,7 +53,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include <ctype.h>
-#include <unistd.h>
 
 #include <gdk/gdkkeysyms.h>
 #include <glib/gi18n.h>
@@ -1478,7 +1477,7 @@ static void trw_layer_export ( gpointer layer_and_vlp[2], guint file_type )
   while ( gtk_dialog_run ( GTK_DIALOG(file_selector) ) == GTK_RESPONSE_ACCEPT )
   {
     fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(file_selector) );
-    if ( access ( fn, F_OK ) != 0 )
+    if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE )
     {
       gtk_widget_hide ( file_selector );
       failed = ! a_file_export ( VIK_TRW_LAYER(layer_and_vlp[0]), fn, file_type );
index 12f4689176e0ebddc75ac26d51cba72b0a264d74..599afb31dbb2ee57a8302298bcc472fbb29997cd 100644 (file)
@@ -1414,7 +1414,7 @@ static gboolean save_file_as ( GtkAction *a, VikWindow *vw )
   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_dia) ) == GTK_RESPONSE_ACCEPT )
   {
     fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_dia) );
-    if ( access ( fn, F_OK ) != 0 || a_dialog_overwrite ( GTK_WINDOW(vw->save_dia), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
+    if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE || a_dialog_overwrite ( GTK_WINDOW(vw->save_dia), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
     {
       window_set_filename ( vw, fn );
       rv = window_save ( vw );
@@ -1782,7 +1782,7 @@ static void draw_to_image_file_cb ( GtkAction *a, VikWindow *vw )
   while ( gtk_dialog_run ( GTK_DIALOG(vw->save_img_dia) ) == GTK_RESPONSE_ACCEPT )
   {
     fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(vw->save_img_dia) );
-    if ( access ( fn, F_OK ) != 0 || a_dialog_overwrite ( GTK_WINDOW(vw->save_img_dia), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
+    if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == FALSE || a_dialog_overwrite ( GTK_WINDOW(vw->save_img_dia), _("The file \"%s\" exists, do you wish to overwrite it?"), a_file_basename ( fn ) ) )
     {
       draw_to_image_file ( vw, fn, TRUE );
       break;