-
Notifications
You must be signed in to change notification settings - Fork 3
/
thdatwrapper.cpp
55 lines (54 loc) · 1.42 KB
/
thdatwrapper.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
#include "thdatwrapper.hpp"
#include <cstdlib>
#include <cstring>
thDatWrapper::thDatWrapper(const fs::path &datpath, unsigned ver)
{
thtk_error_t *e = NULL;
#if PATH_VALSIZE == 2
datf = thtk_io_open_file_w(datpath.c_str(), L"rb", &e);
#else
datf = thtk_io_open_file(datpath.c_str(), "rb", &e);
#endif
thtk_error_free(&e);
dat = thdat_open(ver, datf, &e);
if (!dat)
//just try the latest supported version instead
{
thtk_error_free(&e);
dat = thdat_open(17, datf, &e);
}
thtk_error_free(&e);
}
ssize_t thDatWrapper::getFileSize(const char *path)
{
thtk_error_t *e = NULL;
int en = thdat_entry_by_name(dat, path, &e);
thtk_error_free(&e);
if (!~en)return -1;
return thdat_entry_get_size(dat, en, &e);
}
int thDatWrapper::getFile(const char *path, char *dest)
{
thtk_error_t *e = NULL;
int en = thdat_entry_by_name(dat, path, &e);
thtk_error_free(&e);
if (!~en)return -1;
ssize_t sz = thdat_entry_get_size(dat, en, &e);
thtk_error_free(&e);
void *m = malloc(sz);
thtk_io_t *mf = thtk_io_open_memory(m, sz, &e);
thtk_error_free(&e);
ssize_t r = thdat_entry_read_data(dat, en, mf, &e);
if (!~r)return -1;
thtk_error_free(&e);
memcpy(dest, m, sz);
thtk_io_close(mf);
return 0;
}
thDatWrapper::~thDatWrapper()
{
thtk_error_t *e = NULL;
thdat_close(dat, &e);
thtk_error_free(&e);
thtk_io_close(datf);
}