-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathtb_file_posix.cpp
58 lines (49 loc) · 1.16 KB
/
tb_file_posix.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
// ================================================================================
// == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
// == See tb_core.h for more information. ==
// ================================================================================
#include "tb_system.h"
#ifdef TB_FILE_POSIX
#include <stdio.h>
namespace tb {
class TBPosixFile : public TBFile
{
public:
TBPosixFile(FILE *f) : file(f) {}
virtual ~TBPosixFile() { fclose(file); }
virtual long Size()
{
long oldpos = ftell(file);
fseek(file, 0, SEEK_END);
long num_bytes = ftell(file);
fseek(file, oldpos, SEEK_SET);
return num_bytes;
}
virtual size_t Read(void *buf, size_t elemSize, size_t count)
{
return fread(buf, elemSize, count, file);
}
private:
FILE *file;
};
// static
TBFile *TBFile::Open(const char *filename, TBFileMode mode)
{
FILE *f = nullptr;
switch (mode)
{
case MODE_READ:
f = fopen(filename, "rb");
break;
default:
break;
}
if (!f)
return nullptr;
TBPosixFile *tbf = new TBPosixFile(f);
if (!tbf)
fclose(f);
return tbf;
}
} // namespace tb
#endif // TB_FILE_POSIX