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