-
Notifications
You must be signed in to change notification settings - Fork 18
/
FileUt.cpp
95 lines (76 loc) · 1.26 KB
/
FileUt.cpp
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
// 110619
// 110710
#include "FileUt.h"
void * file_read_m(const wchar_t * path, unsigned int * len, unsigned int m)
{
if(path)
{
FILE * fp = 0;
fopen_p(fp, path, L"rb");
if(fp)
{
void * buf = 0;
unsigned int flen = 0;
fseek(fp, 0L, SEEK_END);
flen = ftell(fp);
fseek(fp, 0L, SEEK_SET);
if(flen)
{
buf = new char[flen + m];
if(buf) fread(buf, flen, 1, fp);
}
fclose(fp);
if(len) *len = flen;
return buf;
}
}
return 0;
}
void * file_read(const wchar_t * path, unsigned int * len)
{
return file_read_m(path, len, 0);
}
char * file_read_str(const wchar_t * path)
{
unsigned int len = 0;
char * buf = (char*)file_read_m(path, &len, 1);
if(buf)
{
buf[len] = 0;
return buf;
}
return 0;
}
bool file_save(const wchar_t * path, const void * buf, unsigned int len)
{
if(path && buf && len)
{
FILE * fp = 0;
fopen_p(fp, path, L"wb");
if(fp)
{
fwrite(buf, len, 1, fp);
fclose(fp);
return true;
}
}
return false;
}
bool file_save_a(const char * path, const void * buf, unsigned int len)
{
if(path && buf && len)
{
FILE * fp = fopen(path, "wb");
if(fp)
{
fwrite(buf, len, 1, fp);
fclose(fp);
return true;
}
}
return false;
}
void file_free(void * buf)
{
if(buf) delete buf;
}