mirror of
https://github.com/lunarmodules/lua-iconv.git
synced 2025-06-23 04:34:33 +02:00
Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
3c729ec869 | ||
|
5452834c16 | ||
|
dbdd9d9ab8 | ||
|
9eced53a8d | ||
|
1e6d024dc2 | ||
|
11545d0533 | ||
|
9e865ff345 | ||
|
8e19fde5f3 | ||
|
e4d200c102 | ||
|
ba4f3a4518 | ||
|
ac7f962b31 |
13
CHANGELOG.md
Normal file
13
CHANGELOG.md
Normal file
@ -0,0 +1,13 @@
|
||||
# Changelog
|
||||
|
||||
### 7.1.0 released 30-Oct-2023
|
||||
|
||||
* fix: rockspecs for MacOS
|
||||
|
||||
* feat: `iconv.new` now returns a second value in case of errors
|
||||
|
||||
---
|
||||
|
||||
### 7.0.0 released 17-Oct-2023
|
||||
|
||||
* First release from its new Lunar Modules home
|
2
Makefile
2
Makefile
@ -68,5 +68,5 @@ clean:
|
||||
$(RM) iconv.so iconv.lo
|
||||
|
||||
test: iconv.so test_iconv.lua
|
||||
lua test_iconv.lua
|
||||
$(LUABIN) test_iconv.lua
|
||||
|
||||
|
21
README.md
21
README.md
@ -55,14 +55,29 @@ call to load up the library (that, of course, must be installed in a directory f
|
||||
## API documentation
|
||||
|
||||
```lua
|
||||
cd = iconv.new(to, from)
|
||||
cd = iconv.open(to, from)
|
||||
cd, err = iconv.new(to, from)
|
||||
cd, err = iconv.open(to, from)
|
||||
```
|
||||
|
||||
Opens a new conversion descriptor, from the 'from' charset to the 'to' charset.
|
||||
Concatenating "//TRANSLIT" to the first argument will enable character transliteration and concatenating "//IGNORE" to the first argument will cause iconv to ignore any invalid characters found in the input string.
|
||||
|
||||
This function returns a new converter or nil on error.
|
||||
The error code, may have any of the following values:
|
||||
|
||||
* `nil`
|
||||
|
||||
No error. Creation was successful.
|
||||
|
||||
* `iconv.ERROR_INVALID`
|
||||
|
||||
The conversion from `from` to `to` is not supported by the implementation.
|
||||
|
||||
* `iconv.ERROR_UNKNOWN`
|
||||
|
||||
There was an unknown error.
|
||||
|
||||
|
||||
This function returns a new converter or nil+err on error.
|
||||
|
||||
|
||||
```lua
|
||||
|
62
lua-iconv-dev-1.rockspec
Normal file
62
lua-iconv-dev-1.rockspec
Normal file
@ -0,0 +1,62 @@
|
||||
local package_name = "lua-iconv"
|
||||
local package_version = "dev"
|
||||
local rockspec_revision = "1"
|
||||
local github_account_name = "lunarmodules"
|
||||
local github_repo_name = package_name
|
||||
|
||||
package = package_name
|
||||
version = package_version.."-"..rockspec_revision
|
||||
|
||||
source = {
|
||||
url = "git+https://github.com/"..github_account_name.."/"..github_repo_name..".git",
|
||||
branch = (package_version == "dev") and "master" or nil,
|
||||
tag = (package_version ~= "dev") and ("v" .. package_version) or nil,
|
||||
}
|
||||
|
||||
description = {
|
||||
summary = "Lua binding to the iconv",
|
||||
detailed = [[
|
||||
Lua binding to the POSIX 'iconv' library, which converts a sequence of
|
||||
characters from one codeset into a sequence of corresponding characters
|
||||
in another codeset.
|
||||
]],
|
||||
license = "MIT/X11",
|
||||
homepage = "https://github.com/"..github_account_name.."/"..github_repo_name,
|
||||
}
|
||||
|
||||
dependencies = {
|
||||
"lua >= 5.1",
|
||||
}
|
||||
|
||||
external_dependencies = {
|
||||
ICONV = {
|
||||
header = "iconv.h"
|
||||
}
|
||||
}
|
||||
|
||||
build = {
|
||||
type = "builtin",
|
||||
modules = {
|
||||
iconv = {
|
||||
sources = {"luaiconv.c"},
|
||||
incdirs = {"$(ICONV_INCDIR)"},
|
||||
libdirs = {"$(ICONV_LIBDIR)"}
|
||||
}
|
||||
},
|
||||
platforms = {
|
||||
cygwin = {
|
||||
modules = {
|
||||
iconv = {
|
||||
libraries = {"iconv"}
|
||||
}
|
||||
}
|
||||
},
|
||||
macosx = {
|
||||
modules = {
|
||||
iconv = {
|
||||
libraries = {"iconv"}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
24
luaiconv.c
24
luaiconv.c
@ -39,6 +39,12 @@
|
||||
#define LIB_VERSION LIB_NAME " 7"
|
||||
#define ICONV_TYPENAME "iconv_t"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define LUAEXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define LUAEXPORT __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#if LUA_VERSION_NUM < 501
|
||||
#error "Unsupported Lua version. You must use Lua >= 5.1"
|
||||
#endif
|
||||
@ -92,11 +98,17 @@ static int Liconv_open(lua_State *L)
|
||||
const char *tocode = luaL_checkstring(L, 1);
|
||||
const char *fromcode = luaL_checkstring(L, 2);
|
||||
iconv_t cd = iconv_open(tocode, fromcode);
|
||||
if (cd != (iconv_t)(-1))
|
||||
if (cd != (iconv_t)(-1)) {
|
||||
push_iconv_t(L, cd); /* ok */
|
||||
else
|
||||
return 1;
|
||||
} else {
|
||||
lua_pushnil(L); /* error */
|
||||
return 1;
|
||||
if (errno == EINVAL)
|
||||
lua_pushnumber(L, ERROR_INVALID);
|
||||
else
|
||||
lua_pushnumber(L, ERROR_UNKNOWN);
|
||||
return 2;
|
||||
};
|
||||
}
|
||||
|
||||
/* Use a fixed-size buffer in the stack to avoid a lot of small mallocs
|
||||
@ -116,6 +128,7 @@ static int Liconv(lua_State *L)
|
||||
char *outbuf = outbufs;
|
||||
size_t obleft = CONV_BUF_SIZE;
|
||||
size_t ret = -1;
|
||||
luaL_Buffer b;
|
||||
|
||||
if (cd == NULL) {
|
||||
lua_pushstring(L, "");
|
||||
@ -123,7 +136,6 @@ static int Liconv(lua_State *L)
|
||||
return 2;
|
||||
}
|
||||
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
|
||||
do {
|
||||
@ -207,7 +219,7 @@ static int Liconv_close(lua_State *L)
|
||||
lua_pushboolean(L, 1); /* ok */
|
||||
}
|
||||
else
|
||||
lua_pushnil(L); /* error */
|
||||
lua_pushnil(L); /* error, called from __gc, so no need for error messsage */
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -223,7 +235,7 @@ static const luaL_Reg iconv_funcs[] = {
|
||||
};
|
||||
|
||||
|
||||
int luaopen_iconv(lua_State *L)
|
||||
LUAEXPORT int luaopen_iconv(lua_State *L)
|
||||
{
|
||||
luaL_newlib(L, iconv_funcs);
|
||||
|
||||
|
52
rockspecs/lua-iconv-6-1.rockspec
Normal file
52
rockspecs/lua-iconv-6-1.rockspec
Normal file
@ -0,0 +1,52 @@
|
||||
|
||||
-- Packs lua-iconv into a LuaRock
|
||||
-- rockspec based uppon the file provided by DarkGod <darkgod at net-core.org>
|
||||
|
||||
package = "lua-iconv"
|
||||
version = "6-1"
|
||||
|
||||
source = {
|
||||
url = "http://luaforge.net/frs/download.php/4181/lua-iconv-6.tar.gz",
|
||||
md5 = "2cec334c5786d7c420a53003d6cb93d4"
|
||||
}
|
||||
|
||||
description = {
|
||||
summary = "Lua binding to the iconv",
|
||||
detailed = [[
|
||||
Lua binding to the POSIX 'iconv' library, which converts a sequence of
|
||||
characters from one codeset into a sequence of corresponding characters
|
||||
in another codeset.
|
||||
]],
|
||||
license = "MIT/X11",
|
||||
homepage = "http://luaforge.net/projects/lua-iconv/"
|
||||
}
|
||||
|
||||
dependencies = {
|
||||
"lua >= 5.1",
|
||||
}
|
||||
|
||||
external_dependencies = {
|
||||
ICONV = {
|
||||
header = "iconv.h"
|
||||
}
|
||||
}
|
||||
|
||||
build = {
|
||||
type = "builtin",
|
||||
modules = {
|
||||
iconv = {
|
||||
sources = {"luaiconv.c"},
|
||||
incdirs = {"$(ICONV_INCDIR)"},
|
||||
libdirs = {"$(ICONV_LIBDIR)"}
|
||||
}
|
||||
},
|
||||
platforms = {
|
||||
cygwin = {
|
||||
modules = {
|
||||
iconv = {
|
||||
libraries = {"iconv"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
51
rockspecs/lua-iconv-7.0.0-2.rockspec
Normal file
51
rockspecs/lua-iconv-7.0.0-2.rockspec
Normal file
@ -0,0 +1,51 @@
|
||||
|
||||
-- Packs lua-iconv into a LuaRock
|
||||
-- rockspec based uppon the file provided by DarkGod <darkgod at net-core.org>
|
||||
|
||||
package = "lua-iconv"
|
||||
version = "7.0.0-2"
|
||||
|
||||
source = {
|
||||
url = "https://github.com/lunarmodules/lua-iconv/archive/v7.0.0/lua-iconv-7.0.0.tar.gz",
|
||||
}
|
||||
|
||||
description = {
|
||||
summary = "Lua binding to the iconv",
|
||||
detailed = [[
|
||||
Lua binding to the POSIX 'iconv' library, which converts a sequence of
|
||||
characters from one codeset into a sequence of corresponding characters
|
||||
in another codeset.
|
||||
]],
|
||||
license = "MIT/X11",
|
||||
homepage = "https://github.com/lunarmodules/lua-iconv/"
|
||||
}
|
||||
|
||||
dependencies = {
|
||||
"lua >= 5.1",
|
||||
}
|
||||
|
||||
external_dependencies = {
|
||||
ICONV = {
|
||||
header = "iconv.h"
|
||||
}
|
||||
}
|
||||
|
||||
build = {
|
||||
type = "builtin",
|
||||
modules = {
|
||||
iconv = {
|
||||
sources = {"luaiconv.c"},
|
||||
incdirs = {"$(ICONV_INCDIR)"},
|
||||
libdirs = {"$(ICONV_LIBDIR)"}
|
||||
}
|
||||
},
|
||||
platforms = {
|
||||
cygwin = {
|
||||
modules = {
|
||||
iconv = {
|
||||
libraries = {"iconv"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
50
rockspecs/lua-iconv-7.0.0-4.rockspec
Normal file
50
rockspecs/lua-iconv-7.0.0-4.rockspec
Normal file
@ -0,0 +1,50 @@
|
||||
-- Packs lua-iconv into a LuaRock
|
||||
-- rockspec based uppon the file provided by DarkGod <darkgod at net-core.org>
|
||||
|
||||
package = "lua-iconv"
|
||||
version = "7.0.0-4"
|
||||
|
||||
source = {
|
||||
url = "https://github.com/lunarmodules/lua-iconv/archive/v7.0.0/lua-iconv-7.0.0.tar.gz",
|
||||
}
|
||||
|
||||
description = {
|
||||
summary = "Lua binding to the iconv",
|
||||
detailed = [[
|
||||
Lua binding to the POSIX 'iconv' library, which converts a sequence of
|
||||
characters from one codeset into a sequence of corresponding characters
|
||||
in another codeset.
|
||||
]],
|
||||
license = "MIT/X11",
|
||||
homepage = "https://github.com/lunarmodules/lua-iconv/"
|
||||
}
|
||||
|
||||
dependencies = {
|
||||
"lua >= 5.1",
|
||||
}
|
||||
|
||||
external_dependencies = {
|
||||
ICONV = {
|
||||
header = "iconv.h"
|
||||
}
|
||||
}
|
||||
|
||||
build = {
|
||||
type = "builtin",
|
||||
modules = {
|
||||
iconv = {
|
||||
sources = {"luaiconv.c"},
|
||||
incdirs = {"$(ICONV_INCDIR)"},
|
||||
libdirs = {"$(ICONV_LIBDIR)"}
|
||||
}
|
||||
},
|
||||
platforms = {
|
||||
cygwin = {
|
||||
modules = {
|
||||
iconv = {
|
||||
libraries = {"iconv"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
62
rockspecs/lua-iconv-7.1.0-1.rockspec
Normal file
62
rockspecs/lua-iconv-7.1.0-1.rockspec
Normal file
@ -0,0 +1,62 @@
|
||||
local package_name = "lua-iconv"
|
||||
local package_version = "7.1.0"
|
||||
local rockspec_revision = "1"
|
||||
local github_account_name = "lunarmodules"
|
||||
local github_repo_name = package_name
|
||||
|
||||
package = package_name
|
||||
version = package_version.."-"..rockspec_revision
|
||||
|
||||
source = {
|
||||
url = "git+https://github.com/"..github_account_name.."/"..github_repo_name..".git",
|
||||
branch = (package_version == "dev") and "master" or nil,
|
||||
tag = (package_version ~= "dev") and ("v" .. package_version) or nil,
|
||||
}
|
||||
|
||||
description = {
|
||||
summary = "Lua binding to the iconv",
|
||||
detailed = [[
|
||||
Lua binding to the POSIX 'iconv' library, which converts a sequence of
|
||||
characters from one codeset into a sequence of corresponding characters
|
||||
in another codeset.
|
||||
]],
|
||||
license = "MIT/X11",
|
||||
homepage = "https://github.com/"..github_account_name.."/"..github_repo_name,
|
||||
}
|
||||
|
||||
dependencies = {
|
||||
"lua >= 5.1",
|
||||
}
|
||||
|
||||
external_dependencies = {
|
||||
ICONV = {
|
||||
header = "iconv.h"
|
||||
}
|
||||
}
|
||||
|
||||
build = {
|
||||
type = "builtin",
|
||||
modules = {
|
||||
iconv = {
|
||||
sources = {"luaiconv.c"},
|
||||
incdirs = {"$(ICONV_INCDIR)"},
|
||||
libdirs = {"$(ICONV_LIBDIR)"}
|
||||
}
|
||||
},
|
||||
platforms = {
|
||||
cygwin = {
|
||||
modules = {
|
||||
iconv = {
|
||||
libraries = {"iconv"}
|
||||
}
|
||||
}
|
||||
},
|
||||
macosx = {
|
||||
modules = {
|
||||
iconv = {
|
||||
libraries = {"iconv"}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
@ -76,20 +76,30 @@ local ebcdic = "\193\150\64\147\150\149\135\133\107\64\129\150\64\147\164\129"
|
||||
.. "\37"
|
||||
|
||||
|
||||
local function ErrMsg(errno)
|
||||
if errno == iconv.ERROR_INCOMPLETE then
|
||||
return "Incomplete input."
|
||||
elseif errno == iconv.ERROR_INVALID then
|
||||
return "Invalid input."
|
||||
elseif errno == iconv.ERROR_NO_MEMORY then
|
||||
return "Failed to allocate memory."
|
||||
elseif errno == iconv.ERROR_UNKNOWN then
|
||||
return "There was an unknown error."
|
||||
elseif errno == iconv.FINALIZED then
|
||||
return "Handle was already finalized."
|
||||
end
|
||||
return "Unknown error: "..tostring(errno)
|
||||
end
|
||||
|
||||
|
||||
local function check_one(to, from, text)
|
||||
print("\n-- Testing conversion from " .. from .. " to " .. to)
|
||||
local cd = iconv.new(to .. "//TRANSLIT", from)
|
||||
assert(cd, "Failed to create a converter object.")
|
||||
local cd, errno = iconv.new(to .. "//TRANSLIT", from)
|
||||
assert(cd, "Failed to create a converter object: " .. ErrMsg(errno))
|
||||
local ostr, err = cd:iconv(text)
|
||||
|
||||
if err == iconv.ERROR_INCOMPLETE then
|
||||
print("ERROR: Incomplete input.")
|
||||
elseif err == iconv.ERROR_INVALID then
|
||||
print("ERROR: Invalid input.")
|
||||
elseif err == iconv.ERROR_NO_MEMORY then
|
||||
print("ERROR: Failed to allocate memory.")
|
||||
elseif err == iconv.ERROR_UNKNOWN then
|
||||
print("ERROR: There was an unknown error.")
|
||||
if err then
|
||||
print("ERROR: " .. ErrMsg(err))
|
||||
end
|
||||
print(ostr)
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user