[demo] Handle paths differently on windows
[adg.git] / demo / demo.c
blob143699725b735a2c046195306875abeb3d3815a2
1 #include "demo.h"
3 /**
4 * demo_find_data_file:
5 * @file: the name of a data file
6 * @caller: caller program (argv[0])
8 * Builds the proper path of a data file, without checking
9 * for @file existence. This wrapper is needed to allow
10 * running the program either when uninstalled and installed
11 * and to deal with different platform issues.
13 * A call is considered coming from an uninstalled program
14 * if @caller (containing the supposed absolute path of
15 * the program) contains the LT_OBJDIR constant (usually
16 * ".libs"), typically used to store an uninstalled binary.
18 * If the call comes from an uninstalled program, @file will
19 * be searched in the parent directory of @caller dirname.
21 * If the call comes from an installed program, the
22 * %ADG_DATADIR will be used instead. This constant is set
23 * by the makefile throught preprocessor flags and will be
24 * considered an absolute path on non-windows platforms,
25 * hence resolving to $(pkgdatadir), or a path relative to
26 * the @caller dirname on windows platforms.
28 * Returns: the full path to @file that must bel freed with
29 * g_free() or %NULL on errors
30 **/
31 gchar *
32 demo_find_data_file(const gchar *file, const gchar *caller)
34 static gchar *data_root = NULL;
36 if (data_root == NULL) {
37 if (caller == NULL || strstr(caller, LT_OBJDIR) == NULL) {
38 /* Supposedly, this is a call to an installed program */
39 #ifdef G_OS_WIN32
41 /* On windows, ADG_DATADIR is a path relative to the
42 * caller dirname. This is needed because the data
43 * files must be relocatable, so the installer is free
44 * to put the project tree under custom directories.
45 * Other files, such as headers and libraries, are
46 * yet properly managed by autotools.
48 gchar *caller_dirname = g_path_get_dirname(caller);
49 data_root = g_build_filename(caller_dirname, ADG_DATADIR, NULL);
50 g_free(caller_dirname);
52 #else /* ! G_OS_WIN32 */
54 /* On other operating systems, ADG_DATADIR is an
55 * absolute path, so no further processing is required.
57 data_root = ADG_DATADIR;
59 #endif
60 } else {
61 /* This is likely an uninstalled program call: set
62 * data_root to the parent directory of the caller dirname.
64 gchar *caller_dirname = g_path_get_dirname(caller);
65 data_root = g_build_filename(caller_dirname, "..", NULL);
66 g_free(caller_dirname);
70 return g_build_filename(data_root, file, NULL);