mirror of
https://github.com/lunarmodules/lua-iconv.git
synced 2025-06-23 04:34:33 +02:00
*** empty log message ***
git-svn-id: file:///var/svn/lua-iconv/trunk@10 9538949d-8f27-0410-946f-ce01ef448559
This commit is contained in:
parent
c03c381b14
commit
0be1828c41
35
luaiconv.c
35
luaiconv.c
@ -99,11 +99,12 @@ static int Liconv(lua_State *L)
|
|||||||
char *inbuf = (char*) getstring(L, 2);
|
char *inbuf = (char*) getstring(L, 2);
|
||||||
char *outbuf;
|
char *outbuf;
|
||||||
char *outbufs;
|
char *outbufs;
|
||||||
size_t bsize = ibleft;
|
size_t bsize = 4 * ibleft; /* Try to avoid concatenations on stack */
|
||||||
size_t obleft = ibleft;
|
size_t obleft = bsize;
|
||||||
size_t ret = -1;
|
size_t ret = -1;
|
||||||
|
int hasone = 0;
|
||||||
|
|
||||||
outbuf = (char*) malloc(ibleft * sizeof(char));
|
outbuf = (char*) malloc(bsize * sizeof(char));
|
||||||
if(outbuf == NULL)
|
if(outbuf == NULL)
|
||||||
{
|
{
|
||||||
lua_pushstring(L, "");
|
lua_pushstring(L, "");
|
||||||
@ -111,7 +112,6 @@ static int Liconv(lua_State *L)
|
|||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
outbufs = outbuf;
|
outbufs = outbuf;
|
||||||
printf("M ibleft=%d, bsize=%d, obleft=%d\n", ibleft, bsize, obleft);
|
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@ -121,6 +121,9 @@ static int Liconv(lua_State *L)
|
|||||||
if(errno == EILSEQ)
|
if(errno == EILSEQ)
|
||||||
{
|
{
|
||||||
lua_pushlstring(L, outbufs, bsize-obleft);
|
lua_pushlstring(L, outbufs, bsize-obleft);
|
||||||
|
if(hasone == 1)
|
||||||
|
lua_concat(L, 2);
|
||||||
|
hasone = 1;
|
||||||
lua_pushnumber(L, ERROR_INVALID);
|
lua_pushnumber(L, ERROR_INVALID);
|
||||||
free(outbufs);
|
free(outbufs);
|
||||||
return 2; /* Invalid character sequence */
|
return 2; /* Invalid character sequence */
|
||||||
@ -128,27 +131,27 @@ static int Liconv(lua_State *L)
|
|||||||
else if(errno == EINVAL)
|
else if(errno == EINVAL)
|
||||||
{
|
{
|
||||||
lua_pushlstring(L, outbufs, bsize-obleft);
|
lua_pushlstring(L, outbufs, bsize-obleft);
|
||||||
|
if(hasone == 1)
|
||||||
|
lua_concat(L, 2);
|
||||||
|
hasone = 1;
|
||||||
lua_pushnumber(L, ERROR_INCOMPLETE);
|
lua_pushnumber(L, ERROR_INCOMPLETE);
|
||||||
free(outbufs);
|
free(outbufs);
|
||||||
return 2; /* Incomplete character sequence */
|
return 2; /* Incomplete character sequence */
|
||||||
}
|
}
|
||||||
else if(errno == E2BIG)
|
else if(errno == E2BIG)
|
||||||
{
|
{
|
||||||
bsize += 2 * ibleft;
|
lua_pushlstring(L, outbufs, bsize-obleft);
|
||||||
obleft += 2 * ibleft;
|
if(hasone == 1)
|
||||||
printf("R ibleft=%d, bsize=%d, obleft=%d\n", ibleft, bsize,
|
lua_concat(L, 2);
|
||||||
obleft);
|
hasone = 1;
|
||||||
outbufs = (char*) realloc(outbufs, bsize * sizeof(char));
|
obleft = bsize;
|
||||||
if(outbufs == NULL)
|
|
||||||
{
|
|
||||||
lua_pushstring(L, "");
|
|
||||||
lua_pushnumber(L, ERROR_NO_MEMORY);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
lua_pushlstring(L, outbufs, bsize-obleft);
|
lua_pushlstring(L, outbufs, bsize-obleft);
|
||||||
|
if(hasone == 1)
|
||||||
|
lua_concat(L, 2);
|
||||||
|
hasone = 1;
|
||||||
lua_pushnumber(L, ERROR_UNKNOWN);
|
lua_pushnumber(L, ERROR_UNKNOWN);
|
||||||
free(outbufs);
|
free(outbufs);
|
||||||
return 2; /* Unknown error */
|
return 2; /* Unknown error */
|
||||||
@ -158,6 +161,8 @@ static int Liconv(lua_State *L)
|
|||||||
while(ret != (size_t) 0);
|
while(ret != (size_t) 0);
|
||||||
|
|
||||||
lua_pushlstring(L, outbufs, bsize-obleft);
|
lua_pushlstring(L, outbufs, bsize-obleft);
|
||||||
|
if(hasone == 1)
|
||||||
|
lua_concat(L, 2);
|
||||||
free(outbufs);
|
free(outbufs);
|
||||||
return 1; /* Done */
|
return 1; /* Done */
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,8 @@ cd = iconv.new("utf-8", "iso-8859-1")
|
|||||||
|
|
||||||
assert(cd, "Invalid conversion")
|
assert(cd, "Invalid conversion")
|
||||||
|
|
||||||
-- ret, err = cd:iconv("Isso é um teste com acentuação")
|
ret, err = cd:iconv("Isso é um teste com acentuação")
|
||||||
ret, err = cd:iconv("çãÃÉÍÓÚÇÑÿ")
|
-- ret, err = cd:iconv("çãÃÉÍÓÚÇÑÿ ")
|
||||||
|
|
||||||
if err == iconv.ERROR_INCOMPLETE then
|
if err == iconv.ERROR_INCOMPLETE then
|
||||||
print("Error: Incomplete input.")
|
print("Error: Incomplete input.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user