From f1261ed615ad5c048806e8b5eb5cca10605ec98e Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Thu, 6 May 2010 01:23:07 +0200 Subject: [PATCH] [demo] Improved data file resolution The data files are searced in $(abs_builddir) too, so the path is resolved on vpath builds. Documented the resolver helper... still a lot of room for improvements but no good solutions found (the problem is not so big, though). --- demo/Makefile.am | 3 ++- demo/adg-demo.c | 2 +- demo/cpml-demo.c | 2 +- demo/demo.c | 42 ++++++++++++++++++++++++++++++++++++++---- demo/demo.h | 3 ++- 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/demo/Makefile.am b/demo/Makefile.am index 68ad4cd9..87d493bf 100644 --- a/demo/Makefile.am +++ b/demo/Makefile.am @@ -4,7 +4,8 @@ AM_CFLAGS= @ADG_GTK_CFLAGS@ \ -I$(top_srcdir)/src \ -I$(top_builddir)/src AM_CPPFLAGS= -DPKGDATADIR='"$(pkgdatadir)"' \ - -DSOURCEDIR='"$(abs_top_srcdir)/demo"' + -DDEMOSRCDIR='"$(abs_top_srcdir)/demo"' \ + -DDEMOBUILDDIR='"$(abs_top_builddir)/demo"' LDADD= @ADG_GTK_LIBS@ \ $(top_builddir)/src/cpml/libcpml-1.la \ $(top_builddir)/src/adg/libadg-1.la \ diff --git a/demo/adg-demo.c b/demo/adg-demo.c index c4bdff6d..d6d5a94a 100644 --- a/demo/adg-demo.c +++ b/demo/adg-demo.c @@ -810,7 +810,7 @@ main(gint argc, gchar **argv) _adg_parse_args(&argc, &argv, &show_extents); adg_switch_extents(show_extents); - path = demo_find_data_file("adg-demo.ui"); + path = demo_find_data_file("adg-demo.ui", argv[0]); if (path == NULL) { g_print("adg-demo.ui not found!\n"); return 1; diff --git a/demo/cpml-demo.c b/demo/cpml-demo.c index d5ed1943..087e8ca2 100644 --- a/demo/cpml-demo.c +++ b/demo/cpml-demo.c @@ -106,7 +106,7 @@ main(gint argc, gchar **argv) parse_args(&argc, &argv); - path = demo_find_data_file("cpml-demo.ui"); + path = demo_find_data_file("cpml-demo.ui", argv[0]); if (path == NULL) { g_print("cpml-demo.ui not found!\n"); return 1; diff --git a/demo/demo.c b/demo/demo.c index 6de59aef..de0b1175 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -2,18 +2,52 @@ #include "demo.h" +static gboolean +adg_file_test(const gchar *filename, GFileTest test) +{ +#ifdef _DEBUG + g_debug("Looking for '%s'\n", filename); +#endif + return g_file_test(filename, test); +} + +/** + * demo_find_data_file: + * @file: the name of the file to look for + * @caller: caller program (argv[0]) + * + * Looks for @file in the data paths. This wrapper is needed + * to allow running the program both when uninstalled and + * installed: in the former case the path must be + * $(top_builddir)/demo while in the latter it should be + * $(pkgdatadir). + * + * The function looks for paths in the following order: + * + * - $(top_scrdir)/demo + * - $(top_builddir)/demo + * - $(pkgdatadir) + * + * Returns: the full path to @file to be freed with g_free() + * or %NULL if not found or on errors + **/ gchar * -demo_find_data_file(const gchar *file) +demo_find_data_file(const gchar *file, const gchar *caller) { gchar *path; - path = g_build_filename(SOURCEDIR, file, NULL); - if (g_file_test(path, G_FILE_TEST_EXISTS)) + path = g_build_filename(DEMOBUILDDIR, file, NULL); + if (adg_file_test(path, G_FILE_TEST_EXISTS)) + return path; + + g_free(path); + path = g_build_filename(DEMOSRCDIR, file, NULL); + if (adg_file_test(path, G_FILE_TEST_EXISTS)) return path; g_free(path); path = g_build_filename(PKGDATADIR, PACKAGE_NAME, file, NULL); - if (g_file_test(path, G_FILE_TEST_EXISTS)) + if (adg_file_test(path, G_FILE_TEST_EXISTS)) return path; g_free(path); diff --git a/demo/demo.h b/demo/demo.h index 2b5aa245..197d6cba 100644 --- a/demo/demo.h +++ b/demo/demo.h @@ -10,7 +10,8 @@ G_BEGIN_DECLS -gchar * demo_find_data_file (const gchar *file); +gchar * demo_find_data_file (const gchar *file, + const gchar *caller); G_END_DECLS -- 2.11.4.GIT