doc: update copyright line for 2021
[adg.git] / demo / demo.c
blob2c94215875352b03887eab6eedc66b10983bde47
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2021 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include "demo.h"
21 #include <string.h>
24 #if GLIB_CHECK_VERSION(2, 58, 0)
26 static gchar *
27 _demo_absolutepath(const gchar *path)
29 return g_canonicalize_filename(path, NULL);
32 #else
34 #include <limits.h>
35 #include <stdlib.h>
37 static gchar *
38 _demo_absolutepath(const gchar *path)
40 char abspath[PATH_MAX];
41 return g_strdup(realpath(path, abspath));
44 #endif
47 /* Base folder all installed files should be referred to. */
48 static gchar *pkg_data_dir = NULL;
51 static gchar *
52 _demo_basedir(const gchar *program)
54 gchar *dir, *absdir;
56 dir = g_path_get_dirname(program);
57 if (dir == NULL) {
58 return NULL;
61 absdir = _demo_absolutepath(dir);
62 g_free(dir);
63 return absdir;
66 void
67 _demo_init(gint argc, gchar *argv[])
69 gchar *basedir;
71 if (argc < 1 || argv == NULL || argv[0] == NULL) {
72 g_error(_("Invalid arguments: arg[0] not set"));
73 g_assert_not_reached();
76 basedir = _demo_basedir(argv[0]);
77 if (basedir == NULL) {
78 /* This should never happen but... just in case */
79 pkg_data_dir = ".";
80 return;
83 g_print("basedir = '%s'\nbuilddir= '%s'\n", basedir, BUILDDIR);
84 if (! g_str_has_prefix(basedir, BUILDDIR)) {
85 /* Installed program: set pkg_data_dir */
86 #ifdef G_OS_WIN32
87 gchar *parent = g_path_get_dirname(dirname);
88 /* XXX: `pkg_data_dir` will be leaked... who cares? */
89 pkg_data_dir = g_build_filename(parent, PKGDATADIR, NULL);
90 g_free(parent);
91 #else
92 pkg_data_dir = PKGDATADIR;
93 #endif
95 g_free(basedir);
98 gchar *
99 _demo_file(const gchar *file_name)
101 if (pkg_data_dir == NULL) {
102 /* Running uninstalled: look up the file in BUILDDIR first and
103 * SRCDIR later, returning the first match */
104 return adg_find_file(file_name, BUILDDIR, SRCDIR, NULL);
107 /* Running installed: look up the file only in `pkg_data_dir` */
108 return adg_find_file(file_name, pkg_data_dir, NULL);