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
+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
# 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
* 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"
*
*/
+#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>
*
*/
-#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>
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();
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);
}
#include <zlib.h>
#include <sys/types.h>
#include <sys/stat.h>
-#include <unistd.h>
#include <sys/mman.h>
#ifdef WINDOWS
#include <io.h>
-#define access(a,b) _access(a,b)
#define close(a) closesocket(a)
char *dirname ( char * dir )
#else
-#include <unistd.h>
#include <sys/types.h>
/* dirname */
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;
#include <string.h>
#include <stdlib.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
#include <glib.h>
#include <glib/gstdio.h>
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);
}
#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
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
-#include <unistd.h>
#include <errno.h>
#include <curl/curl.h>
*
*/
+#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"
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;
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 */
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,
(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;
*/
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);
#ifdef WINDOWS
#include <io.h>
#include <winsock.h>
-#else
-#include <unistd.h>
#endif
#include "config.h"
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixdata.h>
+#include <glib.h>
+#include <glib/gstdio.h>
#include <glib/gi18n.h>
#include <stdio.h>
#include "vikmapslayer.h"
#include "vikmapslayer_pixmap.h"
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#endif
#include <sys/stat.h>
#include <sys/types.h>
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 );
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 );
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 );
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 {
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;
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 ) &&
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 );
}
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++;
}
}
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++;
}
}
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
-#include <unistd.h>
#include <gdk/gdkkeysyms.h>
#include <glib/gi18n.h>
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 );
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 );
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;