JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
compile on debian unstable
[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         stat(dirname, &buf);
95         return S_ISDIR(buf.st_mode);
96 }
97
98 static bool
99 find_data_dir(void)
100 {
101         int i;
102         char *data_options[3] = {
103                 getenv("VOR_DATA"),
104                 "data",
105                 DATA_PREFIX
106         };
107
108         for(i=0; i<3; i++) {
109                 if(!data_options[i]) continue;
110                 g_data_dir = strdup(data_options[i]);
111                 if(is_dir(g_data_dir)) return true;
112         }
113
114         fprintf(stderr, "Can't find VoR data! Tried:\n");
115         for(i=0; i<3; i++) {
116                 if(data_options[i]) fprintf(stderr, "\t%s\n", data_options[i]);
117         }
118         return false;
119 }
120
121 static bool
122 find_score_file(void)
123 {
124         char *dir, *s;
125         
126         /* in case we get called twice */
127         if(g_score_file) return true;
128
129         dir = getenv("HOME"); if(!dir) return false;
130         s = add_path(dir, ".vor-scores");
131         if(s) {
132                 g_score_file = s;
133                 return true;
134         } else return false;
135 }
136
137 bool
138 find_files(void)
139 {
140         return find_data_dir() && find_score_file();
141 }
142
143 FILE *
144 open_score_file(char *mode)
145 {
146         if(!g_score_file) return NULL;
147         return fopen(g_score_file, mode);
148 }
149
150 #endif /* !WIN32 */