summaryrefslogtreecommitdiff
path: root/src/core/paths.c
blob: f58f46c3dce4f73fba2b278a0f2d02fd1d5c254b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

/* Chrysalide - Outil d'analyse de fichiers binaires
 * paths.c - récupération de fichiers secondaires
 *
 * Copyright (C) 2018 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "paths.h"


#include <glib.h>
#include <malloc.h>
#include <stdio.h>
#include <unistd.h>


#include <config.h>



/* Trouve le chemin d'accès complet à un fichier donné. */
static char *find_file_in_directory(const char *, const char *);



/******************************************************************************
*                                                                             *
*  Paramètres  : dirname  = répertoire de travail à fouiller.                 *
*                filename = nom de fichier seul comme indice.                 *
*                                                                             *
*  Description : Trouve le chemin d'accès complet à un fichier donné.         *
*                                                                             *
*  Retour      : Chemin trouvé à libérer de la mémoire ou NULL.               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static char *find_file_in_directory(const char *dirname, const char *filename)
{
    char *result;                           /* Trouvaille à renvoyer       */
    int ret;                                /* Bilan du test de présence   */

    asprintf(&result, "%s%s%s", dirname, G_DIR_SEPARATOR_S, filename);

    ret = access(result, F_OK);

    if (ret != 0)
    {
        free(result);
        result = NULL;
    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : filename = nom de fichier seul comme indice.                 *
*                                                                             *
*  Description : Trouve le chemin d'accès complet à un fichier d'image.       *
*                                                                             *
*  Retour      : Chemin trouvé à libérer de la mémoire ou NULL.               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

char *find_pixmap_file(const char *filename)
{
    char *result;                           /* Trouvaille à renvoyer       */

    /**
     * On privilégie si possible les sources fraiches.
     */

#ifndef DISCARD_LOCAL

    result = find_file_in_directory(PACKAGE_SOURCE_DIR G_DIR_SEPARATOR_S "pixmaps", filename);

#else

    result = NULL;

#endif

    if (result == NULL)
        result = find_file_in_directory(PIXMAPS_DIR, filename);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : pgname   = nom du greffon concerné.                          *
*                filename = nom de fichier seul comme indice.                 *
*                                                                             *
*  Description : Trouve le chemin d'accès complet à un fichier de greffon.    *
*                                                                             *
*  Retour      : Chemin trouvé à libérer de la mémoire ou NULL.               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

char *find_plugin_data_file(const char *pgname, const char *filename)
{
    char *result;                           /* Trouvaille à renvoyer       */
#ifndef DISCARD_LOCAL
    char *dirname;                          /* Répertoire à cibler         */
#endif

    /**
     * On privilégie si possible les sources fraiches.
     */

#ifndef DISCARD_LOCAL

    asprintf(&dirname, "%s%splugins%s%s",
             PACKAGE_SOURCE_DIR, G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S, pgname);

    result = find_file_in_directory(dirname, filename);

    free(dirname);

#else

    result = NULL;

#endif

    if (result == NULL)
        result = find_file_in_directory(PLUGINS_DATA_DIR, filename);

    return result;

}