summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2009-05-22 00:06:45 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2009-05-22 00:06:45 +0000
commit2cd4a94bdc4977e1ff1f26d1e911c1d826ef2f84 (patch)
tree6c8bbc9f76d9185d9dd8cf8b96b8b48456766558 /apps
parentbcfba0805547f5aa7ea7a48f4740b909a2284193 (diff)
downloadrockbox-2cd4a94bdc4977e1ff1f26d1e911c1d826ef2f84.tar.gz
rockbox-2cd4a94bdc4977e1ff1f26d1e911c1d826ef2f84.zip
Lua: add file handling
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21024 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps')
-rw-r--r--apps/plugins/lua/rocklib.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index 8eaa13348e..1c6469641e 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -298,6 +298,114 @@ RB_WRAP(backlight_set_brightness)
}
#endif
+RB_WRAP(open)
+{
+ const char* pathname = luaL_checkstring(L, 1);
+ int flags = luaL_checkint(L, 2);
+ int result = rb->open(pathname, flags);
+ lua_pushinteger(L, result);
+ return 1;
+}
+
+RB_WRAP(close)
+{
+ int fd = luaL_checkint(L, 1);
+ int result = rb->close(fd);
+ lua_pushinteger(L, result);
+ return 1;
+}
+
+RB_WRAP(read)
+{
+ size_t len, n, result = 0;
+ luaL_Buffer b;
+
+ int fd = luaL_checkint(L, 1);
+ size_t count = luaL_checkint(L, 2);
+
+ luaL_buffinit(L, &b);
+ len = LUAL_BUFFERSIZE;
+ do
+ {
+ char *p = luaL_prepbuffer(&b);
+
+ if (len > count)
+ len = count;
+
+ n = rb->read(fd, p, len);
+
+ luaL_addsize(&b, n);
+ count -= n;
+ result += n;
+ } while (count > 0 && n == len);
+ luaL_pushresult(&b); /* close buffer */
+
+ lua_pushinteger(L, result);
+ return 2;
+}
+
+RB_WRAP(lseek)
+{
+ int fd = luaL_checkint(L, 1);
+ off_t offset = luaL_checkint(L, 2);
+ int whence = luaL_checkint(L, 3);
+ off_t result = rb->lseek(fd, offset, whence);
+ lua_pushinteger(L, result);
+ return 1;
+}
+
+RB_WRAP(creat)
+{
+ const char* pathname = luaL_checkstring(L, 1);
+ int result = rb->creat(pathname);
+ lua_pushinteger(L, result);
+ return 1;
+}
+
+RB_WRAP(write)
+{
+ size_t count;
+ int fd = luaL_checkint(L, 1);
+ void* buf = (void*)luaL_checklstring(L, 2, &count);
+ ssize_t result = rb->write(fd, buf, count);
+ lua_pushinteger(L, result);
+ return 1;
+}
+
+RB_WRAP(remove)
+{
+ const char* pathname = luaL_checkstring(L, 1);
+ int result = rb->remove(pathname);
+ lua_pushinteger(L, result);
+ return 1;
+}
+
+RB_WRAP(rename)
+{
+ const char* path = luaL_checkstring(L, 1);
+ const char* newname = luaL_checkstring(L, 2);
+ int result = rb->rename(path, newname);
+ lua_pushinteger(L, result);
+ return 1;
+}
+
+RB_WRAP(ftruncate)
+{
+ int fd = luaL_checkint(L, 1);
+ off_t length = luaL_checkint(L, 2);
+ int result = rb->ftruncate(fd, length);
+ lua_pushinteger(L, result);
+ return 1;
+}
+
+RB_WRAP(filesize)
+{
+ int fd = luaL_checkint(L, 1);
+ off_t result = rb->filesize(fd);
+ lua_pushinteger(L, result);
+ return 1;
+}
+
#define R(NAME) {#NAME, rock_##NAME}
static const luaL_Reg rocklib[] =
{
@@ -321,6 +429,18 @@ static const luaL_Reg rocklib[] =
R(lcd_fillrect),
#endif
+ /* File handling */
+ R(open),
+ R(close),
+ R(read),
+ R(lseek),
+ R(creat),
+ R(write),
+ R(remove),
+ R(rename),
+ R(ftruncate),
+ R(filesize),
+
/* Kernel */
R(sleep),
R(yield),
@@ -361,9 +481,20 @@ LUALIB_API int luaopen_rock(lua_State *L)
luaL_register(L, LUA_ROCKLIBNAME, rocklib);
RB_CONSTANT(HZ);
+
RB_CONSTANT(LCD_WIDTH);
RB_CONSTANT(LCD_HEIGHT);
+ RB_CONSTANT(O_RDONLY);
+ RB_CONSTANT(O_WRONLY);
+ RB_CONSTANT(O_RDWR);
+ RB_CONSTANT(O_CREAT);
+ RB_CONSTANT(O_APPEND);
+ RB_CONSTANT(O_TRUNC);
+ RB_CONSTANT(SEEK_SET);
+ RB_CONSTANT(SEEK_CUR);
+ RB_CONSTANT(SEEK_END);
+
return 1;
}