/
file.c
72 lines (59 loc) · 1.78 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
/*
Copyright (c) 2017-2018 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"
int file_dir_exists(const char* path) {
DIR* d = opendir(path);
if(d) {
closedir(d);
return 1;
} else {
return 0;
}
}
int file_dir_create(const char* path) {
mkdir(path);
}
int file_exists(const char* name) {
return !access(name,F_OK);
}
unsigned char* file_load(const char* name) {
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;
}
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];
}