/
file.c
229 lines (209 loc) · 5.04 KB
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
Copyright (c) 2017-2020 ByteBit
This file is part of BetterSpades.
BetterSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BetterSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BetterSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
struct file_handle {
void* internal;
int type;
};
enum { FILE_STD, FILE_SDL };
void file_url(char* url) {
char cmd[strlen(url) + 16];
#ifdef OS_WINDOWS
sprintf(cmd, "start %s", url);
system(cmd);
#endif
#if defined(OS_LINUX) || defined(OS_APPLE)
sprintf(cmd, "open %s", url);
system(cmd);
#endif
}
int file_dir_exists(const char* path) {
#ifndef USE_ANDROID_FILE
DIR* d = opendir(path);
#else
char str[256];
sprintf(str, "/sdcard/BetterSpades/%s", path);
DIR* d = opendir(str);
#endif
if(d) {
closedir(d);
return 1;
} else {
return 0;
}
}
int file_dir_create(const char* path) {
#ifndef USE_ANDROID_FILE
mkdir(path);
#else
char str[256];
sprintf(str, "/sdcard/BetterSpades/%s", path);
mkdir(str);
#endif
return 1;
}
int file_exists(const char* name) {
#ifdef USE_ANDROID_FILE
void* f = file_open(name, "rb");
if(f == NULL)
return 0;
file_close(f);
return 1;
#else
return !access(name, F_OK);
#endif
}
int file_size(const char* name) {
#ifdef USE_ANDROID_FILE
struct file_handle* f = (struct file_handle*)file_open(name, "rb");
if(!f)
return 0;
if(f->type == FILE_SDL) {
int size = SDL_RWsize((struct SDL_RWops*)f->internal);
file_close(f);
return size;
}
if(f->type == FILE_STD) {
fseek(f->internal, 0, SEEK_END);
int size = ftell(f->internal);
file_close(f);
return size;
}
return 0;
#else
FILE* f = fopen(name, "rb");
if(!f)
return 0;
fseek(f, 0, SEEK_END);
int size = ftell(f);
fclose(f);
return size;
#endif
}
unsigned char* file_load(const char* name) {
#ifdef USE_ANDROID_FILE
int size = file_size(name);
struct file_handle* f = (struct file_handle*)file_open(name, "rb");
if(!f)
return NULL;
unsigned char* data = malloc(size + 1);
CHECK_ALLOCATION_ERROR(data)
data[size] = 0;
if(f->type == FILE_SDL) {
int offset = 0;
while(1) {
int read = SDL_RWread((struct SDL_RWops*)f->internal, data + offset, 1, size - offset);
if(!read)
break;
offset += read;
}
SDL_RWclose((struct SDL_RWops*)f->internal);
if(!offset) {
free(data);
return NULL;
}
}
if(f->type == FILE_STD) {
fread(data, size, 1, f->internal);
fclose(f->internal);
}
return data;
#else
FILE* f;
f = fopen(name, "rb");
if(!f) {
log_fatal("ERROR: failed to open '%s', exiting", name);
exit(1);
}
fseek(f, 0, SEEK_END);
int size = ftell(f);
unsigned char* data = malloc(size + 1);
CHECK_ALLOCATION_ERROR(data)
data[size] = 0;
fseek(f, 0, SEEK_SET);
fread(data, size, 1, f);
fclose(f);
return data;
#endif
}
void* file_open(const char* name, const char* mode) {
#ifdef USE_ANDROID_FILE
struct file_handle* handle = malloc(sizeof(struct file_handle));
handle->internal = (strchr(mode, 'r') != NULL) ? SDL_RWFromFile(name, mode) : NULL;
handle->type = FILE_SDL;
if(!handle->internal) {
char str[256];
sprintf(str, "/sdcard/BetterSpades/%s", name);
handle->internal = fopen(str, mode);
handle->type = FILE_STD;
// log_warn("open %s %i",str,handle->internal);
}
if(!handle->internal) {
free(handle);
return NULL;
}
return handle;
#else
return fopen(name, mode);
#endif
}
void file_printf(void* file, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
#ifdef USE_ANDROID_FILE
struct file_handle* f = (struct file_handle*)file;
if(f->type == FILE_SDL) {
char str[256];
vsprintf(str, fmt, args);
int written = 0;
int total = strlen(str);
while(written < total)
written += SDL_RWwrite((struct SDL_RWops*)f->internal, str + written, 1, total - written);
}
if(f->type == FILE_STD) {
log_warn("%i %i", f->internal, f);
vfprintf((FILE*)f->internal, fmt, args);
}
#else
vfprintf((FILE*)file, fmt, args);
#endif
va_end(args);
}
void file_close(void* file) {
#ifdef USE_ANDROID_FILE
struct file_handle* f = (struct file_handle*)file;
if(f->type == FILE_SDL) {
SDL_RWclose((struct SDL_RWops*)f->internal);
}
if(f->type == FILE_STD) {
fclose((FILE*)f->internal);
}
free(f);
#else
fclose((FILE*)file);
#endif
}
float buffer_readf(unsigned char* buffer, int index) {
return ((float*)(buffer + index))[0];
}
unsigned int buffer_read32(unsigned char* buffer, int index) {
return (buffer[index + 3] << 24) | (buffer[index + 2] << 16) | (buffer[index + 1] << 8) | buffer[index];
}
unsigned short buffer_read16(unsigned char* buffer, int index) {
return (buffer[index + 1] << 8) | buffer[index];
}
unsigned char buffer_read8(unsigned char* buffer, int index) {
return buffer[index];
}