s/TRANSLATORS/Translators/ in xgettext comments
[neverball] / share / base_config.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <stdlib.h>
16 #include <string.h>
17 #include <stdio.h>
18 #include <errno.h>
19 #include <math.h>
20
21 #include "base_config.h"
22 #include "glext.h"
23 #include "vec3.h"
24
25 /*---------------------------------------------------------------------------*/
26
27 /* Define the mkdir symbol. */
28
29 #ifdef _WIN32
30 #include <direct.h>
31 #else
32 #include <sys/stat.h>
33 #endif
34
35 /*---------------------------------------------------------------------------*/
36
37 static char data_path[MAXSTR];
38 static char user_path[MAXSTR];
39
40 /*
41  * Given  a path  and a  file name  relative to  that path,  create an
42  * absolute path name and return a temporary pointer to it.
43  */
44 static const char *config_file(const char *path, const char *file)
45 {
46     static char absolute[MAXSTR];
47
48     size_t d = strlen(path);
49
50     strncpy(absolute, path, MAXSTR - 1);
51     strncat(absolute, "/",  MAXSTR - d - 1);
52     strncat(absolute, file, MAXSTR - d - 2);
53
54     return absolute;
55 }
56
57 static int config_test(const char *path, const char *file)
58 {
59     if (file)
60     {
61         FILE *fp;
62
63         if ((fp = fopen(config_file(path, file), "r")))
64         {
65             fclose(fp);
66             return 1;
67         }
68         return 0;
69     }
70     return 1;
71 }
72
73 const char *config_data(const char *file)
74 {
75     return config_file(data_path, file);
76 }
77
78 const char *config_user(const char *file)
79 {
80     return config_file(user_path, file);
81 }
82
83 /*---------------------------------------------------------------------------*/
84
85 /*
86  * Attempt to find  the game data directory.  Search  the command line
87  * parameter,  the environment,  and the  hard-coded default,  in that
88  * order.  Confirm it by checking for presence of the named file.
89  */
90 int config_data_path(const char *path, const char *file)
91 {
92     char *dir;
93
94     if (path && config_test(path, file))
95     {
96         strncpy(data_path, path, MAXSTR);
97         return 1;
98     }
99
100     if ((dir = getenv("NEVERBALL_DATA")) && config_test(dir, file))
101     {
102         strncpy(data_path, dir, MAXSTR);
103         return 1;
104     }
105
106     if (config_test(CONFIG_DATA, file))
107     {
108         strncpy(data_path, CONFIG_DATA, MAXSTR);
109         return 1;
110     }
111
112     return 0;
113 }
114
115 /*
116  * Determine the location of  the user's home directory.  Ensure there
117  * is a  directory there for  storing configuration, high  scores, and
118  * replays.
119  *
120  * Under Windows check the APPDATA environment variable and if that's
121  * not set, just assume the user has permission to write to the data
122  * directory.
123  */
124 int config_user_path(const char *file)
125 {
126 #ifdef _WIN32
127     char *dir;
128
129     if ((dir = getenv("APPDATA")) || (dir = data_path))
130     {
131         size_t d = strlen(dir);
132
133         strncpy(user_path, dir,         MAXSTR - 1);
134         strncat(user_path, "\\",        MAXSTR - d - 1);
135         strncat(user_path, CONFIG_USER, MAXSTR - d - 2);
136     }
137
138     if ((mkdir(user_path) == 0) || (errno == EEXIST))
139         if (config_test(user_path, file))
140             return 1;
141 #else
142     char *dir;
143
144     if ((dir = getenv("HOME")))
145     {
146         size_t d = strlen(dir);
147
148         strncpy(user_path, dir,         MAXSTR - 1);
149         strncat(user_path, "/",         MAXSTR - d - 1);
150         strncat(user_path, CONFIG_USER, MAXSTR - d - 2);
151     }
152
153     if ((mkdir(user_path, 0777) == 0) || (errno == EEXIST))
154         if (config_test(user_path, file))
155             return 1;
156 #endif
157
158     return 0;
159 }
160
161 /*---------------------------------------------------------------------------*/