luasocket/src/io.c

46 lines
1.6 KiB
C
Raw Normal View History

/*=========================================================================*\
* Input/Output abstraction
* LuaSocket toolkit
*
* RCS ID: $Id$
\*=========================================================================*/
2003-05-25 03:54:13 +02:00
#include "io.h"
/*=========================================================================*\
* Exported functions
\*=========================================================================*/
/*-------------------------------------------------------------------------*\
* Initializes C structure
\*-------------------------------------------------------------------------*/
2004-07-01 05:32:09 +02:00
void io_init(p_io io, p_send send, p_recv recv, p_geterr geterr, void *ctx) {
2003-05-25 03:54:13 +02:00
io->send = send;
io->recv = recv;
2004-07-01 05:32:09 +02:00
io->geterr = geterr;
2003-05-25 03:54:13 +02:00
io->ctx = ctx;
}
/*-------------------------------------------------------------------------*\
* Translate error codes to Lua
\*-------------------------------------------------------------------------*/
2004-07-01 05:32:09 +02:00
const char *io_strerror(int code) {
switch (code) {
case IO_DONE: return NULL;
case IO_CLOSED: return "closed";
2004-07-01 05:32:09 +02:00
case IO_TIMEOUT: return "timeout";
case IO_CLIPPED: return "clipped";
default: return "unknown error";
}
}
/*-------------------------------------------------------------------------*\
2004-07-01 05:32:09 +02:00
* Push error message from code or from driver
\*-------------------------------------------------------------------------*/
2004-07-01 05:32:09 +02:00
void io_pusherror(lua_State *L, p_io io, int code)
{
2004-07-01 05:32:09 +02:00
const char *err = NULL;
if (code < IO_USER) err = io_strerror(code);
else err = io->geterr(io->ctx, code);
if (err) lua_pushstring(L, err);
else lua_pushnil(L);
}