-
Notifications
You must be signed in to change notification settings - Fork 1
/
winxedxx_bytebuffer.cxx
117 lines (100 loc) · 2.21 KB
/
winxedxx_bytebuffer.cxx
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
112
113
114
115
116
117
// winxedxx_bytebuffer.cxx
// (C) 2012 Julián Albo
#include "winxedxx_types.h"
#include "winxedxx_object.h"
#include "winxedxx_default.h"
#include "winxedxx_ptr.h"
#include "winxedxx_bytebuffer.h"
#include <stdlib.h>
namespace WinxedXX
{
WxxByteBuffer::WxxByteBuffer() : WxxDefault("ByteBuffer"),
p(0),
allocated_size(0),
size(0)
{
}
int WxxByteBuffer::elements() const
{
return size;
}
WxxByteBuffer & WxxByteBuffer::set(int i)
{
set_integer_native(i);
return *this;
}
WxxByteBuffer & WxxByteBuffer::set(const std::string & s)
{
const int len = s.size();
set_integer_native(len + 1);
set_integer_native(len);
for (int i = 0; i < len; ++i)
p[i] = (unsigned char)s[i];
return *this;
}
void WxxByteBuffer::set_integer_native(int i)
{
if (allocated_size) {
void *ptr = get_pointer();
if (i <= 0) {
free(ptr);
set_pointer(0);
allocated_size = 0;
size = 0;
}
else {
if ((size_t)i > allocated_size) {
void *newptr = realloc(ptr, i);
if (! newptr)
return;
set_pointer(newptr);
allocated_size = i;
size = i;
}
size = i;
}
}
else {
if (i > 0) {
void *newptr = malloc(i);
if (newptr) {
set_pointer(newptr);
allocated_size = i;
size = i;
}
}
}
}
int WxxByteBuffer::get_integer_keyed(int i)
{
if (i < 0 || (size_t)i >= size)
return 0;
else
return p[i];
}
WxxObjectPtr WxxByteBuffer::get_pmc_keyed(int i)
{
return get_integer_keyed(i);
}
void WxxByteBuffer::set_integer_keyed(int i, int value)
{
if (i >= 0) {
if ((size_t)i >= size)
set_integer_native(i + 1);
p[i] = value;
}
}
void WxxByteBuffer::set_pmc_keyed(int i, const WxxObjectPtr &value)
{
set_integer_keyed(i, value.get_integer());
}
void * WxxByteBuffer::get_pointer()
{
return p;
}
void WxxByteBuffer::set_pointer(void *ptr)
{
p = (unsigned char *)ptr;
}
} // namespace WinxedXX
// End of winxedxx_bytebuffer.cxx