Re: glib and relative file names



pwd.c:
#include <glib.h>

int main(int argc, char **argv) {
 gchar *cwd = g_get_current_dir();

 g_print("%s\n", cwd);

 g_free(cwd);

 return 0;
}

gcc -o pwd pwd.c `pkg-config --cflags --libs glib-2.0`

Works for me.

This should work for converting to absolute path in the event you
detect a relative filename:

gchar *cwd = g_get_current_dir();
gchar *fn = g_build_filename (cwd, argv[i], NULL);
g_free(cwd);
 //use fn here
g_free(fn);

Depending on your platform, you can either just check for a leading /
or [A-Z]:\... or add an else clause to your existence check and
recheck with the full filename.  Just don't forget to free any gchar*
variables you might allocate with get_current_dir or build_filename
;).  Hope that helps.

-Larry

(Sorry Neil, I sent this to you once already, but I'm resending to the
list so people can google it later.)

On Sat, Oct 8, 2011 at 2:54 PM, Neil Munro <neilmunro gmail com> wrote:
Hey guys
             I am trying to work out a system to pass files to the
application I am working on, now it can accept absolute paths but it doesn't
seem to like relative paths can someone have a look at my code and see why
the G_FILE_TEST_EXISTS check fails? I have tried making the argument and
converting it to an absolute path by getting the output of g_get_current_dir
() which I understand to return the current working directory, but it only
returns the name of my home directory, which is unusual. Can someone shed
some light on this?

The code I am using is below.

if( argc == 1 ) // If no arguments were passed
gtk_window_set_focus( GTK_WINDOW( window ), New( NULL, Notebook ) );
else // Check to see if any files were specified on the cmd line
{
gint i;
for( i = 1; i < argc; i++ )
{
g_print( "\n%s\n", argv[ i ] );
if( g_file_test( argv[ i ], G_FILE_TEST_EXISTS ) )
{
g_print( "File exists!\n" );
gtk_window_set_focus( GTK_WINDOW( window ), open_with( argv[ i ], Notebook )
);
}
}
if( gtk_notebook_get_n_pages( GTK_NOTEBOOK( Notebook ) ) == 0 )
gtk_window_set_focus( GTK_WINDOW( window ), New( NULL, Notebook ) );
}
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]