JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
removing most data subdirs
[vor.git] / file.c
1 /* Variations on RockDodger
2  * Copyright (C) 2004 Joshua Grams <josh@qualdan.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #ifndef WIN32
23 # include <sys/stat.h>
24 #endif
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include "common.h"
29 #include "config.h"
30 #include "file.h"
31
32 char *g_data_dir;
33 char *g_score_file;
34
35 static char *
36 add_path(char *path, char *file)
37 {
38         char *r, *s;
39         size_t plen, flen;
40
41         if(!path || !file) return NULL;
42         plen = strlen(path);
43         flen = strlen(file);
44         s = malloc(2+plen+flen);
45         if(!s) return NULL;
46         memcpy(s, path, plen);
47         s[plen] = PATH_SEPARATOR;
48         memcpy(s+plen+1, file, flen+1);
49         return s;
50 }
51
52 char *
53 add_data_path(char *filename)
54 {
55         return add_path(g_data_dir, filename);
56 }
57
58 #ifdef WIN32
59
60 static bool
61 find_data_dir(void)
62 {
63         g_data_dir = ".";
64         return true;
65 }
66
67 static bool
68 find_score_file(void)
69 {
70         g_score_file = "scores";
71         return true;
72 }
73
74 #else /* !WIN32 */
75
76 static bool
77 is_dir(char *dirname)
78 {
79         struct stat buf;
80         stat(dirname, &buf);
81         return S_ISDIR(buf.st_mode);
82 }
83
84 static bool
85 find_data_dir(void)
86 {
87         int i;
88         char *data_options[3] = {
89                 getenv("VOR_DATA"),
90                 "data",
91                 DATA_PREFIX
92         };
93
94         for(i=0; i<3; i++) {
95                 if(!data_options[i]) continue;
96                 g_data_dir = strdup(data_options[i]);
97                 if(is_dir(g_data_dir)) return true;
98         }
99
100         fprintf(stderr, "Can't find VoR data! Tried:\n");
101         for(i=0; i<3; i++) {
102                 fprintf(stderr, "\t%s\n", data_options[i]);
103         }
104         return false;
105 }
106
107 static bool
108 find_score_file(void)
109 {
110         char *dir, *s;
111         
112         /* in case we get called twice */
113         if(g_score_file) return true;
114
115         dir = getenv("HOME"); if(!dir) return false;
116         s = add_path(dir, ".vor-scores");
117         if(s) {
118                 g_score_file = s;
119                 return true;
120         } else return false;
121 }
122
123 #endif /* !WIN32 */
124
125 bool
126 find_files(void)
127 {
128         return find_data_dir() && find_score_file();
129 }
130
131 FILE *
132 open_score_file(char *mode)
133 {
134         if(!g_score_file) return NULL;
135         return fopen(g_score_file, mode);
136 }