JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
code formatting: indentation, arg name
[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 "vorconfig.h"
30 #include "file.h"
31
32 #ifdef WIN32
33
34 #define DATA_DIR "data\\"
35
36 char *
37 add_data_path(char *filename)
38 {
39         char *s;
40         if(!filename) return filename;
41         s = malloc(sizeof(DATA_DIR)+strlen(filename));
42         if(s) {
43                 strcpy(s, DATA_DIR);
44                 strcpy(s+sizeof(DATA_DIR)-1, filename);
45         }
46         return s;
47 }
48
49 bool
50 find_files(void)
51 {
52         return true;
53 }
54
55 FILE *open_score_file(char *mode)
56 {
57         return fopen("scores", mode);
58 }
59
60
61 #else /* !WIN32 */
62
63
64 char *g_data_dir;
65 char *g_score_file;
66
67 static char *
68 add_path(char *path, char *file)
69 {
70         char *s;
71         size_t plen, flen;
72
73         if(!path || !file) return NULL;
74         plen = strlen(path);
75         flen = strlen(file);
76         s = malloc(2+plen+flen);
77         if(!s) return NULL;
78         memcpy(s, path, plen);
79         s[plen] = '/';
80         memcpy(s+plen+1, file, flen+1);
81         return s;
82 }
83
84 char *
85 add_data_path(char *filename)
86 {
87         return add_path(g_data_dir, filename);
88 }
89
90 static bool
91 is_dir(char *dirname)
92 {
93         struct stat buf;
94         if(stat(dirname, &buf)) {
95                 return false;
96         }
97         return S_ISDIR(buf.st_mode);
98 }
99
100 static bool
101 is_file(char *filename)
102 {
103         struct stat buf;
104         if(stat(filename, &buf)) {
105                 return false;
106         }
107         return S_ISREG(buf.st_mode);
108 }
109
110 static bool
111 find_data_dir(void)
112 {
113         int i;
114         char *data_options[3] = {
115                 getenv("VOR_DATA"),
116                 "data",
117                 DATA_PREFIX
118         };
119
120         for(i=0; i<3; i++) {
121                 if(!data_options[i]) continue;
122                 g_data_dir = strdup(data_options[i]);
123                 if(is_dir(g_data_dir)) {
124                         char *s = add_path(g_data_dir, "b_variations.png");
125                         if(s) if(is_file(s))
126                                 return true;
127                 }
128         }
129
130         fprintf(stderr, "Can't find VoR data! Tried:\n");
131         for(i=0; i<3; i++) {
132                 if(data_options[i]) fprintf(stderr, "\t%s\n", data_options[i]);
133         }
134         return false;
135 }
136
137 static bool
138 find_score_file(void)
139 {
140         char *dir, *s;
141         
142         /* in case we get called twice */
143         if(g_score_file) return true;
144
145         dir = getenv("HOME"); if(!dir) return false;
146         s = add_path(dir, ".vor-scores");
147         if(s) {
148                 g_score_file = s;
149                 return true;
150         } else return false;
151 }
152
153 bool
154 find_files(void)
155 {
156         return find_data_dir() && find_score_file();
157 }
158
159 FILE *
160 open_score_file(char *mode)
161 {
162         if(!g_score_file) return NULL;
163         return fopen(g_score_file, mode);
164 }
165
166 #endif /* !WIN32 */