Split config into two files, 'config' and 'base_config'. Therefore #38 is fixed....
[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 <SDL.h>
16 #include <SDL_mixer.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <errno.h>
21 #include <math.h>
22
23 #include "base_config.h"
24 #include "glext.h"
25 #include "vec3.h"
26
27 /*---------------------------------------------------------------------------*/
28
29 /* Define the mkdir symbol. */
30
31 #ifdef _WIN32
32 #include <direct.h>
33 #else
34 #include <sys/stat.h>
35 #endif
36
37 /*---------------------------------------------------------------------------*/
38
39 static char data_path[MAXSTR];
40 static char user_path[MAXSTR];
41
42 /*
43  * Given  a path  and a  file name  relative to  that path,  create an
44  * absolute path name and return a temporary pointer to it.
45  */
46 static const char *config_file(const char *path, const char *file)
47 {
48     static char absolute[MAXSTR];
49
50     size_t d = strlen(path);
51
52     strncpy(absolute, path, MAXSTR - 1);
53     strncat(absolute, "/",  MAXSTR - d - 1);
54     strncat(absolute, file, MAXSTR - d - 2);
55
56     return absolute;
57 }
58
59 static int config_test(const char *path, const char *file)
60 {
61     if (file)
62     {
63         FILE *fp;
64
65         if ((fp = fopen(config_file(path, file), "r")))
66         {
67             fclose(fp);
68             return 1;
69         }
70         return 0;
71     }
72     return 1;
73 }
74
75 const char *config_data(const char *file)
76 {
77     return config_file(data_path, file);
78 }
79
80 const char *config_user(const char *file)
81 {
82     return config_file(user_path, file);
83 }
84
85 /*---------------------------------------------------------------------------*/
86
87 /*
88  * Attempt to find  the game data directory.  Search  the command line
89  * paramater,  the environment,  and the  hard-coded default,  in that
90  * order.  Confirm it by checking for presense of the named file.
91  */
92 int config_data_path(const char *path, const char *file)
93 {
94     char *dir;
95
96     if (path && config_test(path, file))
97     {
98         strncpy(data_path, path, MAXSTR);
99         return 1;
100     }
101
102     if ((dir = getenv("NEVERBALL_DATA")) && config_test(dir, file))
103     {
104         strncpy(data_path, dir, MAXSTR);
105         return 1;
106     }
107
108     if (CONFIG_DATA && config_test(CONFIG_DATA, file))
109     {
110         strncpy(data_path, CONFIG_DATA, MAXSTR);
111         return 1;
112     }
113
114     return 0;
115 }
116
117 /*
118  * Determine the location of  the user's home directory.  Ensure there
119  * is a  directory there for  storing configuration, high  scores, and
120  * replays.
121  *
122  * HACK: under Windows just assume the user has permission to write to
123  * the data  directory.  This is  more reliable than trying  to devine
124  * anything reasonable from the environment.
125  */
126 int config_user_path(const char *file)
127 {
128 #ifdef _WIN32
129     size_t d = strlen(CONFIG_USER);
130
131     strncpy(user_path, data_path,   MAXSTR - 1);
132     strncat(user_path, "\\",        MAXSTR - d - 1);
133     strncat(user_path, CONFIG_USER, MAXSTR - d - 2);
134
135     if ((mkdir(user_path) == 0) || (errno = EEXIST))
136         if (config_test(user_path, file))
137             return 1;
138 #else
139     char *dir;
140
141     if ((dir = getenv("HOME")))
142     {
143         size_t d = strlen(dir);
144
145         strncpy(user_path, getenv("HOME"), MAXSTR - 1);
146         strncat(user_path, "/",            MAXSTR - d - 1);
147         strncat(user_path, CONFIG_USER,    MAXSTR - d - 2);
148     }
149
150     if ((mkdir(user_path, 0777) == 0) || (errno = EEXIST))
151         if (config_test(user_path, file))
152             return 1;
153 #endif
154
155     return 0;
156 }
157
158 /*---------------------------------------------------------------------------*/