forked from ocornut/imgui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimfile.cpp
More file actions
111 lines (93 loc) · 2.13 KB
/
imfile.cpp
File metadata and controls
111 lines (93 loc) · 2.13 KB
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
/**********************************************\
*
* Andrey A. Ugolnik
* http://www.ugolnik.info
*
\**********************************************/
#include "imfile.h"
#include <cstdio>
#include <cstdarg>
class ImInputFileStreamStd : public ImInputStream
{
public:
~ImInputFileStreamStd()
{
if (m_file != NULL)
{
::fclose(m_file);
}
}
bool open(const char* path, const char* mode)
{
m_file = ::fopen(path, mode);
if (m_file != NULL)
{
::fseek(m_file, 0, SEEK_END);
m_size = ::ftell(m_file);
::fseek(m_file, 0, SEEK_SET);
}
return m_file != NULL;
}
unsigned size() const override
{
return m_size;
}
unsigned read(void* buffer, unsigned count) override
{
return (unsigned)::fread(buffer, 1, count, m_file);
}
private:
FILE* m_file = NULL;
unsigned m_size = 0;
};
class ImOutputFileStreamStd : public ImOutputStream
{
public:
~ImOutputFileStreamStd()
{
if (m_file != NULL)
{
::fclose(m_file);
}
}
bool create(const char* path, const char* mode)
{
m_file = ::fopen(path, mode);
return m_file != NULL;
}
unsigned write(const void* buffer, unsigned count) override
{
return (unsigned)::fwrite(buffer, 1, count, m_file);
}
unsigned format(const char* fmt, ...) override
{
va_list argList;
va_start(argList, fmt);
unsigned result = ::vfprintf(m_file, fmt, argList);
va_end(argList);
return result;
}
private:
FILE* m_file = NULL;
};
ImInputStream* ImFile::open(const char* path, const char* mode) const
{
ImInputStream* stream = new ImInputFileStreamStd();
if (stream->open(path, mode))
{
return stream;
}
delete stream;
return NULL;
}
ImOutputStream* ImFile::create(const char* path, const char* mode) const
{
ImOutputStream* stream = new ImOutputFileStreamStd();
if (stream->create(path, mode))
{
return stream;
}
delete stream;
return NULL;
}