aboutsummaryrefslogtreecommitdiff
path: root/src/lua/net/luaopen_Buffer.cpp
blob: 6e10f2580f01ad534bf0aeed4bfa018fc5d941fb (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "lua/luax.h"
#include "../luaopen_types.h"
#include "libjin/jin.h"

namespace jin
{
namespace lua
{
namespace net
{

    class Buffer
    {
    public:
        Buffer(size_t size);
        Buffer(const char* data, size_t size);
        ~Buffer()
        {
        }

        void append(char* data, size_t size)
        {

        }

        const char* grabString(int offset = 0)
        {
            int l = offset;
            for (; l < size; ++l)
            {
                if (buffer[l] == 0)
                {

                }
            }
            return nullptr;
        }

        int grabInteger(int offset = 0)
        {

        }

        float grabfloat(int offset = 0)
        {

        }

        bool grabboolean(int offset = 0)
        {

        }

    private:
        char* buffer;
        size_t size;

    };

    static int l_buffer(lua_State* L)
    {
        int size = luax_checkinteger(L, 1);
        char* buffer = (char*)malloc(size);
        memset(buffer, 0, size);
        luax_pushlstring(L, buffer, size);
        return 1;
    }

    // return buffer, size
    static int l_write(lua_State* L)
    {
        const char* data = luax_checklstring(L, 1, NULL);
        int len = luax_checkinteger(L, 2);
        if (luax_isinteger(L, 3))
        {
            int n = luax_checkinteger(L, 3);
            int size = len + sizeof(n);
            char* buffer = (char*)malloc(size);
            memcpy(buffer, data, len);
            memcpy(buffer + len, &n, sizeof(n));
            //free((void*)data);
            luax_pushlstring(L, buffer, size);
            luax_pushinteger(L, size);
            return 2;
        }
        else if (luax_isfloat(L, 3))
        {
            float n = luax_checknumber(L, 3);
            int size = len + sizeof(n);
            char* buffer = (char*)malloc(size);
            memcpy(buffer, data, len);
            memcpy(buffer + len, &n, sizeof(n));
            //free((void*)data);
            luax_pushlstring(L, buffer, size);
            luax_pushinteger(L, size);
            return 2;
        }
        else if (luax_isboolean(L, 3))
        {
            bool b = luax_checkbool(L, 3);
            int size = len + sizeof(b);
            char* buffer = (char*)malloc(size);
            memcpy(buffer, data, len);
            memcpy(buffer + len, &b, sizeof(b));
            //free((void*)data);
            luax_pushlstring(L, buffer, size);
            luax_pushinteger(L, size);
            return 2;
        }
        else if (luax_isstring(L, 3))
        {
            const char* s = luax_checkstring(L, 3);
            int l = strlen(s) + 1; // with \0
            int size = len + l;
            char* buffer = (char*)malloc(size);
            memcpy(buffer, data, len);
            memcpy(buffer + len, s, l);
            luax_pushlstring(L, buffer, size);
            luax_pushinteger(L, size);
            return 2;
        }
        else
        {
            luax_typerror(L, 3, "number, bool or string");
            return 0;
        }
    }

    // jin.shift(buffer, shift)
    static int l_shift(lua_State* L)
    {
        const char* buffer = luax_checklstring(L, 1, NULL);
        int size = luax_checkinteger(L, 2);
        int shift = luax_checkinteger(L, 3);
        int ss = size - shift;
        luax_pushlstring(L, buffer + shift, ss);
        luax_pushinteger(L, ss);
        return 2;
    }

    // jin.bit.grabstring(buffer, size)
    static int l_grabstring(lua_State* L)
    {
        const char* buffer = luax_checklstring(L, 1, NULL);
        int size = luax_checkinteger(L, 2);
        int l = 0;
        for (; l < size; ++l)
        {
            if (buffer[l] == 0)
            {
                int len = l + 1;
                char* str = (char*)malloc(len);
                memcpy(str, buffer, len);
                luax_pushstring(L, str);
                luax_pushinteger(L, l);
                return 2;
            }
        }
        return 0;
    }

    static int l_grabinteger(lua_State* L)
    {
        const char* buffer = luax_checklstring(L, 1, NULL);
        int size = luax_checkinteger(L, 2);
        int n = *((int*)buffer);
        luax_pushinteger(L, n);
        return 1;
    }

    static int l_grabfloat(lua_State* L)
    {
        const char* buffer = luax_checklstring(L, 1, NULL);
        int size = luax_checkinteger(L, 2);
        float n = *((float*)buffer);
        luax_pushnumber(L, n);
        return 1;
    }

    static int l_grabboolean(lua_State* L)
    {
        const char* buffer = luax_checklstring(L, 1, NULL);
        int size = luax_checkinteger(L, 2);
        bool n = *((bool*)buffer);
        luax_pushboolean(L, n);
        return 1;
    }

} // net
} // lua
} // jin