File: compat.c

package info (click to toggle)
gpredict 2.3-72-gc596101-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 12,264 kB
  • sloc: ansic: 39,918; makefile: 469; python: 143; sh: 85
file content (398 lines) | stat: -rw-r--r-- 10,190 bytes parent folder | download
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
    Gpredict: Real-time satellite tracking and orbit prediction program

    Copyright (C)  2001-2017  Alexandru Csete, OZ9AEC.

    Comments, questions and bugreports should be submitted via
    http://sourceforge.net/projects/gpredict/
    More details can be found at the project home page:

            http://gpredict.oz9aec.net/
 
    This program 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 2 of the License, or
    (at your option) any later version.
  
    This program 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 this program; if not, visit http://www.fsf.org/
*/
#ifdef HAVE_CONFIG_H
#include <build-config.h>
#endif
#include <glib.h>
#include <stdlib.h>
#include "compat.h"

/**
 * Get data directory.
 *
 * On linux it corresponds to the PACKAGE_DATA_DIR macro defined in build-config.h
 * The function returns a newly allocated gchar * which must be free when
 * it is no longer needed.
 */
gchar          *get_data_dir()
{
    gchar          *dir = NULL;

#ifdef G_OS_UNIX
    char* data_dir = getenv("GPREDICT_DATA_DIR");
    dir = g_strconcat(data_dir ? data_dir : PACKAGE_DATA_DIR, G_DIR_SEPARATOR_S, "data", NULL);
#else
#ifdef G_OS_WIN32
    gchar          *buff =
        g_win32_get_package_installation_directory_of_module(NULL);

    dir = g_strconcat(buff, G_DIR_SEPARATOR_S,
                      "share", G_DIR_SEPARATOR_S, "gpredict",
                      G_DIR_SEPARATOR_S, "data", NULL);
    g_free(buff);
#endif
#endif

    return dir;
}

/**
 * Get absolute file name of a data file.
 *
 * This function returns the absolute file name of a data file. It is intended to
 * be a one-line filename constructor.
 * The returned gchar * should be freed when no longer needed.
 */
gchar          *data_file_name(const gchar * data)
{
    gchar          *filename = NULL;
    gchar          *buff;

    buff = get_data_dir();
    filename = g_strconcat(buff, G_DIR_SEPARATOR_S, data, NULL);
    g_free(buff);

    return filename;
}

/**
 * Get maps directory.
 *
 * On linux it corresponds to the PACKAGE_DATA_DIR/pixmaps/maps
 * The function returns a newly allocated gchar * which must be free when
 * it is no longer needed.
 */
gchar          *get_maps_dir()
{
    gchar          *dir = NULL;

#ifdef G_OS_UNIX
    dir = g_strconcat(PACKAGE_PIXMAPS_DIR, G_DIR_SEPARATOR_S, "maps", NULL);
#else
#ifdef G_OS_WIN32
    gchar          *buff =
        g_win32_get_package_installation_directory_of_module(NULL);

    dir = g_strconcat(buff, G_DIR_SEPARATOR_S, "share", G_DIR_SEPARATOR_S,
                      /* FIXME */
                      "gpredict", G_DIR_SEPARATOR_S, "pixmaps",
                      G_DIR_SEPARATOR_S, "maps", NULL);
    g_free(buff);
#endif
#endif

    return dir;
}

/**
 * Get absolute file name of a map file.
 *
 * This function returns the absolute file name of a map file. It is intended to
 * be a one-line filename constructor.
 * The returned gchar * should be freed when no longer needed.
 */
gchar          *map_file_name(const gchar * map)
{
    gchar          *filename = NULL;
    gchar          *buff;

    buff = get_maps_dir();
    filename = g_strconcat(buff, G_DIR_SEPARATOR_S, map, NULL);
    g_free(buff);

    return filename;
}

/**
 * Get logo directory.
 *
 * On linux it corresponds to the PACKAGE_DATA_DIR/pixmaps/logos
 * The function returns a newly allocated gchar * which must be free when
 * it is no longer needed.
 */
gchar          *get_logo_dir()
{
    gchar          *dir = NULL;

#ifdef G_OS_UNIX
    dir = g_strconcat(PACKAGE_PIXMAPS_DIR, G_DIR_SEPARATOR_S, "logos", NULL);
#else
#ifdef G_OS_WIN32
    gchar          *buff =
        g_win32_get_package_installation_directory_of_module(NULL);

    dir = g_strconcat(buff, G_DIR_SEPARATOR_S,
                      "share", G_DIR_SEPARATOR_S,
                      "gpredict", G_DIR_SEPARATOR_S, "pixmaps",
                      G_DIR_SEPARATOR_S, "logos", NULL);
    g_free(buff);
#endif
#endif

    return dir;
}

/**
 * Get icon directory.
 *
 * On linux it corresponds to the PACKAGE_DATA_DIR/pixmaps/icons
 * The function returns a newly allocated gchar * which must be free when
 * it is no longer needed.
 */
gchar          *get_icon_dir()
{
    gchar          *dir = NULL;

#ifdef G_OS_UNIX
    dir = g_strconcat(PACKAGE_PIXMAPS_DIR, G_DIR_SEPARATOR_S, "icons", NULL);
#else
#ifdef G_OS_WIN32
    gchar          *buff =
        g_win32_get_package_installation_directory_of_module(NULL);

    dir = g_strconcat(buff, G_DIR_SEPARATOR_S,
                      "share", G_DIR_SEPARATOR_S,
                      "gpredict", G_DIR_SEPARATOR_S, "pixmaps",
                      G_DIR_SEPARATOR_S, "icons", NULL);
    g_free(buff);
#endif
#endif

    return dir;
}

/**
 * Get absolute file name of a logo file.
 *
 * This function returns the absolute file name of a logo file. It is intended
 * to be a one-line filename constructor.
 * The returned gchar * should be freed when no longer needed.
 */
gchar          *logo_file_name(const gchar * logo)
{
    gchar          *filename = NULL;
    gchar          *buff;

    buff = get_logo_dir();
    filename = g_strconcat(buff, G_DIR_SEPARATOR_S, logo, NULL);
    g_free(buff);

    return filename;
}

/**
 * Get absolute file name of an icon file.
 *
 * This function returns the absolute file name of an icon file. It is intended to
 * be a one-line filename constructor.
 * The returned gchar * should be freed when no longer needed.
 */
gchar          *icon_file_name(const gchar * icon)
{
    gchar          *filename = NULL;
    gchar          *buff;

    buff = get_icon_dir();
    filename = g_strconcat(buff, G_DIR_SEPARATOR_S, icon, NULL);
    g_free(buff);

    return filename;
}

/**
 * Get the old user configuration directory.
 *
 * On linux it corresponds to $HOME/.gpredict2
 * The function returns a newly allocated gchar * which must be free when
 * it is no longer needed.
 */
gchar          *get_old_conf_dir(void)
{
    gchar          *dir;

    dir = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, ".gpredict2", NULL);
    return dir;
}

/**
 * Get user configuration directory.
 *
 * Linux: $HOME/.config/Gpredict
 * Windows: C:\Users\username\Gpredict
 * Mac OS X: /home/username/Library/Application Support/Gpredict
 *
 * The function returns a newly allocated gchar * which must be free when
 * it is no longer needed.
 */
gchar          *get_user_conf_dir(void)
{
    gchar          *dir = NULL;

#ifdef G_OS_UNIX
    dir =
        g_strconcat(g_get_user_config_dir(), G_DIR_SEPARATOR_S, "Gpredict",
                    NULL);
#endif
#ifdef G_OS_WIN32
    // FIXME: does this work?
    dir = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, "Gpredict", NULL);
#endif
/* see gtk-osx.sourceforge.net -> Integration */
#ifdef MAC_INTEGRATION
    dir = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S,
                      "Library", G_DIR_SEPARATOR_S,
                      "Application Support", G_DIR_SEPARATOR_S, "Gpredict",
                      NULL);
#endif

    return dir;
}

/** Get USER_CONF_DIR/modules */
gchar          *get_modules_dir(void)
{
    gchar          *confdir;
    gchar          *dir;

    confdir = get_user_conf_dir();
    dir = g_strconcat(confdir, G_DIR_SEPARATOR_S, "modules", NULL);
    g_free(confdir);

    return dir;
}

/** Get USER_CONF_DIR/satdata */
gchar          *get_satdata_dir(void)
{
    gchar          *confdir;
    gchar          *dir;

    confdir = get_user_conf_dir();
    dir = g_strconcat(confdir, G_DIR_SEPARATOR_S, "satdata", NULL);
    g_free(confdir);

    return dir;
}

/** Get USER_CONF_DIR/trsp */
gchar          *get_trsp_dir(void)
{
    gchar          *confdir;
    gchar          *dir;

    confdir = get_user_conf_dir();
    dir = g_strconcat(confdir, G_DIR_SEPARATOR_S, "trsp", NULL);
    g_free(confdir);

    return dir;
}

/** Get USER_CONF_DIR/hwconf */
gchar          *get_hwconf_dir(void)
{
    gchar          *confdir;
    gchar          *dir;

    confdir = get_user_conf_dir();
    dir = g_strconcat(confdir, G_DIR_SEPARATOR_S, "hwconf", NULL);
    g_free(confdir);

    return dir;
}

/** Get full path of a .sat or .cat file */
gchar          *sat_file_name(const gchar * satfile)
{
    gchar          *filename = NULL;
    gchar          *buff;

    buff = get_satdata_dir();
    filename = g_strconcat(buff, G_DIR_SEPARATOR_S, satfile, NULL);
    g_free(buff);

    return filename;
}

/** Build satellite file path from catnum (integer) */
gchar          *sat_file_name_from_catnum(guint catnum)
{
    gchar          *filename;
    gchar          *buff;
    gchar          *dir;

    buff = g_strdup_printf("%d.sat", catnum);
    dir = get_satdata_dir();

    filename = g_strconcat(dir, G_DIR_SEPARATOR_S, buff, NULL);

    g_free(buff);
    g_free(dir);

    return filename;
}

/** Build satellite file path from catnum (string) */
gchar          *sat_file_name_from_catnum_s(gchar * catnum)
{
    gchar          *filename;
    gchar          *buff;
    gchar          *dir;

    buff = g_strdup_printf("%s.sat", catnum);
    dir = get_satdata_dir();

    filename = g_strconcat(dir, G_DIR_SEPARATOR_S, buff, NULL);

    g_free(buff);
    g_free(dir);

    return filename;
}

/** Get full path of a .trsp file */
gchar          *trsp_file_name(const gchar * trspfile)
{
    gchar          *filename = NULL;
    gchar          *buff;

    buff = get_trsp_dir();
    filename = g_strconcat(buff, G_DIR_SEPARATOR_S, trspfile, NULL);
    g_free(buff);

    return filename;
}

/** Get full path of a .rig or .rot file */
gchar          *hw_file_name(const gchar * hwfile)
{
    gchar          *filename = NULL;
    gchar          *buff;

    buff = get_hwconf_dir();
    filename = g_strconcat(buff, G_DIR_SEPARATOR_S, hwfile, NULL);
    g_free(buff);

    return filename;
}