lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Paul Moore wrote:
The biggest issue I have is that converting char <-> wchar_t requires
memory buffers for the conversion. I can obtain these in a number of
ways (the lua allocator, creating userdata objects, etc) but I need to
manage the lifetime of the buffers
One of the API's in Lua that I think might solve at least the wchar_t -> char problem is the luaL_Buffer setup [1]. I envision that you would take your wchar_t and execute a function in a setup like this:

wchar_t *wdata...;
int idx = 0;
int remaining = length(wdata);
luaL_Buffer b;
luaL_buffinit(L, &b);
while(remaining > 0) {
char* out = luaL_prepbuffer(&b); // Prepare buffer to contain up to LUAL_BUFFERSIZE bytes
 int outputSize = LUAL_BUFFERSIZE;
 int ret = _convert_(wdata + idx, remaining, out, &outputSize);
 if(ret < 0) // handle err
 remaining -= ret;
 luaL_addsize(&b, outputSize);
}
luaL_pushresult(&b);

I'd hope that some sort of _convert_ API exists like that....


[1] http://www.lua.org/manual/5.1/manual.html#luaL_Buffer