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